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.

TestPackageCoreProperties.java 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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.io.ByteArrayInputStream;
  17. import java.io.ByteArrayOutputStream;
  18. import java.io.File;
  19. import java.io.IOException;
  20. import java.io.InputStream;
  21. import java.text.ParsePosition;
  22. import java.text.SimpleDateFormat;
  23. import java.util.Date;
  24. import java.util.TimeZone;
  25. import junit.framework.TestCase;
  26. import org.apache.poi.openxml4j.OpenXML4JTestDataSamples;
  27. import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
  28. import org.apache.poi.openxml4j.exceptions.OpenXML4JException;
  29. import org.apache.poi.openxml4j.opc.internal.PackagePropertiesPart;
  30. import org.apache.poi.openxml4j.util.Nullable;
  31. import org.apache.poi.util.POILogFactory;
  32. import org.apache.poi.util.POILogger;
  33. public final class TestPackageCoreProperties extends TestCase {
  34. private static final POILogger logger = POILogFactory.getLogger(TestPackageCoreProperties.class);
  35. /**
  36. * Test package core properties getters.
  37. */
  38. public void testGetProperties() {
  39. try {
  40. // Open the package
  41. OPCPackage p = OPCPackage.open(OpenXML4JTestDataSamples.openSampleStream("TestPackageCoreProperiesGetters.docx"));
  42. compareProperties(p);
  43. p.revert();
  44. } catch (OpenXML4JException e) {
  45. logger.log(POILogger.DEBUG, e.getMessage());
  46. throw new RuntimeException(e);
  47. } catch (IOException e) {
  48. throw new RuntimeException(e);
  49. }
  50. }
  51. /**
  52. * Test package core properties setters.
  53. */
  54. public void testSetProperties() throws Exception {
  55. String inputPath = OpenXML4JTestDataSamples.getSampleFileName("TestPackageCoreProperiesSetters.docx");
  56. File outputFile = OpenXML4JTestDataSamples.getOutputFile("TestPackageCoreProperiesSettersOUTPUT.docx");
  57. // Open package
  58. OPCPackage p = OPCPackage.open(inputPath, PackageAccess.READ_WRITE);
  59. SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
  60. df.setTimeZone(TimeZone.getTimeZone("UTC"));
  61. Date dateToInsert = df.parse("2007-05-12T08:00:00Z", new ParsePosition(
  62. 0));
  63. PackageProperties props = p.getPackageProperties();
  64. props.setCategoryProperty("MyCategory");
  65. props.setContentStatusProperty("MyContentStatus");
  66. props.setContentTypeProperty("MyContentType");
  67. props.setCreatedProperty(new Nullable<Date>(dateToInsert));
  68. props.setCreatorProperty("MyCreator");
  69. props.setDescriptionProperty("MyDescription");
  70. props.setIdentifierProperty("MyIdentifier");
  71. props.setKeywordsProperty("MyKeywords");
  72. props.setLanguageProperty("MyLanguage");
  73. props.setLastModifiedByProperty("Julien Chable");
  74. props.setLastPrintedProperty(new Nullable<Date>(dateToInsert));
  75. props.setModifiedProperty(new Nullable<Date>(dateToInsert));
  76. props.setRevisionProperty("2");
  77. props.setTitleProperty("MyTitle");
  78. props.setSubjectProperty("MySubject");
  79. props.setVersionProperty("2");
  80. // Save the package in the output directory
  81. p.save(outputFile);
  82. // Open the newly created file to check core properties saved values.
  83. OPCPackage p2 = OPCPackage.open(outputFile.getAbsolutePath(), PackageAccess.READ);
  84. compareProperties(p2);
  85. p2.revert();
  86. outputFile.delete();
  87. }
  88. private void compareProperties(OPCPackage p) throws InvalidFormatException {
  89. SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
  90. df.setTimeZone(TimeZone.getTimeZone("UTC"));
  91. Date expectedDate = df.parse("2007-05-12T08:00:00Z", new ParsePosition(
  92. 0));
  93. // Gets the core properties
  94. PackageProperties props = p.getPackageProperties();
  95. assertEquals("MyCategory", props.getCategoryProperty().getValue());
  96. assertEquals("MyContentStatus", props.getContentStatusProperty()
  97. .getValue());
  98. assertEquals("MyContentType", props.getContentTypeProperty().getValue());
  99. assertEquals(expectedDate, props.getCreatedProperty().getValue());
  100. assertEquals("MyCreator", props.getCreatorProperty().getValue());
  101. assertEquals("MyDescription", props.getDescriptionProperty().getValue());
  102. assertEquals("MyIdentifier", props.getIdentifierProperty().getValue());
  103. assertEquals("MyKeywords", props.getKeywordsProperty().getValue());
  104. assertEquals("MyLanguage", props.getLanguageProperty().getValue());
  105. assertEquals("Julien Chable", props.getLastModifiedByProperty()
  106. .getValue());
  107. assertEquals(expectedDate, props.getLastPrintedProperty().getValue());
  108. assertEquals(expectedDate, props.getModifiedProperty().getValue());
  109. assertEquals("2", props.getRevisionProperty().getValue());
  110. assertEquals("MySubject", props.getSubjectProperty().getValue());
  111. assertEquals("MyTitle", props.getTitleProperty().getValue());
  112. assertEquals("2", props.getVersionProperty().getValue());
  113. }
  114. public void testCoreProperties_bug51374() throws Exception {
  115. SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
  116. df.setTimeZone(TimeZone.getTimeZone("UTC"));
  117. String strDate = "2007-05-12T08:00:00Z";
  118. Date date = df.parse(strDate);
  119. OPCPackage pkg = new ZipPackage();
  120. PackagePropertiesPart props = (PackagePropertiesPart)pkg.getPackageProperties();
  121. // created
  122. assertEquals("", props.getCreatedPropertyString());
  123. assertNull(props.getCreatedProperty().getValue());
  124. props.setCreatedProperty((String)null);
  125. assertEquals("", props.getCreatedPropertyString());
  126. assertNull(props.getCreatedProperty().getValue());
  127. props.setCreatedProperty(new Nullable<Date>());
  128. assertEquals("", props.getCreatedPropertyString());
  129. assertNull(props.getCreatedProperty().getValue());
  130. props.setCreatedProperty(new Nullable<Date>(date));
  131. assertEquals(strDate, props.getCreatedPropertyString());
  132. assertEquals(date, props.getCreatedProperty().getValue());
  133. props.setCreatedProperty(strDate);
  134. assertEquals(strDate, props.getCreatedPropertyString());
  135. assertEquals(date, props.getCreatedProperty().getValue());
  136. // lastPrinted
  137. assertEquals("", props.getLastPrintedPropertyString());
  138. assertNull(props.getLastPrintedProperty().getValue());
  139. props.setLastPrintedProperty((String)null);
  140. assertEquals("", props.getLastPrintedPropertyString());
  141. assertNull(props.getLastPrintedProperty().getValue());
  142. props.setLastPrintedProperty(new Nullable<Date>());
  143. assertEquals("", props.getLastPrintedPropertyString());
  144. assertNull(props.getLastPrintedProperty().getValue());
  145. props.setLastPrintedProperty(new Nullable<Date>(date));
  146. assertEquals(strDate, props.getLastPrintedPropertyString());
  147. assertEquals(date, props.getLastPrintedProperty().getValue());
  148. props.setLastPrintedProperty(strDate);
  149. assertEquals(strDate, props.getLastPrintedPropertyString());
  150. assertEquals(date, props.getLastPrintedProperty().getValue());
  151. // modified
  152. assertNull(props.getModifiedProperty().getValue());
  153. props.setModifiedProperty((String)null);
  154. assertNull(props.getModifiedProperty().getValue());
  155. props.setModifiedProperty(new Nullable<Date>());
  156. assertNull(props.getModifiedProperty().getValue());
  157. props.setModifiedProperty(new Nullable<Date>(date));
  158. assertEquals(strDate, props.getModifiedPropertyString());
  159. assertEquals(date, props.getModifiedProperty().getValue());
  160. props.setModifiedProperty(strDate);
  161. assertEquals(strDate, props.getModifiedPropertyString());
  162. assertEquals(date, props.getModifiedProperty().getValue());
  163. // Tidy
  164. pkg.close();
  165. }
  166. public void testGetPropertiesLO() throws Exception {
  167. // Open the package
  168. OPCPackage pkg1 = OPCPackage.open(OpenXML4JTestDataSamples.openSampleStream("51444.xlsx"));
  169. PackageProperties props1 = pkg1.getPackageProperties();
  170. assertEquals(null, props1.getTitleProperty().getValue());
  171. props1.setTitleProperty("Bug 51444 fixed");
  172. ByteArrayOutputStream out = new ByteArrayOutputStream();
  173. pkg1.save(out);
  174. out.close();
  175. OPCPackage pkg2 = OPCPackage.open(new ByteArrayInputStream(out.toByteArray()));
  176. PackageProperties props2 = pkg2.getPackageProperties();
  177. props2.setTitleProperty("Bug 51444 fixed");
  178. }
  179. public void testEntitiesInCoreProps_56164() throws Exception {
  180. InputStream is = OpenXML4JTestDataSamples.openSampleStream("CorePropertiesHasEntities.ooxml");
  181. OPCPackage p = OPCPackage.open(is);
  182. is.close();
  183. // Should have 3 root relationships
  184. boolean foundDocRel = false, foundCorePropRel = false, foundExtPropRel = false;
  185. for (PackageRelationship pr : p.getRelationships()) {
  186. if (pr.getRelationshipType().equals(PackageRelationshipTypes.CORE_DOCUMENT))
  187. foundDocRel = true;
  188. if (pr.getRelationshipType().equals(PackageRelationshipTypes.CORE_PROPERTIES))
  189. foundCorePropRel = true;
  190. if (pr.getRelationshipType().equals(PackageRelationshipTypes.EXTENDED_PROPERTIES))
  191. foundExtPropRel = true;
  192. }
  193. assertTrue("Core/Doc Relationship not found in " + p.getRelationships(), foundDocRel);
  194. assertTrue("Core Props Relationship not found in " + p.getRelationships(), foundCorePropRel);
  195. assertTrue("Ext Props Relationship not found in " + p.getRelationships(), foundExtPropRel);
  196. // Get the Core Properties
  197. PackagePropertiesPart props = (PackagePropertiesPart)p.getPackageProperties();
  198. // Check
  199. assertEquals("Stefan Kopf", props.getCreatorProperty().getValue());
  200. }
  201. }