aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNick Burch <nick@apache.org>2015-02-23 13:21:23 +0000
committerNick Burch <nick@apache.org>2015-02-23 13:21:23 +0000
commit92b1045a50eebc06f85c94a3e7672937caa00a25 (patch)
tree0d3221c331811f3b0fcac92ec206507e70f8c6db
parent0311b89efba3b11ce342096dc03ef6981c6c8144 (diff)
downloadpoi-92b1045a50eebc06f85c94a3e7672937caa00a25.tar.gz
poi-92b1045a50eebc06f85c94a3e7672937caa00a25.zip
Unit tests for OPC handling of files without a Core Properties part
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1661663 13f79535-47bb-0310-9956-ffa450edef68
-rw-r--r--src/ooxml/testcases/org/apache/poi/openxml4j/opc/compliance/TestOPCComplianceCoreProperties.java81
-rw-r--r--test-data/openxml4j/OPCCompliance_NoCoreProperties.xlsxbin0 -> 11483 bytes
2 files changed, 81 insertions, 0 deletions
diff --git a/src/ooxml/testcases/org/apache/poi/openxml4j/opc/compliance/TestOPCComplianceCoreProperties.java b/src/ooxml/testcases/org/apache/poi/openxml4j/opc/compliance/TestOPCComplianceCoreProperties.java
index 0cf5200092..cc6f7ca83d 100644
--- a/src/ooxml/testcases/org/apache/poi/openxml4j/opc/compliance/TestOPCComplianceCoreProperties.java
+++ b/src/ooxml/testcases/org/apache/poi/openxml4j/opc/compliance/TestOPCComplianceCoreProperties.java
@@ -17,6 +17,10 @@
package org.apache.poi.openxml4j.opc.compliance;
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.File;
+import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
@@ -34,6 +38,8 @@ import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.openxml4j.opc.PackageRelationshipTypes;
import org.apache.poi.openxml4j.opc.PackagingURIHelper;
import org.apache.poi.openxml4j.opc.TargetMode;
+import org.apache.poi.util.IOUtils;
+import org.apache.poi.util.TempFile;
/**
* Test core properties Open Packaging Convention compliance.
@@ -224,4 +230,79 @@ public final class TestOPCComplianceCoreProperties extends TestCase {
String msg = extractInvalidFormatMessage("LimitedXSITypeAttribute_PresentWithUnauthorizedValueFAIL.docx");
assertEquals("The element 'modified' must have the 'xsi:type' attribute with the value 'dcterms:W3CDTF' !", msg);
}
+
+ /**
+ * Document with no core properties - testing at the OPC level,
+ * saving into a new stream
+ */
+ public void testNoCoreProperties_saveNew() throws Exception {
+ String sampleFileName = "OPCCompliance_NoCoreProperties.xlsx";
+ OPCPackage pkg = null;
+ try {
+ pkg = OPCPackage.open(POIDataSamples.getOpenXML4JInstance().getFile(sampleFileName).getPath());
+ } catch (Exception e) {
+ throw new RuntimeException(e);
+ }
+
+ // Empty properties
+ assertEquals(0, pkg.getPartsByContentType(ContentTypes.CORE_PROPERTIES_PART).size());
+ assertNotNull(pkg.getPackageProperties());
+ assertNotNull(pkg.getPackageProperties().getLanguageProperty());
+ assertNull(pkg.getPackageProperties().getLanguageProperty().getValue());
+
+ // Save and re-load
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
+ pkg.save(baos);
+ ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
+
+ pkg = OPCPackage.open(bais);
+
+ // An Empty Properties part has been added in the save/load
+ assertEquals(1, pkg.getPartsByContentType(ContentTypes.CORE_PROPERTIES_PART).size());
+ assertNotNull(pkg.getPackageProperties());
+ assertNotNull(pkg.getPackageProperties().getLanguageProperty());
+ assertNull(pkg.getPackageProperties().getLanguageProperty().getValue());
+ }
+
+ /**
+ * Document with no core properties - testing at the OPC level,
+ * from a temp-file, saving in-place
+ */
+ public void testNoCoreProperties_saveInPlace() throws Exception {
+ String sampleFileName = "OPCCompliance_NoCoreProperties.xlsx";
+
+ // Copy this into a temp file, so we can play with it
+ File tmp = TempFile.createTempFile("poi-test", ".opc");
+ FileOutputStream out = new FileOutputStream(tmp);
+ IOUtils.copy(
+ POIDataSamples.getOpenXML4JInstance().openResourceAsStream(sampleFileName),
+ out);
+ out.close();
+
+ // Open it from that temp file
+ OPCPackage pkg = OPCPackage.open(tmp);
+
+ // Empty properties
+ assertEquals(0, pkg.getPartsByContentType(ContentTypes.CORE_PROPERTIES_PART).size());
+ assertNotNull(pkg.getPackageProperties());
+ assertNotNull(pkg.getPackageProperties().getLanguageProperty());
+ assertNull(pkg.getPackageProperties().getLanguageProperty().getValue());
+
+ // Save and close
+ pkg.close();
+
+
+ // Re-open and check
+ pkg = OPCPackage.open(tmp);
+
+ // An Empty Properties part has been added in the save/load
+ assertEquals(1, pkg.getPartsByContentType(ContentTypes.CORE_PROPERTIES_PART).size());
+ assertNotNull(pkg.getPackageProperties());
+ assertNotNull(pkg.getPackageProperties().getLanguageProperty());
+ assertNull(pkg.getPackageProperties().getLanguageProperty().getValue());
+
+ // Finish and tidy
+ pkg.revert();
+ tmp.delete();
+ }
}
diff --git a/test-data/openxml4j/OPCCompliance_NoCoreProperties.xlsx b/test-data/openxml4j/OPCCompliance_NoCoreProperties.xlsx
new file mode 100644
index 0000000000..fa1049c68d
--- /dev/null
+++ b/test-data/openxml4j/OPCCompliance_NoCoreProperties.xlsx
Binary files differ