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.

TestOPCComplianceCoreProperties.java 9.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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 java.io.IOException;
  17. import java.io.InputStream;
  18. import java.net.URI;
  19. import java.net.URISyntaxException;
  20. import junit.framework.AssertionFailedError;
  21. import junit.framework.TestCase;
  22. import org.apache.poi.POIDataSamples;
  23. import org.apache.poi.openxml4j.OpenXML4JTestDataSamples;
  24. import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
  25. import org.apache.poi.openxml4j.exceptions.InvalidOperationException;
  26. import org.apache.poi.openxml4j.opc.ContentTypes;
  27. import org.apache.poi.openxml4j.opc.OPCPackage;
  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. /**
  32. * Test core properties Open Packaging Convention compliance.
  33. *
  34. * M4.1: The format designer shall specify and the format producer shall create
  35. * at most one core properties relationship for a package. A format consumer
  36. * shall consider more than one core properties relationship for a package to be
  37. * an error. If present, the relationship shall target the Core Properties part.
  38. * (POI relaxes this on reading, as Office sometimes breaks this)
  39. *
  40. * M4.2: The format designer shall not specify and the format producer shall not
  41. * create Core Properties that use the Markup Compatibility namespace as defined
  42. * in Annex F, "Standard Namespaces and Content Types". A format consumer shall
  43. * consider the use of the Markup Compatibility namespace to be an error.
  44. *
  45. * M4.3: Producers shall not create a document element that contains refinements
  46. * to the Dublin Core elements, except for the two specified in the schema:
  47. * <dcterms:created> and <dcterms:modified> Consumers shall consider a document
  48. * element that violates this constraint to be an error.
  49. *
  50. * M4.4: Producers shall not create a document element that contains the
  51. * xml:lang attribute. Consumers shall consider a document element that violates
  52. * this constraint to be an error.
  53. *
  54. * M4.5: Producers shall not create a document element that contains the
  55. * xsi:type attribute, except for a <dcterms:created> or <dcterms:modified>
  56. * element where the xsi:type attribute shall be present and shall hold the
  57. * value dcterms:W3CDTF, where dcterms is the namespace prefix of the Dublin
  58. * Core namespace. Consumers shall consider a document element that violates
  59. * this constraint to be an error.
  60. *
  61. * @author Julien Chable
  62. */
  63. public final class TestOPCComplianceCoreProperties extends TestCase {
  64. public void testCorePropertiesPart() {
  65. OPCPackage pkg;
  66. try {
  67. InputStream is = OpenXML4JTestDataSamples.openComplianceSampleStream("OPCCompliance_CoreProperties_OnlyOneCorePropertiesPart.docx");
  68. pkg = OPCPackage.open(is);
  69. } catch (InvalidFormatException e) {
  70. throw new RuntimeException(e);
  71. } catch (IOException e) {
  72. throw new RuntimeException(e);
  73. }
  74. pkg.revert();
  75. }
  76. private static String extractInvalidFormatMessage(String sampleNameSuffix) {
  77. InputStream is = OpenXML4JTestDataSamples.openComplianceSampleStream("OPCCompliance_CoreProperties_" + sampleNameSuffix);
  78. OPCPackage pkg;
  79. try {
  80. pkg = OPCPackage.open(is);
  81. } catch (InvalidFormatException e) {
  82. // no longer required for successful test
  83. return e.getMessage();
  84. } catch (IOException e) {
  85. throw new RuntimeException(e);
  86. }
  87. pkg.revert();
  88. throw new AssertionFailedError("expected OPC compliance exception was not thrown");
  89. }
  90. /**
  91. * Test M4.1 rule.
  92. */
  93. public void testOnlyOneCorePropertiesPart() throws Exception {
  94. // We have relaxed this check, so we can read the file anyway
  95. try {
  96. extractInvalidFormatMessage("OnlyOneCorePropertiesPartFAIL.docx");
  97. fail("M4.1 should be being relaxed");
  98. } catch (AssertionFailedError e) {}
  99. // We will use the first core properties, and ignore the others
  100. InputStream is = OpenXML4JTestDataSamples.openSampleStream("MultipleCoreProperties.docx");
  101. OPCPackage pkg = OPCPackage.open(is);
  102. // We can see 2 by type
  103. assertEquals(2, pkg.getPartsByContentType(ContentTypes.CORE_PROPERTIES_PART).size());
  104. // But only the first one by relationship
  105. assertEquals(1, pkg.getPartsByRelationshipType(PackageRelationshipTypes.CORE_PROPERTIES).size());
  106. // It should be core.xml not the older core1.xml
  107. assertEquals(
  108. "/docProps/core.xml",
  109. pkg.getPartsByRelationshipType(PackageRelationshipTypes.CORE_PROPERTIES).get(0).getPartName().toString()
  110. );
  111. }
  112. private static URI createURI(String text) {
  113. try {
  114. return new URI(text);
  115. } catch (URISyntaxException e) {
  116. throw new RuntimeException(e);
  117. }
  118. }
  119. /**
  120. * Test M4.1 rule.
  121. */
  122. public void testOnlyOneCorePropertiesPart_AddRelationship() {
  123. InputStream is = OpenXML4JTestDataSamples.openComplianceSampleStream("OPCCompliance_CoreProperties_OnlyOneCorePropertiesPart.docx");
  124. OPCPackage pkg;
  125. try {
  126. pkg = OPCPackage.open(is);
  127. } catch (InvalidFormatException e) {
  128. throw new RuntimeException(e);
  129. } catch (IOException e) {
  130. throw new RuntimeException(e);
  131. }
  132. URI partUri = createURI("/docProps/core2.xml");
  133. try {
  134. pkg.addRelationship(PackagingURIHelper.createPartName(partUri), TargetMode.INTERNAL,
  135. PackageRelationshipTypes.CORE_PROPERTIES);
  136. // no longer fail on compliance error
  137. //fail("expected OPC compliance exception was not thrown");
  138. } catch (InvalidFormatException e) {
  139. throw new RuntimeException(e);
  140. } catch (InvalidOperationException e) {
  141. // expected during successful test
  142. assertEquals("OPC Compliance error [M4.1]: can't add another core properties part ! Use the built-in package method instead.", e.getMessage());
  143. }
  144. pkg.revert();
  145. }
  146. /**
  147. * Test M4.1 rule.
  148. */
  149. public void testOnlyOneCorePropertiesPart_AddPart() {
  150. String sampleFileName = "OPCCompliance_CoreProperties_OnlyOneCorePropertiesPart.docx";
  151. OPCPackage pkg = null;
  152. try {
  153. pkg = OPCPackage.open(POIDataSamples.getOpenXML4JInstance().getFile(sampleFileName).getPath());
  154. } catch (Exception e) {
  155. throw new RuntimeException(e);
  156. }
  157. URI partUri = createURI("/docProps/core2.xml");
  158. try {
  159. pkg.createPart(PackagingURIHelper.createPartName(partUri),
  160. ContentTypes.CORE_PROPERTIES_PART);
  161. // no longer fail on compliance error
  162. //fail("expected OPC compliance exception was not thrown");
  163. } catch (InvalidFormatException e) {
  164. throw new RuntimeException(e);
  165. } catch (InvalidOperationException e) {
  166. // expected during successful test
  167. assertEquals("OPC Compliance error [M4.1]: you try to add more than one core properties relationship in the package !", e.getMessage());
  168. }
  169. pkg.revert();
  170. }
  171. /**
  172. * Test M4.2 rule.
  173. */
  174. public void testDoNotUseCompatibilityMarkup() {
  175. String msg = extractInvalidFormatMessage("DoNotUseCompatibilityMarkupFAIL.docx");
  176. assertEquals("OPC Compliance error [M4.2]: A format consumer shall consider the use of the Markup Compatibility namespace to be an error.", msg);
  177. }
  178. /**
  179. * Test M4.3 rule.
  180. */
  181. public void testDCTermsNamespaceLimitedUse() {
  182. String msg = extractInvalidFormatMessage("DCTermsNamespaceLimitedUseFAIL.docx");
  183. assertEquals("OPC Compliance error [M4.3]: Producers shall not create a document element that contains refinements to the Dublin Core elements, except for the two specified in the schema: <dcterms:created> and <dcterms:modified> Consumers shall consider a document element that violates this constraint to be an error.", msg);
  184. }
  185. /**
  186. * Test M4.4 rule.
  187. */
  188. public void testUnauthorizedXMLLangAttribute() {
  189. String msg = extractInvalidFormatMessage("UnauthorizedXMLLangAttributeFAIL.docx");
  190. assertEquals("OPC Compliance error [M4.4]: Producers shall not create a document element that contains the xml:lang attribute. Consumers shall consider a document element that violates this constraint to be an error.", msg);
  191. }
  192. /**
  193. * Test M4.5 rule.
  194. */
  195. public void testLimitedXSITypeAttribute_NotPresent() {
  196. String msg = extractInvalidFormatMessage("LimitedXSITypeAttribute_NotPresentFAIL.docx");
  197. assertEquals("The element 'created' must have the 'xsi:type' attribute present !", msg);
  198. }
  199. /**
  200. * Test M4.5 rule.
  201. */
  202. public void testLimitedXSITypeAttribute_PresentWithUnauthorizedValue() {
  203. String msg = extractInvalidFormatMessage("LimitedXSITypeAttribute_PresentWithUnauthorizedValueFAIL.docx");
  204. assertEquals("The element 'modified' must have the 'xsi:type' attribute with the value 'dcterms:W3CDTF' !", msg);
  205. }
  206. }