Example #1
0
        public void TestAvailable()
        {
            DocumentInputStream ostream = new DocumentInputStream(_workbook_o);
            DocumentInputStream nstream = new NDocumentInputStream(_workbook_n);

            Assert.AreEqual(_workbook_size, ostream.Available());
            Assert.AreEqual(_workbook_size, nstream.Available());
            ostream.Close();
            nstream.Close();

            try
            {
                ostream.Available();
                Assert.Fail("Should have caught IOException");
            }
            catch (InvalidOperationException)
            {
                // as expected
            }
            try
            {
                nstream.Available();
                Assert.Fail("Should have caught IOException");
            }
            catch (InvalidOperationException)
            {
                // as expected
            }
        }
Example #2
0
        protected void assertContentsMatches(byte[] expected, DocumentEntry doc)
        {
            NDocumentInputStream inp = new NDocumentInputStream(doc);

            byte[] contents = new byte[doc.Size];
            Assert.AreEqual(doc.Size, inp.Read(contents));
            inp.Close();

            if (expected != null)
            {
                Assert.That(expected, new EqualConstraint(contents));
            }
        }
Example #3
0
        public void TestConstructor()
        {
            DocumentInputStream ostream = new ODocumentInputStream(_workbook_o);
            DocumentInputStream nstream = new NDocumentInputStream(_workbook_n);

            Assert.AreEqual(_workbook_size, _workbook_o.Size);
            Assert.AreEqual(_workbook_size, _workbook_n.Size);
            Assert.AreEqual(_workbook_size, ostream.Available());
            Assert.AreEqual(_workbook_size, nstream.Available());

            ostream.Close();
            nstream.Close();
        }
Example #4
0
        public void TestInPlaceNPOIFSWrite()
        {
            NPOIFSFileSystem           fs      = null;
            DirectoryEntry             root    = null;
            DocumentNode               sinfDoc = null;
            DocumentNode               dinfDoc = null;
            SummaryInformation         sinf    = null;
            DocumentSummaryInformation dinf    = null;

            // We need to work on a File for in-place changes, so create a temp one
            FileInfo copy = TempFile.CreateTempFile("Test-HPSF", "ole2");
            //copy.DeleteOnExit();

            // Copy a test file over to a temp location
            Stream     inp  = _samples.OpenResourceAsStream("TestShiftJIS.doc");
            FileStream out1 = new FileStream(copy.FullName, FileMode.Create);

            IOUtils.Copy(inp, out1);
            inp.Close();
            out1.Close();

            // Open the copy in Read/write mode
            fs   = new NPOIFSFileSystem(copy, false);
            root = fs.Root;

            // Read the properties in there
            sinfDoc = (DocumentNode)root.GetEntry(SummaryInformation.DEFAULT_STREAM_NAME);
            dinfDoc = (DocumentNode)root.GetEntry(DocumentSummaryInformation.DEFAULT_STREAM_NAME);

            InputStream sinfStream = new NDocumentInputStream(sinfDoc);

            sinf = (SummaryInformation)PropertySetFactory.Create(sinfStream);
            sinfStream.Close();
            Assert.AreEqual(131077, sinf.OSVersion);

            InputStream dinfStream = new NDocumentInputStream(dinfDoc);

            dinf = (DocumentSummaryInformation)PropertySetFactory.Create(dinfStream);
            dinfStream.Close();
            Assert.AreEqual(131077, dinf.OSVersion);

            // Check they start as we expect
            Assert.AreEqual("Reiichiro Hori", sinf.Author);
            Assert.AreEqual("Microsoft Word 9.0", sinf.ApplicationName);
            Assert.AreEqual("\u7b2c1\u7ae0", sinf.Title);

            Assert.AreEqual("", dinf.Company);
            Assert.AreEqual(null, dinf.Manager);

            // Do an in-place replace via an InputStream
            new NPOIFSDocument(sinfDoc).ReplaceContents(sinf.ToInputStream());
            new NPOIFSDocument(dinfDoc).ReplaceContents(dinf.ToInputStream());


            // Check it didn't Get Changed
            sinfDoc = (DocumentNode)root.GetEntry(SummaryInformation.DEFAULT_STREAM_NAME);
            dinfDoc = (DocumentNode)root.GetEntry(DocumentSummaryInformation.DEFAULT_STREAM_NAME);

            InputStream sinfStream2 = new NDocumentInputStream(sinfDoc);

            sinf = (SummaryInformation)PropertySetFactory.Create(sinfStream2);
            sinfStream2.Close();
            Assert.AreEqual(131077, sinf.OSVersion);

            InputStream dinfStream2 = new NDocumentInputStream(dinfDoc);

            dinf = (DocumentSummaryInformation)PropertySetFactory.Create(dinfStream2);
            dinfStream2.Close();
            Assert.AreEqual(131077, dinf.OSVersion);


            // Start again!
            fs.Close();
            inp  = _samples.OpenResourceAsStream("TestShiftJIS.doc");
            out1 = new FileStream(copy.FullName, FileMode.Open, FileAccess.ReadWrite);
            IOUtils.Copy(inp, out1);
            inp.Close();
            out1.Close();

            fs = new NPOIFSFileSystem(new FileStream(copy.FullName, FileMode.Open, FileAccess.ReadWrite),
                                      null, false, true);
            root = fs.Root;

            // Read the properties in once more
            sinfDoc = (DocumentNode)root.GetEntry(SummaryInformation.DEFAULT_STREAM_NAME);
            dinfDoc = (DocumentNode)root.GetEntry(DocumentSummaryInformation.DEFAULT_STREAM_NAME);

            InputStream sinfStream3 = new NDocumentInputStream(sinfDoc);

            sinf = (SummaryInformation)PropertySetFactory.Create(sinfStream3);
            sinfStream3.Close();
            Assert.AreEqual(131077, sinf.OSVersion);

            InputStream dinfStream3 = new NDocumentInputStream(dinfDoc);

            dinf = (DocumentSummaryInformation)PropertySetFactory.Create(dinfStream3);
            dinfStream3.Close();
            Assert.AreEqual(131077, dinf.OSVersion);


            // Have them write themselves in-place with no Changes
            Stream soufStream = new NDocumentOutputStream(sinfDoc);

            sinf.Write(soufStream);
            soufStream.Close();
            Stream doufStream = new NDocumentOutputStream(dinfDoc);

            dinf.Write(doufStream);
            doufStream.Close();

            // And also write to some bytes for Checking
            MemoryStream sinfBytes = new MemoryStream();

            sinf.Write(sinfBytes);
            MemoryStream dinfBytes = new MemoryStream();

            dinf.Write(dinfBytes);


            // Check that the filesystem can give us back the same bytes
            sinfDoc = (DocumentNode)root.GetEntry(SummaryInformation.DEFAULT_STREAM_NAME);
            dinfDoc = (DocumentNode)root.GetEntry(DocumentSummaryInformation.DEFAULT_STREAM_NAME);

            InputStream sinfStream4 = new NDocumentInputStream(sinfDoc);

            byte[] sinfData = IOUtils.ToByteArray(sinfStream4);
            sinfStream4.Close();
            InputStream dinfStream4 = new NDocumentInputStream(dinfDoc);

            byte[] dinfData = IOUtils.ToByteArray(dinfStream4);
            dinfStream4.Close();
            Assert.That(sinfBytes.ToArray(), new EqualConstraint(sinfData));
            Assert.That(dinfBytes.ToArray(), new EqualConstraint(dinfData));


            // Read back in as-is
            InputStream sinfStream5 = new NDocumentInputStream(sinfDoc);

            sinf = (SummaryInformation)PropertySetFactory.Create(sinfStream5);
            sinfStream5.Close();
            Assert.AreEqual(131077, sinf.OSVersion);

            InputStream dinfStream5 = new NDocumentInputStream(dinfDoc);

            dinf = (DocumentSummaryInformation)PropertySetFactory.Create(dinfStream5);
            dinfStream5.Close();
            Assert.AreEqual(131077, dinf.OSVersion);

            Assert.AreEqual("Reiichiro Hori", sinf.Author);
            Assert.AreEqual("Microsoft Word 9.0", sinf.ApplicationName);
            Assert.AreEqual("\u7b2c1\u7ae0", sinf.Title);

            Assert.AreEqual("", dinf.Company);
            Assert.AreEqual(null, dinf.Manager);


            // Now alter a few of them
            sinf.Author  = (/*setter*/ "Changed Author");
            sinf.Title   = (/*setter*/ "Le titre \u00e9tait chang\u00e9");
            dinf.Manager = (/*setter*/ "Changed Manager");


            // Save this into the filesystem
            Stream soufStream2 = new NDocumentOutputStream(sinfDoc);

            sinf.Write(soufStream2);
            soufStream2.Close();
            Stream doufStream2 = new NDocumentOutputStream(dinfDoc);

            dinf.Write(doufStream2);
            doufStream2.Close();


            // Read them back in again
            sinfDoc = (DocumentNode)root.GetEntry(SummaryInformation.DEFAULT_STREAM_NAME);
            InputStream sinfStream6 = new NDocumentInputStream(sinfDoc);

            sinf = (SummaryInformation)PropertySetFactory.Create(sinfStream6);
            sinfStream6.Close();
            Assert.AreEqual(131077, sinf.OSVersion);

            dinfDoc = (DocumentNode)root.GetEntry(DocumentSummaryInformation.DEFAULT_STREAM_NAME);
            InputStream dinfStream6 = new NDocumentInputStream(dinfDoc);

            dinf = (DocumentSummaryInformation)PropertySetFactory.Create(dinfStream6);
            dinfStream6.Close();
            Assert.AreEqual(131077, dinf.OSVersion);

            Assert.AreEqual("Changed Author", sinf.Author);
            Assert.AreEqual("Microsoft Word 9.0", sinf.ApplicationName);
            Assert.AreEqual("Le titre \u00e9tait chang\u00e9", sinf.Title);

            Assert.AreEqual("", dinf.Company);
            Assert.AreEqual("Changed Manager", dinf.Manager);


            // Close the whole filesystem, and open it once more
            fs.WriteFileSystem();
            fs.Close();

            fs   = new NPOIFSFileSystem(new FileStream(copy.FullName, FileMode.Open));
            root = fs.Root;

            // Re-check on load
            sinfDoc = (DocumentNode)root.GetEntry(SummaryInformation.DEFAULT_STREAM_NAME);
            InputStream sinfStream7 = new NDocumentInputStream(sinfDoc);

            sinf = (SummaryInformation)PropertySetFactory.Create(sinfStream7);
            sinfStream7.Close();
            Assert.AreEqual(131077, sinf.OSVersion);

            dinfDoc = (DocumentNode)root.GetEntry(DocumentSummaryInformation.DEFAULT_STREAM_NAME);
            InputStream dinfStream7 = new NDocumentInputStream(dinfDoc);

            dinf = (DocumentSummaryInformation)PropertySetFactory.Create(dinfStream7);
            dinfStream7.Close();
            Assert.AreEqual(131077, dinf.OSVersion);

            Assert.AreEqual("Changed Author", sinf.Author);
            Assert.AreEqual("Microsoft Word 9.0", sinf.ApplicationName);
            Assert.AreEqual("Le titre \u00e9tait chang\u00e9", sinf.Title);

            Assert.AreEqual("", dinf.Company);
            Assert.AreEqual("Changed Manager", dinf.Manager);


            // Tidy up
            fs.Close();
            copy.Delete();
        }