From 327df5cf76dfad1150c1a28c316782bf6ff99874 Mon Sep 17 00:00:00 2001 From: Nick Burch Date: Mon, 23 Feb 2015 13:21:23 +0000 Subject: [PATCH] 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 --- .../TestOPCComplianceCoreProperties.java | 81 ++++++++++++++++++ .../OPCCompliance_NoCoreProperties.xlsx | Bin 0 -> 11483 bytes 2 files changed, 81 insertions(+) create mode 100644 test-data/openxml4j/OPCCompliance_NoCoreProperties.xlsx 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 0cf520009..cc6f7ca83 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 0000000000000000000000000000000000000000..fa1049c68d788b928b5bb7c8d44eff9560ab0474 GIT binary patch literal 11483 zcmeHNWmJ?~)E=Z6NS%~;*g;DM8- zLFK*pFn57vOY*m`n2W*hoX=E220Y{icH?R!0ci9Uje5$9riDqd z>{w}QTcYI~n=t4oyM!SmoJN@$_g>_xML07I8^{xtk-u~h|I*^yA`qZFtL}r%o$>aL z>Ln$93FqP7R;N*Kqqo&2*My$fbSPL4gSXWV`FA3bN{sUn^7MG-+^FR5TVXS}()k-W z;C+TcoC$>X*0Y8WD49poPZFysUj%I4kj!rJ@#|{&LJL}%x9d-n9;(Y*s8W5=?9UXM! zeYihyPeYhGIsN5&K=xa_qvt!Z*y z6UNb}Jx<81^RWnit|G?v?A^QS=BbV_x~0wV4uktv)hzw4(PfxPn^`v@0rq@2x>c-F z8iiWDLQMS#kt3~dfF&WK;&h1d^uS=`XJBZ6OV1S^3`VdUIM>ULAj@|6rir#}Lrq^1 z7XM_KaNR^PqZ56st|^DPT(gYgp{>E4k(q{^4r)3vW}hHXxK8ZL*t+lt3U8T#&KeY! zt$2b~E3BO>q=GF*E34Z16{W>WO{E%y5-VQl>>4aZQjAH?Tln@lCcSNoslwKx_#-j51rLA%$;xZ4JCB_6&<_p}r^UVnM9_Bu zfdcoYiL-^>*JyD6T{IlM-}MBAyB8i%M~Mym>wUH(l;e;(0y%oV3xtBm4uwVzT)$=S z5RNa4t{0>;KVFmWPn7rei&q-z;>C|vhd4U2r6VKbP|PkTI%bR5&!>+vS#(M?lsJg1 z7cY1bUlIO-6KtraujFJoAx_E`JJF-Ph<{nOP3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