KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > poi > poifs > filesystem > TestDirectoryNode


1
2 /* ====================================================================
3    Copyright 2002-2004 Apache Software Foundation
4
5    Licensed under the Apache License, Version 2.0 (the "License");
6    you may not use this file except in compliance with the License.
7    You may obtain a copy of the License at
8
9        http://www.apache.org/licenses/LICENSE-2.0
10
11    Unless required by applicable law or agreed to in writing, software
12    distributed under the License is distributed on an "AS IS" BASIS,
13    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14    See the License for the specific language governing permissions and
15    limitations under the License.
16 ==================================================================== */

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 /**
31  * Class to test DirectoryNode functionality
32  *
33  * @author Marc Johnson
34  */

35
36 public class TestDirectoryNode
37     extends TestCase
38 {
39
40     /**
41      * Constructor TestDirectoryNode
42      *
43      * @param name
44      */

45
46     public TestDirectoryNode(String JavaDoc name)
47     {
48         super(name);
49     }
50
51     /**
52      * test trivial constructor (a DirectoryNode with no children)
53      *
54      * @exception IOException
55      */

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         // verify that getEntries behaves correctly
72
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         // verify behavior of isEmpty
83
assertTrue(node.isEmpty());
84
85         // verify behavior of getEntryCount
86
assertEquals(0, node.getEntryCount());
87
88         // verify behavior of getEntry
89
try
90         {
91             node.getEntry("foo");
92             fail("should have caught FileNotFoundException");
93         }
94         catch (FileNotFoundException ignored)
95         {
96
97             // as expected
98
}
99
100         // verify behavior of isDirectoryEntry
101
assertTrue(node.isDirectoryEntry());
102
103         // verify behavior of getName
104
assertEquals(property2.getName(), node.getName());
105
106         // verify behavior of isDocumentEntry
107
assertTrue(!node.isDocumentEntry());
108
109         // verify behavior of getParent
110
assertEquals(parent, node.getParent());
111     }
112
113     /**
114      * test non-trivial constructor (a DirectoryNode with children)
115      *
116      * @exception IOException
117      */

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         // verify that getEntries behaves correctly
132
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         // verify behavior of isEmpty
143
assertTrue(!node.isEmpty());
144
145         // verify behavior of getEntryCount
146
assertEquals(2, node.getEntryCount());
147
148         // verify behavior of getEntry
149
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             // as expected
162
}
163
164         // verify behavior of isDirectoryEntry
165
assertTrue(node.isDirectoryEntry());
166
167         // verify behavior of getName
168
assertEquals(property1.getName(), node.getName());
169
170         // verify behavior of isDocumentEntry
171
assertTrue(!node.isDocumentEntry());
172
173         // verify behavior of getParent
174
assertNull(node.getParent());
175     }
176
177     /**
178      * test deletion methods
179      *
180      * @exception IOException
181      */

182
183     public void testDeletion()
184         throws IOException
185     {
186         POIFSFileSystem fs = new POIFSFileSystem();
187         DirectoryEntry root = fs.getRoot();
188
189         // verify cannot delete the root directory
190
assertTrue(!root.delete());
191         DirectoryEntry dir = fs.createDirectory("myDir");
192
193         assertTrue(!root.isEmpty());
194
195         // verify can delete empty directory
196
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         // verify cannot delete empty directory
205
assertTrue(!dir.delete());
206         assertTrue(doc.delete());
207
208         // verify now we can delete it
209
assertTrue(dir.delete());
210         assertTrue(root.isEmpty());
211     }
212
213     /**
214      * test change name methods
215      *
216      * @exception IOException
217      */

218
219     public void testRename()
220         throws IOException
221     {
222         POIFSFileSystem fs = new POIFSFileSystem();
223         DirectoryEntry root = fs.getRoot();
224
225         // verify cannot rename the root directory
226
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     /**
241      * main method to run the unit tests
242      *
243      * @param ignored_args
244      */

245
246     public static void main(String JavaDoc [] 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