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
This commit is contained in:
Nick Burch 2011-03-25 16:53:26 +00:00
parent 915713a4d3
commit e520f5b82f
2 changed files with 54 additions and 5 deletions

View File

@ -70,8 +70,7 @@ public class DirectoryNode
final POIFSFileSystem filesystem, final POIFSFileSystem filesystem,
final DirectoryNode parent) final DirectoryNode parent)
{ {
this(property, parent); this(property, parent, filesystem, (NPOIFSFileSystem)null);
_ofilesystem = filesystem;
} }
/** /**
@ -86,14 +85,18 @@ public class DirectoryNode
final NPOIFSFileSystem nfilesystem, final NPOIFSFileSystem nfilesystem,
final DirectoryNode parent) final DirectoryNode parent)
{ {
this(property, parent); this(property, parent, (POIFSFileSystem)null, nfilesystem);
_nfilesystem = nfilesystem;
} }
private DirectoryNode(final DirectoryProperty property, private DirectoryNode(final DirectoryProperty property,
final DirectoryNode parent) final DirectoryNode parent,
final POIFSFileSystem ofilesystem,
final NPOIFSFileSystem nfilesystem)
{ {
super(property, parent); super(property, parent);
this._ofilesystem = ofilesystem;
this._nfilesystem = nfilesystem;
if (parent == null) if (parent == null)
{ {
_path = new POIFSDocumentPath(); _path = new POIFSDocumentPath();

View File

@ -18,11 +18,14 @@
package org.apache.poi.poifs.filesystem; package org.apache.poi.poifs.filesystem;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException; import java.io.IOException;
import java.util.Arrays; import java.util.Arrays;
import junit.framework.TestCase; import junit.framework.TestCase;
import org.apache.poi.POIDataSamples;
import org.apache.poi.poifs.property.DirectoryProperty; import org.apache.poi.poifs.property.DirectoryProperty;
import org.apache.poi.poifs.storage.RawDataBlock; import org.apache.poi.poifs.storage.RawDataBlock;
@ -365,4 +368,47 @@ public final class TestDocumentInputStream extends TestCase {
stream.skip(2 + ( long ) Integer.MAX_VALUE)); stream.skip(2 + ( long ) Integer.MAX_VALUE));
assertEquals(0, stream.available()); 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();
}
}
} }