From: Nick Burch Date: Fri, 25 Mar 2011 16:53:26 +0000 (+0000) Subject: Fix up constructor to ensure that the filesystem objects are around when we need... X-Git-Tag: REL_3_8_BETA2~11 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=2330d4fedc36191dc9e6d2d89d6ee2d50ef22f0a;p=poi.git Fix up constructor to ensure that the filesystem objects are around when we need them, and not just after we wanted them as it was... git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1085467 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/src/java/org/apache/poi/poifs/filesystem/DirectoryNode.java b/src/java/org/apache/poi/poifs/filesystem/DirectoryNode.java index ea733ea879..738a8e1a90 100644 --- a/src/java/org/apache/poi/poifs/filesystem/DirectoryNode.java +++ b/src/java/org/apache/poi/poifs/filesystem/DirectoryNode.java @@ -70,8 +70,7 @@ public class DirectoryNode final POIFSFileSystem filesystem, final DirectoryNode parent) { - this(property, parent); - _ofilesystem = filesystem; + this(property, parent, filesystem, (NPOIFSFileSystem)null); } /** @@ -86,14 +85,18 @@ public class DirectoryNode final NPOIFSFileSystem nfilesystem, final DirectoryNode parent) { - this(property, parent); - _nfilesystem = nfilesystem; + this(property, parent, (POIFSFileSystem)null, nfilesystem); } private DirectoryNode(final DirectoryProperty property, - final DirectoryNode parent) + final DirectoryNode parent, + final POIFSFileSystem ofilesystem, + final NPOIFSFileSystem nfilesystem) { super(property, parent); + this._ofilesystem = ofilesystem; + this._nfilesystem = nfilesystem; + if (parent == null) { _path = new POIFSDocumentPath(); diff --git a/src/testcases/org/apache/poi/poifs/filesystem/TestDocumentInputStream.java b/src/testcases/org/apache/poi/poifs/filesystem/TestDocumentInputStream.java index 0b1b6b096a..e0600e8d56 100644 --- a/src/testcases/org/apache/poi/poifs/filesystem/TestDocumentInputStream.java +++ b/src/testcases/org/apache/poi/poifs/filesystem/TestDocumentInputStream.java @@ -18,11 +18,14 @@ package org.apache.poi.poifs.filesystem; import java.io.ByteArrayInputStream; +import java.io.File; +import java.io.FileInputStream; import java.io.IOException; import java.util.Arrays; import junit.framework.TestCase; +import org.apache.poi.POIDataSamples; import org.apache.poi.poifs.property.DirectoryProperty; import org.apache.poi.poifs.storage.RawDataBlock; @@ -365,4 +368,47 @@ public final class TestDocumentInputStream extends TestCase { stream.skip(2 + ( long ) Integer.MAX_VALUE)); assertEquals(0, stream.available()); } + + /** + * Test that we can read files at multiple levels down the tree + */ + public void testReadMultipleTreeLevels() throws Exception { + final POIDataSamples _samples = POIDataSamples.getPublisherInstance(); + File sample = _samples.getFile("Sample.pub"); + + DocumentInputStream stream; + + NPOIFSFileSystem npoifs = new NPOIFSFileSystem(sample); + POIFSFileSystem opoifs = new POIFSFileSystem(new FileInputStream(sample)); + + // Ensure we have what we expect on the root + assertEquals(npoifs, npoifs.getRoot().getNFileSystem()); + assertEquals(null, npoifs.getRoot().getFileSystem()); + assertEquals(opoifs, opoifs.getRoot().getFileSystem()); + assertEquals(null, opoifs.getRoot().getNFileSystem()); + + // Check inside + for(DirectoryNode root : new DirectoryNode[] { opoifs.getRoot(), npoifs.getRoot() }) { + // Top Level + Entry top = root.getEntry("Contents"); + assertEquals(true, top.isDocumentEntry()); + stream = root.createDocumentInputStream(top); + stream.read(); + + // One Level Down + DirectoryNode escher = (DirectoryNode)root.getEntry("Escher"); + Entry one = escher.getEntry("EscherStm"); + assertEquals(true, one.isDocumentEntry()); + stream = escher.createDocumentInputStream(one); + stream.read(); + + // Two Levels Down + DirectoryNode quill = (DirectoryNode)root.getEntry("Quill"); + DirectoryNode quillSub = (DirectoryNode)quill.getEntry("QuillSub"); + Entry two = quillSub.getEntry("CONTENTS"); + assertEquals(true, two.isDocumentEntry()); + stream = quillSub.createDocumentInputStream(two); + stream.read(); + } + } }