Beispiel #1
0
        public void Test54233()
        {
            DocumentInputStream dis;
            POIFSFileSystem     fs =
                new POIFSFileSystem(_samples.OpenResourceAsStream("TestNon4ByteBoundary.doc"));

            dis = fs.CreateDocumentInputStream(SummaryInformation.DEFAULT_STREAM_NAME);
            SummaryInformation si = (SummaryInformation)PropertySetFactory.Create(dis);

            dis = fs.CreateDocumentInputStream(DocumentSummaryInformation.DEFAULT_STREAM_NAME);
            DocumentSummaryInformation dsi = (DocumentSummaryInformation)PropertySetFactory.Create(dis);

            // Test
            Assert.AreEqual("Microsoft Word 10.0", si.ApplicationName);
            Assert.AreEqual("", si.Title);
            Assert.AreEqual("", si.Author);
            Assert.AreEqual("Cour de Justice", dsi.Company);


            // Write out and read back, should still be valid
            POIDocument  doc  = new HPSFPropertiesOnlyDocument(fs);
            MemoryStream baos = new MemoryStream();

            doc.Write(baos);
            MemoryStream bais = new MemoryStream(baos.ToArray());

            doc = new HPSFPropertiesOnlyDocument(new POIFSFileSystem(bais));

            // Check properties are still there
            Assert.AreEqual("Microsoft Word 10.0", si.ApplicationName);
            Assert.AreEqual("", si.Title);
            Assert.AreEqual("", si.Author);
            Assert.AreEqual("Cour de Justice", dsi.Company);
        }
 protected DirectoryNode[] openSSSample(string name, bool oldFails)
 {
     return(openSamples(new Stream[] {
         _ssSamples.OpenResourceAsStream(name),
         _ssSamples.OpenResourceAsStream(name)
     }, oldFails));
 }
Beispiel #3
0
        public void testTidyStreamOnInvalidFile()
        {
            // Spreadsheet has a good mix of alternate file types
            POIDataSamples files = POIDataSamples.GetSpreadSheetInstance();

            FileInfo[] notValidF = new FileInfo[] {
                files.GetFileInfo("SampleSS.ods"), files.GetFileInfo("SampleSS.txt")
            };
            Stream[] notValidS = new Stream[] {
                files.OpenResourceAsStream("SampleSS.ods"), files.OpenResourceAsStream("SampleSS.txt")
            };
            foreach (FileInfo notValid in notValidF)
            {
                ZipPackage pkg = new ZipPackage(notValid, PackageAccess.READ);
                Assert.IsNotNull(pkg.ZipArchive);
                Assert.IsFalse(pkg.ZipArchive.IsClosed);
                try
                {
                    pkg.GetParts();
                    Assert.Fail("Shouldn't work");
                }
                catch (ODFNotOfficeXmlFileException e)
                {
                }
                catch (NotOfficeXmlFileException ne) { }
                pkg.Close();

                Assert.IsNotNull(pkg.ZipArchive);
                Assert.IsTrue(pkg.ZipArchive.IsClosed);
            }
            foreach (InputStream notValid in notValidS)
            {
                ZipPackage pkg = new ZipPackage(notValid, PackageAccess.READ);
                Assert.IsNotNull(pkg.ZipArchive);
                Assert.IsFalse(pkg.ZipArchive.IsClosed);
                try
                {
                    pkg.GetParts();
                    Assert.Fail("Shouldn't work");
                }
                catch (ODFNotOfficeXmlFileException e)
                {
                }
                catch (NotOfficeXmlFileException ne) { }
                pkg.Close();

                Assert.IsNotNull(pkg.ZipArchive);
                Assert.IsTrue(pkg.ZipArchive.IsClosed);
            }
        }
Beispiel #4
0
        public void TestLoadSample()
        {
            XSSFWorkbook workbook = new XSSFWorkbook(_ssSampels.OpenResourceAsStream("sample.xlsx"));

            Assert.AreEqual(3, workbook.NumberOfSheets);
            Assert.AreEqual("Sheet1", workbook.GetSheetName(0));
            ISheet sheet = workbook.GetSheetAt(0);
            IRow   row   = sheet.GetRow(0);
            ICell  cell  = row.GetCell((short)1);

            Assert.IsNotNull(cell);
            Assert.AreEqual(111.0, cell.NumericCellValue, 0.0);
            cell = row.GetCell((short)0);
            Assert.AreEqual("Lorem", cell.RichStringCellValue.String);
        }
        /**
         * Constructor
         *
         * @param name The name of the Test case
         */
        public TestEmptyProperties()
        {
            POIDataSamples samples = POIDataSamples.GetHPSFInstance();
            Stream         data    = samples.OpenResourceAsStream(POI_FS);;

            poiFiles = Util.ReadPOIFiles(data);
        }
Beispiel #6
0
        public void SetUp()
        {
            POIDataSamples samples = POIDataSamples.GetHPSFInstance();
            Stream         data    = samples.OpenResourceAsStream(POI_FS);;

            poiFiles = Util.ReadPOIFiles(data);
        }
Beispiel #7
0
        /**
         * Test case constructor.
         *
         * @param name The Test case's name.
         */
        public TestBasic()
        {
            //FileStream data =File.OpenRead(dataDir+POI_FS);
            POIDataSamples samples = POIDataSamples.GetHPSFInstance();
            Stream         data    = (Stream)samples.OpenResourceAsStream(POI_FS);

            poiFiles = Util.ReadPOIFiles(data);
        }
Beispiel #8
0
        public void TestDOCX()
        {
            POIDataSamples pds = POIDataSamples.GetDocumentInstance();

            AssertReadWrite(
                PackageHelper.Open(pds.OpenResourceAsStream("WordWithAttachments.docx"))
                );
        }
Beispiel #9
0
        public void TestXLSX()
        {
            POIDataSamples pds = POIDataSamples.GetSpreadSheetInstance();

            AssertReadWrite(
                PackageHelper.Open(pds.OpenResourceAsStream("ExcelWithAttachments.xlsm"))
                );
        }
Beispiel #10
0
        public void TestPPTX()
        {
            POIDataSamples pds = POIDataSamples.GetSlideShowInstance();

            AssertReadWrite(
                PackageHelper.Open(pds.OpenResourceAsStream("PPTWithAttachments.pptm"))
                );
        }
Beispiel #11
0
        public void TestOleNative()
        {
            POIFSFileSystem fs = new POIFSFileSystem(dataSamples.OpenResourceAsStream("oleObject1.bin"));

            Ole10Native ole = Ole10Native.CreateFromEmbeddedOleObject(fs);

            Assert.AreEqual("File1.svg", ole.Label);
            Assert.AreEqual("D:\\Documents and Settings\\rsc\\My Documents\\file1.svg", ole.Command);
        }
        public void TestShortLastBlock()
        {
            String[] files = new String[] { "ShortLastBlock.qwp", "ShortLastBlock.wps" };

            for (int i = 0; i < files.Length; i++)
            {
                // Open the file up
                POIFSFileSystem fs = new POIFSFileSystem(
                    _samples.OpenResourceAsStream(files[i])
                    );

                // Write it into a temp output array
                MemoryStream baos = new MemoryStream();
                fs.WriteFileSystem(baos);

                // Check sizes
            }
        }
        public void TestGetFromMainExtractor()
        {
            OPCPackage pkg = PackageHelper.Open(_ssSamples.OpenResourceAsStream("ExcelWithAttachments.xlsm"));

            XSSFWorkbook wb = new XSSFWorkbook(pkg);

            XSSFExcelExtractor            ext     = new XSSFExcelExtractor(wb);
            POIXMLPropertiesTextExtractor textExt = ext.GetMetadataTextExtractor();

            // Check basics
            assertNotNull(textExt);
            Assert.IsTrue(textExt.GetText().Length > 0);

            // Check some of the content
            String text  = textExt.GetText();
            String cText = textExt.GetCorePropertiesText();

            Assert.IsTrue(text.Contains("LastModifiedBy = Yury Batrakov"));
            Assert.IsTrue(cText.Contains("LastModifiedBy = Yury Batrakov"));
        }
Beispiel #14
0
        public void SetUp()
        {
            String filename  = "test2.doc";
            String filename2 = "test.doc";

            filename3 = "excel_with_embeded.xls";
            filename4 = "ThreeColHeadFoot.doc";
            filename5 = "HeaderFooterUnicode.doc";
            filename6 = "footnote.doc";
            POIDataSamples docTests = POIDataSamples.GetDocumentInstance();

            extractor  = new WordExtractor(docTests.OpenResourceAsStream(filename));
            extractor2 = new WordExtractor(docTests.OpenResourceAsStream(filename2));

            // Build splat'd out text version
            for (int i = 0; i < p_text1.Length; i++)
            {
                p_text1_block += p_text1[i];
            }
        }
        public void TestNormalProperties()
        {
            POIFSFileSystem         fs  = new POIFSFileSystem(_samples.OpenResourceAsStream("TestMickey.doc"));
            HPSFPropertiesExtractor ext = new HPSFPropertiesExtractor(fs);
            String text = ext.Text;

            // Check each bit in turn
            String sinfText = ext.SummaryInformationText;
            String dinfText = ext.DocumentSummaryInformationText;

            Assert.IsTrue(sinfText.IndexOf("TEMPLATE = Normal") > -1);
            Assert.IsTrue(sinfText.IndexOf("SUBJECT = sample subject") > -1);
            Assert.IsTrue(dinfText.IndexOf("MANAGER = sample manager") > -1);
            Assert.IsTrue(dinfText.IndexOf("COMPANY = sample company") > -1);

            // Now overall
            text = ext.Text;
            Assert.IsTrue(text.IndexOf("TEMPLATE = Normal") > -1);
            Assert.IsTrue(text.IndexOf("SUBJECT = sample subject") > -1);
            Assert.IsTrue(text.IndexOf("MANAGER = sample manager") > -1);
            Assert.IsTrue(text.IndexOf("COMPANY = sample company") > -1);
        }
        public void TestWithSomeNulls()
        {
            OPCPackage pkg = OPCPackage.Open(
                _slSamples.OpenResourceAsStream("49386-null_dates.pptx")
                );
            XSLFSlideShow sl = new XSLFSlideShow(pkg);

            POIXMLPropertiesTextExtractor ext = new POIXMLPropertiesTextExtractor(sl);

            ext.GetText();

            String text = ext.GetText();

            Assert.IsFalse(text.Contains("Created ="));       // With date is null
            Assert.IsTrue(text.Contains("CreatedString = ")); // Via string is blank
            Assert.IsTrue(text.Contains("LastModifiedBy = IT Client Services"));
        }
Beispiel #17
0
        private List <String> ReadStrings(String filename)
        {
            List <String>  strs    = new List <String>();
            POIDataSamples samples = POIDataSamples.GetSpreadSheetInstance();
            StreamReader   br      =
                new StreamReader(samples.OpenResourceAsStream(filename));
            String s;

            while ((s = br.ReadLine()) != null)
            {
                if (s.Trim().Length > 0)
                {
                    strs.Add(s.Trim());
                }
            }
            br.Close();
            return(strs);
        }
Beispiel #18
0
        public void TestRelationOrder()
        {
            POIDataSamples pds = POIDataSamples.GetDocumentInstance();
            OPCPackage     pkg = PackageHelper.Open(pds.OpenResourceAsStream("WordWithAttachments.docx"));
            OPCParser      doc = new OPCParser(pkg);

            try
            {
                doc.Parse(new TestFactory());

                foreach (POIXMLDocumentPart rel in doc.GetRelations())
                {
                    //TODO finish me
                    Assert.IsNotNull(rel);
                }
            }
            finally
            {
                doc.Close();
            }
        }
Beispiel #19
0
        private void WriteDataToFile(FileInfo temp)
        {
            FileStream str = temp.Create();

            try
            {
                Stream in1 = data.OpenResourceAsStream("Notes.ole2");
                try
                {
                    IOUtils.Copy(in1, str);
                }
                finally
                {
                    in1.Close();
                }
            }
            finally
            {
                str.Close();
            }
        }
Beispiel #20
0
        protected void FromStream(POIDataSamples dataSamples, String filename)
        {
            InputStream fis = new FileInputStream(dataSamples.OpenResourceAsStream(filename));

            try
            {
                VBAMacroReader r = new VBAMacroReader(fis);
                try
                {
                    AssertMacroContents(dataSamples, r);
                }
                finally
                {
                    r.Close();
                }
            }
            finally
            {
                fis.Close();
            }
        }
Beispiel #21
0
        public void TestNotesOLE2Files()
        {
            POIDataSamples _samples = POIDataSamples.GetPOIFSInstance();

            // Open the file up
            POIFSFileSystem fs = new POIFSFileSystem(
                _samples.OpenResourceAsStream("Notes.ole2")
                );

            // Check the contents
            Assert.AreEqual(1, fs.Root.EntryCount);
            fs.Root.Entries.MoveNext();

            //fs.Root.Entries.Current is always null. WHY???
            //Entry entry = fs.Root.Entries.Current;
            Entry entry = fs.Root.GetEntry(0);

            Assert.IsTrue(entry.IsDirectoryEntry);
            Assert.IsTrue(entry is DirectoryEntry);

            // The directory lacks a name!
            DirectoryEntry dir = (DirectoryEntry)entry;

            Assert.AreEqual("", dir.Name);

            // Has two children
            Assert.AreEqual(2, dir.EntryCount);

            // Check them
            IEnumerator <Entry> it = dir.Entries;

            it.MoveNext();
            entry = it.Current;
            Assert.AreEqual(true, entry.IsDocumentEntry);
            Assert.AreEqual("\u0001Ole10Native", entry.Name);
            it.MoveNext();
            entry = it.Current;
            Assert.AreEqual(true, entry.IsDocumentEntry);
            Assert.AreEqual("\u0001CompObj", entry.Name);
        }
Beispiel #22
0
        public void TestReadMiniStreams()
        {
            NPOIFSFileSystem fs        = new NPOIFSFileSystem(_inst.OpenResourceAsStream("BlockSize512.zvi"));
            NPOIFSMiniStore  ministore = fs.GetMiniStore();

            // 178 -> 179 -> 180 -> end
            NPOIFSStream             stream = new NPOIFSStream(ministore, 178);
            IEnumerator <ByteBuffer> i      = stream.GetBlockIterator();

            Assert.AreEqual(true, i.MoveNext());
            //  Assert.AreEqual(true, i.MoveNext());
            //  Assert.AreEqual(true, i.MoveNext());

            // i.MoveNext();
            ByteBuffer b178 = i.Current;

            Assert.AreEqual(true, i.MoveNext());
            // Assert.AreEqual(true, i.MoveNext());

            // i.MoveNext();
            ByteBuffer b179 = i.Current;

            Assert.AreEqual(true, i.MoveNext());

            // i.MoveNext();
            ByteBuffer b180 = i.Current;

            Assert.AreEqual(false, i.MoveNext());
            Assert.AreEqual(false, i.MoveNext());
            // Assert.AreEqual(false, i.MoveNext());

            // Check the contents of the 1st block
            Assert.AreEqual((byte)0xfe, b178[0]);
            Assert.AreEqual((byte)0xff, b178[1]);
            Assert.AreEqual((byte)0x00, b178[2]);
            Assert.AreEqual((byte)0x00, b178[3]);
            Assert.AreEqual((byte)0x05, b178[4]);
            Assert.AreEqual((byte)0x01, b178[5]);
            Assert.AreEqual((byte)0x02, b178[6]);
            Assert.AreEqual((byte)0x00, b178[7]);

            // And the 2nd
            Assert.AreEqual((byte)0x6c, b179[0]);
            Assert.AreEqual((byte)0x00, b179[1]);
            Assert.AreEqual((byte)0x00, b179[2]);
            Assert.AreEqual((byte)0x00, b179[3]);
            Assert.AreEqual((byte)0x28, b179[4]);
            Assert.AreEqual((byte)0x00, b179[5]);
            Assert.AreEqual((byte)0x00, b179[6]);
            Assert.AreEqual((byte)0x00, b179[7]);

            // And the 3rd
            Assert.AreEqual((byte)0x30, b180[0]);
            Assert.AreEqual((byte)0x00, b180[1]);
            Assert.AreEqual((byte)0x00, b180[2]);
            Assert.AreEqual((byte)0x00, b180[3]);
            Assert.AreEqual((byte)0x00, b180[4]);
            Assert.AreEqual((byte)0x00, b180[5]);
            Assert.AreEqual((byte)0x00, b180[6]);
            Assert.AreEqual((byte)0x80, b180[7]);

            fs.Close();
        }
Beispiel #23
0
        public void NonOOXMLFileTypes()
        {
            // Spreadsheet has a good mix of alternate file types
            POIDataSamples files = POIDataSamples.GetSpreadSheetInstance();

            // OLE2 - Stream
            try
            {
                Stream stream = files.OpenResourceAsStream("SampleSS.xls");
                try
                {
                    OPCPackage.Open(stream);
                }
                finally
                {
                    stream.Dispose();//.Close();
                }
                Assert.Fail("Shouldn't be able to open OLE2");
            }
            catch (OLE2NotOfficeXmlFileException e)
            {
                Assert.IsTrue(e.Message.Contains("The supplied data appears to be in the OLE2 Format"));
                Assert.IsTrue(e.Message.Contains("You are calling the part of POI that deals with OOXML"));
            }
            // OLE2 - File
            try
            {
                OPCPackage.Open(files.GetFile("SampleSS.xls"));
                Assert.Fail("Shouldn't be able to open OLE2");
            }
            catch (OLE2NotOfficeXmlFileException e)
            {
                Assert.IsTrue(e.Message.Contains("The supplied data appears to be in the OLE2 Format"));
                Assert.IsTrue(e.Message.Contains("You are calling the part of POI that deals with OOXML"));
            }

            // Raw XML - Stream
            try
            {
                Stream stream = files.OpenResourceAsStream("SampleSS.xml");
                try
                {
                    OPCPackage.Open(stream);
                }
                finally
                {
                    stream.Dispose();//.Close();
                }
                Assert.Fail("Shouldn't be able to open XML");
            }
            catch (NotOfficeXmlFileException e)
            {
                Assert.IsTrue(e.Message.Contains("The supplied data appears to be a raw XML file"));
                Assert.IsTrue(e.Message.Contains("Formats such as Office 2003 XML"));
            }
            // Raw XML - File
            try
            {
                OPCPackage.Open(files.GetFile("SampleSS.xml"));
                Assert.Fail("Shouldn't be able to open XML");
            }
            catch (NotOfficeXmlFileException e)
            {
                Assert.IsTrue(e.Message.Contains("The supplied data appears to be a raw XML file"));
                Assert.IsTrue(e.Message.Contains("Formats such as Office 2003 XML"));
            }

            // ODF / ODS - Stream
            try
            {
                Stream stream = files.OpenResourceAsStream("SampleSS.ods");
                try
                {
                    OPCPackage.Open(stream);
                }
                finally
                {
                    stream.Dispose();//.Close();
                }
                Assert.Fail("Shouldn't be able to open ODS");
            }
            catch (ODFNotOfficeXmlFileException e)
            {
                Assert.IsTrue(e.ToString().Contains("The supplied data appears to be in ODF"));
                Assert.IsTrue(e.ToString().Contains("Formats like these (eg ODS"));
            }
            // ODF / ODS - File
            try
            {
                OPCPackage.Open(files.GetFile("SampleSS.ods"));
                Assert.Fail("Shouldn't be able to open ODS");
            }
            catch (ODFNotOfficeXmlFileException e)
            {
                Assert.IsTrue(e.ToString().Contains("The supplied data appears to be in ODF"));
                Assert.IsTrue(e.ToString().Contains("Formats like these (eg ODS"));
            }

            // Plain Text - Stream
            try
            {
                Stream stream = files.OpenResourceAsStream("SampleSS.txt");
                try
                {
                    OPCPackage.Open(stream);
                }
                finally
                {
                    stream.Dispose();//.Close();
                }
                Assert.Fail("Shouldn't be able to open Plain Text");
            }
            catch (NotOfficeXmlFileException e)
            {
                Assert.IsTrue(e.Message.Contains("No valid entries or contents found"));
                Assert.IsTrue(e.Message.Contains("not a valid OOXML"));
            }
            // Plain Text - File
            try
            {
                OPCPackage.Open(files.GetFile("SampleSS.txt"));
                Assert.Fail("Shouldn't be able to open Plain Text");
            }
            catch (UnsupportedFileFormatException)
            {
                // Unhelpful low-level error, sorry
            }
        }
 public static Stream OpenSampleStream(String sampleFileName)
 {
     return(_samples.OpenResourceAsStream(sampleFileName));
 }
Beispiel #25
0
        public void TestNextBlock()
        {
            // It's the same on 512 byte and 4096 byte block files!
            NPOIFSFileSystem fsA = new NPOIFSFileSystem(_inst.GetFile("BlockSize512.zvi"));
            NPOIFSFileSystem fsB = new NPOIFSFileSystem(_inst.OpenResourceAsStream("BlockSize512.zvi"));
            NPOIFSFileSystem fsC = new NPOIFSFileSystem(_inst.GetFile("BlockSize4096.zvi"));
            NPOIFSFileSystem fsD = new NPOIFSFileSystem(_inst.OpenResourceAsStream("BlockSize4096.zvi"));

            foreach (NPOIFSFileSystem fs in new NPOIFSFileSystem[] { fsA, fsB, fsC, fsD })
            {
                NPOIFSMiniStore ministore = fs.GetMiniStore();

                // 0 -> 51 is one stream
                for (int i = 0; i < 50; i++)
                {
                    Assert.AreEqual(i + 1, ministore.GetNextBlock(i));
                }
                Assert.AreEqual(POIFSConstants.END_OF_CHAIN, ministore.GetNextBlock(50));

                // 51 -> 103 is the next
                for (int i = 51; i < 103; i++)
                {
                    Assert.AreEqual(i + 1, ministore.GetNextBlock(i));
                }
                Assert.AreEqual(POIFSConstants.END_OF_CHAIN, ministore.GetNextBlock(103));

                // Then there are 3 one block ones
                Assert.AreEqual(POIFSConstants.END_OF_CHAIN, ministore.GetNextBlock(104));
                Assert.AreEqual(POIFSConstants.END_OF_CHAIN, ministore.GetNextBlock(105));
                Assert.AreEqual(POIFSConstants.END_OF_CHAIN, ministore.GetNextBlock(106));

                // 107 -> 154 is the next
                for (int i = 107; i < 154; i++)
                {
                    Assert.AreEqual(i + 1, ministore.GetNextBlock(i));
                }
                Assert.AreEqual(POIFSConstants.END_OF_CHAIN, ministore.GetNextBlock(154));

                // 155 -> 160 is the next
                for (int i = 155; i < 160; i++)
                {
                    Assert.AreEqual(i + 1, ministore.GetNextBlock(i));
                }
                Assert.AreEqual(POIFSConstants.END_OF_CHAIN, ministore.GetNextBlock(160));

                // 161 -> 166 is the next
                for (int i = 161; i < 166; i++)
                {
                    Assert.AreEqual(i + 1, ministore.GetNextBlock(i));
                }
                Assert.AreEqual(POIFSConstants.END_OF_CHAIN, ministore.GetNextBlock(166));

                // 167 -> 172 is the next
                for (int i = 167; i < 172; i++)
                {
                    Assert.AreEqual(i + 1, ministore.GetNextBlock(i));
                }
                Assert.AreEqual(POIFSConstants.END_OF_CHAIN, ministore.GetNextBlock(172));

                // Now some short ones
                Assert.AreEqual(174, ministore.GetNextBlock(173));
                Assert.AreEqual(POIFSConstants.END_OF_CHAIN, ministore.GetNextBlock(174));

                Assert.AreEqual(POIFSConstants.END_OF_CHAIN, ministore.GetNextBlock(175));

                Assert.AreEqual(177, ministore.GetNextBlock(176));
                Assert.AreEqual(POIFSConstants.END_OF_CHAIN, ministore.GetNextBlock(177));

                Assert.AreEqual(179, ministore.GetNextBlock(178));
                Assert.AreEqual(180, ministore.GetNextBlock(179));
                Assert.AreEqual(POIFSConstants.END_OF_CHAIN, ministore.GetNextBlock(180));

                // 181 onwards is free
                for (int i = 181; i < fs.GetBigBlockSizeDetails().GetBATEntriesPerBlock(); i++)
                {
                    Assert.AreEqual(POIFSConstants.UNUSED_BLOCK, ministore.GetNextBlock(i));
                }
                fs.Close();
            }

            fsD.Close();
            fsC.Close();
            fsB.Close();
            fsA.Close();
        }
Beispiel #26
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(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();
        }
Beispiel #27
0
        /**
         * Returns test files with 512 byte and 4k block sizes, loaded
         *  both from InputStreams and Files
         */
        protected NPOIFSFileSystem[] get512and4kFileAndInput()
        {
            NPOIFSFileSystem fsA = new NPOIFSFileSystem(_inst.GetFile("BlockSize512.zvi"));
            NPOIFSFileSystem fsB = new NPOIFSFileSystem(_inst.OpenResourceAsStream("BlockSize512.zvi"));
            NPOIFSFileSystem fsC = new NPOIFSFileSystem(_inst.GetFile("BlockSize4096.zvi"));
            NPOIFSFileSystem fsD = new NPOIFSFileSystem(_inst.OpenResourceAsStream("BlockSize4096.zvi"));

            return(new NPOIFSFileSystem[] { fsA, fsB, fsC, fsD });
        }
Beispiel #28
0
        public void TestBasicOpen()
        {
            NPOIFSFileSystem fsA, fsB;

            fsA = new NPOIFSFileSystem(_inst.GetFile("BlockSize512.zvi"));
            fsB = new NPOIFSFileSystem(_inst.OpenResourceAsStream("BlockSize512.zvi"));

            foreach (NPOIFSFileSystem fs in new NPOIFSFileSystem[] { fsA, fsB })
            {
                Assert.AreEqual(512, fs.GetBigBlockSize());
            }

            fsA = new NPOIFSFileSystem(_inst.GetFile("BlockSize4096.zvi"));
            fsB = new NPOIFSFileSystem(_inst.OpenResourceAsStream("BlockSize4096.zvi"));

            foreach (NPOIFSFileSystem fs in new NPOIFSFileSystem[] { fsA, fsB })
            {
                Assert.AreEqual(4096, fs.GetBigBlockSize());
            }
        }