Ejemplo n.º 1
0
        public void TestCopyRecursively()
        {
            POIFSFileSystem fsD = new POIFSFileSystem();
            POIFSFileSystem fs = new POIFSFileSystem();
            DirectoryEntry dirA = fs.CreateDirectory("DirA");
            DirectoryEntry dirB = fs.CreateDirectory("DirB");

            DocumentEntry entryR = fs.CreateDocument(new ByteArrayInputStream(dataSmallA), "EntryRoot");
            DocumentEntry entryA1 = dirA.CreateDocument("EntryA1", new ByteArrayInputStream(dataSmallA));
            DocumentEntry entryA2 = dirA.CreateDocument("EntryA2", new ByteArrayInputStream(dataSmallB));

            // Copy docs
            Assert.AreEqual(0, fsD.Root.EntryCount);
            EntryUtils.CopyNodeRecursively(entryR, fsD.Root);

            Assert.AreEqual(1, fsD.Root.EntryCount);
            Assert.IsNotNull(fsD.Root.GetEntry("EntryRoot"));

            EntryUtils.CopyNodeRecursively(entryA1, fsD.Root);
            Assert.AreEqual(2, fsD.Root.EntryCount);
            Assert.IsNotNull(fsD.Root.GetEntry("EntryRoot"));
            Assert.IsNotNull(fsD.Root.GetEntry("EntryA1"));

            EntryUtils.CopyNodeRecursively(entryA2, fsD.Root);
            Assert.AreEqual(3, fsD.Root.EntryCount);
            Assert.IsNotNull(fsD.Root.GetEntry("EntryRoot"));
            Assert.IsNotNull(fsD.Root.GetEntry("EntryA1"));
            Assert.IsNotNull(fsD.Root.GetEntry("EntryA2"));

            // Copy directories
            fsD = new POIFSFileSystem();
            Assert.AreEqual(0, fsD.Root.EntryCount);

            EntryUtils.CopyNodeRecursively(dirB, fsD.Root);
            Assert.AreEqual(1, fsD.Root.EntryCount);
            Assert.IsNotNull(fsD.Root.GetEntry("DirB"));
            Assert.AreEqual(0, ((DirectoryEntry)fsD.Root.GetEntry("DirB")).EntryCount);

            EntryUtils.CopyNodeRecursively(dirA, fsD.Root);
            Assert.AreEqual(2, fsD.Root.EntryCount);
            Assert.IsNotNull(fsD.Root.GetEntry("DirB"));
            Assert.AreEqual(0, ((DirectoryEntry)fsD.Root.GetEntry("DirB")).EntryCount);
            Assert.IsNotNull(fsD.Root.GetEntry("DirA"));
            Assert.AreEqual(2, ((DirectoryEntry)fsD.Root.GetEntry("DirA")).EntryCount);

            // Copy the whole lot
            fsD = new POIFSFileSystem();
            Assert.AreEqual(0, fsD.Root.EntryCount);

            EntryUtils.CopyNodes(fs, fsD, new List<String>());
            Assert.AreEqual(3, fsD.Root.EntryCount);
            Assert.IsNotNull(fsD.Root.GetEntry(dirA.Name));
            Assert.IsNotNull(fsD.Root.GetEntry(dirB.Name));
            Assert.IsNotNull(fsD.Root.GetEntry(entryR.Name));
            Assert.AreEqual(0, ((DirectoryEntry)fsD.Root.GetEntry("DirB")).EntryCount);
            Assert.AreEqual(2, ((DirectoryEntry)fsD.Root.GetEntry("DirA")).EntryCount);
        }
Ejemplo n.º 2
0
        public void TestEmptyDocumentBug11744()
        {
            byte[] TestData = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };

            POIFSFileSystem fs = new POIFSFileSystem();
            fs.CreateDocument(new MemoryStream(new byte[0]), "Empty");
            fs.CreateDocument(new MemoryStream(TestData), "NotEmpty");
            MemoryStream output = new MemoryStream();
            fs.WriteFileSystem(output);

            // This line caused the error.
            fs = new POIFSFileSystem(new MemoryStream(output.ToArray()));

            DocumentEntry entry = (DocumentEntry)fs.Root.GetEntry("Empty");
            Assert.AreEqual(0, entry.Size, "Expected zero size");
            byte[] actualReadbackData;
            actualReadbackData = NStorage.Utility.IOUtils.ToByteArray(new DocumentInputStream(entry));
            Assert.AreEqual(0, actualReadbackData.Length, "Expected zero read from stream");

            entry = (DocumentEntry)fs.Root.GetEntry("NotEmpty");
            actualReadbackData = NStorage.Utility.IOUtils.ToByteArray(new DocumentInputStream(entry));
            Assert.AreEqual(TestData.Length, entry.Size, "Expected size was wrong");
            Assert.IsTrue(
                    Arrays.Equals(TestData,actualReadbackData), "Expected different data Read from stream");
        }