2009-07-19 14:26:36 -04:00
|
|
|
/* ====================================================================
|
|
|
|
Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
contributor license agreements. See the NOTICE file distributed with
|
|
|
|
this work for additional information regarding copyright ownership.
|
|
|
|
The ASF licenses this file to You under the Apache License, Version 2.0
|
|
|
|
(the "License"); you may not use this file except in compliance with
|
|
|
|
the License. You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
==================================================================== */
|
|
|
|
|
|
|
|
package org.apache.poi;
|
|
|
|
|
2015-09-07 16:19:50 -04:00
|
|
|
import static org.junit.Assert.assertEquals;
|
|
|
|
import static org.junit.Assert.assertNotNull;
|
2016-06-21 10:27:41 -04:00
|
|
|
import static org.junit.Assert.assertNull;
|
2015-09-07 16:19:50 -04:00
|
|
|
import static org.junit.Assert.assertTrue;
|
|
|
|
import static org.junit.Assert.fail;
|
|
|
|
|
2016-06-21 10:27:41 -04:00
|
|
|
import java.io.ByteArrayInputStream;
|
2011-06-01 08:07:16 -04:00
|
|
|
import java.io.IOException;
|
|
|
|
import java.util.Calendar;
|
|
|
|
import java.util.Date;
|
2009-07-19 14:26:36 -04:00
|
|
|
|
2009-07-21 06:23:05 -04:00
|
|
|
import org.apache.poi.POIXMLProperties.CoreProperties;
|
2011-06-01 08:07:16 -04:00
|
|
|
import org.apache.poi.openxml4j.util.Nullable;
|
2015-09-07 16:19:50 -04:00
|
|
|
import org.apache.poi.util.LocaleUtil;
|
2009-07-20 13:10:43 -04:00
|
|
|
import org.apache.poi.xssf.XSSFTestDataSamples;
|
|
|
|
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
2009-08-21 02:08:31 -04:00
|
|
|
import org.apache.poi.xwpf.XWPFTestDataSamples;
|
2009-08-22 01:50:32 -04:00
|
|
|
import org.apache.poi.xwpf.usermodel.XWPFDocument;
|
2015-09-07 16:19:50 -04:00
|
|
|
import org.junit.After;
|
|
|
|
import org.junit.Before;
|
|
|
|
import org.junit.Test;
|
2009-07-20 13:10:43 -04:00
|
|
|
|
2009-07-19 14:26:36 -04:00
|
|
|
/**
|
|
|
|
* Test setting extended and custom OOXML properties
|
|
|
|
*/
|
2015-09-07 16:19:50 -04:00
|
|
|
public final class TestPOIXMLProperties {
|
|
|
|
private XWPFDocument sampleDoc;
|
2016-06-21 10:27:41 -04:00
|
|
|
private XWPFDocument sampleNoThumb;
|
2016-06-21 10:34:12 -04:00
|
|
|
private POIXMLProperties _props;
|
|
|
|
private CoreProperties _coreProperties;
|
2009-08-18 15:49:28 -04:00
|
|
|
|
2016-06-21 10:34:12 -04:00
|
|
|
@Before
|
|
|
|
public void setUp() throws IOException {
|
|
|
|
sampleDoc = XWPFTestDataSamples.openSampleDocument("documentProperties.docx");
|
|
|
|
sampleNoThumb = XWPFTestDataSamples.openSampleDocument("SampleDoc.docx");
|
2016-06-21 10:27:41 -04:00
|
|
|
assertNotNull(sampleDoc);
|
|
|
|
assertNotNull(sampleNoThumb);
|
2016-06-21 10:34:12 -04:00
|
|
|
_props = sampleDoc.getProperties();
|
|
|
|
_coreProperties = _props.getCoreProperties();
|
|
|
|
assertNotNull(_props);
|
|
|
|
}
|
|
|
|
|
|
|
|
@After
|
|
|
|
public void closeResources() throws Exception {
|
|
|
|
sampleDoc.close();
|
|
|
|
sampleNoThumb.close();
|
|
|
|
}
|
2009-07-19 14:26:36 -04:00
|
|
|
|
2016-06-21 10:34:12 -04:00
|
|
|
@Test
|
|
|
|
public void testWorkbookExtendedProperties() throws Exception {
|
|
|
|
XSSFWorkbook workbook = new XSSFWorkbook();
|
|
|
|
POIXMLProperties props = workbook.getProperties();
|
|
|
|
assertNotNull(props);
|
2009-08-18 15:49:28 -04:00
|
|
|
|
2016-06-21 10:34:12 -04:00
|
|
|
org.apache.poi.POIXMLProperties.ExtendedProperties properties =
|
|
|
|
props.getExtendedProperties();
|
2009-07-19 14:26:36 -04:00
|
|
|
|
2016-06-21 10:34:12 -04:00
|
|
|
org.openxmlformats.schemas.officeDocument.x2006.extendedProperties.CTProperties
|
|
|
|
ctProps = properties.getUnderlyingProperties();
|
2009-07-19 14:26:36 -04:00
|
|
|
|
|
|
|
|
2016-06-21 10:34:12 -04:00
|
|
|
String appVersion = "3.5 beta";
|
|
|
|
String application = "POI";
|
2009-07-19 14:26:36 -04:00
|
|
|
|
2016-06-21 10:34:12 -04:00
|
|
|
ctProps.setApplication(application);
|
|
|
|
ctProps.setAppVersion(appVersion);
|
2009-07-19 14:26:36 -04:00
|
|
|
|
2016-06-21 10:34:12 -04:00
|
|
|
XSSFWorkbook newWorkbook =
|
|
|
|
XSSFTestDataSamples.writeOutAndReadBack(workbook);
|
|
|
|
workbook.close();
|
|
|
|
assertTrue(workbook != newWorkbook);
|
2009-07-19 14:26:36 -04:00
|
|
|
|
|
|
|
|
2016-06-21 10:34:12 -04:00
|
|
|
POIXMLProperties newProps = newWorkbook.getProperties();
|
|
|
|
assertNotNull(newProps);
|
|
|
|
org.apache.poi.POIXMLProperties.ExtendedProperties newProperties =
|
|
|
|
newProps.getExtendedProperties();
|
2009-07-19 14:26:36 -04:00
|
|
|
|
2014-09-03 09:47:50 -04:00
|
|
|
assertEquals(application, newProperties.getApplication());
|
|
|
|
assertEquals(appVersion, newProperties.getAppVersion());
|
2009-07-19 14:26:36 -04:00
|
|
|
|
2016-06-21 10:34:12 -04:00
|
|
|
org.openxmlformats.schemas.officeDocument.x2006.extendedProperties.CTProperties
|
|
|
|
newCtProps = newProperties.getUnderlyingProperties();
|
|
|
|
|
|
|
|
assertEquals(application, newCtProps.getApplication());
|
|
|
|
assertEquals(appVersion, newCtProps.getAppVersion());
|
|
|
|
|
|
|
|
newWorkbook.close();
|
|
|
|
}
|
2009-07-19 14:26:36 -04:00
|
|
|
|
|
|
|
|
2009-10-04 06:00:52 -04:00
|
|
|
/**
|
|
|
|
* Test usermodel API for setting custom properties
|
|
|
|
*/
|
2016-06-21 10:34:12 -04:00
|
|
|
@Test
|
|
|
|
public void testCustomProperties() throws Exception {
|
2015-09-07 16:19:50 -04:00
|
|
|
POIXMLDocument wb1 = new XSSFWorkbook();
|
2009-10-04 06:00:52 -04:00
|
|
|
|
2015-09-07 16:19:50 -04:00
|
|
|
POIXMLProperties.CustomProperties customProps = wb1.getProperties().getCustomProperties();
|
2009-10-04 06:00:52 -04:00
|
|
|
customProps.addProperty("test-1", "string val");
|
|
|
|
customProps.addProperty("test-2", 1974);
|
|
|
|
customProps.addProperty("test-3", 36.6);
|
|
|
|
//adding a duplicate
|
|
|
|
try {
|
|
|
|
customProps.addProperty("test-3", 36.6);
|
|
|
|
fail("expected exception");
|
|
|
|
} catch(IllegalArgumentException e){
|
|
|
|
assertEquals("A property with this name already exists in the custom properties", e.getMessage());
|
|
|
|
}
|
|
|
|
customProps.addProperty("test-4", true);
|
|
|
|
|
2015-09-07 16:19:50 -04:00
|
|
|
POIXMLDocument wb2 = XSSFTestDataSamples.writeOutAndReadBack((XSSFWorkbook)wb1);
|
|
|
|
wb1.close();
|
2009-10-04 06:00:52 -04:00
|
|
|
org.openxmlformats.schemas.officeDocument.x2006.customProperties.CTProperties ctProps =
|
2015-09-07 16:19:50 -04:00
|
|
|
wb2.getProperties().getCustomProperties().getUnderlyingProperties();
|
2009-10-04 06:00:52 -04:00
|
|
|
assertEquals(4, ctProps.sizeOfPropertyArray());
|
|
|
|
org.openxmlformats.schemas.officeDocument.x2006.customProperties.CTProperty p;
|
|
|
|
|
|
|
|
p = ctProps.getPropertyArray(0);
|
|
|
|
assertEquals("{D5CDD505-2E9C-101B-9397-08002B2CF9AE}", p.getFmtid());
|
|
|
|
assertEquals("test-1", p.getName());
|
|
|
|
assertEquals("string val", p.getLpwstr());
|
|
|
|
assertEquals(2, p.getPid());
|
|
|
|
|
|
|
|
p = ctProps.getPropertyArray(1);
|
|
|
|
assertEquals("{D5CDD505-2E9C-101B-9397-08002B2CF9AE}", p.getFmtid());
|
|
|
|
assertEquals("test-2", p.getName());
|
|
|
|
assertEquals(1974, p.getI4());
|
|
|
|
assertEquals(3, p.getPid());
|
|
|
|
|
|
|
|
p = ctProps.getPropertyArray(2);
|
|
|
|
assertEquals("{D5CDD505-2E9C-101B-9397-08002B2CF9AE}", p.getFmtid());
|
|
|
|
assertEquals("test-3", p.getName());
|
2015-09-07 16:19:50 -04:00
|
|
|
assertEquals(36.6, p.getR8(), 0);
|
2009-10-04 06:00:52 -04:00
|
|
|
assertEquals(4, p.getPid());
|
|
|
|
|
|
|
|
p = ctProps.getPropertyArray(3);
|
|
|
|
assertEquals("{D5CDD505-2E9C-101B-9397-08002B2CF9AE}", p.getFmtid());
|
|
|
|
assertEquals("test-4", p.getName());
|
|
|
|
assertEquals(true, p.getBool());
|
|
|
|
assertEquals(5, p.getPid());
|
2015-09-07 16:19:50 -04:00
|
|
|
|
|
|
|
wb2.close();
|
2009-10-04 06:00:52 -04:00
|
|
|
}
|
|
|
|
|
2016-06-21 10:34:12 -04:00
|
|
|
@Test
|
|
|
|
public void testDocumentProperties() {
|
|
|
|
String category = _coreProperties.getCategory();
|
|
|
|
assertEquals("test", category);
|
|
|
|
String contentStatus = "Draft";
|
|
|
|
_coreProperties.setContentStatus(contentStatus);
|
|
|
|
assertEquals("Draft", contentStatus);
|
|
|
|
Date created = _coreProperties.getCreated();
|
|
|
|
// the original file contains a following value: 2009-07-20T13:12:00Z
|
|
|
|
assertTrue(dateTimeEqualToUTCString(created, "2009-07-20T13:12:00Z"));
|
|
|
|
String creator = _coreProperties.getCreator();
|
|
|
|
assertEquals("Paolo Mottadelli", creator);
|
|
|
|
String subject = _coreProperties.getSubject();
|
|
|
|
assertEquals("Greetings", subject);
|
|
|
|
String title = _coreProperties.getTitle();
|
|
|
|
assertEquals("Hello World", title);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void testTransitiveSetters() throws IOException {
|
|
|
|
XWPFDocument doc = new XWPFDocument();
|
2010-04-20 08:57:27 -04:00
|
|
|
CoreProperties cp = doc.getProperties().getCoreProperties();
|
|
|
|
|
2015-09-07 16:19:50 -04:00
|
|
|
|
|
|
|
Date dateCreated = LocaleUtil.getLocaleCalendar(2010, 6, 15, 10, 0, 0).getTime();
|
2010-04-20 08:57:27 -04:00
|
|
|
cp.setCreated(new Nullable<Date>(dateCreated));
|
2015-11-04 19:15:51 -05:00
|
|
|
assertEquals(dateCreated, cp.getCreated());
|
2010-04-20 08:57:27 -04:00
|
|
|
|
2015-09-07 16:19:50 -04:00
|
|
|
XWPFDocument doc2 = XWPFTestDataSamples.writeOutAndReadBack(doc);
|
|
|
|
doc.close();
|
2016-07-08 13:56:54 -04:00
|
|
|
cp = doc2.getProperties().getCoreProperties();
|
2010-04-20 08:57:27 -04:00
|
|
|
Date dt3 = cp.getCreated();
|
2015-11-04 19:15:51 -05:00
|
|
|
assertEquals(dateCreated, dt3);
|
2015-09-07 16:19:50 -04:00
|
|
|
doc2.close();
|
2010-04-20 08:57:27 -04:00
|
|
|
}
|
|
|
|
|
2016-06-21 10:34:12 -04:00
|
|
|
@Test
|
|
|
|
public void testGetSetRevision() {
|
|
|
|
String revision = _coreProperties.getRevision();
|
|
|
|
assertTrue("Revision number is 1", Integer.parseInt(revision) > 1);
|
|
|
|
_coreProperties.setRevision("20");
|
|
|
|
assertEquals("20", _coreProperties.getRevision());
|
|
|
|
_coreProperties.setRevision("20xx");
|
|
|
|
assertEquals("20", _coreProperties.getRevision());
|
|
|
|
}
|
|
|
|
|
2016-07-08 13:56:54 -04:00
|
|
|
@Test
|
2016-07-08 14:05:02 -04:00
|
|
|
public void testLastModifiedByUserProperty() {
|
|
|
|
String lastModifiedByUser = _coreProperties.getLastModifiedByUser();
|
|
|
|
assertEquals("Paolo Mottadelli", lastModifiedByUser);
|
|
|
|
_coreProperties.setLastModifiedByUser("Test User");
|
|
|
|
assertEquals("Test User", _coreProperties.getLastModifiedByUser());
|
2016-07-08 13:56:54 -04:00
|
|
|
}
|
|
|
|
|
2016-06-21 10:34:12 -04:00
|
|
|
public static boolean dateTimeEqualToUTCString(Date dateTime, String utcString) {
|
|
|
|
Calendar utcCalendar = LocaleUtil.getLocaleCalendar(LocaleUtil.TIMEZONE_UTC);
|
2010-04-20 08:57:27 -04:00
|
|
|
utcCalendar.setTimeInMillis(dateTime.getTime());
|
|
|
|
String dateTimeUtcString = utcCalendar.get(Calendar.YEAR) + "-" +
|
2016-06-21 10:34:12 -04:00
|
|
|
zeroPad((utcCalendar.get(Calendar.MONTH)+1)) + "-" +
|
|
|
|
zeroPad(utcCalendar.get(Calendar.DAY_OF_MONTH)) + "T" +
|
|
|
|
zeroPad(utcCalendar.get(Calendar.HOUR_OF_DAY)) + ":" +
|
|
|
|
zeroPad(utcCalendar.get(Calendar.MINUTE)) + ":" +
|
|
|
|
zeroPad(utcCalendar.get(Calendar.SECOND)) + "Z";
|
|
|
|
|
2010-04-20 08:57:27 -04:00
|
|
|
return utcString.equals(dateTimeUtcString);
|
|
|
|
}
|
2016-06-21 10:34:12 -04:00
|
|
|
|
|
|
|
public void testThumbnails() throws Exception {
|
|
|
|
POIXMLProperties noThumbProps = sampleNoThumb.getProperties();
|
|
|
|
|
|
|
|
assertNotNull(_props.getThumbnailPart());
|
|
|
|
assertNull(noThumbProps.getThumbnailPart());
|
|
|
|
|
2016-06-21 10:27:41 -04:00
|
|
|
assertNotNull(_props.getThumbnailFilename());
|
|
|
|
assertNull(noThumbProps.getThumbnailFilename());
|
2016-06-21 10:34:12 -04:00
|
|
|
|
2016-06-21 10:27:41 -04:00
|
|
|
assertNotNull(_props.getThumbnailImage());
|
|
|
|
assertNull(noThumbProps.getThumbnailImage());
|
2016-06-21 10:34:12 -04:00
|
|
|
|
2016-06-21 10:27:41 -04:00
|
|
|
assertEquals("thumbnail.jpeg", _props.getThumbnailFilename());
|
2016-06-21 10:34:12 -04:00
|
|
|
|
|
|
|
|
2016-06-21 10:27:41 -04:00
|
|
|
// Adding / changing
|
|
|
|
noThumbProps.setThumbnail("Testing.png", new ByteArrayInputStream(new byte[1]));
|
|
|
|
assertNotNull(noThumbProps.getThumbnailPart());
|
|
|
|
assertEquals("Testing.png", noThumbProps.getThumbnailFilename());
|
|
|
|
assertNotNull(noThumbProps.getThumbnailImage());
|
|
|
|
assertEquals(1, noThumbProps.getThumbnailImage().available());
|
2016-06-21 10:34:12 -04:00
|
|
|
|
2016-06-21 10:27:41 -04:00
|
|
|
noThumbProps.setThumbnail("Testing2.png", new ByteArrayInputStream(new byte[2]));
|
|
|
|
assertNotNull(noThumbProps.getThumbnailPart());
|
|
|
|
assertEquals("Testing.png", noThumbProps.getThumbnailFilename());
|
|
|
|
assertNotNull(noThumbProps.getThumbnailImage());
|
|
|
|
assertEquals(2, noThumbProps.getThumbnailImage().available());
|
2016-06-21 10:34:12 -04:00
|
|
|
}
|
2010-04-20 08:57:27 -04:00
|
|
|
|
2016-06-21 10:34:12 -04:00
|
|
|
private static String zeroPad(long i) {
|
2010-04-20 08:57:27 -04:00
|
|
|
if (i >= 0 && i <=9) {
|
|
|
|
return "0" + i;
|
|
|
|
} else {
|
|
|
|
return String.valueOf(i);
|
|
|
|
}
|
|
|
|
}
|
2009-08-18 15:49:28 -04:00
|
|
|
}
|