예제 #1
0
        public void AssertReadWrite(OPCPackage pkg1)
        {
            OPCParser doc = new OPCParser(pkg1);

            doc.Parse(new TestFactory());

            Dictionary <String, POIXMLDocumentPart> context = new Dictionary <String, POIXMLDocumentPart>();

            Traverse(doc, context);
            context.Clear();

            string     tmp  = TempFile.GetTempFilePath("poi-ooxml", ".tmp");
            FileStream out1 = new FileStream(tmp, FileMode.CreateNew);

            doc.Write(out1);
            out1.Close();

            OPCPackage pkg2 = OPCPackage.Open(tmp);

            try
            {
                doc = new OPCParser(pkg1);
                doc.Parse(new TestFactory());
                context = new Dictionary <String, POIXMLDocumentPart>();
                Traverse(doc, context);
                context.Clear();

                Assert.AreEqual(pkg1.Relationships.Size, pkg2.Relationships.Size);

                List <PackagePart> l1 = pkg1.GetParts();
                List <PackagePart> l2 = pkg2.GetParts();

                Assert.AreEqual(l1.Count, l2.Count);
                for (int i = 0; i < l1.Count; i++)
                {
                    PackagePart p1 = l1[i];
                    PackagePart p2 = l2[i];

                    Assert.AreEqual(p1.ContentType, p2.ContentType);
                    Assert.AreEqual(p1.HasRelationships, p2.HasRelationships);
                    if (p1.HasRelationships)
                    {
                        Assert.AreEqual(p1.Relationships.Size, p2.Relationships.Size);
                    }
                    Assert.AreEqual(p1.PartName, p2.PartName);
                }
            }
            finally
            {
                pkg2.Revert();
            }
        }
예제 #2
0
        public void AssertReadWrite(OPCPackage pkg1)
        {
            OPCParser doc = new OPCParser(pkg1);
            doc.Parse(new TestFactory());

            Dictionary<String, POIXMLDocumentPart> context = new Dictionary<String, POIXMLDocumentPart>();
            Traverse(doc, context);
            context.Clear();

            string tmp = TempFile.GetTempFilePath("poi-ooxml", ".tmp");
            FileStream out1 = new FileStream(tmp, FileMode.CreateNew);
            doc.Write(out1);
            out1.Close();

            OPCPackage pkg2 = OPCPackage.Open(tmp);

            doc = new OPCParser(pkg1);
            doc.Parse(new TestFactory());
            context = new Dictionary<String, POIXMLDocumentPart>();
            Traverse(doc, context);
            context.Clear();

            Assert.AreEqual(pkg1.Relationships.Size, pkg2.Relationships.Size);

            List<PackagePart> l1 = pkg1.GetParts();
            List<PackagePart> l2 = pkg2.GetParts();

            Assert.AreEqual(l1.Count, l2.Count);
            for (int i = 0; i < l1.Count; i++)
            {
                PackagePart p1 = l1[i];
                PackagePart p2 = l2[i];

                Assert.AreEqual(p1.ContentType, p2.ContentType);
                Assert.AreEqual(p1.HasRelationships, p2.HasRelationships);
                if (p1.HasRelationships)
                {
                    Assert.AreEqual(p1.Relationships.Size, p2.Relationships.Size);
                }
                Assert.AreEqual(p1.PartName, p2.PartName);
            }
        }
예제 #3
0
        public void AssertReadWrite(OPCPackage pkg1)
        {
            OPCParser doc = new OPCParser(pkg1);

            doc.Parse(new TestFactory());

            Dictionary <String, POIXMLDocumentPart> context = new Dictionary <String, POIXMLDocumentPart>();

            Traverse(doc, context);
            context.Clear();

            string     tmp  = TempFile.GetTempFilePath("poi-ooxml", ".tmp");
            FileStream out1 = new FileStream(tmp, FileMode.CreateNew);

            doc.Write(out1);
            out1.Close();

            // Should not be able to write to an output stream that has been closed
            try
            {
                doc.Write(out1);
                Assert.Fail("Should not be able to write to an output stream that has been closed.");
            }
            catch (OpenXML4NetRuntimeException e) {
                //OpenXml4NetRuntimeException
                // FIXME: A better exception class (IOException?) and message should be raised
                // indicating that the document could not be written because the output stream is closed.
                // see {@link org.apache.poi.openxml4j.opc.ZipPackage#saveImpl(java.io.OutputStream)}
                if (Regex.IsMatch(e.Message, "Fail to save: an error occurs while saving the package : Must support writing.+"))
                {
                    // expected
                }
                else
                {
                    throw e;
                }
            }

            // Should not be able to write a document that has been closed
            doc.Close();
            try
            {
                doc.Write(new NullOutputStream());
                Assert.Fail("Should not be able to write a document that has been closed.");
            }
            catch (IOException e) {
                if (e.Message.Equals("Cannot write data, document seems to have been closed already"))
                {
                    // expected
                }
                else
                {
                    throw e;
                }
            }

            // Should be able to close a document multiple times, though subsequent closes will have no effect.
            doc.Close();

            OPCPackage pkg2 = OPCPackage.Open(tmp);

            doc = new OPCParser(pkg1);
            try
            {
                doc = new OPCParser(pkg1);
                doc.Parse(new TestFactory());
                context = new Dictionary <String, POIXMLDocumentPart>();
                Traverse(doc, context);
                context.Clear();

                Assert.AreEqual(pkg1.Relationships.Size, pkg2.Relationships.Size);

                List <PackagePart> l1 = pkg1.GetParts();
                List <PackagePart> l2 = pkg2.GetParts();

                Assert.AreEqual(l1.Count, l2.Count);
                for (int i = 0; i < l1.Count; i++)
                {
                    PackagePart p1 = l1[i];
                    PackagePart p2 = l2[i];

                    Assert.AreEqual(p1.ContentType, p2.ContentType);
                    Assert.AreEqual(p1.HasRelationships, p2.HasRelationships);
                    if (p1.HasRelationships)
                    {
                        Assert.AreEqual(p1.Relationships.Size, p2.Relationships.Size);
                    }
                    Assert.AreEqual(p1.PartName, p2.PartName);
                }
            }
            finally
            {
                doc.Close();
                pkg1.Close();
                pkg2.Close();
            }
        }