You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TestFilteringDirectoryNode.java 8.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /* ====================================================================
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.poifs.filesystem;
  16. import static org.junit.jupiter.api.Assertions.assertEquals;
  17. import static org.junit.jupiter.api.Assertions.assertFalse;
  18. import static org.junit.jupiter.api.Assertions.assertThrows;
  19. import static org.junit.jupiter.api.Assertions.assertTrue;
  20. import static org.junit.jupiter.api.Assertions.fail;
  21. import java.io.ByteArrayInputStream;
  22. import java.io.FileNotFoundException;
  23. import java.util.Arrays;
  24. import java.util.HashSet;
  25. import java.util.Iterator;
  26. import java.util.List;
  27. import java.util.NoSuchElementException;
  28. import java.util.Spliterator;
  29. import org.junit.jupiter.api.BeforeEach;
  30. import org.junit.jupiter.api.Test;
  31. /**
  32. * Class to test FilteringDirectoryNode functionality
  33. */
  34. final class TestFilteringDirectoryNode {
  35. private POIFSFileSystem fs;
  36. private DirectoryEntry dirA;
  37. private DirectoryEntry dirAA;
  38. private DirectoryEntry dirB;
  39. private DocumentEntry eRoot;
  40. private DocumentEntry eA;
  41. private DocumentEntry eAA;
  42. @BeforeEach
  43. void setUp() throws Exception {
  44. fs = new POIFSFileSystem();
  45. dirA = fs.createDirectory("DirA");
  46. dirB = fs.createDirectory("DirB");
  47. dirAA = dirA.createDirectory("DirAA");
  48. eRoot = fs.getRoot().createDocument("Root", new ByteArrayInputStream(new byte[]{}));
  49. eA = dirA.createDocument("NA", new ByteArrayInputStream(new byte[]{}));
  50. eAA = dirAA.createDocument("NAA", new ByteArrayInputStream(new byte[]{}));
  51. }
  52. @Test
  53. void testNoFiltering() throws Exception {
  54. FilteringDirectoryNode d = new FilteringDirectoryNode(fs.getRoot(), new HashSet<>());
  55. assertEquals(3, d.getEntryCount());
  56. assertEquals(dirA.getName(), d.getEntry(dirA.getName()).getName());
  57. assertTrue(d.getEntry(dirA.getName()).isDirectoryEntry());
  58. assertFalse(d.getEntry(dirA.getName()).isDocumentEntry());
  59. assertTrue(d.getEntry(dirB.getName()).isDirectoryEntry());
  60. assertFalse(d.getEntry(dirB.getName()).isDocumentEntry());
  61. assertFalse(d.getEntry(eRoot.getName()).isDirectoryEntry());
  62. assertTrue(d.getEntry(eRoot.getName()).isDocumentEntry());
  63. Iterator<Entry> i = d.getEntries();
  64. assertEquals(dirA, i.next());
  65. assertEquals(dirB, i.next());
  66. assertEquals(eRoot, i.next());
  67. assertThrows(NoSuchElementException.class, i::next, "Should throw NoSuchElementException when depleted");
  68. Spliterator<Entry> s = d.spliterator();
  69. s.tryAdvance(entry -> assertEquals(dirA, entry));
  70. s.tryAdvance(entry -> assertEquals(dirB, entry));
  71. s.tryAdvance(entry -> assertEquals(eRoot, entry));
  72. assertFalse(s.tryAdvance(entry -> fail("Should be depleted")), "Should return false when depleted");
  73. }
  74. @Test
  75. void testChildFiltering() throws Exception {
  76. List<String> excl = Arrays.asList("NotThere", "AlsoNotThere", eRoot.getName());
  77. FilteringDirectoryNode d1 = new FilteringDirectoryNode(fs.getRoot(), excl);
  78. assertEquals(2, d1.getEntryCount());
  79. assertTrue(d1.hasEntry(dirA.getName()));
  80. assertTrue(d1.hasEntry(dirB.getName()));
  81. assertFalse(d1.hasEntry(eRoot.getName()));
  82. assertEquals(dirA, d1.getEntry(dirA.getName()));
  83. assertEquals(dirB, d1.getEntry(dirB.getName()));
  84. assertThrows(FileNotFoundException.class, () -> d1.getEntry(eRoot.getName()));
  85. Iterator<Entry> i = d1.getEntries();
  86. assertEquals(dirA, i.next());
  87. assertEquals(dirB, i.next());
  88. assertThrows(NoSuchElementException.class, i::next, "Should throw NoSuchElementException when depleted");
  89. Spliterator<Entry> s1 = d1.spliterator();
  90. s1.tryAdvance(entry -> assertEquals(dirA, entry));
  91. s1.tryAdvance(entry -> assertEquals(dirB, entry));
  92. assertFalse(s1.tryAdvance(entry -> fail("Should be depleted")), "Should return false when depleted");
  93. // Filter more
  94. excl = Arrays.asList("NotThere", "AlsoNotThere", eRoot.getName(), dirA.getName());
  95. FilteringDirectoryNode d2 = new FilteringDirectoryNode(fs.getRoot(), excl);
  96. assertEquals(1, d2.getEntryCount());
  97. assertFalse(d2.hasEntry(dirA.getName()));
  98. assertTrue(d2.hasEntry(dirB.getName()));
  99. assertFalse(d2.hasEntry(eRoot.getName()));
  100. assertThrows(FileNotFoundException.class, () -> d2.getEntry(dirA.getName()), "Should be filtered");
  101. assertEquals(dirB, d2.getEntry(dirB.getName()));
  102. assertThrows(FileNotFoundException.class, () -> d2.getEntry(eRoot.getName()), "Should be filtered");
  103. i = d2.getEntries();
  104. assertEquals(dirB, i.next());
  105. assertThrows(NoSuchElementException.class, i::next, "Should throw NoSuchElementException when depleted");
  106. Spliterator<Entry> s2 = d2.spliterator();
  107. s2.tryAdvance(entry -> assertEquals(dirB, entry));
  108. assertFalse(s2.tryAdvance(entry -> fail("Should be depleted")), "Should return false when depleted");
  109. // Filter everything
  110. excl = Arrays.asList("NotThere", eRoot.getName(), dirA.getName(), dirB.getName());
  111. FilteringDirectoryNode d3 = new FilteringDirectoryNode(fs.getRoot(), excl);
  112. assertEquals(0, d3.getEntryCount());
  113. assertFalse(d3.hasEntry(dirA.getName()));
  114. assertFalse(d3.hasEntry(dirB.getName()));
  115. assertFalse(d3.hasEntry(eRoot.getName()));
  116. assertThrows(FileNotFoundException.class, () -> d3.getEntry(dirA.getName()), "Should be filtered");
  117. assertThrows(FileNotFoundException.class, () -> d3.getEntry(dirB.getName()), "Should be filtered");
  118. assertThrows(FileNotFoundException.class, () -> d3.getEntry(eRoot.getName()), "Should be filtered");
  119. i = d3.getEntries();
  120. assertThrows(NoSuchElementException.class, i::next, "Should throw NoSuchElementException when depleted");
  121. Spliterator<Entry> s3 = d3.spliterator();
  122. assertFalse(s3.tryAdvance(entry -> fail("Should be depleted")), "Should return false when depleted");
  123. }
  124. @Test
  125. void testNestedFiltering() throws Exception {
  126. List<String> excl = Arrays.asList(dirA.getName() + "/" + "MadeUp",
  127. dirA.getName() + "/" + eA.getName(),
  128. dirA.getName() + "/" + dirAA.getName() + "/Test",
  129. eRoot.getName());
  130. FilteringDirectoryNode d = new FilteringDirectoryNode(fs.getRoot(), excl);
  131. // Check main
  132. assertEquals(2, d.getEntryCount());
  133. assertTrue(d.hasEntry(dirA.getName()));
  134. assertTrue(d.hasEntry(dirB.getName()));
  135. assertFalse(d.hasEntry(eRoot.getName()));
  136. // Check filtering down
  137. assertTrue(d.getEntry(dirA.getName()) instanceof FilteringDirectoryNode);
  138. assertFalse(d.getEntry(dirB.getName()) instanceof FilteringDirectoryNode);
  139. DirectoryEntry fdA = (DirectoryEntry) d.getEntry(dirA.getName());
  140. assertFalse(fdA.hasEntry(eA.getName()));
  141. assertTrue(fdA.hasEntry(dirAA.getName()));
  142. DirectoryEntry fdAA = (DirectoryEntry) fdA.getEntry(dirAA.getName());
  143. assertTrue(fdAA.hasEntry(eAA.getName()));
  144. }
  145. @Test
  146. void testNullDirectory() {
  147. assertThrows(IllegalArgumentException.class, () -> new FilteringDirectoryNode(null, null));
  148. }
  149. }