001: /* ====================================================================
002: Licensed to the Apache Software Foundation (ASF) under one or more
003: contributor license agreements. See the NOTICE file distributed with
004: this work for additional information regarding copyright ownership.
005: The ASF licenses this file to You under the Apache License, Version 2.0
006: (the "License"); you may not use this file except in compliance with
007: the License. You may obtain a copy of the License at
008:
009: http://www.apache.org/licenses/LICENSE-2.0
010:
011: Unless required by applicable law or agreed to in writing, software
012: distributed under the License is distributed on an "AS IS" BASIS,
013: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: See the License for the specific language governing permissions and
015: limitations under the License.
016: ==================================================================== */
017:
018: package org.apache.poi.poifs.filesystem;
019:
020: import java.io.*;
021:
022: import java.util.*;
023:
024: import junit.framework.*;
025:
026: import org.apache.poi.poifs.property.DirectoryProperty;
027: import org.apache.poi.poifs.property.DocumentProperty;
028:
029: /**
030: * Class to test DirectoryNode functionality
031: *
032: * @author Marc Johnson
033: */
034:
035: public class TestDirectoryNode extends TestCase {
036:
037: /**
038: * Constructor TestDirectoryNode
039: *
040: * @param name
041: */
042:
043: public TestDirectoryNode(String name) {
044: super (name);
045: }
046:
047: /**
048: * test trivial constructor (a DirectoryNode with no children)
049: *
050: * @exception IOException
051: */
052:
053: public void testEmptyConstructor() throws IOException {
054: POIFSFileSystem fs = new POIFSFileSystem();
055: DirectoryProperty property1 = new DirectoryProperty("parent");
056: DirectoryProperty property2 = new DirectoryProperty("child");
057: DirectoryNode parent = new DirectoryNode(property1, fs, null);
058: DirectoryNode node = new DirectoryNode(property2, fs, parent);
059:
060: assertEquals(0, parent.getPath().length());
061: assertEquals(1, node.getPath().length());
062: assertEquals("child", node.getPath().getComponent(0));
063:
064: // verify that getEntries behaves correctly
065: int count = 0;
066: Iterator iter = node.getEntries();
067:
068: while (iter.hasNext()) {
069: count++;
070: iter.next();
071: }
072: assertEquals(0, count);
073:
074: // verify behavior of isEmpty
075: assertTrue(node.isEmpty());
076:
077: // verify behavior of getEntryCount
078: assertEquals(0, node.getEntryCount());
079:
080: // verify behavior of getEntry
081: try {
082: node.getEntry("foo");
083: fail("should have caught FileNotFoundException");
084: } catch (FileNotFoundException ignored) {
085:
086: // as expected
087: }
088:
089: // verify behavior of isDirectoryEntry
090: assertTrue(node.isDirectoryEntry());
091:
092: // verify behavior of getName
093: assertEquals(property2.getName(), node.getName());
094:
095: // verify behavior of isDocumentEntry
096: assertTrue(!node.isDocumentEntry());
097:
098: // verify behavior of getParent
099: assertEquals(parent, node.getParent());
100: }
101:
102: /**
103: * test non-trivial constructor (a DirectoryNode with children)
104: *
105: * @exception IOException
106: */
107:
108: public void testNonEmptyConstructor() throws IOException {
109: DirectoryProperty property1 = new DirectoryProperty("parent");
110: DirectoryProperty property2 = new DirectoryProperty("child1");
111:
112: property1.addChild(property2);
113: property1.addChild(new DocumentProperty("child2", 2000));
114: property2.addChild(new DocumentProperty("child3", 30000));
115: DirectoryNode node = new DirectoryNode(property1,
116: new POIFSFileSystem(), null);
117:
118: // verify that getEntries behaves correctly
119: int count = 0;
120: Iterator iter = node.getEntries();
121:
122: while (iter.hasNext()) {
123: count++;
124: iter.next();
125: }
126: assertEquals(2, count);
127:
128: // verify behavior of isEmpty
129: assertTrue(!node.isEmpty());
130:
131: // verify behavior of getEntryCount
132: assertEquals(2, node.getEntryCount());
133:
134: // verify behavior of getEntry
135: DirectoryNode child1 = (DirectoryNode) node.getEntry("child1");
136:
137: child1.getEntry("child3");
138: node.getEntry("child2");
139: try {
140: node.getEntry("child3");
141: fail("should have caught FileNotFoundException");
142: } catch (FileNotFoundException ignored) {
143:
144: // as expected
145: }
146:
147: // verify behavior of isDirectoryEntry
148: assertTrue(node.isDirectoryEntry());
149:
150: // verify behavior of getName
151: assertEquals(property1.getName(), node.getName());
152:
153: // verify behavior of isDocumentEntry
154: assertTrue(!node.isDocumentEntry());
155:
156: // verify behavior of getParent
157: assertNull(node.getParent());
158: }
159:
160: /**
161: * test deletion methods
162: *
163: * @exception IOException
164: */
165:
166: public void testDeletion() throws IOException {
167: POIFSFileSystem fs = new POIFSFileSystem();
168: DirectoryEntry root = fs.getRoot();
169:
170: // verify cannot delete the root directory
171: assertTrue(!root.delete());
172: DirectoryEntry dir = fs.createDirectory("myDir");
173:
174: assertTrue(!root.isEmpty());
175:
176: // verify can delete empty directory
177: assertTrue(dir.delete());
178: dir = fs.createDirectory("NextDir");
179: DocumentEntry doc = dir.createDocument("foo",
180: new ByteArrayInputStream(new byte[1]));
181:
182: assertTrue(!dir.isEmpty());
183:
184: // verify cannot delete empty directory
185: assertTrue(!dir.delete());
186: assertTrue(doc.delete());
187:
188: // verify now we can delete it
189: assertTrue(dir.delete());
190: assertTrue(root.isEmpty());
191: }
192:
193: /**
194: * test change name methods
195: *
196: * @exception IOException
197: */
198:
199: public void testRename() throws IOException {
200: POIFSFileSystem fs = new POIFSFileSystem();
201: DirectoryEntry root = fs.getRoot();
202:
203: // verify cannot rename the root directory
204: assertTrue(!root.renameTo("foo"));
205: DirectoryEntry dir = fs.createDirectory("myDir");
206:
207: assertTrue(dir.renameTo("foo"));
208: assertEquals("foo", dir.getName());
209: DirectoryEntry dir2 = fs.createDirectory("myDir");
210:
211: assertTrue(!dir2.renameTo("foo"));
212: assertEquals("myDir", dir2.getName());
213: assertTrue(dir.renameTo("FirstDir"));
214: assertTrue(dir2.renameTo("foo"));
215: assertEquals("foo", dir2.getName());
216: }
217:
218: /**
219: * main method to run the unit tests
220: *
221: * @param ignored_args
222: */
223:
224: public static void main(String[] ignored_args) {
225: System.out
226: .println("Testing org.apache.poi.poifs.filesystem.DirectoryNode");
227: junit.textui.TestRunner.run(TestDirectoryNode.class);
228: }
229: }
|