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 java.io.*;
026: import junit.framework.*;
027: import org.apache.log4j.*;
028: import org.apache.commons.io.*;
029: import com.methodhead.persistable.*;
030: import com.methodhead.test.*;
031:
032: public class FileManagerTest extends TestCase {
033:
034: private File testDir_ = null;
035: private File testDir1_ = null;
036: private File testDir2_ = null;
037: private File testDir3_ = null;
038: private File testFile1_ = null;
039: private File testFile2_ = null;
040: private File testFile3_ = null;
041: private File testFile4_ = null;
042:
043: private FileManager fileManager = null;
044: private File file = null;
045: private Directory directory = null;
046: private File[] files = null;
047: private String[] fileNames = null;
048: private Directory[] directories = null;
049: private List list = null;
050:
051: static {
052: TestUtils.initLogger();
053: TestUtils.initDb();
054: }
055:
056: public FileManagerTest(String name) {
057: super (name);
058: }
059:
060: private void setUpFileManager() throws IOException {
061:
062: //
063: // delete testdir if it exists
064: //
065: testDir_ = new File("build/testdir");
066: if (testDir_.exists())
067: FileUtils.deleteDirectory(testDir_);
068:
069: //
070: // create testdir
071: //
072: testDir_.mkdir();
073:
074: //
075: // build/
076: // testdir/
077: // testdir1/ (Dir1)
078: // testfile1.txt
079: // testfile2.txt
080: // testdir2/ (Dir2)
081: // testdir3/
082: // testfile3.txt
083: // testfile4.txt
084: //
085: testDir1_ = new File("build/testdir/testdir1");
086: testDir1_.mkdir();
087:
088: testFile1_ = new File("build/testdir/testdir1/testfile1.txt");
089: testFile1_.createNewFile();
090:
091: testFile2_ = new File("build/testdir/testdir1/testfile2.txt");
092: testFile2_.createNewFile();
093:
094: testDir2_ = new File("build/testdir/testdir2");
095: testDir2_.mkdir();
096:
097: testDir3_ = new File("build/testdir/testdir2/testdir3");
098: testDir3_.mkdir();
099:
100: testFile3_ = new File(
101: "build/testdir/testdir2/testdir3/testfile3.txt");
102: testFile3_.createNewFile();
103:
104: testFile4_ = new File("build/testdir/testdir2/testfile4.txt");
105: testFile4_.createNewFile();
106:
107: fileManager = new FileManager();
108: fileManager.addDirectory("Dir1", testDir1_);
109: fileManager.addDirectory("Dir2", testDir2_);
110: }
111:
112: protected void setUp() {
113: //setLogLevel( Level.DEBUG );
114: try {
115: } catch (Exception e) {
116: fail(e.getMessage());
117: }
118: }
119:
120: protected void tearDown() {
121: }
122:
123: public void testFileForPath() {
124: try {
125: setUpFileManager();
126:
127: //
128: // should throw an exception if dir is invalid
129: //
130: try {
131: fileManager.getFileForPath("Dir1/../../passwd");
132: fail("No exception thrown.");
133: } catch (ResException e) {
134: // success
135: }
136:
137: assertEquals(testDir1_, fileManager.getFileForPath("Dir1"));
138: assertEquals(testDir3_, fileManager
139: .getFileForPath("Dir2/testdir3"));
140: assertNull(fileManager.getFileForPath("BadDir"));
141: assertNull(fileManager.getFileForPath("Dir2/baddir"));
142: } catch (Exception e) {
143: e.printStackTrace();
144: fail();
145: }
146: }
147:
148: public void testAddDirectory() {
149: try {
150: setUpFileManager();
151:
152: //
153: // should throw an exception if dir is invalid
154: //
155: fileManager = new FileManager();
156:
157: try {
158: fileManager.addDirectory("TestDir", new File(
159: "testdir/foo"));
160: fail("No exception thrown.");
161: } catch (ResException e) {
162: // success
163: }
164:
165: try {
166: fileManager.addDirectory("TestDir", testFile1_);
167: fail("No exception thrown.");
168: } catch (ResException e) {
169: // success
170: }
171:
172: //
173: // add a single valid dir
174: //
175: testDir1_.mkdir();
176: fileManager.addDirectory("TestDir", testDir1_);
177: assertEquals(1, fileManager.directories_.keySet().size());
178:
179: directory = (Directory) fileManager.directories_
180: .get("TestDir");
181: assertNotNull(directory);
182: assertEquals("TestDir", directory.getName());
183: assertEquals(directory.getFile(), testDir1_);
184:
185: //
186: // add a directory with the same name
187: //
188: try {
189: fileManager.addDirectory("TestDir", testDir2_);
190: fail("No exception thrown.");
191: } catch (ResException e) {
192: // success
193: }
194:
195: //
196: // add a directory with an empty name
197: //
198: fileManager.addDirectory("", testDir2_);
199: assertEquals(2, fileManager.directories_.keySet().size());
200:
201: directory = (Directory) fileManager.directories_.get("");
202: assertNotNull(directory);
203: assertEquals("", directory.getName());
204: assertEquals(directory.getFile(), testDir2_);
205: } catch (Exception e) {
206: e.printStackTrace();
207: fail();
208: }
209: }
210:
211: public void testGetFile() {
212: try {
213: setUpFileManager();
214:
215: //
216: // get a file in a root directory
217: //
218: file = fileManager.getFile("Dir1", "testfile1.txt");
219: assertEquals(testFile1_, file);
220:
221: //
222: // get a file in a subdirectory
223: //
224: file = fileManager
225: .getFile("Dir2/testdir3", "testfile3.txt");
226: assertEquals(testFile3_, file);
227:
228: //
229: // get a directory
230: //
231: file = fileManager.getFile("Dir2", "testdir3");
232: assertEquals(testDir3_, file);
233:
234: //
235: // get a non-existant file
236: //
237: file = fileManager
238: .getFile("Dir2/testdir3", "testfile4.txt");
239: assertNull(file);
240:
241: //
242: // get some files that aren't there
243: //
244: assertNull(fileManager.getFile("BadDir", "testfile1.txt"));
245: assertNull(fileManager.getFile("Dir1/BadDir",
246: "testfile1.txt"));
247: assertNull(fileManager.getFile("Dir1", "badfile.txt"));
248: } catch (Exception e) {
249: e.printStackTrace();
250: fail();
251: }
252: }
253:
254: public void testGetNewFile() {
255: try {
256: setUpFileManager();
257:
258: //
259: // attempt to get an existing file as a new file
260: //
261: try {
262: file = fileManager.getNewFile("Dir1", "testfile1.txt");
263: fail("No exception thrown");
264: } catch (ResException e) {
265: }
266:
267: //
268: // get a file in a top-level directory
269: //
270: file = fileManager.getNewFile("Dir1", "testfile5.txt");
271: assertNotNull(file);
272: assertTrue(!file.exists());
273:
274: //
275: // get a file in a subdirectory
276: //
277: file = fileManager.getNewFile("Dir2/testdir3",
278: "testfile6.txt");
279: assertNotNull(file);
280: assertTrue(!file.exists());
281: } catch (Exception e) {
282: e.printStackTrace();
283: fail();
284: }
285: }
286:
287: public void testGetFiles() {
288: try {
289: setUpFileManager();
290:
291: //
292: // get files from a root dir
293: //
294: files = fileManager.getFiles("Dir1");
295: assertNotNull(files);
296: assertEquals(2, files.length);
297: assertEquals(testFile1_, files[0]);
298: assertEquals(testFile2_, files[1]);
299:
300: //
301: // get files from a subdirectory dir
302: //
303: files = fileManager.getFiles("Dir2/testdir3");
304: assertNotNull(files);
305: assertEquals(1, files.length);
306: assertEquals(testFile3_, files[0]);
307:
308: //
309: // get files from a non-existant dir
310: //
311: assertNull(fileManager.getFiles("Dir2/testdir4"));
312: assertNull(fileManager.getFiles("Dir3"));
313: } catch (Exception e) {
314: e.printStackTrace();
315: fail();
316: }
317: }
318:
319: public void testGetDirectories() {
320: try {
321: setUpFileManager();
322:
323: //
324: // get directories
325: //
326: directories = fileManager.getDirectories();
327: assertEquals(2, directories.length);
328:
329: directory = directories[0];
330: assertNotNull(directory);
331: assertEquals("Dir1", directory.getName());
332: assertEquals(testDir1_, directory.getFile());
333:
334: directory = directories[1];
335: assertNotNull(directory);
336: assertEquals("Dir2", directory.getName());
337: assertEquals(testDir2_, directory.getFile());
338: } catch (Exception e) {
339: e.printStackTrace();
340: fail();
341: }
342: }
343:
344: public void testIsDestSubdir() {
345: try {
346: setUpFileManager();
347:
348: //
349: // set up a file manager
350: //
351: assertTrue(fileManager.isDestSubdir("Dir1",
352: new String[] { "subdir1" }, "Dir1/subdir1"));
353: assertTrue(fileManager.isDestSubdir("Dir1",
354: new String[] { "subdir1" }, "Dir1/subdir1/subdir2"));
355: assertTrue(!fileManager.isDestSubdir("Dir1",
356: new String[] { "subdir1" }, "Dir2/subdir1"));
357: assertTrue(!fileManager.isDestSubdir("Dir1",
358: new String[] { "subdir1" }, "Dir3"));
359: } catch (Exception e) {
360: e.printStackTrace();
361: fail();
362: }
363: }
364:
365: public void testCanOverwrite() {
366: try {
367: setUpFileManager();
368:
369: //
370: // set up a file manager
371: //
372: assertTrue(fileManager.canOverwrite(null, true));
373: assertTrue(fileManager.canOverwrite(null, false));
374: assertTrue(!fileManager.canOverwrite(testDir1_, true));
375: assertTrue(!fileManager.canOverwrite(testDir1_, false));
376: assertTrue(fileManager.canOverwrite(testFile1_, false));
377: assertTrue(!fileManager.canOverwrite(testFile1_, true));
378:
379: assertTrue(fileManager.canOverwrite(testFile1_, null));
380: assertTrue(fileManager.canOverwrite(testDir1_, null));
381: assertTrue(fileManager.canOverwrite(testFile1_, testFile2_));
382: assertTrue(!fileManager.canOverwrite(testFile1_, testDir1_));
383: assertTrue(!fileManager.canOverwrite(testDir1_, testDir2_));
384: assertTrue(!fileManager.canOverwrite(testDir1_, testFile1_));
385: } catch (Exception e) {
386: e.printStackTrace();
387: fail();
388: }
389: }
390:
391: public void testValidateMove() {
392: try {
393: setUpFileManager();
394:
395: //
396: // create some duplicate files
397: //
398: File dupDir = new File("build/testdir/testdir1/testdir3");
399: dupDir.mkdir();
400:
401: File dupFile2 = new File(
402: "build/testdir/testdir1/testfile3.txt");
403: dupFile2.mkdir();
404:
405: //
406: // moving a single file
407: //
408: assertNull(fileManager.validateMove("Dir1",
409: new String[] { "testfile1.txt" }, "Dir2",
410: "testfile1.txt"));
411: assertEquals(FileManager.VALIDATE_INVALIDDESTPATH,
412: fileManager.validateMove("Dir1",
413: new String[] { "testfile1.txt" }, "Dir3",
414: "testfile1.txt"));
415: assertEquals(FileManager.VALIDATE_INVALIDDESTPATH,
416: fileManager.validateMove("Dir1",
417: new String[] { "testfile1.txt" },
418: "Dir3/../../etc", "testfile1.txt"));
419: assertEquals(FileManager.VALIDATE_INVALIDDESTPATH,
420: fileManager.validateMove("Dir1",
421: new String[] { "testfile1.txt" }, "",
422: "testfile1.txt"));
423: assertEquals(FileManager.VALIDATE_INVALIDDESTPATH,
424: fileManager.validateMove("Dir1",
425: new String[] { "testfile1.txt" },
426: "Dir2/baddir", "testfile1.txt"));
427: assertEquals(FileManager.VALIDATE_SUBDIROFSELF, fileManager
428: .validateMove("Dir2", new String[] { "testdir3" },
429: "Dir2/testdir3", "testdir3"));
430: assertEquals(FileManager.VALIDATE_INVALIDDESTNAME,
431: fileManager.validateMove("Dir1",
432: new String[] { "testfile1.txt" }, "Dir2",
433: ""));
434: assertEquals(FileManager.VALIDATE_CANTOVERWRITE,
435: fileManager.validateMove("Dir1",
436: new String[] { "testfile1.txt" }, "Dir2",
437: "testdir3"));
438:
439: //
440: // moving a set of files
441: //
442: assertNull(fileManager.validateMove("Dir1", new String[] {
443: "testfile1.txt", "testfile2.txt" }, "Dir2", ""));
444: assertEquals(FileManager.VALIDATE_INVALIDDESTPATH,
445: fileManager.validateMove("Dir1", new String[] {
446: "testfile1.txt", "testfile2.txt" }, "Dir3",
447: ""));
448: assertEquals(FileManager.VALIDATE_INVALIDDESTPATH,
449: fileManager.validateMove("Dir1", new String[] {
450: "testfile1.txt", "testfile2.txt" },
451: "Dir3/../../etc", "testfile1.txt"));
452: assertEquals(FileManager.VALIDATE_INVALIDDESTPATH,
453: fileManager.validateMove("Dir1", new String[] {
454: "testfile1.txt", "testfile2.txt" }, "",
455: "testfile1.txt"));
456: assertEquals(FileManager.VALIDATE_INVALIDDESTPATH,
457: fileManager.validateMove("Dir1", new String[] {
458: "testfile1.txt", "testfile2.txt" },
459: "Dir2/baddir", "testfile1.txt"));
460: assertEquals(FileManager.VALIDATE_SUBDIROFSELF, fileManager
461: .validateMove("Dir2", new String[] { "testdir3",
462: "testfile4.txt" }, "Dir2/testdir3",
463: "testdir3"));
464: assertEquals(FileManager.VALIDATE_CANTOVERWRITE,
465: fileManager.validateMove("Dir1", new String[] {
466: "testdir3", "testfile1.txt" }, "Dir2", ""));
467: } catch (Exception e) {
468: e.printStackTrace();
469: fail();
470: }
471: }
472:
473: public void testValidateCreate() {
474: try {
475: setUpFileManager();
476:
477: //
478: // overwriting a file
479: //
480: assertNull(fileManager.validateCreate("Dir1",
481: "testfile1.txt", false));
482: assertEquals(FileManager.VALIDATE_CANTOVERWRITE,
483: fileManager.validateCreate("Dir1", "testfile1.txt",
484: true));
485:
486: //
487: // overwriting a directory
488: //
489: assertEquals(FileManager.VALIDATE_CANTOVERWRITE,
490: fileManager.validateCreate("Dir2", "testdir3",
491: false));
492: assertEquals(FileManager.VALIDATE_CANTOVERWRITE,
493: fileManager
494: .validateCreate("Dir2", "testdir3", true));
495: } catch (Exception e) {
496: e.printStackTrace();
497: fail();
498: }
499: }
500:
501: public void testFindOverwriteFiles() {
502: try {
503: setUpFileManager();
504:
505: //
506: // create some duplicate files
507: //
508: File dupFile = new File(
509: "build/testdir/testdir1/testfile4.txt");
510: dupFile.createNewFile();
511:
512: //
513: // no files will be overwritten
514: //
515: assertNull(fileManager.findOverwriteFiles("Dir1",
516: new String[] { "testfile1.txt" }, "Dir2",
517: "testfile1.txt"));
518:
519: //
520: // one file will be overwritten
521: //
522: fileNames = fileManager.findOverwriteFiles("Dir1",
523: new String[] { "testfile1.txt" }, "Dir2",
524: "testfile4.txt");
525: assertNotNull(fileNames);
526: assertEquals(1, fileNames.length);
527: assertEquals("testfile4.txt", fileNames[0]);
528:
529: //
530: // one file in a set will be overwritten
531: //
532: fileNames = fileManager.findOverwriteFiles("Dir1",
533: new String[] { "testfile1.txt", "testfile4.txt" },
534: "Dir2", "");
535: assertNotNull(fileNames);
536: assertEquals(1, fileNames.length);
537: assertEquals("testfile4.txt", fileNames[0]);
538: } catch (Exception e) {
539: e.printStackTrace();
540: fail();
541: }
542: }
543:
544: public void testMoveSingle() {
545: try {
546: setUpFileManager();
547:
548: //
549: // move a single file
550: //
551: fileManager.move("Dir1", new String[] { "testfile1.txt" },
552: "Dir2", "newtestfile1.txt");
553: assertTrue(!testFile1_.exists());
554:
555: file = new File("build/testdir/testdir2/newtestfile1.txt");
556: assertTrue(file.exists());
557: assertTrue(file.isFile());
558: } catch (Exception e) {
559: e.printStackTrace();
560: fail();
561: }
562: }
563:
564: public void testMoveSet() {
565: try {
566: setUpFileManager();
567:
568: //
569: // move a set of files
570: //
571: fileManager.move("Dir1", new String[] { "testfile1.txt",
572: "testfile2.txt" }, "Dir2", "newtestfile1.txt");
573: assertTrue(!testFile1_.exists());
574: assertTrue(!testFile2_.exists());
575:
576: file = new File("build/testdir/testdir2/testfile1.txt");
577: assertTrue(file.exists());
578: assertTrue(file.isFile());
579:
580: file = new File("build/testdir/testdir2/testfile2.txt");
581: assertTrue(file.exists());
582: assertTrue(file.isFile());
583: } catch (Exception e) {
584: e.printStackTrace();
585: fail();
586: }
587: }
588:
589: public void testCopyFile() {
590: try {
591: setUpFileManager();
592:
593: //
594: // copy a file
595: //
596: file = new File("build/testdir/testdir1/newtestfile1.txt");
597: FileManager.copyFile(testFile1_, file);
598: assertTrue(testFile1_.exists());
599: assertTrue(file.exists());
600: assertTrue(file.isFile());
601:
602: //
603: // copy a directory
604: //
605: file = new File("build/testdir/testdir2/newtestdir3");
606: FileManager.copyFile(testDir3_, file);
607: assertTrue(testDir3_.exists());
608: assertTrue(testDir3_.isDirectory());
609: assertTrue(file.exists());
610: assertTrue(file.isDirectory());
611:
612: file = new File(
613: "build/testdir/testdir2/newtestdir3/testfile3.txt");
614: assertTrue(file.exists());
615: assertTrue(file.isFile());
616: } catch (Exception e) {
617: e.printStackTrace();
618: fail();
619: }
620: }
621:
622: public void testCopy() {
623: try {
624: setUpFileManager();
625:
626: //
627: // copy a single file
628: //
629: fileManager.copy("Dir1", new String[] { "testfile1.txt" },
630: "Dir2", "newtestfile1.txt");
631: assertTrue(testFile1_.exists());
632:
633: file = new File("build/testdir/testdir2/newtestfile1.txt");
634: assertTrue(file.exists());
635: assertTrue(file.isFile());
636:
637: //
638: // copy a single directory
639: //
640: fileManager.copy("Dir2", new String[] { "testdir3" },
641: "Dir1", "newtestdir3");
642: assertTrue(testDir3_.exists());
643:
644: file = new File("build/testdir/testdir1/newtestdir3");
645: assertTrue(file.exists());
646: assertTrue(file.isDirectory());
647:
648: file = new File(
649: "build/testdir/testdir1/newtestdir3/testfile3.txt");
650: assertTrue(file.exists());
651: assertTrue(file.isFile());
652: } catch (Exception e) {
653: e.printStackTrace();
654: fail();
655: }
656: }
657:
658: public void testDelete() {
659: try {
660:
661: //
662: // attempt to delete a non-existant file
663: //
664: setUpFileManager();
665:
666: try {
667: fileManager.delete("Dir1",
668: new String[] { "badfile.txt" });
669: fail("No exception thrown.");
670: } catch (Exception e) {
671: }
672:
673: try {
674: fileManager.delete("Dir3",
675: new String[] { "badfile.txt" });
676: fail("No exception thrown.");
677: } catch (Exception e) {
678: }
679:
680: //
681: // attempt to delete a top-level directory
682: //
683: try {
684: fileManager.delete("Dir1", new String[] { "" });
685: fail("No exception thrown.");
686: } catch (Exception e) {
687: }
688:
689: //
690: // delete a single file
691: //
692: setUpFileManager();
693:
694: fileManager
695: .delete("Dir1", new String[] { "testfile1.txt" });
696: assertTrue(!testFile1_.exists());
697:
698: //
699: // delete a single directory
700: //
701: setUpFileManager();
702:
703: fileManager.delete("Dir2", new String[] { "testdir3" });
704: assertTrue(!testDir3_.exists());
705: assertTrue(!testFile3_.exists());
706:
707: //
708: // delete a set of files
709: //
710: setUpFileManager();
711:
712: fileManager.delete("Dir2", new String[] { "testdir3",
713: "testfile4.txt" });
714: assertTrue(!testDir3_.exists());
715: assertTrue(!testFile3_.exists());
716: assertTrue(!testFile4_.exists());
717: } catch (Exception e) {
718: e.printStackTrace();
719: fail();
720: }
721: }
722:
723: public void testCreate() {
724: try {
725:
726: //
727: // create an empty file
728: //
729: setUpFileManager();
730: fileManager.create("Dir1", "testfile5.txt", false);
731:
732: file = new File(testDir1_, "testfile5.txt");
733: assertTrue(file.exists());
734: assertTrue(file.isFile());
735:
736: //
737: // create a directory
738: //
739: fileManager.create("Dir1", "testdir4", true);
740:
741: file = new File(testDir1_, "testdir4");
742: assertTrue(file.exists());
743: assertTrue(file.isDirectory());
744:
745: //
746: // create a file from an input stream
747: //
748: fileManager.create("Dir1", "testfile5.txt",
749: new ByteArrayInputStream("This is a test"
750: .getBytes()));
751: file = new File(testDir1_, "testfile5.txt");
752: assertTrue(file.exists());
753: assertTrue(file.isFile());
754: assertEquals("This is a test", FileUtils.readFileToString(
755: file, "ISO-8859-1"));
756: } catch (Exception e) {
757: e.printStackTrace();
758: fail();
759: }
760: }
761: }
|