*/
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);
- }
+ OPCPackage pkg = OPCPackage.open(POIDataSamples.getOpenXML4JInstance().getFile(sampleFileName).getPath());
- // Empty properties
+ // Verify it has empty properties
assertEquals(0, pkg.getPartsByContentType(ContentTypes.CORE_PROPERTIES_PART).size());
assertNotNull(pkg.getPackageProperties());
assertNotNull(pkg.getPackageProperties().getLanguageProperty());
assertNotNull(pkg.getPackageProperties());
assertNotNull(pkg.getPackageProperties().getLanguageProperty());
assertNull(pkg.getPackageProperties().getLanguageProperty().getValue());
+
+
+ // Open a new copy of it
+ pkg = OPCPackage.open(POIDataSamples.getOpenXML4JInstance().getFile(sampleFileName).getPath());
+
+ // Save and re-load, without having touched the properties yet
+ baos = new ByteArrayOutputStream();
+ pkg.save(baos);
+ bais = new ByteArrayInputStream(baos.toByteArray());
+ pkg = OPCPackage.open(bais);
+
+ // Check that this too added empty properties without error
+ assertEquals(1, pkg.getPartsByContentType(ContentTypes.CORE_PROPERTIES_PART).size());
+ assertNotNull(pkg.getPackageProperties());
+ assertNotNull(pkg.getPackageProperties().getLanguageProperty());
+ assertNull(pkg.getPackageProperties().getLanguageProperty().getValue());
}
/**
\r
import static org.junit.Assert.assertArrayEquals;\r
import static org.junit.Assert.assertEquals;\r
+import static org.junit.Assert.assertNotNull;\r
+import static org.junit.Assert.assertNull;\r
import static org.junit.Assert.assertTrue;\r
\r
import java.io.ByteArrayInputStream;\r
import javax.crypto.Cipher;\r
\r
import org.apache.poi.POIDataSamples;\r
+import org.apache.poi.openxml4j.opc.ContentTypes;\r
+import org.apache.poi.openxml4j.opc.OPCPackage;\r
import org.apache.poi.poifs.crypt.agile.AgileEncryptionHeader;\r
import org.apache.poi.poifs.filesystem.DirectoryNode;\r
import org.apache.poi.poifs.filesystem.DocumentNode;\r
assertArrayEquals(payloadExpected, payloadActual);\r
}\r
\r
+ /**\r
+ * Ensure we can encrypt a package that is missing the Core\r
+ * Properties, eg one from dodgy versions of Jasper Reports \r
+ * See https://github.com/nestoru/xlsxenc/ and\r
+ * http://stackoverflow.com/questions/28593223\r
+ */\r
+ @Test\r
+ public void encryptPackageWithoutCoreProperties() throws Exception {\r
+ // Open our file without core properties\r
+ File inp = POIDataSamples.getOpenXML4JInstance().getFile("OPCCompliance_NoCoreProperties.xlsx");\r
+ OPCPackage pkg = OPCPackage.open(inp.getPath());\r
+ \r
+ // It doesn't have any core properties yet\r
+ assertEquals(0, pkg.getPartsByContentType(ContentTypes.CORE_PROPERTIES_PART).size());\r
+ assertNotNull(pkg.getPackageProperties());\r
+ assertNotNull(pkg.getPackageProperties().getLanguageProperty());\r
+ assertNull(pkg.getPackageProperties().getLanguageProperty().getValue());\r
+ \r
+ // Encrypt it\r
+ EncryptionInfo info = new EncryptionInfo(EncryptionMode.agile);\r
+ NPOIFSFileSystem fs = new NPOIFSFileSystem();\r
+ \r
+ Encryptor enc = info.getEncryptor();\r
+ enc.confirmPassword("password");\r
+ OutputStream os = enc.getDataStream(fs);\r
+ pkg.save(os);\r
+ pkg.revert();\r
+ \r
+ // Save the resulting OLE2 document, and re-open it\r
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();\r
+ fs.writeFilesystem(baos);\r
+ \r
+ ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());\r
+ NPOIFSFileSystem inpFS = new NPOIFSFileSystem(bais);\r
+ \r
+ // Check we can decrypt it\r
+ info = new EncryptionInfo(inpFS);\r
+ Decryptor d = Decryptor.getInstance(info);\r
+ assertEquals(true, d.verifyPassword("password"));\r
+ \r
+ OPCPackage inpPkg = OPCPackage.open(d.getDataStream(inpFS));\r
+ \r
+ // Check it now has empty core properties\r
+ assertEquals(1, inpPkg.getPartsByContentType(ContentTypes.CORE_PROPERTIES_PART).size());\r
+ assertNotNull(inpPkg.getPackageProperties());\r
+ assertNotNull(inpPkg.getPackageProperties().getLanguageProperty());\r
+ assertNull(inpPkg.getPackageProperties().getLanguageProperty().getValue());\r
+ }\r
+ \r
@Test\r
@Ignore\r
public void inPlaceRewrite() throws Exception {\r