選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

TestOPCCompliancePackageModel.java 7.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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.compliance;
  16. import static org.junit.jupiter.api.Assertions.assertEquals;
  17. import static org.junit.jupiter.api.Assertions.assertThrows;
  18. import static org.junit.jupiter.api.Assertions.assertTrue;
  19. import java.io.File;
  20. import java.io.IOException;
  21. import org.apache.poi.POIDataSamples;
  22. import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
  23. import org.apache.poi.openxml4j.exceptions.InvalidOperationException;
  24. import org.apache.poi.openxml4j.exceptions.PartAlreadyExistsException;
  25. import org.apache.poi.openxml4j.opc.ContentTypes;
  26. import org.apache.poi.openxml4j.opc.OPCPackage;
  27. import org.apache.poi.openxml4j.opc.PackagePartName;
  28. import org.apache.poi.openxml4j.opc.PackageRelationshipTypes;
  29. import org.apache.poi.openxml4j.opc.PackagingURIHelper;
  30. import org.apache.poi.openxml4j.opc.TargetMode;
  31. import org.apache.poi.util.TempFile;
  32. import org.junit.jupiter.api.Test;
  33. /**
  34. * Test Open Packaging Convention package model compliance.
  35. *
  36. * M1.11 : A package implementer shall neither create nor recognize a part with
  37. * a part name derived from another part name by appending segments to it.
  38. */
  39. class TestOPCCompliancePackageModel {
  40. private static File getTempFile() throws IOException {
  41. File tf = TempFile.createTempFile("TODELETEIFEXIST", ".docx");
  42. assertTrue(tf::delete);
  43. return tf;
  44. }
  45. /**
  46. * A package implementer shall neither create nor recognize a part with a
  47. * part name derived from another part name by appending segments to it.
  48. * [M1.11]
  49. */
  50. @Test
  51. void testPartNameDerivationAdditionFailure() throws InvalidFormatException, IOException {
  52. File tf = getTempFile();
  53. try (OPCPackage pkg = OPCPackage.create(tf)) {
  54. PackagePartName name = PackagingURIHelper.createPartName("/word/document.xml");
  55. PackagePartName nameDerived = PackagingURIHelper.createPartName("/word/document.xml/image1.gif");
  56. pkg.createPart(name, ContentTypes.XML);
  57. assertThrows(InvalidOperationException.class, () -> pkg.createPart(nameDerived, ContentTypes.EXTENSION_GIF),
  58. "A package implementer shall neither create nor recognize a part with a part name derived from another " +
  59. "part name by appending segments to it. [M1.11]");
  60. pkg.revert();
  61. } finally {
  62. assertTrue(tf::delete);
  63. }
  64. }
  65. /**
  66. * A package implementer shall neither create nor recognize a part with a
  67. * part name derived from another part name by appending segments to it.
  68. * [M1.11]
  69. */
  70. @Test
  71. void testPartNameDerivationReadingFailure() {
  72. String filename = "OPCCompliance_DerivedPartNameFAIL.docx";
  73. assertThrows(InvalidFormatException.class, () ->
  74. OPCPackage.open(POIDataSamples.getOpenXML4JInstance().openResourceAsStream(filename)),
  75. "A package implementer shall neither create nor recognize a part with a part name derived from another" +
  76. " part name by appending segments to it. [M1.11]"
  77. );
  78. }
  79. /**
  80. * Rule M1.12 : Packages shall not contain equivalent part names and package
  81. * implementers shall neither create nor recognize packages with equivalent
  82. * part names.
  83. */
  84. @Test
  85. void testAddPackageAlreadyAddFailure() throws IOException, InvalidFormatException {
  86. File tf = getTempFile();
  87. try (OPCPackage pkg = OPCPackage.create(tf)) {
  88. PackagePartName name1 = PackagingURIHelper.createPartName("/word/document.xml");
  89. PackagePartName name2 = PackagingURIHelper.createPartName("/word/document.xml");
  90. pkg.createPart(name1, ContentTypes.XML);
  91. assertThrows(PartAlreadyExistsException.class, () -> pkg.createPart(name2, ContentTypes.XML),
  92. "Packages shall not contain equivalent part names and package implementers shall neither create nor " +
  93. "recognize packages with equivalent part names. [M1.12]"
  94. );
  95. pkg.revert();
  96. } finally {
  97. assertTrue(tf::delete);
  98. }
  99. }
  100. /**
  101. * Rule M1.12 : Packages shall not contain equivalent part names and package
  102. * implementers shall neither create nor recognize packages with equivalent
  103. * part names.
  104. */
  105. @Test
  106. void testAddPackageAlreadyAddFailure2() throws IOException, InvalidFormatException {
  107. File tf = getTempFile();
  108. try (OPCPackage pkg = OPCPackage.create(tf)) {
  109. PackagePartName partName = PackagingURIHelper.createPartName("/word/document.xml");
  110. pkg.createPart(partName, ContentTypes.XML);
  111. assertThrows(InvalidOperationException.class, () -> pkg.createPart(partName, ContentTypes.XML),
  112. "Packages shall not contain equivalent part names and package implementers shall neither create nor " +
  113. "recognize packages with equivalent part names. [M1.12]"
  114. );
  115. pkg.revert();
  116. } finally {
  117. assertTrue(tf::delete);
  118. }
  119. }
  120. /**
  121. * Try to add a relationship to a relationship part.
  122. * <p>
  123. * Check rule M1.25: The Relationships part shall not have relationships to
  124. * any other part. Package implementers shall enforce this requirement upon
  125. * the attempt to create such a relationship and shall treat any such
  126. * relationship as invalid.
  127. */
  128. @Test
  129. void testAddRelationshipRelationshipsPartFailure() throws IOException, InvalidFormatException {
  130. File tf = getTempFile();
  131. try (OPCPackage pkg = OPCPackage.create(tf)) {
  132. PackagePartName name1 = PackagingURIHelper.createPartName("/test/_rels/document.xml.rels");
  133. assertThrows(InvalidOperationException.class,
  134. () -> pkg.addRelationship(name1, TargetMode.INTERNAL, PackageRelationshipTypes.CORE_DOCUMENT),
  135. "The Relationships part shall not have relationships to any other part [M1.25]"
  136. );
  137. pkg.revert();
  138. } finally {
  139. assertTrue(tf::delete);
  140. }
  141. }
  142. @Test
  143. void testToString() throws IOException {
  144. File tf = getTempFile();
  145. try (OPCPackage pkg = OPCPackage.create(tf)) {
  146. assertEquals("OPCPackage{" +
  147. "packageAccess=READ_WRITE, " +
  148. "relationships=null, " +
  149. "packageProperties=Name: /docProps/core.xml - Content Type: application/vnd.openxmlformats-package.core-properties+xml, " +
  150. "isDirty=false}", pkg.toString());
  151. } finally {
  152. assertTrue(tf::delete);
  153. }
  154. }
  155. }