1 2 17 18 19 package org.apache.poi.poifs.filesystem; 20 21 import java.io.*; 22 23 import java.util.*; 24 25 import junit.framework.*; 26 27 import org.apache.poi.poifs.property.DirectoryProperty; 28 import org.apache.poi.poifs.property.DocumentProperty; 29 30 35 36 public class TestDirectoryNode 37 extends TestCase 38 { 39 40 45 46 public TestDirectoryNode(String name) 47 { 48 super(name); 49 } 50 51 56 57 public void testEmptyConstructor() 58 throws IOException 59 { 60 POIFSFileSystem fs = new POIFSFileSystem(); 61 DirectoryProperty property1 = new DirectoryProperty("parent"); 62 DirectoryProperty property2 = new DirectoryProperty("child"); 63 DirectoryNode parent = new DirectoryNode(property1, fs, null); 64 DirectoryNode node = new DirectoryNode(property2, fs, 65 parent); 66 67 assertEquals(0, parent.getPath().length()); 68 assertEquals(1, node.getPath().length()); 69 assertEquals("child", node.getPath().getComponent(0)); 70 71 int count = 0; 73 Iterator iter = node.getEntries(); 74 75 while (iter.hasNext()) 76 { 77 count++; 78 iter.next(); 79 } 80 assertEquals(0, count); 81 82 assertTrue(node.isEmpty()); 84 85 assertEquals(0, node.getEntryCount()); 87 88 try 90 { 91 node.getEntry("foo"); 92 fail("should have caught FileNotFoundException"); 93 } 94 catch (FileNotFoundException ignored) 95 { 96 97 } 99 100 assertTrue(node.isDirectoryEntry()); 102 103 assertEquals(property2.getName(), node.getName()); 105 106 assertTrue(!node.isDocumentEntry()); 108 109 assertEquals(parent, node.getParent()); 111 } 112 113 118 119 public void testNonEmptyConstructor() 120 throws IOException 121 { 122 DirectoryProperty property1 = new DirectoryProperty("parent"); 123 DirectoryProperty property2 = new DirectoryProperty("child1"); 124 125 property1.addChild(property2); 126 property1.addChild(new DocumentProperty("child2", 2000)); 127 property2.addChild(new DocumentProperty("child3", 30000)); 128 DirectoryNode node = new DirectoryNode(property1, 129 new POIFSFileSystem(), null); 130 131 int count = 0; 133 Iterator iter = node.getEntries(); 134 135 while (iter.hasNext()) 136 { 137 count++; 138 iter.next(); 139 } 140 assertEquals(2, count); 141 142 assertTrue(!node.isEmpty()); 144 145 assertEquals(2, node.getEntryCount()); 147 148 DirectoryNode child1 = ( DirectoryNode ) node.getEntry("child1"); 150 151 child1.getEntry("child3"); 152 node.getEntry("child2"); 153 try 154 { 155 node.getEntry("child3"); 156 fail("should have caught FileNotFoundException"); 157 } 158 catch (FileNotFoundException ignored) 159 { 160 161 } 163 164 assertTrue(node.isDirectoryEntry()); 166 167 assertEquals(property1.getName(), node.getName()); 169 170 assertTrue(!node.isDocumentEntry()); 172 173 assertNull(node.getParent()); 175 } 176 177 182 183 public void testDeletion() 184 throws IOException 185 { 186 POIFSFileSystem fs = new POIFSFileSystem(); 187 DirectoryEntry root = fs.getRoot(); 188 189 assertTrue(!root.delete()); 191 DirectoryEntry dir = fs.createDirectory("myDir"); 192 193 assertTrue(!root.isEmpty()); 194 195 assertTrue(dir.delete()); 197 dir = fs.createDirectory("NextDir"); 198 DocumentEntry doc = 199 dir.createDocument("foo", 200 new ByteArrayInputStream(new byte[ 1 ])); 201 202 assertTrue(!dir.isEmpty()); 203 204 assertTrue(!dir.delete()); 206 assertTrue(doc.delete()); 207 208 assertTrue(dir.delete()); 210 assertTrue(root.isEmpty()); 211 } 212 213 218 219 public void testRename() 220 throws IOException 221 { 222 POIFSFileSystem fs = new POIFSFileSystem(); 223 DirectoryEntry root = fs.getRoot(); 224 225 assertTrue(!root.renameTo("foo")); 227 DirectoryEntry dir = fs.createDirectory("myDir"); 228 229 assertTrue(dir.renameTo("foo")); 230 assertEquals("foo", dir.getName()); 231 DirectoryEntry dir2 = fs.createDirectory("myDir"); 232 233 assertTrue(!dir2.renameTo("foo")); 234 assertEquals("myDir", dir2.getName()); 235 assertTrue(dir.renameTo("FirstDir")); 236 assertTrue(dir2.renameTo("foo")); 237 assertEquals("foo", dir2.getName()); 238 } 239 240 245 246 public static void main(String [] ignored_args) 247 { 248 System.out 249 .println("Testing org.apache.poi.poifs.filesystem.DirectoryNode"); 250 junit.textui.TestRunner.run(TestDirectoryNode.class); 251 } 252 } 253 | Popular Tags |