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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. assertEquals("", retURI2.getPath());
  47. // relativization against root
  48. URI root = new URI("/");
  49. uriRes = PackagingURIHelper.relativizeURI(root, uri1);
  50. assertEquals("/word/document.xml", uriRes.toString());
  51. //URI compatible with MS Office and OpenOffice: leading slash is removed
  52. uriRes = PackagingURIHelper.relativizeURI(root, uri1, true);
  53. assertEquals("word/document.xml", uriRes.toString());
  54. //preserve URI fragments
  55. uriRes = PackagingURIHelper.relativizeURI(uri1, uri3, true);
  56. assertEquals("media/image1.gif#Sheet1!A1", uriRes.toString());
  57. uriRes = PackagingURIHelper.relativizeURI(root, uri4, true);
  58. assertEquals("#'My%20Sheet1'!A1", uriRes.toString());
  59. }
  60. /**
  61. * Test createPartName(String, y)
  62. */
  63. public void testCreatePartNameRelativeString()
  64. throws InvalidFormatException {
  65. PackagePartName partNameToValid = PackagingURIHelper
  66. .createPartName("/word/media/image1.gif");
  67. OPCPackage pkg = OPCPackage.create("DELETEIFEXISTS.docx");
  68. // Base part
  69. PackagePartName nameBase = PackagingURIHelper
  70. .createPartName("/word/document.xml");
  71. PackagePart partBase = pkg.createPart(nameBase, ContentTypes.XML);
  72. // Relative part name
  73. PackagePartName relativeName = PackagingURIHelper.createPartName(
  74. "media/image1.gif", partBase);
  75. assertTrue("The part name must be equal to "
  76. + partNameToValid.getName(), partNameToValid
  77. .equals(relativeName));
  78. pkg.revert();
  79. }
  80. /**
  81. * Test createPartName(URI, y)
  82. */
  83. public void testCreatePartNameRelativeURI() throws Exception {
  84. PackagePartName partNameToValid = PackagingURIHelper
  85. .createPartName("/word/media/image1.gif");
  86. OPCPackage pkg = OPCPackage.create("DELETEIFEXISTS.docx");
  87. // Base part
  88. PackagePartName nameBase = PackagingURIHelper
  89. .createPartName("/word/document.xml");
  90. PackagePart partBase = pkg.createPart(nameBase, ContentTypes.XML);
  91. // Relative part name
  92. PackagePartName relativeName = PackagingURIHelper.createPartName(
  93. new URI("media/image1.gif"), partBase);
  94. assertTrue("The part name must be equal to "
  95. + partNameToValid.getName(), partNameToValid
  96. .equals(relativeName));
  97. pkg.revert();
  98. }
  99. public void testCreateURIFromString() throws Exception {
  100. String[] href = {
  101. "..\\\\\\cygwin\\home\\yegor\\.vim\\filetype.vim",
  102. "..\\Program%20Files\\AGEIA%20Technologies\\v2.3.3\\NxCooking.dll",
  103. "file:///D:\\seva\\1981\\r810102ns.mp3",
  104. "..\\cygwin\\home\\yegor\\dinom\\%5baccess%5d.2010-10-26.log",
  105. "#'Instructions (Text)'!B21"
  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. }