1 | /*
|
---|
2 | * Copyright 2001-2005 The Apache Software Foundation
|
---|
3 | *
|
---|
4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
---|
5 | * you may not use this file except in compliance with the License.
|
---|
6 | * You may obtain a copy of the License at
|
---|
7 | *
|
---|
8 | * http://www.apache.org/licenses/LICENSE-2.0
|
---|
9 | *
|
---|
10 | * Unless required by applicable law or agreed to in writing, software
|
---|
11 | * distributed under the License is distributed on an "AS IS" BASIS,
|
---|
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
---|
13 | * See the License for the specific language governing permissions and
|
---|
14 | * limitations under the License.
|
---|
15 | *
|
---|
16 | */
|
---|
17 |
|
---|
18 | package org.apache.tools.ant;
|
---|
19 |
|
---|
20 | import org.apache.tools.ant.taskdefs.condition.Os;
|
---|
21 | import org.apache.tools.ant.types.Resource;
|
---|
22 | import org.apache.tools.ant.util.FileUtils;
|
---|
23 | import org.apache.tools.ant.util.JavaEnvUtils;
|
---|
24 |
|
---|
25 | import junit.framework.TestCase;
|
---|
26 | import junit.framework.AssertionFailedError;
|
---|
27 | import java.io.File;
|
---|
28 | import java.io.IOException;
|
---|
29 | import java.util.Set;
|
---|
30 | import java.util.TreeSet;
|
---|
31 | import java.util.Iterator;
|
---|
32 |
|
---|
33 | /**
|
---|
34 | * JUnit 3 testcases for org.apache.tools.ant.DirectoryScanner
|
---|
35 | *
|
---|
36 | */
|
---|
37 | public class DirectoryScannerTest extends BuildFileTest {
|
---|
38 |
|
---|
39 | public DirectoryScannerTest(String name) {super(name);}
|
---|
40 |
|
---|
41 | // keep track of what operating systems are supported here.
|
---|
42 | private boolean supportsSymlinks = Os.isFamily("unix");
|
---|
43 |
|
---|
44 | public void setUp() {
|
---|
45 | configureProject("src/etc/testcases/core/directoryscanner.xml");
|
---|
46 | getProject().executeTarget("setup");
|
---|
47 | }
|
---|
48 |
|
---|
49 | public void tearDown() {
|
---|
50 | getProject().executeTarget("cleanup");
|
---|
51 | }
|
---|
52 |
|
---|
53 | public void test1() {
|
---|
54 | DirectoryScanner ds = new DirectoryScanner();
|
---|
55 | ds.setBasedir(new File(getProject().getBaseDir(), "tmp"));
|
---|
56 | ds.setIncludes(new String[] {"alpha"});
|
---|
57 | ds.scan();
|
---|
58 | compareFiles(ds, new String[] {} ,new String[] {"alpha"});
|
---|
59 | }
|
---|
60 |
|
---|
61 | public void test2() {
|
---|
62 | DirectoryScanner ds = new DirectoryScanner();
|
---|
63 | ds.setBasedir(new File(getProject().getBaseDir(), "tmp"));
|
---|
64 | ds.setIncludes(new String[] {"alpha/"});
|
---|
65 | ds.scan();
|
---|
66 | compareFiles(ds, new String[] {"alpha/beta/beta.xml",
|
---|
67 | "alpha/beta/gamma/gamma.xml"},
|
---|
68 | new String[] {"alpha", "alpha/beta", "alpha/beta/gamma"});
|
---|
69 | }
|
---|
70 |
|
---|
71 | public void test3() {
|
---|
72 | DirectoryScanner ds = new DirectoryScanner();
|
---|
73 | ds.setBasedir(new File(getProject().getBaseDir(), "tmp"));
|
---|
74 | ds.scan();
|
---|
75 | compareFiles(ds, new String[] {"alpha/beta/beta.xml",
|
---|
76 | "alpha/beta/gamma/gamma.xml"},
|
---|
77 | new String[] {"", "alpha", "alpha/beta",
|
---|
78 | "alpha/beta/gamma"});
|
---|
79 | }
|
---|
80 |
|
---|
81 | public void testFullPathMatchesCaseSensitive() {
|
---|
82 | DirectoryScanner ds = new DirectoryScanner();
|
---|
83 | ds.setBasedir(new File(getProject().getBaseDir(), "tmp"));
|
---|
84 | ds.setIncludes(new String[] {"alpha/beta/gamma/GAMMA.XML"});
|
---|
85 | ds.scan();
|
---|
86 | compareFiles(ds, new String[] {}, new String[] {});
|
---|
87 | }
|
---|
88 |
|
---|
89 | public void testFullPathMatchesCaseInsensitive() {
|
---|
90 | DirectoryScanner ds = new DirectoryScanner();
|
---|
91 | ds.setCaseSensitive(false);
|
---|
92 | ds.setBasedir(new File(getProject().getBaseDir(), "tmp"));
|
---|
93 | ds.setIncludes(new String[] {"alpha/beta/gamma/GAMMA.XML"});
|
---|
94 | ds.scan();
|
---|
95 | compareFiles(ds, new String[] {"alpha/beta/gamma/gamma.xml"},
|
---|
96 | new String[] {});
|
---|
97 | }
|
---|
98 |
|
---|
99 | public void test2ButCaseInsensitive() {
|
---|
100 | DirectoryScanner ds = new DirectoryScanner();
|
---|
101 | ds.setBasedir(new File(getProject().getBaseDir(), "tmp"));
|
---|
102 | ds.setIncludes(new String[] {"ALPHA/"});
|
---|
103 | ds.setCaseSensitive(false);
|
---|
104 | ds.scan();
|
---|
105 | compareFiles(ds, new String[] {"alpha/beta/beta.xml",
|
---|
106 | "alpha/beta/gamma/gamma.xml"},
|
---|
107 | new String[] {"alpha", "alpha/beta", "alpha/beta/gamma"});
|
---|
108 | }
|
---|
109 |
|
---|
110 | public void testAllowSymlinks() {
|
---|
111 | if (!supportsSymlinks) {
|
---|
112 | return;
|
---|
113 | }
|
---|
114 |
|
---|
115 | getProject().executeTarget("symlink-setup");
|
---|
116 | DirectoryScanner ds = new DirectoryScanner();
|
---|
117 | ds.setBasedir(new File(getProject().getBaseDir(), "tmp"));
|
---|
118 | ds.setIncludes(new String[] {"alpha/beta/gamma/"});
|
---|
119 | ds.scan();
|
---|
120 | compareFiles(ds, new String[] {"alpha/beta/gamma/gamma.xml"},
|
---|
121 | new String[] {"alpha/beta/gamma"});
|
---|
122 | }
|
---|
123 |
|
---|
124 | public void testProhibitSymlinks() {
|
---|
125 | if (!supportsSymlinks) {
|
---|
126 | return;
|
---|
127 | }
|
---|
128 |
|
---|
129 | getProject().executeTarget("symlink-setup");
|
---|
130 | DirectoryScanner ds = new DirectoryScanner();
|
---|
131 | ds.setBasedir(new File(getProject().getBaseDir(), "tmp"));
|
---|
132 | ds.setIncludes(new String[] {"alpha/beta/gamma/"});
|
---|
133 | ds.setFollowSymlinks(false);
|
---|
134 | ds.scan();
|
---|
135 | compareFiles(ds, new String[] {}, new String[] {});
|
---|
136 | }
|
---|
137 |
|
---|
138 | // father and child pattern test
|
---|
139 | public void testOrderOfIncludePatternsIrrelevant() {
|
---|
140 | String [] expectedFiles = {"alpha/beta/beta.xml",
|
---|
141 | "alpha/beta/gamma/gamma.xml"};
|
---|
142 | String [] expectedDirectories = {"alpha/beta", "alpha/beta/gamma" };
|
---|
143 | DirectoryScanner ds = new DirectoryScanner();
|
---|
144 | ds.setBasedir(new File(getProject().getBaseDir(), "tmp"));
|
---|
145 | ds.setIncludes(new String[] {"alpha/be?a/**", "alpha/beta/gamma/"});
|
---|
146 | ds.scan();
|
---|
147 | compareFiles(ds, expectedFiles, expectedDirectories);
|
---|
148 | // redo the test, but the 2 include patterns are inverted
|
---|
149 | ds = new DirectoryScanner();
|
---|
150 | ds.setBasedir(new File(getProject().getBaseDir(), "tmp"));
|
---|
151 | ds.setIncludes(new String[] {"alpha/beta/gamma/", "alpha/be?a/**"});
|
---|
152 | ds.scan();
|
---|
153 | compareFiles(ds, expectedFiles, expectedDirectories);
|
---|
154 | }
|
---|
155 |
|
---|
156 | public void testPatternsDifferInCaseScanningSensitive() {
|
---|
157 | DirectoryScanner ds = new DirectoryScanner();
|
---|
158 | ds.setBasedir(new File(getProject().getBaseDir(), "tmp"));
|
---|
159 | ds.setIncludes(new String[] {"alpha/", "ALPHA/"});
|
---|
160 | ds.scan();
|
---|
161 | compareFiles(ds, new String[] {"alpha/beta/beta.xml",
|
---|
162 | "alpha/beta/gamma/gamma.xml"},
|
---|
163 | new String[] {"alpha", "alpha/beta", "alpha/beta/gamma"});
|
---|
164 | }
|
---|
165 |
|
---|
166 | public void testPatternsDifferInCaseScanningInsensitive() {
|
---|
167 | DirectoryScanner ds = new DirectoryScanner();
|
---|
168 | ds.setBasedir(new File(getProject().getBaseDir(), "tmp"));
|
---|
169 | ds.setIncludes(new String[] {"alpha/", "ALPHA/"});
|
---|
170 | ds.setCaseSensitive(false);
|
---|
171 | ds.scan();
|
---|
172 | compareFiles(ds, new String[] {"alpha/beta/beta.xml",
|
---|
173 | "alpha/beta/gamma/gamma.xml"},
|
---|
174 | new String[] {"alpha", "alpha/beta", "alpha/beta/gamma"});
|
---|
175 | }
|
---|
176 |
|
---|
177 | public void testFullpathDiffersInCaseScanningSensitive() {
|
---|
178 | DirectoryScanner ds = new DirectoryScanner();
|
---|
179 | ds.setBasedir(new File(getProject().getBaseDir(), "tmp"));
|
---|
180 | ds.setIncludes(new String[] {
|
---|
181 | "alpha/beta/gamma/gamma.xml",
|
---|
182 | "alpha/beta/gamma/GAMMA.XML"
|
---|
183 | });
|
---|
184 | ds.scan();
|
---|
185 | compareFiles(ds, new String[] {"alpha/beta/gamma/gamma.xml"},
|
---|
186 | new String[] {});
|
---|
187 | }
|
---|
188 |
|
---|
189 | public void testFullpathDiffersInCaseScanningInsensitive() {
|
---|
190 | DirectoryScanner ds = new DirectoryScanner();
|
---|
191 | ds.setBasedir(new File(getProject().getBaseDir(), "tmp"));
|
---|
192 | ds.setIncludes(new String[] {
|
---|
193 | "alpha/beta/gamma/gamma.xml",
|
---|
194 | "alpha/beta/gamma/GAMMA.XML"
|
---|
195 | });
|
---|
196 | ds.setCaseSensitive(false);
|
---|
197 | ds.scan();
|
---|
198 | compareFiles(ds, new String[] {"alpha/beta/gamma/gamma.xml"},
|
---|
199 | new String[] {});
|
---|
200 | }
|
---|
201 |
|
---|
202 | public void testParentDiffersInCaseScanningSensitive() {
|
---|
203 | DirectoryScanner ds = new DirectoryScanner();
|
---|
204 | ds.setBasedir(new File(getProject().getBaseDir(), "tmp"));
|
---|
205 | ds.setIncludes(new String[] {"alpha/", "ALPHA/beta/"});
|
---|
206 | ds.scan();
|
---|
207 | compareFiles(ds, new String[] {"alpha/beta/beta.xml",
|
---|
208 | "alpha/beta/gamma/gamma.xml"},
|
---|
209 | new String[] {"alpha", "alpha/beta", "alpha/beta/gamma"});
|
---|
210 | }
|
---|
211 |
|
---|
212 | public void testParentDiffersInCaseScanningInsensitive() {
|
---|
213 | DirectoryScanner ds = new DirectoryScanner();
|
---|
214 | ds.setBasedir(new File(getProject().getBaseDir(), "tmp"));
|
---|
215 | ds.setIncludes(new String[] {"alpha/", "ALPHA/beta/"});
|
---|
216 | ds.setCaseSensitive(false);
|
---|
217 | ds.scan();
|
---|
218 | compareFiles(ds, new String[] {"alpha/beta/beta.xml",
|
---|
219 | "alpha/beta/gamma/gamma.xml"},
|
---|
220 | new String[] {"alpha", "alpha/beta", "alpha/beta/gamma"});
|
---|
221 | }
|
---|
222 |
|
---|
223 | /**
|
---|
224 | * Test case for setFollowLinks() and associated functionality.
|
---|
225 | * Only supports test on Linux at the moment because Java has
|
---|
226 | * no real notion of symlinks built in, so an os-specfic call
|
---|
227 | * to Runtime.exec() must be made to create a link to test against.
|
---|
228 | */
|
---|
229 | public void testSetFollowLinks() throws IOException {
|
---|
230 | if (supportsSymlinks) {
|
---|
231 | File linkFile = new File(System.getProperty("root"), "src/main/org/apache/tools/ThisIsALink");
|
---|
232 | System.err.println("link exists pre-test? " + linkFile.exists());
|
---|
233 |
|
---|
234 | try {
|
---|
235 | // add conditions and more commands as soon as the need arises
|
---|
236 | String[] command = new String[] {
|
---|
237 | "ln", "-s", "ant", linkFile.getAbsolutePath()
|
---|
238 | };
|
---|
239 | try {
|
---|
240 | Runtime.getRuntime().exec(command);
|
---|
241 | // give ourselves some time for the system call
|
---|
242 | // to execute... tweak if you have a really over
|
---|
243 | // loaded system.
|
---|
244 | Thread.sleep(1000);
|
---|
245 | } catch (IOException ioe) {
|
---|
246 | fail("IOException making link "+ioe);
|
---|
247 | } catch (InterruptedException ie) {
|
---|
248 | }
|
---|
249 |
|
---|
250 | File dir = new File(System.getProperty("root"), "src/main/org/apache/tools");
|
---|
251 | System.err.println("link exists after exec? " + linkFile.exists());
|
---|
252 | System.err.println("Ant knows it is a link? " + FileUtils.getFileUtils().isSymbolicLink(dir, "ThisIsALink"));
|
---|
253 |
|
---|
254 | DirectoryScanner ds = new DirectoryScanner();
|
---|
255 |
|
---|
256 | // followLinks should be true by default, but if this ever
|
---|
257 | // changes we will need this line.
|
---|
258 | ds.setFollowSymlinks(true);
|
---|
259 |
|
---|
260 | ds.setBasedir(dir);
|
---|
261 | ds.setExcludes(new String[] {"ant/**"});
|
---|
262 | ds.scan();
|
---|
263 |
|
---|
264 | boolean haveZipPackage = false;
|
---|
265 | boolean haveTaskdefsPackage = false;
|
---|
266 |
|
---|
267 | String[] included = ds.getIncludedDirectories();
|
---|
268 | for (int i=0; i<included.length; i++) {
|
---|
269 | if (included[i].equals("zip")) {
|
---|
270 | haveZipPackage = true;
|
---|
271 | } else if (included[i].equals("ThisIsALink"
|
---|
272 | + File.separator
|
---|
273 | + "taskdefs")) {
|
---|
274 | haveTaskdefsPackage = true;
|
---|
275 | }
|
---|
276 | }
|
---|
277 |
|
---|
278 | // if we followed the symlink we just made we should
|
---|
279 | // bypass the excludes.
|
---|
280 |
|
---|
281 | assertTrue("(1) zip package included", haveZipPackage);
|
---|
282 | assertTrue("(1) taskdefs package included",
|
---|
283 | haveTaskdefsPackage);
|
---|
284 |
|
---|
285 |
|
---|
286 | ds = new DirectoryScanner();
|
---|
287 | ds.setFollowSymlinks(false);
|
---|
288 |
|
---|
289 | ds.setBasedir(dir);
|
---|
290 | ds.setExcludes(new String[] {"ant/**"});
|
---|
291 | ds.scan();
|
---|
292 |
|
---|
293 | haveZipPackage = false;
|
---|
294 | haveTaskdefsPackage = false;
|
---|
295 | included = ds.getIncludedDirectories();
|
---|
296 | for (int i=0; i<included.length; i++) {
|
---|
297 | if (included[i].equals("zip")) {
|
---|
298 | haveZipPackage = true;
|
---|
299 | } else if (included[i].equals("ThisIsALink"
|
---|
300 | + File.separator
|
---|
301 | + "taskdefs")) {
|
---|
302 | haveTaskdefsPackage = true;
|
---|
303 | }
|
---|
304 | }
|
---|
305 | assertTrue("(2) zip package included", haveZipPackage);
|
---|
306 | assertTrue("(2) taskdefs package not included",
|
---|
307 | !haveTaskdefsPackage);
|
---|
308 |
|
---|
309 | } finally {
|
---|
310 | System.err.println("link exists pre-delete? " + linkFile.exists());
|
---|
311 | if (!linkFile.delete()) {
|
---|
312 | throw new RuntimeException("Failed to delete " + linkFile);
|
---|
313 | }
|
---|
314 | System.err.println("link exists post-delete? " + linkFile.exists());
|
---|
315 | }
|
---|
316 | }
|
---|
317 | }
|
---|
318 |
|
---|
319 | public void testExcludeOneFile() {
|
---|
320 | DirectoryScanner ds = new DirectoryScanner();
|
---|
321 | ds.setBasedir(new File(getProject().getBaseDir(), "tmp"));
|
---|
322 | ds.setIncludes(new String[] {
|
---|
323 | "**/*.xml"
|
---|
324 | });
|
---|
325 | ds.setExcludes(new String[] {
|
---|
326 | "alpha/beta/b*xml"
|
---|
327 | });
|
---|
328 | ds.scan();
|
---|
329 | compareFiles(ds, new String[] {"alpha/beta/gamma/gamma.xml"},
|
---|
330 | new String[] {});
|
---|
331 | }
|
---|
332 |
|
---|
333 | public void testExcludeHasPrecedence() {
|
---|
334 | DirectoryScanner ds = new DirectoryScanner();
|
---|
335 | ds.setBasedir(new File(getProject().getBaseDir(), "tmp"));
|
---|
336 | ds.setIncludes(new String[] {
|
---|
337 | "alpha/**"
|
---|
338 | });
|
---|
339 | ds.setExcludes(new String[] {
|
---|
340 | "alpha/**"
|
---|
341 | });
|
---|
342 | ds.scan();
|
---|
343 | compareFiles(ds, new String[] {},
|
---|
344 | new String[] {});
|
---|
345 |
|
---|
346 | }
|
---|
347 |
|
---|
348 | public void testAlternateIncludeExclude() {
|
---|
349 | DirectoryScanner ds = new DirectoryScanner();
|
---|
350 | ds.setBasedir(new File(getProject().getBaseDir(), "tmp"));
|
---|
351 | ds.setIncludes(new String[] {
|
---|
352 | "alpha/**",
|
---|
353 | "alpha/beta/gamma/**"
|
---|
354 | });
|
---|
355 | ds.setExcludes(new String[] {
|
---|
356 | "alpha/beta/**"
|
---|
357 | });
|
---|
358 | ds.scan();
|
---|
359 | compareFiles(ds, new String[] {},
|
---|
360 | new String[] {"alpha"});
|
---|
361 |
|
---|
362 | }
|
---|
363 |
|
---|
364 | public void testAlternateExcludeInclude() {
|
---|
365 | DirectoryScanner ds = new DirectoryScanner();
|
---|
366 | ds.setBasedir(new File(getProject().getBaseDir(), "tmp"));
|
---|
367 | ds.setExcludes(new String[] {
|
---|
368 | "alpha/**",
|
---|
369 | "alpha/beta/gamma/**"
|
---|
370 | });
|
---|
371 | ds.setIncludes(new String[] {
|
---|
372 | "alpha/beta/**"
|
---|
373 | });
|
---|
374 | ds.scan();
|
---|
375 | compareFiles(ds, new String[] {},
|
---|
376 | new String[] {});
|
---|
377 |
|
---|
378 | }
|
---|
379 |
|
---|
380 | /**
|
---|
381 | * Test inspired by Bug#1415.
|
---|
382 | */
|
---|
383 | public void testChildrenOfExcludedDirectory() {
|
---|
384 | getProject().executeTarget("children-of-excluded-dir-setup");
|
---|
385 | DirectoryScanner ds = new DirectoryScanner();
|
---|
386 | ds.setBasedir(new File(getProject().getBaseDir(), "tmp"));
|
---|
387 | ds.setExcludes(new String[] {"alpha/**"});
|
---|
388 | ds.setFollowSymlinks(false);
|
---|
389 | ds.scan();
|
---|
390 | compareFiles(ds, new String[] {"delta/delta.xml"},
|
---|
391 | new String[] {"", "delta"});
|
---|
392 |
|
---|
393 | ds = new DirectoryScanner();
|
---|
394 | ds.setBasedir(new File(getProject().getBaseDir(), "tmp"));
|
---|
395 | ds.setExcludes(new String[] {"alpha"});
|
---|
396 | ds.setFollowSymlinks(false);
|
---|
397 | ds.scan();
|
---|
398 | compareFiles(ds, new String[] {"alpha/beta/beta.xml",
|
---|
399 | "alpha/beta/gamma/gamma.xml",
|
---|
400 | "delta/delta.xml"},
|
---|
401 | new String[] {"", "alpha/beta", "alpha/beta/gamma", "delta"});
|
---|
402 |
|
---|
403 | }
|
---|
404 |
|
---|
405 | public void testIsExcludedDirectoryScanned() {
|
---|
406 | getProject().executeTarget("children-of-excluded-dir-setup");
|
---|
407 | DirectoryScanner ds = new DirectoryScanner();
|
---|
408 | ds.setBasedir(new File(getProject().getBaseDir(), "tmp"));
|
---|
409 | ds.setExcludes(new String[] {"**/gamma/**"});
|
---|
410 | ds.setFollowSymlinks(false);
|
---|
411 | ds.scan();
|
---|
412 | Set set = ds.getScannedDirs();
|
---|
413 | assertFalse("empty set", set.isEmpty());
|
---|
414 | String s = "alpha/beta/gamma/".replace('/', File.separatorChar);
|
---|
415 | assertFalse("scanned " + s, set.contains(s));
|
---|
416 | }
|
---|
417 |
|
---|
418 | private void compareFiles(DirectoryScanner ds, String[] expectedFiles,
|
---|
419 | String[] expectedDirectories) {
|
---|
420 | String includedFiles[] = ds.getIncludedFiles();
|
---|
421 | String includedDirectories[] = ds.getIncludedDirectories();
|
---|
422 | assertEquals("file present: ", expectedFiles.length,
|
---|
423 | includedFiles.length);
|
---|
424 | assertEquals("directories present: ", expectedDirectories.length,
|
---|
425 | includedDirectories.length);
|
---|
426 |
|
---|
427 | TreeSet files = new TreeSet();
|
---|
428 | for (int counter=0; counter < includedFiles.length; counter++) {
|
---|
429 | files.add(includedFiles[counter].replace(File.separatorChar, '/'));
|
---|
430 | }
|
---|
431 | TreeSet directories = new TreeSet();
|
---|
432 | for (int counter=0; counter < includedDirectories.length; counter++) {
|
---|
433 | directories.add(includedDirectories[counter]
|
---|
434 | .replace(File.separatorChar, '/'));
|
---|
435 | }
|
---|
436 |
|
---|
437 | String currentfile;
|
---|
438 | Iterator i = files.iterator();
|
---|
439 | int counter = 0;
|
---|
440 | while (i.hasNext()) {
|
---|
441 | currentfile = (String) i.next();
|
---|
442 | assertEquals(expectedFiles[counter], currentfile);
|
---|
443 | counter++;
|
---|
444 | }
|
---|
445 | String currentdirectory;
|
---|
446 | Iterator dirit = directories.iterator();
|
---|
447 | counter = 0;
|
---|
448 | while (dirit.hasNext()) {
|
---|
449 | currentdirectory = (String) dirit.next();
|
---|
450 | assertEquals(expectedDirectories[counter], currentdirectory);
|
---|
451 | counter++;
|
---|
452 | }
|
---|
453 | }
|
---|
454 |
|
---|
455 | }
|
---|