public void TestFail() { Stream is1 = HSSFTestDataSamples.OpenSampleFileStream("Simple.xls"); POIFSFileSystem fs = new POIFSFileSystem(is1); HSSFWorkbook wb = new HSSFWorkbook(fs); //set POIFS properties after constructing HSSFWorkbook //(a piece of code that used to work up to POI 3.0.2) SummaryInformation summary1 = (SummaryInformation)PropertySetFactory.Create(fs.CreateDocumentInputStream(SummaryInformation.DEFAULT_STREAM_NAME)); summary1.Title = (title); //Write the modified property back to POIFS fs.Root.GetEntry(SummaryInformation.DEFAULT_STREAM_NAME).Delete(); fs.CreateDocument(summary1.ToInputStream(), SummaryInformation.DEFAULT_STREAM_NAME); //save the workbook and read the property MemoryStream out1 = new MemoryStream(); wb.Write(out1); out1.Close(); POIFSFileSystem fs2 = new POIFSFileSystem(new MemoryStream(out1.ToArray())); SummaryInformation summary2 = (SummaryInformation)PropertySetFactory.Create(fs2.CreateDocumentInputStream(SummaryInformation.DEFAULT_STREAM_NAME)); //Assert.Failing assertion Assert.AreEqual(title, summary2.Title); }
public void TestOK() { Stream is1 = HSSFTestDataSamples.OpenSampleFileStream("Simple.xls"); POIFSFileSystem fs = new POIFSFileSystem(is1); //set POIFS properties before constructing HSSFWorkbook SummaryInformation summary1 = (SummaryInformation)PropertySetFactory.Create(fs.CreateDocumentInputStream(SummaryInformation.DEFAULT_STREAM_NAME)); summary1.Title = (title); fs.Root.GetEntry(SummaryInformation.DEFAULT_STREAM_NAME).Delete(); fs.CreateDocument(summary1.ToInputStream(), SummaryInformation.DEFAULT_STREAM_NAME); HSSFWorkbook wb = new HSSFWorkbook(fs); MemoryStream out1 = new MemoryStream(); wb.Write(out1); out1.Close(); //read the property POIFSFileSystem fs2 = new POIFSFileSystem(new MemoryStream(out1.ToArray())); SummaryInformation summary2 = (SummaryInformation)PropertySetFactory.Create(fs2.CreateDocumentInputStream(SummaryInformation.DEFAULT_STREAM_NAME)); Assert.AreEqual(title, summary2.Title); }
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(new FileStream(copy.FullName, FileMode.Open, FileAccess.ReadWrite), null, false, true); root = fs.Root; // Read the properties in there sinfDoc = (DocumentNode)root.GetEntry(SummaryInformation.DEFAULT_STREAM_NAME); dinfDoc = (DocumentNode)root.GetEntry(DocumentSummaryInformation.DEFAULT_STREAM_NAME); sinf = (SummaryInformation)PropertySetFactory.Create(new NDocumentInputStream(sinfDoc)); Assert.AreEqual(131077, sinf.OSVersion); dinf = (DocumentSummaryInformation)PropertySetFactory.Create(new NDocumentInputStream(dinfDoc)); 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); sinf = (SummaryInformation)PropertySetFactory.Create(new NDocumentInputStream(sinfDoc)); Assert.AreEqual(131077, sinf.OSVersion); dinf = (DocumentSummaryInformation)PropertySetFactory.Create(new NDocumentInputStream(dinfDoc)); 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); sinf = (SummaryInformation)PropertySetFactory.Create(new NDocumentInputStream(sinfDoc)); Assert.AreEqual(131077, sinf.OSVersion); dinf = (DocumentSummaryInformation)PropertySetFactory.Create(new NDocumentInputStream(dinfDoc)); Assert.AreEqual(131077, dinf.OSVersion); // Have them write themselves in-place with no Changes sinf.Write(new NDocumentOutputStream(sinfDoc)); dinf.Write(new NDocumentOutputStream(dinfDoc)); // 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); byte[] sinfData = IOUtils.ToByteArray(new NDocumentInputStream(sinfDoc)); byte[] dinfData = IOUtils.ToByteArray(new NDocumentInputStream(dinfDoc)); Assert.That(sinfBytes.ToArray(), new EqualConstraint(sinfData)); Assert.That(dinfBytes.ToArray(), new EqualConstraint(dinfData)); // Read back in as-is sinf = (SummaryInformation)PropertySetFactory.Create(new NDocumentInputStream(sinfDoc)); Assert.AreEqual(131077, sinf.OSVersion); dinf = (DocumentSummaryInformation)PropertySetFactory.Create(new NDocumentInputStream(dinfDoc)); 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 sinf.Write(new NDocumentOutputStream(sinfDoc)); dinf.Write(new NDocumentOutputStream(dinfDoc)); // Read them back in again sinfDoc = (DocumentNode)root.GetEntry(SummaryInformation.DEFAULT_STREAM_NAME); sinf = (SummaryInformation)PropertySetFactory.Create(new NDocumentInputStream(sinfDoc)); Assert.AreEqual(131077, sinf.OSVersion); dinfDoc = (DocumentNode)root.GetEntry(DocumentSummaryInformation.DEFAULT_STREAM_NAME); dinf = (DocumentSummaryInformation)PropertySetFactory.Create(new NDocumentInputStream(dinfDoc)); 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); sinf = (SummaryInformation)PropertySetFactory.Create(new NDocumentInputStream(sinfDoc)); Assert.AreEqual(131077, sinf.OSVersion); dinfDoc = (DocumentNode)root.GetEntry(DocumentSummaryInformation.DEFAULT_STREAM_NAME); dinf = (DocumentSummaryInformation)PropertySetFactory.Create(new NDocumentInputStream(dinfDoc)); 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(); }