1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
|
23 | |
|
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
package com.jeantessier.classreader; |
34 | |
|
35 | |
import java.io.*; |
36 | |
import java.util.*; |
37 | |
|
38 | |
import junit.framework.*; |
39 | |
|
40 | 6 | public class TestDirectoryExplorer extends TestCase { |
41 | |
private File testFile; |
42 | |
private File otherFile; |
43 | |
private File missingFile; |
44 | |
private File testDir; |
45 | |
private File otherDir; |
46 | |
|
47 | |
protected void setUp() throws Exception { |
48 | 6 | super.setUp(); |
49 | |
|
50 | 6 | testFile = File.createTempFile(getName(), null); |
51 | |
|
52 | 6 | otherFile = File.createTempFile(getName(), null); |
53 | |
|
54 | 6 | missingFile = File.createTempFile(getName(), null); |
55 | 6 | missingFile.delete(); |
56 | |
|
57 | 6 | testDir = File.createTempFile(getName(), null); |
58 | 6 | testDir.delete(); |
59 | 6 | testDir.mkdir(); |
60 | 6 | File.createTempFile(getName(), null, testDir); |
61 | 6 | File.createTempFile(getName(), null, testDir); |
62 | |
|
63 | 6 | otherDir = File.createTempFile(getName(), null); |
64 | 6 | otherDir.delete(); |
65 | 6 | otherDir.mkdir(); |
66 | 6 | File.createTempFile(getName(), null, otherDir); |
67 | 6 | } |
68 | |
|
69 | |
protected void tearDown() throws Exception { |
70 | 6 | testFile.delete(); |
71 | |
|
72 | 6 | otherFile.delete(); |
73 | |
|
74 | 18 | for (File file : testDir.listFiles()) { |
75 | 12 | file.delete(); |
76 | |
} |
77 | 6 | testDir.delete(); |
78 | |
|
79 | 12 | for (File file : otherDir.listFiles()) { |
80 | 6 | file.delete(); |
81 | |
} |
82 | 6 | otherDir.delete(); |
83 | |
|
84 | 6 | super.tearDown(); |
85 | 6 | } |
86 | |
|
87 | |
public void testExploreFilename() throws IOException { |
88 | 1 | DirectoryExplorer explorer = new DirectoryExplorer(testFile.getPath()); |
89 | |
|
90 | 1 | List<File> list = new ArrayList<File>(explorer.getFiles()); |
91 | |
|
92 | 1 | assertEquals("size", 1, list.size()); |
93 | 1 | assertEquals(testFile, list.get(0)); |
94 | 1 | } |
95 | |
|
96 | |
public void testExploreOtherFilename() throws IOException { |
97 | 1 | DirectoryExplorer explorer = new DirectoryExplorer(otherFile.getPath()); |
98 | |
|
99 | 1 | List<File> list = new ArrayList<File>(explorer.getFiles()); |
100 | |
|
101 | 1 | assertEquals("size", 1, list.size()); |
102 | 1 | assertEquals(otherFile, list.get(0)); |
103 | 1 | } |
104 | |
|
105 | |
public void testExploreMissingFilename() throws IOException { |
106 | 1 | DirectoryExplorer explorer = new DirectoryExplorer(missingFile.getPath()); |
107 | |
|
108 | 1 | assertEquals("size", 0, explorer.getFiles().size()); |
109 | 1 | } |
110 | |
|
111 | |
public void testExploreDirectory() throws IOException { |
112 | 1 | DirectoryExplorer explorer = new DirectoryExplorer(testDir.getPath()); |
113 | |
|
114 | 1 | List<File> list = new ArrayList<File>(explorer.getFiles()); |
115 | |
|
116 | 1 | assertEquals("size", 3, list.size()); |
117 | 1 | assertEquals(testDir, list.get(0)); |
118 | 1 | } |
119 | |
|
120 | |
public void testExploreMultipleDirectories() throws IOException { |
121 | 1 | Collection<String> directories = new ArrayList<String>(); |
122 | 1 | directories.add(testDir.getPath()); |
123 | 1 | directories.add(otherDir.getPath()); |
124 | |
|
125 | 1 | DirectoryExplorer explorer = new DirectoryExplorer(directories); |
126 | |
|
127 | 1 | List<File> list = new ArrayList<File>(explorer.getFiles()); |
128 | |
|
129 | 1 | assertEquals("size", 5, list.size()); |
130 | 1 | assertEquals(testDir, list.get(0)); |
131 | 1 | } |
132 | |
|
133 | |
public void testExploreSingletonFile() throws IOException { |
134 | 1 | Collection<String> files = new ArrayList<String>(); |
135 | 1 | files.add(testFile.getPath()); |
136 | |
|
137 | 1 | DirectoryExplorer explorer = new DirectoryExplorer(files); |
138 | |
|
139 | 1 | List<File> list = new ArrayList<File>(explorer.getFiles()); |
140 | |
|
141 | 1 | assertEquals("size", 1, list.size()); |
142 | 1 | assertEquals(testFile, list.get(0)); |
143 | 1 | } |
144 | |
} |