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.

TestDirectoryNode.java 7.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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.assertNull;
  19. import static org.junit.jupiter.api.Assertions.assertThrows;
  20. import static org.junit.jupiter.api.Assertions.assertTrue;
  21. import java.io.ByteArrayInputStream;
  22. import java.io.FileNotFoundException;
  23. import java.io.IOException;
  24. import java.util.Iterator;
  25. import org.apache.poi.poifs.property.DirectoryProperty;
  26. import org.apache.poi.poifs.property.DocumentProperty;
  27. import org.junit.jupiter.api.Test;
  28. /**
  29. * Class to test DirectoryNode functionality
  30. */
  31. final class TestDirectoryNode {
  32. /**
  33. * test trivial constructor (a DirectoryNode with no children)
  34. */
  35. @Test
  36. void testEmptyConstructor() throws IOException {
  37. try (POIFSFileSystem fs = new POIFSFileSystem()) {
  38. DirectoryProperty property1 = new DirectoryProperty("parent");
  39. DirectoryProperty property2 = new DirectoryProperty("child");
  40. DirectoryNode parent = new DirectoryNode(property1, fs, null);
  41. DirectoryNode node = new DirectoryNode(property2, fs, parent);
  42. assertEquals(0, parent.getPath().length());
  43. assertEquals(1, node.getPath().length());
  44. assertEquals("child", node.getPath().getComponent(0));
  45. // verify that getEntries behaves correctly
  46. int count = 0;
  47. Iterator<Entry> iter = node.getEntries();
  48. while (iter.hasNext()) {
  49. count++;
  50. iter.next();
  51. }
  52. assertEquals(0, count);
  53. // verify that spliterator behaves correctly
  54. assertEquals(0, node.spliterator().getExactSizeIfKnown());
  55. // verify behavior of isEmpty
  56. assertTrue(node.isEmpty());
  57. // verify behavior of getEntryCount
  58. assertEquals(0, node.getEntryCount());
  59. // verify behavior of getEntry
  60. assertThrows(FileNotFoundException.class, () -> node.getEntry("foo"));
  61. // verify behavior of isDirectoryEntry
  62. assertTrue(node.isDirectoryEntry());
  63. // verify behavior of getName
  64. assertEquals(property2.getName(), node.getName());
  65. // verify behavior of isDocumentEntry
  66. assertFalse(node.isDocumentEntry());
  67. // verify behavior of getParent
  68. assertEquals(parent, node.getParent());
  69. }
  70. }
  71. /**
  72. * test non-trivial constructor (a DirectoryNode with children)
  73. */
  74. @Test
  75. void testNonEmptyConstructor() throws IOException {
  76. DirectoryProperty property1 = new DirectoryProperty("parent");
  77. DirectoryProperty property2 = new DirectoryProperty("child1");
  78. property1.addChild(property2);
  79. property1.addChild(new DocumentProperty("child2", 2000));
  80. property2.addChild(new DocumentProperty("child3", 30000));
  81. try (POIFSFileSystem fs = new POIFSFileSystem()) {
  82. DirectoryNode node = new DirectoryNode(property1, fs, null);
  83. // verify that getEntries behaves correctly
  84. int count = 0;
  85. Iterator<Entry> iter = node.getEntries();
  86. while (iter.hasNext()) {
  87. count++;
  88. iter.next();
  89. }
  90. assertEquals(2, count);
  91. // verify that spliterator behaves correctly
  92. assertEquals(2, node.spliterator().getExactSizeIfKnown());
  93. // verify behavior of isEmpty
  94. assertFalse(node.isEmpty());
  95. // verify behavior of getEntryCount
  96. assertEquals(2, node.getEntryCount());
  97. // verify behavior of getEntry
  98. DirectoryNode child1 = (DirectoryNode) node.getEntry("child1");
  99. child1.getEntry("child3");
  100. node.getEntry("child2");
  101. assertThrows(FileNotFoundException.class, () -> node.getEntry("child3"));
  102. // verify behavior of isDirectoryEntry
  103. assertTrue(node.isDirectoryEntry());
  104. // verify behavior of getName
  105. assertEquals(property1.getName(), node.getName());
  106. // verify behavior of isDocumentEntry
  107. assertFalse(node.isDocumentEntry());
  108. // verify behavior of getParent
  109. assertNull(node.getParent());
  110. }
  111. }
  112. /**
  113. * test deletion methods
  114. */
  115. @Test
  116. void testDeletion() throws IOException {
  117. try (POIFSFileSystem fs = new POIFSFileSystem()) {
  118. DirectoryEntry root = fs.getRoot();
  119. // verify cannot delete the root directory
  120. assertFalse(root.delete());
  121. assertTrue(root.isEmpty());
  122. DirectoryEntry dir = fs.createDirectory("myDir");
  123. assertFalse(root.isEmpty());
  124. assertTrue(dir.isEmpty());
  125. // verify can delete empty directory
  126. assertFalse(root.delete());
  127. assertTrue(dir.delete());
  128. // Now look at a non-empty one
  129. dir = fs.createDirectory("NextDir");
  130. DocumentEntry doc =
  131. dir.createDocument("foo",
  132. new ByteArrayInputStream(new byte[1]));
  133. assertFalse(root.isEmpty());
  134. assertFalse(dir.isEmpty());
  135. // verify cannot delete non-empty directory
  136. assertFalse(dir.delete());
  137. // but we can delete it if we remove the document
  138. assertTrue(doc.delete());
  139. assertTrue(dir.isEmpty());
  140. assertTrue(dir.delete());
  141. // It's really gone!
  142. assertTrue(root.isEmpty());
  143. }
  144. }
  145. /**
  146. * test change name methods
  147. */
  148. @Test
  149. void testRename() throws IOException {
  150. try (POIFSFileSystem fs = new POIFSFileSystem()) {
  151. DirectoryEntry root = fs.getRoot();
  152. // verify cannot rename the root directory
  153. assertFalse(root.renameTo("foo"));
  154. DirectoryEntry dir = fs.createDirectory("myDir");
  155. assertTrue(dir.renameTo("foo"));
  156. assertEquals("foo", dir.getName());
  157. DirectoryEntry dir2 = fs.createDirectory("myDir");
  158. assertFalse(dir2.renameTo("foo"));
  159. assertEquals("myDir", dir2.getName());
  160. assertTrue(dir.renameTo("FirstDir"));
  161. assertTrue(dir2.renameTo("foo"));
  162. assertEquals("foo", dir2.getName());
  163. }
  164. }
  165. }