コード例 #1
0
        public void TestOnlyOneCorePropertiesPart_AddPart()
        {
            String     sampleFileName = "OPCCompliance_CoreProperties_OnlyOneCorePropertiesPart.docx";
            OPCPackage pkg            = null;

            pkg = OPCPackage.Open(POIDataSamples.GetOpenXml4NetInstance().GetFile(sampleFileName));


            Uri partUri = CreateURI("/docProps/core2.xml");

            try
            {
                pkg.CreatePart(PackagingUriHelper.CreatePartName(partUri),
                               ContentTypes.CORE_PROPERTIES_PART);
                Assert.Fail("expected OPC compliance exception was not thrown");
            }
            catch (InvalidFormatException e)
            {
                throw;
            }
            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();
        }
コード例 #2
0
        public void TestPartNameDerivationReadingAssert_Failure()
        {
            string filename = "OPCCompliance_DerivedPartNameFail.docx";

            try {
                OPCPackage.Open(POIDataSamples.GetOpenXml4NetInstance().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]");
        }
コード例 #3
0
        public void TestNoCoreProperties_saveNew()
        {
            String     sampleFileName = "OPCCompliance_NoCoreProperties.xlsx";
            OPCPackage pkg            = OPCPackage.Open(POIDataSamples.GetOpenXml4NetInstance().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.GetOpenXml4NetInstance().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);
        }
コード例 #4
0
        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.GetOpenXml4NetInstance().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.");
        }