001: /*
002: * Copyright (c) 2001-2007, Jean Tessier
003: * All rights reserved.
004: *
005: * Redistribution and use in source and binary forms, with or without
006: * modification, are permitted provided that the following conditions
007: * are met:
008: *
009: * * Redistributions of source code must retain the above copyright
010: * notice, this list of conditions and the following disclaimer.
011: *
012: * * Redistributions in binary form must reproduce the above copyright
013: * notice, this list of conditions and the following disclaimer in the
014: * documentation and/or other materials provided with the distribution.
015: *
016: * * Neither the name of Jean Tessier nor the names of his contributors
017: * may be used to endorse or promote products derived from this software
018: * without specific prior written permission.
019: *
020: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
021: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
022: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
023: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
024: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
025: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
026: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
027: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
028: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
029: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
030: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
031: */
032:
033: package com.jeantessier.classreader;
034:
035: import java.io.*;
036: import java.util.*;
037:
038: import junit.framework.*;
039:
040: public class TestDirectoryExplorer extends TestCase {
041: private File testFile;
042: private File otherFile;
043: private File missingFile;
044: private File testDir;
045: private File otherDir;
046:
047: protected void setUp() throws Exception {
048: super .setUp();
049:
050: testFile = File.createTempFile(getName(), null);
051:
052: otherFile = File.createTempFile(getName(), null);
053:
054: missingFile = File.createTempFile(getName(), null);
055: missingFile.delete();
056:
057: testDir = File.createTempFile(getName(), null);
058: testDir.delete();
059: testDir.mkdir();
060: File.createTempFile(getName(), null, testDir);
061: File.createTempFile(getName(), null, testDir);
062:
063: otherDir = File.createTempFile(getName(), null);
064: otherDir.delete();
065: otherDir.mkdir();
066: File.createTempFile(getName(), null, otherDir);
067: }
068:
069: protected void tearDown() throws Exception {
070: testFile.delete();
071:
072: otherFile.delete();
073:
074: for (File file : testDir.listFiles()) {
075: file.delete();
076: }
077: testDir.delete();
078:
079: for (File file : otherDir.listFiles()) {
080: file.delete();
081: }
082: otherDir.delete();
083:
084: super .tearDown();
085: }
086:
087: public void testExploreFilename() throws IOException {
088: DirectoryExplorer explorer = new DirectoryExplorer(testFile
089: .getPath());
090:
091: List<File> list = new ArrayList<File>(explorer.getFiles());
092:
093: assertEquals("size", 1, list.size());
094: assertEquals(testFile, list.get(0));
095: }
096:
097: public void testExploreOtherFilename() throws IOException {
098: DirectoryExplorer explorer = new DirectoryExplorer(otherFile
099: .getPath());
100:
101: List<File> list = new ArrayList<File>(explorer.getFiles());
102:
103: assertEquals("size", 1, list.size());
104: assertEquals(otherFile, list.get(0));
105: }
106:
107: public void testExploreMissingFilename() throws IOException {
108: DirectoryExplorer explorer = new DirectoryExplorer(missingFile
109: .getPath());
110:
111: assertEquals("size", 0, explorer.getFiles().size());
112: }
113:
114: public void testExploreDirectory() throws IOException {
115: DirectoryExplorer explorer = new DirectoryExplorer(testDir
116: .getPath());
117:
118: List<File> list = new ArrayList<File>(explorer.getFiles());
119:
120: assertEquals("size", 3, list.size());
121: assertEquals(testDir, list.get(0));
122: }
123:
124: public void testExploreMultipleDirectories() throws IOException {
125: Collection<String> directories = new ArrayList<String>();
126: directories.add(testDir.getPath());
127: directories.add(otherDir.getPath());
128:
129: DirectoryExplorer explorer = new DirectoryExplorer(directories);
130:
131: List<File> list = new ArrayList<File>(explorer.getFiles());
132:
133: assertEquals("size", 5, list.size());
134: assertEquals(testDir, list.get(0));
135: }
136:
137: public void testExploreSingletonFile() throws IOException {
138: Collection<String> files = new ArrayList<String>();
139: files.add(testFile.getPath());
140:
141: DirectoryExplorer explorer = new DirectoryExplorer(files);
142:
143: List<File> list = new ArrayList<File>(explorer.getFiles());
144:
145: assertEquals("size", 1, list.size());
146: assertEquals(testFile, list.get(0));
147: }
148: }
|