mirror of
https://github.com/apache/poi.git
synced 2026-02-27 20:40:08 +08:00
throw IllegalArgumnetException if null directory specified
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1850394 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
6d87e36227
commit
0849bbc614
@ -59,8 +59,12 @@ public class FilteringDirectoryNode implements DirectoryEntry
|
||||
*
|
||||
* @param directory The Directory to filter
|
||||
* @param excludes The Entries to exclude
|
||||
* @throws IllegalArgumentException if directory is null
|
||||
*/
|
||||
public FilteringDirectoryNode(DirectoryEntry directory, Collection<String> excludes) {
|
||||
if (directory == null) {
|
||||
throw new IllegalArgumentException("directory cannot be null");
|
||||
}
|
||||
this.directory = directory;
|
||||
|
||||
// Process the excludes
|
||||
|
||||
@ -25,148 +25,165 @@ import java.util.HashSet;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
/**
|
||||
* Class to test FilteringDirectoryNode functionality
|
||||
*/
|
||||
public final class TestFilteringDirectoryNode extends TestCase {
|
||||
private POIFSFileSystem fs;
|
||||
private DirectoryEntry dirA;
|
||||
private DirectoryEntry dirAA;
|
||||
private DirectoryEntry dirB;
|
||||
private DocumentEntry eRoot;
|
||||
private DocumentEntry eA;
|
||||
private DocumentEntry eAA;
|
||||
|
||||
@Override
|
||||
protected void setUp() throws Exception {
|
||||
fs = new POIFSFileSystem();
|
||||
dirA = fs.createDirectory("DirA");
|
||||
dirB = fs.createDirectory("DirB");
|
||||
dirAA = dirA.createDirectory("DirAA");
|
||||
eRoot = fs.getRoot().createDocument("Root", new ByteArrayInputStream(new byte[] {}));
|
||||
eA = dirA.createDocument("NA", new ByteArrayInputStream(new byte[] {}));
|
||||
eAA = dirAA.createDocument("NAA", new ByteArrayInputStream(new byte[] {}));
|
||||
}
|
||||
|
||||
public void testNoFiltering() throws Exception {
|
||||
FilteringDirectoryNode d = new FilteringDirectoryNode(fs.getRoot(), new HashSet<>());
|
||||
assertEquals(3, d.getEntryCount());
|
||||
assertEquals(dirA.getName(), d.getEntry(dirA.getName()).getName());
|
||||
|
||||
assertEquals(true, d.getEntry(dirA.getName()).isDirectoryEntry());
|
||||
assertEquals(false, d.getEntry(dirA.getName()).isDocumentEntry());
|
||||
|
||||
assertEquals(true, d.getEntry(dirB.getName()).isDirectoryEntry());
|
||||
assertEquals(false, d.getEntry(dirB.getName()).isDocumentEntry());
|
||||
|
||||
assertEquals(false, d.getEntry(eRoot.getName()).isDirectoryEntry());
|
||||
assertEquals(true, d.getEntry(eRoot.getName()).isDocumentEntry());
|
||||
|
||||
Iterator<Entry> i = d.getEntries();
|
||||
assertEquals(dirA, i.next());
|
||||
assertEquals(dirB, i.next());
|
||||
assertEquals(eRoot, i.next());
|
||||
assertEquals(null, i.next());
|
||||
}
|
||||
|
||||
public void testChildFiltering() throws Exception {
|
||||
List<String> excl = Arrays.asList(new String[] {"NotThere","AlsoNotThere", eRoot.getName()});
|
||||
FilteringDirectoryNode d = new FilteringDirectoryNode(fs.getRoot(), excl);
|
||||
|
||||
assertEquals(2, d.getEntryCount());
|
||||
assertEquals(true, d.hasEntry(dirA.getName()));
|
||||
assertEquals(true, d.hasEntry(dirB.getName()));
|
||||
assertEquals(false, d.hasEntry(eRoot.getName()));
|
||||
|
||||
assertEquals(dirA, d.getEntry(dirA.getName()));
|
||||
assertEquals(dirB, d.getEntry(dirB.getName()));
|
||||
try {
|
||||
d.getEntry(eRoot.getName());
|
||||
fail("Should be filtered");
|
||||
} catch(FileNotFoundException e) {}
|
||||
|
||||
Iterator<Entry> i = d.getEntries();
|
||||
assertEquals(dirA, i.next());
|
||||
assertEquals(dirB, i.next());
|
||||
assertEquals(null, i.next());
|
||||
|
||||
|
||||
// Filter more
|
||||
excl = Arrays.asList(new String[] {"NotThere","AlsoNotThere", eRoot.getName(), dirA.getName()});
|
||||
d = new FilteringDirectoryNode(fs.getRoot(), excl);
|
||||
|
||||
assertEquals(1, d.getEntryCount());
|
||||
assertEquals(false, d.hasEntry(dirA.getName()));
|
||||
assertEquals(true, d.hasEntry(dirB.getName()));
|
||||
assertEquals(false, d.hasEntry(eRoot.getName()));
|
||||
|
||||
try {
|
||||
d.getEntry(dirA.getName());
|
||||
fail("Should be filtered");
|
||||
} catch(FileNotFoundException e) {}
|
||||
assertEquals(dirB, d.getEntry(dirB.getName()));
|
||||
try {
|
||||
d.getEntry(eRoot.getName());
|
||||
fail("Should be filtered");
|
||||
} catch(FileNotFoundException e) {}
|
||||
|
||||
i = d.getEntries();
|
||||
assertEquals(dirB, i.next());
|
||||
assertEquals(null, i.next());
|
||||
|
||||
|
||||
// Filter everything
|
||||
excl = Arrays.asList(new String[] {"NotThere", eRoot.getName(), dirA.getName(), dirB.getName()});
|
||||
d = new FilteringDirectoryNode(fs.getRoot(), excl);
|
||||
|
||||
assertEquals(0, d.getEntryCount());
|
||||
assertEquals(false, d.hasEntry(dirA.getName()));
|
||||
assertEquals(false, d.hasEntry(dirB.getName()));
|
||||
assertEquals(false, d.hasEntry(eRoot.getName()));
|
||||
|
||||
try {
|
||||
d.getEntry(dirA.getName());
|
||||
fail("Should be filtered");
|
||||
} catch(FileNotFoundException e) {}
|
||||
try {
|
||||
d.getEntry(dirB.getName());
|
||||
fail("Should be filtered");
|
||||
} catch(FileNotFoundException e) {}
|
||||
try {
|
||||
d.getEntry(eRoot.getName());
|
||||
fail("Should be filtered");
|
||||
} catch(FileNotFoundException e) {}
|
||||
|
||||
i = d.getEntries();
|
||||
assertEquals(null, i.next());
|
||||
}
|
||||
|
||||
public void testNestedFiltering() throws Exception {
|
||||
List<String> excl = Arrays.asList(new String[] {
|
||||
dirA.getName()+"/"+"MadeUp",
|
||||
dirA.getName()+"/"+eA.getName(),
|
||||
dirA.getName()+"/"+dirAA.getName()+"/Test",
|
||||
eRoot.getName()
|
||||
});
|
||||
FilteringDirectoryNode d = new FilteringDirectoryNode(fs.getRoot(), excl);
|
||||
|
||||
// Check main
|
||||
assertEquals(2, d.getEntryCount());
|
||||
assertEquals(true, d.hasEntry(dirA.getName()));
|
||||
assertEquals(true, d.hasEntry(dirB.getName()));
|
||||
assertEquals(false, d.hasEntry(eRoot.getName()));
|
||||
|
||||
// Check filtering down
|
||||
assertEquals(true, d.getEntry(dirA.getName()) instanceof FilteringDirectoryNode);
|
||||
assertEquals(false, d.getEntry(dirB.getName()) instanceof FilteringDirectoryNode);
|
||||
|
||||
DirectoryEntry fdA = (DirectoryEntry)d.getEntry(dirA.getName());
|
||||
assertEquals(false, fdA.hasEntry(eA.getName()));
|
||||
assertEquals(true, fdA.hasEntry(dirAA.getName()));
|
||||
|
||||
DirectoryEntry fdAA = (DirectoryEntry)fdA.getEntry(dirAA.getName());
|
||||
assertEquals(true, fdAA.hasEntry(eAA.getName()));
|
||||
}
|
||||
}
|
||||
public final class TestFilteringDirectoryNode {
|
||||
private POIFSFileSystem fs;
|
||||
private DirectoryEntry dirA;
|
||||
private DirectoryEntry dirAA;
|
||||
private DirectoryEntry dirB;
|
||||
private DocumentEntry eRoot;
|
||||
private DocumentEntry eA;
|
||||
private DocumentEntry eAA;
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
fs = new POIFSFileSystem();
|
||||
dirA = fs.createDirectory("DirA");
|
||||
dirB = fs.createDirectory("DirB");
|
||||
dirAA = dirA.createDirectory("DirAA");
|
||||
eRoot = fs.getRoot().createDocument("Root", new ByteArrayInputStream(new byte[]{}));
|
||||
eA = dirA.createDocument("NA", new ByteArrayInputStream(new byte[]{}));
|
||||
eAA = dirAA.createDocument("NAA", new ByteArrayInputStream(new byte[]{}));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNoFiltering() throws Exception {
|
||||
FilteringDirectoryNode d = new FilteringDirectoryNode(fs.getRoot(), new HashSet<>());
|
||||
assertEquals(3, d.getEntryCount());
|
||||
assertEquals(dirA.getName(), d.getEntry(dirA.getName()).getName());
|
||||
|
||||
assertEquals(true, d.getEntry(dirA.getName()).isDirectoryEntry());
|
||||
assertEquals(false, d.getEntry(dirA.getName()).isDocumentEntry());
|
||||
|
||||
assertEquals(true, d.getEntry(dirB.getName()).isDirectoryEntry());
|
||||
assertEquals(false, d.getEntry(dirB.getName()).isDocumentEntry());
|
||||
|
||||
assertEquals(false, d.getEntry(eRoot.getName()).isDirectoryEntry());
|
||||
assertEquals(true, d.getEntry(eRoot.getName()).isDocumentEntry());
|
||||
|
||||
Iterator<Entry> i = d.getEntries();
|
||||
assertEquals(dirA, i.next());
|
||||
assertEquals(dirB, i.next());
|
||||
assertEquals(eRoot, i.next());
|
||||
assertEquals(null, i.next());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testChildFiltering() throws Exception {
|
||||
List<String> excl = Arrays.asList(new String[]{"NotThere", "AlsoNotThere", eRoot.getName()});
|
||||
FilteringDirectoryNode d = new FilteringDirectoryNode(fs.getRoot(), excl);
|
||||
|
||||
assertEquals(2, d.getEntryCount());
|
||||
assertEquals(true, d.hasEntry(dirA.getName()));
|
||||
assertEquals(true, d.hasEntry(dirB.getName()));
|
||||
assertEquals(false, d.hasEntry(eRoot.getName()));
|
||||
|
||||
assertEquals(dirA, d.getEntry(dirA.getName()));
|
||||
assertEquals(dirB, d.getEntry(dirB.getName()));
|
||||
try {
|
||||
d.getEntry(eRoot.getName());
|
||||
fail("Should be filtered");
|
||||
} catch (FileNotFoundException e) {
|
||||
}
|
||||
|
||||
Iterator<Entry> i = d.getEntries();
|
||||
assertEquals(dirA, i.next());
|
||||
assertEquals(dirB, i.next());
|
||||
assertEquals(null, i.next());
|
||||
|
||||
|
||||
// Filter more
|
||||
excl = Arrays.asList(new String[]{"NotThere", "AlsoNotThere", eRoot.getName(), dirA.getName()});
|
||||
d = new FilteringDirectoryNode(fs.getRoot(), excl);
|
||||
|
||||
assertEquals(1, d.getEntryCount());
|
||||
assertEquals(false, d.hasEntry(dirA.getName()));
|
||||
assertEquals(true, d.hasEntry(dirB.getName()));
|
||||
assertEquals(false, d.hasEntry(eRoot.getName()));
|
||||
|
||||
try {
|
||||
d.getEntry(dirA.getName());
|
||||
fail("Should be filtered");
|
||||
} catch (FileNotFoundException e) {
|
||||
}
|
||||
assertEquals(dirB, d.getEntry(dirB.getName()));
|
||||
try {
|
||||
d.getEntry(eRoot.getName());
|
||||
fail("Should be filtered");
|
||||
} catch (FileNotFoundException e) {
|
||||
}
|
||||
|
||||
i = d.getEntries();
|
||||
assertEquals(dirB, i.next());
|
||||
assertEquals(null, i.next());
|
||||
|
||||
|
||||
// Filter everything
|
||||
excl = Arrays.asList(new String[]{"NotThere", eRoot.getName(), dirA.getName(), dirB.getName()});
|
||||
d = new FilteringDirectoryNode(fs.getRoot(), excl);
|
||||
|
||||
assertEquals(0, d.getEntryCount());
|
||||
assertEquals(false, d.hasEntry(dirA.getName()));
|
||||
assertEquals(false, d.hasEntry(dirB.getName()));
|
||||
assertEquals(false, d.hasEntry(eRoot.getName()));
|
||||
|
||||
try {
|
||||
d.getEntry(dirA.getName());
|
||||
fail("Should be filtered");
|
||||
} catch (FileNotFoundException e) {
|
||||
}
|
||||
try {
|
||||
d.getEntry(dirB.getName());
|
||||
fail("Should be filtered");
|
||||
} catch (FileNotFoundException e) {
|
||||
}
|
||||
try {
|
||||
d.getEntry(eRoot.getName());
|
||||
fail("Should be filtered");
|
||||
} catch (FileNotFoundException e) {
|
||||
}
|
||||
|
||||
i = d.getEntries();
|
||||
assertEquals(null, i.next());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNestedFiltering() throws Exception {
|
||||
List<String> excl = Arrays.asList(new String[]{
|
||||
dirA.getName() + "/" + "MadeUp",
|
||||
dirA.getName() + "/" + eA.getName(),
|
||||
dirA.getName() + "/" + dirAA.getName() + "/Test",
|
||||
eRoot.getName()
|
||||
});
|
||||
FilteringDirectoryNode d = new FilteringDirectoryNode(fs.getRoot(), excl);
|
||||
|
||||
// Check main
|
||||
assertEquals(2, d.getEntryCount());
|
||||
assertEquals(true, d.hasEntry(dirA.getName()));
|
||||
assertEquals(true, d.hasEntry(dirB.getName()));
|
||||
assertEquals(false, d.hasEntry(eRoot.getName()));
|
||||
|
||||
// Check filtering down
|
||||
assertEquals(true, d.getEntry(dirA.getName()) instanceof FilteringDirectoryNode);
|
||||
assertEquals(false, d.getEntry(dirB.getName()) instanceof FilteringDirectoryNode);
|
||||
|
||||
DirectoryEntry fdA = (DirectoryEntry) d.getEntry(dirA.getName());
|
||||
assertEquals(false, fdA.hasEntry(eA.getName()));
|
||||
assertEquals(true, fdA.hasEntry(dirAA.getName()));
|
||||
|
||||
DirectoryEntry fdAA = (DirectoryEntry) fdA.getEntry(dirAA.getName());
|
||||
assertEquals(true, fdAA.hasEntry(eAA.getName()));
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void testNullDirectory() {
|
||||
new FilteringDirectoryNode(null, null);
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user