public void TestPartNameDerivationReadingAssert_Failure() { String filename = "OPCCompliance_DerivedPartNameFail.docx"; try { OPCPackage.Open(POIDataSamples.GetOpenXML4JInstance().OpenResourceAsStream(filename)); } catch (InvalidFormatException e) { return; } Assert.Fail("A package implementer shall neither create nor recognize a part with a" + " part name derived from another part name by appending segments to it." + " [M1.11]"); }
public void EncryptPackageWithoutCoreProperties() { // Open our file without core properties FileStream inp = POIDataSamples.GetOpenXML4JInstance().GetFile("OPCCompliance_NoCoreProperties.xlsx"); OPCPackage pkg = OPCPackage.Open(inp.Name); // It doesn't have any core properties yet Assert.AreEqual(0, pkg.GetPartsByContentType(ContentTypes.CORE_PROPERTIES_PART).Count); Assert.IsNotNull(pkg.GetPackageProperties()); Assert.IsNotNull(pkg.GetPackageProperties().GetLanguageProperty()); //Assert.IsNull(pkg.GetPackageProperties().GetLanguageProperty().GetValue()); // Encrypt it EncryptionInfo info = new EncryptionInfo(EncryptionMode.Agile); NPOIFSFileSystem fs = new NPOIFSFileSystem(); Encryptor enc = info.Encryptor; enc.ConfirmPassword("password"); OutputStream os = enc.GetDataStream(fs); pkg.Save(os); pkg.Revert(); // Save the resulting OLE2 document, and re-open it MemoryStream baos = new MemoryStream(); fs.WriteFileSystem(baos); MemoryStream bais = new MemoryStream(baos.ToArray()); NPOIFSFileSystem inpFS = new NPOIFSFileSystem(bais); // Check we can decrypt it info = new EncryptionInfo(inpFS); Decryptor d = Decryptor.GetInstance(info); Assert.AreEqual(true, d.VerifyPassword("password")); OPCPackage inpPkg = OPCPackage.Open(d.GetDataStream(inpFS)); // Check it now has empty core properties Assert.AreEqual(1, inpPkg.GetPartsByContentType(ContentTypes.CORE_PROPERTIES_PART).Count); Assert.IsNotNull(inpPkg.GetPackageProperties()); Assert.IsNotNull(inpPkg.GetPackageProperties().GetLanguageProperty()); //Assert.IsNull(inpPkg.PackageProperties.LanguageProperty.Value); }
public void TestNoCoreProperties_saveNew() { String sampleFileName = "OPCCompliance_NoCoreProperties.xlsx"; OPCPackage pkg = OPCPackage.Open(POIDataSamples.GetOpenXML4JInstance().GetFileInfo(sampleFileName).FullName); // Verify it has empty properties Assert.AreEqual(0, pkg.GetPartsByContentType(ContentTypes.CORE_PROPERTIES_PART).Count); Assert.IsNotNull(pkg.GetPackageProperties()); Assert.IsNull(pkg.GetPackageProperties().GetLanguageProperty()); //Assert.IsNull(pkg.GetPackageProperties().GetLanguageProperty().Value); // Save and re-load MemoryStream baos = new MemoryStream(); pkg.Save(baos); MemoryStream bais = new MemoryStream(baos.ToArray()); pkg.Revert(); pkg = OPCPackage.Open(bais); // An Empty Properties part has been Added in the save/load Assert.AreEqual(1, pkg.GetPartsByContentType(ContentTypes.CORE_PROPERTIES_PART).Count); Assert.IsNotNull(pkg.GetPackageProperties()); Assert.IsNull(pkg.GetPackageProperties().GetLanguageProperty()); //Assert.IsNull(pkg.GetPackageProperties().GetLanguageProperty().Value); pkg.Close(); // Open a new copy of it pkg = OPCPackage.Open(POIDataSamples.GetOpenXML4JInstance().GetFileInfo(sampleFileName).FullName); // Save and re-load, without having touched the properties yet baos = new MemoryStream(); pkg.Save(baos); pkg.Revert(); bais = new MemoryStream(baos.ToArray()); pkg = OPCPackage.Open(bais); // Check that this too Added empty properties without error Assert.AreEqual(1, pkg.GetPartsByContentType(ContentTypes.CORE_PROPERTIES_PART).Count); Assert.IsNotNull(pkg.GetPackageProperties()); Assert.IsNull(pkg.GetPackageProperties().GetLanguageProperty()); //Assert.IsNull(pkg.GetPackageProperties().GetLanguageProperty().Value); }
public void TestNoCoreProperties_saveInPlace() { String sampleFileName = "OPCCompliance_NoCoreProperties.xlsx"; // Copy this into a temp file, so we can play with it FileInfo tmp = TempFile.CreateTempFile("poi-test", ".opc"); FileStream out1 = new FileStream(tmp.FullName, FileMode.Create, FileAccess.ReadWrite); Stream in1 = POIDataSamples.GetOpenXML4JInstance().OpenResourceAsStream(sampleFileName); IOUtils.Copy( in1, out1); out1.Close(); in1.Close(); // Open it from that temp file OPCPackage pkg = OPCPackage.Open(tmp); // Empty properties Assert.AreEqual(0, pkg.GetPartsByContentType(ContentTypes.CORE_PROPERTIES_PART).Count); Assert.IsNotNull(pkg.GetPackageProperties()); Assert.IsNull(pkg.GetPackageProperties().GetLanguageProperty()); //Assert.IsNull(pkg.GetPackageProperties().GetLanguageProperty().Value); // Save and close pkg.Close(); // Re-open and check pkg = OPCPackage.Open(tmp); // An Empty Properties part has been Added in the save/load Assert.AreEqual(1, pkg.GetPartsByContentType(ContentTypes.CORE_PROPERTIES_PART).Count); Assert.IsNotNull(pkg.GetPackageProperties()); Assert.IsNull(pkg.GetPackageProperties().GetLanguageProperty()); //Assert.IsNull(pkg.GetPackageProperties().GetLanguageProperty().Value); // Finish and tidy pkg.Revert(); tmp.Delete(); Assert.AreEqual(0, Directory.GetFiles(AppDomain.CurrentDomain.BaseDirectory, "*.tmp").Length, "At Last: There are no temporary files."); }
public void TestOnlyOneCorePropertiesPart_AddPart() { String sampleFileName = "OPCCompliance_CoreProperties_OnlyOneCorePropertiesPart.docx"; OPCPackage pkg = null; pkg = OPCPackage.Open(POIDataSamples.GetOpenXML4JInstance().GetFile(sampleFileName)); Uri partUri = CreateURI("/docProps/core2.xml"); try { pkg.CreatePart(PackagingUriHelper.CreatePartName(partUri), ContentTypes.CORE_PROPERTIES_PART); // no longer fail on compliance error //fail("expected OPC compliance exception was not thrown"); } catch (InvalidOperationException e) { // expected during successful test Assert.AreEqual("OPC Compliance error [M4.1]: you try to add more than one core properties relationship in the package !", e.Message); } pkg.Revert(); }