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.

TestPOIXMLProperties.java 7.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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;
  16. import java.io.IOException;
  17. import java.util.Calendar;
  18. import java.util.Date;
  19. import java.util.GregorianCalendar;
  20. import java.util.Locale;
  21. import java.util.TimeZone;
  22. import junit.framework.TestCase;
  23. import org.apache.poi.POIXMLProperties.CoreProperties;
  24. import org.apache.poi.openxml4j.util.Nullable;
  25. import org.apache.poi.xssf.XSSFTestDataSamples;
  26. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  27. import org.apache.poi.xwpf.XWPFTestDataSamples;
  28. import org.apache.poi.xwpf.usermodel.XWPFDocument;
  29. /**
  30. * Test setting extended and custom OOXML properties
  31. */
  32. public final class TestPOIXMLProperties extends TestCase {
  33. private POIXMLProperties _props;
  34. private CoreProperties _coreProperties;
  35. public void setUp() throws IOException {
  36. XWPFDocument sampleDoc = XWPFTestDataSamples.openSampleDocument("documentProperties.docx");
  37. _props = sampleDoc.getProperties();
  38. _coreProperties = _props.getCoreProperties();
  39. assertNotNull(_props);
  40. }
  41. public void testWorkbookExtendedProperties() {
  42. XSSFWorkbook workbook = new XSSFWorkbook();
  43. POIXMLProperties props = workbook.getProperties();
  44. assertNotNull(props);
  45. org.apache.poi.POIXMLProperties.ExtendedProperties properties =
  46. props.getExtendedProperties();
  47. org.openxmlformats.schemas.officeDocument.x2006.extendedProperties.CTProperties
  48. ctProps = properties.getUnderlyingProperties();
  49. String appVersion = "3.5 beta";
  50. String application = "POI";
  51. ctProps.setApplication(application);
  52. ctProps.setAppVersion(appVersion);
  53. ctProps = null;
  54. properties = null;
  55. props = null;
  56. XSSFWorkbook newWorkbook =
  57. XSSFTestDataSamples.writeOutAndReadBack(workbook);
  58. assertTrue(workbook != newWorkbook);
  59. POIXMLProperties newProps = newWorkbook.getProperties();
  60. assertNotNull(newProps);
  61. org.apache.poi.POIXMLProperties.ExtendedProperties newProperties =
  62. newProps.getExtendedProperties();
  63. assertEquals(application, newProperties.getApplication());
  64. assertEquals(appVersion, newProperties.getAppVersion());
  65. org.openxmlformats.schemas.officeDocument.x2006.extendedProperties.CTProperties
  66. newCtProps = newProperties.getUnderlyingProperties();
  67. assertEquals(application, newCtProps.getApplication());
  68. assertEquals(appVersion, newCtProps.getAppVersion());
  69. }
  70. /**
  71. * Test usermodel API for setting custom properties
  72. */
  73. public void testCustomProperties() {
  74. POIXMLDocument wb = new XSSFWorkbook();
  75. POIXMLProperties.CustomProperties customProps = wb.getProperties().getCustomProperties();
  76. customProps.addProperty("test-1", "string val");
  77. customProps.addProperty("test-2", 1974);
  78. customProps.addProperty("test-3", 36.6);
  79. //adding a duplicate
  80. try {
  81. customProps.addProperty("test-3", 36.6);
  82. fail("expected exception");
  83. } catch(IllegalArgumentException e){
  84. assertEquals("A property with this name already exists in the custom properties", e.getMessage());
  85. }
  86. customProps.addProperty("test-4", true);
  87. wb = XSSFTestDataSamples.writeOutAndReadBack((XSSFWorkbook)wb);
  88. org.openxmlformats.schemas.officeDocument.x2006.customProperties.CTProperties ctProps =
  89. wb.getProperties().getCustomProperties().getUnderlyingProperties();
  90. assertEquals(4, ctProps.sizeOfPropertyArray());
  91. org.openxmlformats.schemas.officeDocument.x2006.customProperties.CTProperty p;
  92. p = ctProps.getPropertyArray(0);
  93. assertEquals("{D5CDD505-2E9C-101B-9397-08002B2CF9AE}", p.getFmtid());
  94. assertEquals("test-1", p.getName());
  95. assertEquals("string val", p.getLpwstr());
  96. assertEquals(2, p.getPid());
  97. p = ctProps.getPropertyArray(1);
  98. assertEquals("{D5CDD505-2E9C-101B-9397-08002B2CF9AE}", p.getFmtid());
  99. assertEquals("test-2", p.getName());
  100. assertEquals(1974, p.getI4());
  101. assertEquals(3, p.getPid());
  102. p = ctProps.getPropertyArray(2);
  103. assertEquals("{D5CDD505-2E9C-101B-9397-08002B2CF9AE}", p.getFmtid());
  104. assertEquals("test-3", p.getName());
  105. assertEquals(36.6, p.getR8());
  106. assertEquals(4, p.getPid());
  107. p = ctProps.getPropertyArray(3);
  108. assertEquals("{D5CDD505-2E9C-101B-9397-08002B2CF9AE}", p.getFmtid());
  109. assertEquals("test-4", p.getName());
  110. assertEquals(true, p.getBool());
  111. assertEquals(5, p.getPid());
  112. }
  113. public void testDocumentProperties() {
  114. String category = _coreProperties.getCategory();
  115. assertEquals("test", category);
  116. String contentStatus = "Draft";
  117. _coreProperties.setContentStatus(contentStatus);
  118. assertEquals("Draft", contentStatus);
  119. Date created = _coreProperties.getCreated();
  120. // the original file contains a following value: 2009-07-20T13:12:00Z
  121. assertTrue(dateTimeEqualToUTCString(created, "2009-07-20T13:12:00Z"));
  122. String creator = _coreProperties.getCreator();
  123. assertEquals("Paolo Mottadelli", creator);
  124. String subject = _coreProperties.getSubject();
  125. assertEquals("Greetings", subject);
  126. String title = _coreProperties.getTitle();
  127. assertEquals("Hello World", title);
  128. }
  129. public void testTransitiveSetters() throws IOException {
  130. XWPFDocument doc = new XWPFDocument();
  131. CoreProperties cp = doc.getProperties().getCoreProperties();
  132. Date dateCreated = new GregorianCalendar(2010, 6, 15, 10, 0, 0).getTime();
  133. cp.setCreated(new Nullable<Date>(dateCreated));
  134. assertEquals(dateCreated.toString(), cp.getCreated().toString());
  135. doc = XWPFTestDataSamples.writeOutAndReadBack(doc);
  136. cp = doc.getProperties().getCoreProperties();
  137. Date dt3 = cp.getCreated();
  138. assertEquals(dateCreated.toString(), dt3.toString());
  139. }
  140. public void testGetSetRevision() {
  141. String revision = _coreProperties.getRevision();
  142. assertTrue("Revision number is 1", Integer.parseInt(revision) > 1);
  143. _coreProperties.setRevision("20");
  144. assertEquals("20", _coreProperties.getRevision());
  145. _coreProperties.setRevision("20xx");
  146. assertEquals("20", _coreProperties.getRevision());
  147. }
  148. public static boolean dateTimeEqualToUTCString(Date dateTime, String utcString) {
  149. Calendar utcCalendar = Calendar.getInstance(TimeZone.getTimeZone("UTC"), Locale.UK);
  150. utcCalendar.setTimeInMillis(dateTime.getTime());
  151. String dateTimeUtcString = utcCalendar.get(Calendar.YEAR) + "-" +
  152. zeroPad((utcCalendar.get(Calendar.MONTH)+1)) + "-" +
  153. zeroPad(utcCalendar.get(Calendar.DAY_OF_MONTH)) + "T" +
  154. zeroPad(utcCalendar.get(Calendar.HOUR_OF_DAY)) + ":" +
  155. zeroPad(utcCalendar.get(Calendar.MINUTE)) + ":" +
  156. zeroPad(utcCalendar.get(Calendar.SECOND)) + "Z";
  157. return utcString.equals(dateTimeUtcString);
  158. }
  159. private static String zeroPad(long i) {
  160. if (i >= 0 && i <=9) {
  161. return "0" + i;
  162. } else {
  163. return String.valueOf(i);
  164. }
  165. }
  166. }