1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.finder;
18
19 import junit.framework.Test;
20 import junit.framework.TestCase;
21 import junit.framework.TestSuite;
22 import junit.textui.TestRunner;
23
24 import java.io.IOException;
25 import java.io.File;
26
27 import java.util.HashMap;
28
29 public class FileFinderTest extends TestCase {
30
31 private HashMap options;
32 private FileFinder finder;
33 private String dirStr = "src" + File.separator + "test" + File.separator + "find-data" + File.separator;
34 private File dir = new File(dirStr);
35
36 public FileFinderTest(String name) {
37 super(name);
38 }
39
40 public void setUp() {
41 finder = new FileFinder();
42 options = new HashMap();
43 options.put(Finder.NOT+Finder.PATH, "*.svn*");
44
45
46 }
47
48
49
50
51
52 public void testFindName() {
53 options.put(Finder.NAME, "file");
54 File[] files = finder.find(new File(dir, "name"), options);
55 assertEquals(1, files.length);
56 }
57
58 public void testFindIName() {
59 options.put(Finder.INAME, "FiLe");
60 File[] files = finder.find(new File(dir, "name"), options);
61 assertEquals(1, files.length);
62 }
63
64 public void testFindPath() {
65 options.put(Finder.PATH, dirStr+"path" + File.separator + "dir" + File.separator + "file");
66 File[] files = finder.find(new File(dir, "path"), options);
67 assertEquals(1, files.length);
68 }
69
70 public void testFindIPath() {
71 options.put(Finder.IPATH, dirStr+"PAth" + File.separator + "dIR" + File.separator + "fILe");
72 File[] files = finder.find(new File(dir, "path"), options);
73 assertEquals(1, files.length);
74 }
75
76 public void testFindNotPath() {
77 options.put(Finder.NOT+Finder.PATH, dirStr+"*");
78 File[] files = finder.find(new File(dir, "path"), options);
79 assertEquals(0, files.length);
80 }
81
82 public void testFindRegex() {
83 options.put(Finder.REGEX, escapePath(dirStr+"regex" + File.separator + "f.*"));
84 File[] files = finder.find(new File(dir, "regex"), options);
85 assertEquals(3, files.length);
86 }
87
88 public void testFindIRegex() {
89 options.put(Finder.IREGEX, escapePath(dirStr+"REgeX" + File.separator + "F.*"));
90 File[] files = finder.find(new File(dir, "regex"), options);
91 assertEquals(3, files.length);
92 }
93
94 public void testFindEmpty() {
95 options.put(Finder.EMPTY, "true");
96 File[] files = finder.find(new File(dir, "empty"), options);
97 assertEquals(1, files.length);
98 }
99
100
101
102
103
104
105 public void testFindSize() {
106 options.put(Finder.FILE, null);
107 options.put(Finder.SIZE, "4k");
108 File[] files = finder.find(new File(dir, "size"), options);
109 assertEquals(1, files.length);
110 }
111
112
113
114 public void testFindTypeFile() {
115 options.put(Finder.DIRECTORY, "f");
116 File[] files = finder.find(new File(dir, "type"), options);
117 assertEquals(2, files.length);
118 }
119
120 public void testFindTypeD() {
121 options.put(Finder.FILE, "d");
122 File[] files = finder.find(new File(dir, "type"), options);
123 assertEquals(2, files.length);
124 }
125
126 public void testCanWriteTrue() {
127 options.put(Finder.CAN_WRITE, "true");
128 File[] files = finder.find(new File(dir, "can_write"), options);
129 assertEquals(2, files.length);
130 }
131
132 public void testCanReadTrue() {
133 options.put(Finder.CAN_READ, "true");
134 File[] files = finder.find(new File(dir, "can_read"), options);
135 assertEquals(2, files.length);
136 }
137
138 public void testCanWriteFalse() {
139 options.put(Finder.CANNOT_WRITE, "false");
140 File[] files = finder.find(new File(dir, "can_write"), options);
141 assertEquals(0, files.length);
142 }
143
144 public void testCanReadFalse() {
145 options.put(Finder.CANNOT_READ, "false");
146 File[] files = finder.find(new File(dir, "can_read"), options);
147 assertEquals(0, files.length);
148 }
149
150 private static String escapePath(String text) {
151 String repl = "\\";
152 String with = "\\\\";
153
154 StringBuffer buf = new StringBuffer(text.length());
155 int start = 0, end = 0;
156 while ((end = text.indexOf(repl, start)) != -1) {
157 buf.append(text.substring(start, end)).append(with);
158 start = end + repl.length();
159 }
160 buf.append(text.substring(start));
161 return buf.toString();
162 }
163
164 }
165
166 class DebugListener implements FindListener {
167 public void fileFound(FindEvent fe) {
168 System.out.println("EVENT: "+fe);
169 }
170 public void directoryStarted(FindEvent fe) {
171 System.out.println("EVENT: "+fe);
172 }
173 public void directoryFinished(FindEvent fe) {
174 System.out.println("EVENT: "+fe);
175 }
176 }