001: /*
002: * Copyright (C) 2006 Methodhead Software LLC. All rights reserved.
003: *
004: * This file is part of TransferCM.
005: *
006: * TransferCM is free software; you can redistribute it and/or modify it under the
007: * terms of the GNU General Public License as published by the Free Software
008: * Foundation; either version 2 of the License, or (at your option) any later
009: * version.
010: *
011: * TransferCM is distributed in the hope that it will be useful, but WITHOUT ANY
012: * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
013: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
014: * details.
015: *
016: * You should have received a copy of the GNU General Public License along with
017: * TransferCM; if not, write to the Free Software Foundation, Inc., 51 Franklin St,
018: * Fifth Floor, Boston, MA 02110-1301 USA
019: */
020:
021: package com.methodhead.res;
022:
023: import java.util.*;
024: import java.sql.*;
025: import junit.framework.*;
026: import org.apache.log4j.*;
027: import com.methodhead.persistable.*;
028: import com.methodhead.test.*;
029: import com.methodhead.tree.*;
030: import java.io.*;
031: import org.apache.commons.io.*;
032:
033: public class FileTreeTest extends TestCase {
034:
035: private File testDir_ = null;
036: private File testDir1_ = null;
037: private File testDir2_ = null;
038: private File testDir3_ = null;
039: private File testDir4_ = null;
040: private File testFile1_ = null;
041: private File testFile2_ = null;
042: private File testFile3_ = null;
043: private File testFile4_ = null;
044:
045: private FileManager fileManager = null;
046: private File file = null;
047: private Directory directory = null;
048: private File[] files = null;
049: private String[] fileNames = null;
050: private Directory[] directories = null;
051: private List list = null;
052: private FoldingTreeNode node = null;
053: private FoldingTreeNode root = null;
054: private FileTree fileTree = null;
055:
056: static {
057: TestUtils.initLogger();
058: }
059:
060: public FileTreeTest(String name) {
061: super (name);
062: }
063:
064: private void setUpFileManager() throws IOException {
065:
066: //
067: // delete testdir if it exists
068: //
069: testDir_ = new File("build/testdir");
070: if (testDir_.exists())
071: FileUtils.deleteDirectory(testDir_);
072:
073: //
074: // create testdir
075: //
076: testDir_.mkdir();
077:
078: //
079: // build/
080: // testdir/
081: // testdir1/ (Dir1)
082: // testfile1.txt
083: // testfile2.txt
084: // testdir2/ (Dir2)
085: // testdir3/
086: // testfile3.txt
087: // testdir4/
088: // testfile4.txt
089: //
090: testDir1_ = new File("build/testdir/testdir1");
091: testDir1_.mkdir();
092:
093: testFile1_ = new File("build/testdir/testdir1/testfile1.txt");
094: testFile1_.createNewFile();
095:
096: testFile2_ = new File("build/testdir/testdir1/testfile2.txt");
097: testFile2_.createNewFile();
098:
099: testDir2_ = new File("build/testdir/testdir2");
100: testDir2_.mkdir();
101:
102: testDir3_ = new File("build/testdir/testdir2/testdir3");
103: testDir3_.mkdir();
104:
105: testDir4_ = new File("build/testdir/testdir2/testdir4");
106: testDir4_.mkdir();
107:
108: testFile3_ = new File(
109: "build/testdir/testdir2/testdir3/testfile3.txt");
110: testFile3_.createNewFile();
111:
112: testFile4_ = new File("build/testdir/testdir2/testfile4.txt");
113: testFile4_.createNewFile();
114:
115: fileManager = new FileManager();
116: fileManager.addDirectory("Dir1", testDir1_);
117: fileManager.addDirectory("Dir2", testDir2_);
118: }
119:
120: private void setUpFileTree() {
121: fileTree = new FileTree();
122: fileTree.build(fileManager);
123: }
124:
125: protected void setUp() {
126: try {
127: } catch (Exception e) {
128: fail(e.getMessage());
129: }
130: }
131:
132: protected void tearDown() {
133: }
134:
135: public void testNewNode() {
136: try {
137: setUpFileManager();
138:
139: fileTree = new FileTree();
140:
141: node = fileTree.newNode("Dir1");
142: assertNotNull(node);
143: assertEquals("Dir1", node.getLabel());
144: assertEquals("listFiles.do?path=Dir1", node.getUrl());
145: assertEquals(false, node.getOpened());
146: assertEquals(0, node.getChildCount());
147: assertEquals("dir", node.getIconHint());
148:
149: node = fileTree.newNode("Dir2/testdir3");
150: assertNotNull(node);
151: assertEquals("testdir3", node.getLabel());
152: assertEquals("listFiles.do?path=Dir2/testdir3", node
153: .getUrl());
154: assertEquals(false, node.getOpened());
155: assertEquals(0, node.getChildCount());
156: assertEquals("dir", node.getIconHint());
157: } catch (Exception e) {
158: e.printStackTrace();
159: fail();
160: }
161: }
162:
163: public void testUpdateNode() {
164: try {
165: setUpFileManager();
166:
167: fileTree = new FileTree();
168:
169: //
170: // set up a node
171: //
172: node = fileTree.newNode("Dir1");
173: node.setOpened(true);
174:
175: fileTree.updateNode(node, "Dir2/testdir3");
176: assertNotNull(node);
177: assertEquals("testdir3", node.getLabel());
178: assertEquals("listFiles.do?path=Dir2/testdir3", node
179: .getUrl());
180: assertEquals(true, node.getOpened());
181: assertEquals(0, node.getChildCount());
182: assertEquals("dir", node.getIconHint());
183: } catch (Exception e) {
184: e.printStackTrace();
185: fail();
186: }
187: }
188:
189: public void testBuildNode() {
190: try {
191: setUpFileManager();
192:
193: fileTree = new FileTree();
194:
195: node = fileTree.buildNode("Dir1", testDir1_);
196: assertNotNull(node);
197: assertEquals("Dir1", node.getLabel());
198: assertEquals("listFiles.do?path=Dir1", node.getUrl());
199: assertEquals(false, node.getOpened());
200: assertEquals(0, node.getChildCount());
201:
202: node = fileTree.buildNode("Dir2", testDir2_);
203: assertNotNull(node);
204: assertEquals("Dir2", node.getLabel());
205: assertEquals("listFiles.do?path=Dir2", node.getUrl());
206: assertEquals(false, node.getOpened());
207: assertEquals(2, node.getChildCount());
208:
209: node = (FoldingTreeNode) node.getChildAt(0);
210: assertNotNull(node);
211: assertEquals("testdir3", node.getLabel());
212: assertEquals("listFiles.do?path=Dir2/testdir3", node
213: .getUrl());
214: assertEquals(false, node.getOpened());
215: assertEquals(0, node.getChildCount());
216: } catch (Exception e) {
217: e.printStackTrace();
218: fail();
219: }
220: }
221:
222: public void testBuild() {
223: try {
224: setUpFileManager();
225:
226: fileTree = new FileTree();
227: fileTree.build(fileManager);
228:
229: assertNotNull(fileTree.getRoot());
230:
231: root = (FoldingTreeNode) fileTree.getRoot();
232: assertEquals(2, root.getChildCount());
233: assertEquals(true, root.getOpened());
234:
235: node = (FoldingTreeNode) root.getChildAt(0);
236: assertEquals("Dir1", node.getLabel());
237: assertEquals("listFiles.do?path=Dir1", node.getUrl());
238: assertEquals("dir", node.getIconHint());
239: assertEquals(0, node.getChildCount());
240: assertEquals(false, node.getOpened());
241:
242: node = (FoldingTreeNode) root.getChildAt(1);
243: assertEquals("Dir2", node.getLabel());
244: assertEquals("listFiles.do?path=Dir2", node.getUrl());
245: assertEquals("dir", node.getIconHint());
246: assertEquals(2, node.getChildCount());
247: assertEquals(false, node.getOpened());
248:
249: node = (FoldingTreeNode) node.getChildAt(0);
250: assertEquals("testdir3", node.getLabel());
251: assertEquals("listFiles.do?path=Dir2/testdir3", node
252: .getUrl());
253: assertEquals("dir", node.getIconHint());
254: assertEquals(false, node.getOpened());
255: } catch (Exception e) {
256: e.printStackTrace();
257: fail();
258: }
259: }
260:
261: public void testFind() {
262: try {
263: setUpFileManager();
264: setUpFileTree();
265:
266: //
267: // find a top-level node
268: //
269: node = fileTree.find("Dir1");
270: assertNotNull(node);
271: assertEquals("Dir1", node.getLabel());
272:
273: //
274: // find a sub-directory
275: //
276: node = fileTree.find("Dir2/testdir3");
277: assertNotNull(node);
278: assertEquals("testdir3", node.getLabel());
279: } catch (Exception e) {
280: e.printStackTrace();
281: fail();
282: }
283: }
284:
285: public void testMove() {
286: try {
287: //
288: // move and rename a directory
289: //
290: setUpFileManager();
291: setUpFileTree();
292:
293: fileTree.move("Dir2", new String[] { "testdir3" }, "Dir1",
294: "newtestdir3");
295:
296: node = fileTree.find("Dir1/newtestdir3");
297: assertNotNull(node);
298:
299: node = fileTree.find("Dir2/testdir3");
300: assertNull(node);
301:
302: //
303: // move a set of directories
304: //
305: setUpFileManager();
306: setUpFileTree();
307:
308: fileTree
309: .move("Dir2",
310: new String[] { "testdir3", "testdir4" },
311: "Dir1", "");
312:
313: node = fileTree.find("Dir1/testdir3");
314: assertNotNull(node);
315:
316: node = fileTree.find("Dir1/testdir4");
317: assertNotNull(node);
318:
319: node = fileTree.find("Dir2/testdir3");
320: assertNull(node);
321:
322: node = fileTree.find("Dir2/testdir4");
323: assertNull(node);
324: } catch (Exception e) {
325: e.printStackTrace();
326: fail();
327: }
328: }
329:
330: public void testCopy() {
331: try {
332: //
333: // copy and rename a directory
334: //
335: setUpFileManager();
336: setUpFileTree();
337:
338: fileTree.copy("Dir2", new String[] { "testdir3" }, "Dir1",
339: "newtestdir3");
340:
341: node = fileTree.find("Dir1/newtestdir3");
342: assertNotNull(node);
343:
344: node = fileTree.find("Dir2/testdir3");
345: assertNotNull(node);
346:
347: //
348: // move a set of directories
349: //
350: setUpFileManager();
351: setUpFileTree();
352:
353: fileTree
354: .copy("Dir2",
355: new String[] { "testdir3", "testdir4" },
356: "Dir1", "");
357:
358: node = fileTree.find("Dir1/testdir3");
359: assertNotNull(node);
360:
361: node = fileTree.find("Dir1/testdir4");
362: assertNotNull(node);
363:
364: node = fileTree.find("Dir2/testdir3");
365: assertNotNull(node);
366:
367: node = fileTree.find("Dir2/testdir4");
368: assertNotNull(node);
369: } catch (Exception e) {
370: e.printStackTrace();
371: fail();
372: }
373: }
374:
375: public void testDelete() {
376: try {
377: //
378: // create a directory
379: //
380: setUpFileManager();
381: setUpFileTree();
382:
383: fileTree.delete("Dir2", new String[] { "testdir3",
384: "testdir4" });
385:
386: node = fileTree.find("Dir2/testdir3");
387: assertNull(node);
388:
389: node = fileTree.find("Dir2/testdir4");
390: assertNull(node);
391: } catch (Exception e) {
392: e.printStackTrace();
393: fail();
394: }
395: }
396:
397: public void testCreate() {
398: try {
399: //
400: // create a directory
401: //
402: setUpFileManager();
403: setUpFileTree();
404:
405: fileTree.create("Dir2", "testdir5", false);
406:
407: node = fileTree.find("Dir2/testdir5");
408: assertNull(node);
409:
410: fileTree.create("Dir2", "testdir5", true);
411:
412: node = fileTree.find("Dir2/testdir5");
413: assertNotNull(node);
414: } catch (Exception e) {
415: e.printStackTrace();
416: fail();
417: }
418: }
419: }
|