001: // Copyright (C) 2003,2004,2005 by Object Mentor, Inc. All rights reserved.
002: // Released under the terms of the GNU General Public License version 2 or later.
003: package fitnesse.util;
004:
005: import java.io.File;
006:
007: import junit.framework.TestCase;
008:
009: public class FileUtilTest extends TestCase {
010: public void testCreateDir() throws Exception {
011: File dir = FileUtil.createDir("temp");
012: assertTrue(dir.exists());
013: assertTrue(dir.isDirectory());
014: FileUtil.deleteFileSystemDirectory(dir);
015: }
016:
017: public void testGetDirectoryListingEmpty() throws Exception {
018: File dir = FileUtil.createDir("temp");
019: assertEquals(0, FileUtil.getDirectoryListing(dir).length);
020: FileUtil.deleteFileSystemDirectory(dir);
021: }
022:
023: public void testOrganizeFilesOneFile() throws Exception {
024: File dir = FileUtil.createDir("temp");
025: File file = createFileInDir(dir, "file.txt");
026: assertEquals(1, FileUtil.getDirectoryListing(dir).length);
027: assertEquals(file, FileUtil.getDirectoryListing(dir)[0]);
028: FileUtil.deleteFileSystemDirectory(dir);
029: }
030:
031: public void testOrganizeFilesFiveFiles() throws Exception {
032: File dir = FileUtil.createDir("temp");
033: File file3 = createFileInDir(dir, "dFile.txt");
034: File file1 = createFileInDir(dir, "bFile.txt");
035: File file4 = createFileInDir(dir, "eFile.txt");
036: File file0 = createFileInDir(dir, "aFile.txt");
037: File file2 = createFileInDir(dir, "cFile.txt");
038: assertEquals(5, FileUtil.getDirectoryListing(dir).length);
039: assertEquals(file0, FileUtil.getDirectoryListing(dir)[0]);
040: assertEquals(file1, FileUtil.getDirectoryListing(dir)[1]);
041: assertEquals(file2, FileUtil.getDirectoryListing(dir)[2]);
042: assertEquals(file3, FileUtil.getDirectoryListing(dir)[3]);
043: assertEquals(file4, FileUtil.getDirectoryListing(dir)[4]);
044: FileUtil.deleteFileSystemDirectory(dir);
045: }
046:
047: public void testOrganizeFilesOneSubDir() throws Exception {
048: File dir = FileUtil.createDir("temp");
049: File subDir = createSubDir(dir, "subDir");
050: assertEquals(1, FileUtil.getDirectoryListing(dir).length);
051: assertEquals(subDir, FileUtil.getDirectoryListing(dir)[0]);
052: FileUtil.deleteFileSystemDirectory(dir);
053: }
054:
055: public void testOrganizeFilesFiveSubDirs() throws Exception {
056: File dir = FileUtil.createDir("temp");
057: File dir3 = createSubDir(dir, "dDir");
058: File dir1 = createSubDir(dir, "bDir");
059: File dir4 = createSubDir(dir, "eDir");
060: File dir0 = createSubDir(dir, "aDir");
061: File dir2 = createSubDir(dir, "cDir");
062: assertEquals(5, FileUtil.getDirectoryListing(dir).length);
063: assertEquals(dir0, FileUtil.getDirectoryListing(dir)[0]);
064: assertEquals(dir1, FileUtil.getDirectoryListing(dir)[1]);
065: assertEquals(dir2, FileUtil.getDirectoryListing(dir)[2]);
066: assertEquals(dir3, FileUtil.getDirectoryListing(dir)[3]);
067: assertEquals(dir4, FileUtil.getDirectoryListing(dir)[4]);
068: FileUtil.deleteFileSystemDirectory(dir);
069: }
070:
071: public void testOrganizeFilesMixOfFilesAndDirs() {
072: File dir = FileUtil.createDir("temp");
073: File dir3 = createSubDir(dir, "dDir");
074: File file3 = createFileInDir(dir, "dFile.txt");
075: File file0 = createFileInDir(dir, "aFile.txt");
076: File dir1 = createSubDir(dir, "bDir");
077: File file4 = createFileInDir(dir, "eFile.txt");
078: File dir4 = createSubDir(dir, "eDir");
079: File dir0 = createSubDir(dir, "aDir");
080: File file1 = createFileInDir(dir, "bFile.txt");
081: File dir2 = createSubDir(dir, "cDir");
082: File file2 = createFileInDir(dir, "cFile.txt");
083: assertEquals(10, FileUtil.getDirectoryListing(dir).length);
084: assertEquals(dir0, FileUtil.getDirectoryListing(dir)[0]);
085: assertEquals(dir1, FileUtil.getDirectoryListing(dir)[1]);
086: assertEquals(dir2, FileUtil.getDirectoryListing(dir)[2]);
087: assertEquals(dir3, FileUtil.getDirectoryListing(dir)[3]);
088: assertEquals(dir4, FileUtil.getDirectoryListing(dir)[4]);
089: assertEquals(file0, FileUtil.getDirectoryListing(dir)[5]);
090: assertEquals(file1, FileUtil.getDirectoryListing(dir)[6]);
091: assertEquals(file2, FileUtil.getDirectoryListing(dir)[7]);
092: assertEquals(file3, FileUtil.getDirectoryListing(dir)[8]);
093: assertEquals(file4, FileUtil.getDirectoryListing(dir)[9]);
094: FileUtil.deleteFileSystemDirectory(dir);
095: }
096:
097: private File createFileInDir(File dir, String fileName) {
098: return FileUtil.createFile(FileUtil.buildPath(new String[] {
099: dir.getPath(), fileName }), "");
100: }
101:
102: private File createSubDir(File dir, String subDirName) {
103: return FileUtil.createDir(FileUtil.buildPath(new String[] {
104: dir.getPath(), subDirName }));
105: }
106:
107: public void testBuildPathEmpty() throws Exception {
108: assertEquals("", FileUtil.buildPath(new String[] {}));
109: }
110:
111: public void testBuildPathOneElement() throws Exception {
112: assertEquals("a", FileUtil.buildPath(new String[] { "a" }));
113: }
114:
115: public void testBuildPathThreeElements() throws Exception {
116: String separator = System.getProperty("file.separator");
117: assertEquals("a" + separator + "b" + separator + "c", FileUtil
118: .buildPath(new String[] { "a", "b", "c" }));
119: }
120:
121: }
|