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.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. import org.apache.poi.openxml4j.opc.ContentTypes;
  21. import org.apache.poi.openxml4j.opc.OPCPackage;
  22. import org.apache.poi.openxml4j.opc.PackagePart;
  23. import org.apache.poi.openxml4j.opc.PackagePartName;
  24. import org.apache.poi.openxml4j.opc.PackagingURIHelper;
  25. public class TestPackagingURIHelper extends TestCase {
  26. /**
  27. * Test relativizePartName() method.
  28. */
  29. public void testRelativizeURI() throws Exception {
  30. URI uri1 = new URI("/word/document.xml");
  31. URI uri2 = new URI("/word/media/image1.gif");
  32. URI uri3 = new URI("/word/media/image1.gif#Sheet1!A1");
  33. URI uri4 = new URI("#'My%20Sheet1'!A1");
  34. // Document to image is down a directory
  35. URI retURI1to2 = PackagingURIHelper.relativizeURI(uri1, uri2);
  36. assertEquals("media/image1.gif", retURI1to2.getPath());
  37. // Image to document is up a directory
  38. URI retURI2to1 = PackagingURIHelper.relativizeURI(uri2, uri1);
  39. assertEquals("../document.xml", retURI2to1.getPath());
  40. // Document and CustomXML parts totally different [Julien C.]
  41. URI uriCustomXml = new URI("/customXml/item1.xml");
  42. URI uriRes = PackagingURIHelper.relativizeURI(uri1, uriCustomXml);
  43. assertEquals("../customXml/item1.xml", uriRes.toString());
  44. // Document to itself is the same place (empty URI)
  45. URI retURI2 = PackagingURIHelper.relativizeURI(uri1, uri1);
  46. // YK: the line below used to assert empty string which is wrong
  47. // if source and target are the same they should be relaitivized as the last segment,
  48. // see Bugzilla 51187
  49. assertEquals("document.xml", retURI2.getPath());
  50. // relativization against root
  51. URI root = new URI("/");
  52. uriRes = PackagingURIHelper.relativizeURI(root, uri1);
  53. assertEquals("/word/document.xml", uriRes.toString());
  54. //URI compatible with MS Office and OpenOffice: leading slash is removed
  55. uriRes = PackagingURIHelper.relativizeURI(root, uri1, true);
  56. assertEquals("word/document.xml", uriRes.toString());
  57. //preserve URI fragments
  58. uriRes = PackagingURIHelper.relativizeURI(uri1, uri3, true);
  59. assertEquals("media/image1.gif#Sheet1!A1", uriRes.toString());
  60. uriRes = PackagingURIHelper.relativizeURI(root, uri4, true);
  61. assertEquals("#'My%20Sheet1'!A1", uriRes.toString());
  62. }
  63. /**
  64. * Test createPartName(String, y)
  65. */
  66. public void testCreatePartNameRelativeString()
  67. throws InvalidFormatException {
  68. PackagePartName partNameToValid = PackagingURIHelper
  69. .createPartName("/word/media/image1.gif");
  70. OPCPackage pkg = OPCPackage.create("DELETEIFEXISTS.docx");
  71. // Base part
  72. PackagePartName nameBase = PackagingURIHelper
  73. .createPartName("/word/document.xml");
  74. PackagePart partBase = pkg.createPart(nameBase, ContentTypes.XML);
  75. // Relative part name
  76. PackagePartName relativeName = PackagingURIHelper.createPartName(
  77. "media/image1.gif", partBase);
  78. assertTrue("The part name must be equal to "
  79. + partNameToValid.getName(), partNameToValid
  80. .equals(relativeName));
  81. pkg.revert();
  82. }
  83. /**
  84. * Test createPartName(URI, y)
  85. */
  86. public void testCreatePartNameRelativeURI() throws Exception {
  87. PackagePartName partNameToValid = PackagingURIHelper
  88. .createPartName("/word/media/image1.gif");
  89. OPCPackage pkg = OPCPackage.create("DELETEIFEXISTS.docx");
  90. // Base part
  91. PackagePartName nameBase = PackagingURIHelper
  92. .createPartName("/word/document.xml");
  93. PackagePart partBase = pkg.createPart(nameBase, ContentTypes.XML);
  94. // Relative part name
  95. PackagePartName relativeName = PackagingURIHelper.createPartName(
  96. new URI("media/image1.gif"), partBase);
  97. assertTrue("The part name must be equal to "
  98. + partNameToValid.getName(), partNameToValid
  99. .equals(relativeName));
  100. pkg.revert();
  101. }
  102. public void testCreateURIFromString() throws Exception {
  103. String[] href = {
  104. "..\\\\\\cygwin\\home\\yegor\\.vim\\filetype.vim",
  105. "..\\Program%20Files\\AGEIA%20Technologies\\v2.3.3\\NxCooking.dll",
  106. "file:///D:\\seva\\1981\\r810102ns.mp3",
  107. "..\\cygwin\\home\\yegor\\dinom\\%5baccess%5d.2010-10-26.log",
  108. "#'Instructions (Text)'!B21",
  109. "javascript://"
  110. };
  111. for(String s : href){
  112. try {
  113. URI uri = PackagingURIHelper.toURI(s);
  114. } catch (URISyntaxException e){
  115. fail("Failed to create URI from " + s);
  116. }
  117. }
  118. }
  119. public void test53734() throws Exception {
  120. URI uri = PackagingURIHelper.toURI("javascript://");
  121. // POI appends a trailing slash tpo avoid "Expected authority at index 13: javascript://"
  122. // https://issues.apache.org/bugzilla/show_bug.cgi?id=53734
  123. assertEquals("javascript:///", uri.toASCIIString());
  124. }
  125. }