You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TestPackagingURIHelper.java 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /* ====================================================================
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.openxml4j.opc;
  16. import java.net.URI;
  17. import java.net.URISyntaxException;
  18. import junit.framework.TestCase;
  19. import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
  20. public class TestPackagingURIHelper extends TestCase {
  21. /**
  22. * Test relativizePartName() method.
  23. */
  24. public void testRelativizeURI() throws Exception {
  25. URI uri1 = new URI("/word/document.xml");
  26. URI uri2 = new URI("/word/media/image1.gif");
  27. URI uri3 = new URI("/word/media/image1.gif#Sheet1!A1");
  28. URI uri4 = new URI("#'My%20Sheet1'!A1");
  29. // Document to image is down a directory
  30. URI retURI1to2 = PackagingURIHelper.relativizeURI(uri1, uri2);
  31. assertEquals("media/image1.gif", retURI1to2.getPath());
  32. // Image to document is up a directory
  33. URI retURI2to1 = PackagingURIHelper.relativizeURI(uri2, uri1);
  34. assertEquals("../document.xml", retURI2to1.getPath());
  35. // Document and CustomXML parts totally different [Julien C.]
  36. URI uriCustomXml = new URI("/customXml/item1.xml");
  37. URI uriRes = PackagingURIHelper.relativizeURI(uri1, uriCustomXml);
  38. assertEquals("../customXml/item1.xml", uriRes.toString());
  39. // Document to itself is the same place (empty URI)
  40. URI retURI2 = PackagingURIHelper.relativizeURI(uri1, uri1);
  41. // YK: the line below used to assert empty string which is wrong
  42. // if source and target are the same they should be relaitivized as the last segment,
  43. // see Bugzilla 51187
  44. assertEquals("document.xml", retURI2.getPath());
  45. // relativization against root
  46. URI root = new URI("/");
  47. uriRes = PackagingURIHelper.relativizeURI(root, uri1);
  48. assertEquals("/word/document.xml", uriRes.toString());
  49. //URI compatible with MS Office and OpenOffice: leading slash is removed
  50. uriRes = PackagingURIHelper.relativizeURI(root, uri1, true);
  51. assertEquals("word/document.xml", uriRes.toString());
  52. //preserve URI fragments
  53. uriRes = PackagingURIHelper.relativizeURI(uri1, uri3, true);
  54. assertEquals("media/image1.gif#Sheet1!A1", uriRes.toString());
  55. uriRes = PackagingURIHelper.relativizeURI(root, uri4, true);
  56. assertEquals("#'My%20Sheet1'!A1", uriRes.toString());
  57. }
  58. /**
  59. * Test createPartName(String, y)
  60. */
  61. public void testCreatePartNameRelativeString()
  62. throws InvalidFormatException {
  63. PackagePartName partNameToValid = PackagingURIHelper
  64. .createPartName("/word/media/image1.gif");
  65. OPCPackage pkg = OPCPackage.create("DELETEIFEXISTS.docx");
  66. // Base part
  67. PackagePartName nameBase = PackagingURIHelper
  68. .createPartName("/word/document.xml");
  69. PackagePart partBase = pkg.createPart(nameBase, ContentTypes.XML);
  70. // Relative part name
  71. PackagePartName relativeName = PackagingURIHelper.createPartName(
  72. "media/image1.gif", partBase);
  73. assertTrue("The part name must be equal to "
  74. + partNameToValid.getName(), partNameToValid
  75. .equals(relativeName));
  76. pkg.revert();
  77. }
  78. /**
  79. * Test createPartName(URI, y)
  80. */
  81. public void testCreatePartNameRelativeURI() throws Exception {
  82. PackagePartName partNameToValid = PackagingURIHelper
  83. .createPartName("/word/media/image1.gif");
  84. OPCPackage pkg = OPCPackage.create("DELETEIFEXISTS.docx");
  85. // Base part
  86. PackagePartName nameBase = PackagingURIHelper
  87. .createPartName("/word/document.xml");
  88. PackagePart partBase = pkg.createPart(nameBase, ContentTypes.XML);
  89. // Relative part name
  90. PackagePartName relativeName = PackagingURIHelper.createPartName(
  91. new URI("media/image1.gif"), partBase);
  92. assertTrue("The part name must be equal to "
  93. + partNameToValid.getName(), partNameToValid
  94. .equals(relativeName));
  95. pkg.revert();
  96. }
  97. public void testCreateURIFromString() throws Exception {
  98. String[] href = {
  99. "..\\\\\\cygwin\\home\\yegor\\.vim\\filetype.vim",
  100. "..\\Program%20Files\\AGEIA%20Technologies\\v2.3.3\\NxCooking.dll",
  101. "file:///D:\\seva\\1981\\r810102ns.mp3",
  102. "..\\cygwin\\home\\yegor\\dinom\\%5baccess%5d.2010-10-26.log",
  103. "#'Instructions (Text)'!B21",
  104. "#'性'!B21",
  105. "javascript://"
  106. };
  107. for(String s : href){
  108. try {
  109. URI uri = PackagingURIHelper.toURI(s);
  110. } catch (URISyntaxException e){
  111. fail("Failed to create URI from " + s);
  112. }
  113. }
  114. }
  115. public void test53734() throws Exception {
  116. URI uri = PackagingURIHelper.toURI("javascript://");
  117. // POI appends a trailing slash tpo avoid "Expected authority at index 13: javascript://"
  118. // https://issues.apache.org/bugzilla/show_bug.cgi?id=53734
  119. assertEquals("javascript:///", uri.toASCIIString());
  120. }
  121. }