KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > DirectoryScannerTest


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 JavaDoc;
28 import java.io.IOException JavaDoc;
29 import java.util.Set JavaDoc;
30 import java.util.TreeSet JavaDoc;
31 import java.util.Iterator JavaDoc;
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 JavaDoc 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 JavaDoc(getProject().getBaseDir(), "tmp"));
56         ds.setIncludes(new String JavaDoc[] {"alpha"});
57         ds.scan();
58         compareFiles(ds, new String JavaDoc[] {} ,new String JavaDoc[] {"alpha"});
59     }
60
61     public void test2() {
62         DirectoryScanner ds = new DirectoryScanner();
63         ds.setBasedir(new File JavaDoc(getProject().getBaseDir(), "tmp"));
64         ds.setIncludes(new String JavaDoc[] {"alpha/"});
65         ds.scan();
66         compareFiles(ds, new String JavaDoc[] {"alpha/beta/beta.xml",
67                                        "alpha/beta/gamma/gamma.xml"},
68                      new String JavaDoc[] {"alpha", "alpha/beta", "alpha/beta/gamma"});
69     }
70
71     public void test3() {
72         DirectoryScanner ds = new DirectoryScanner();
73         ds.setBasedir(new File JavaDoc(getProject().getBaseDir(), "tmp"));
74         ds.scan();
75         compareFiles(ds, new String JavaDoc[] {"alpha/beta/beta.xml",
76                                        "alpha/beta/gamma/gamma.xml"},
77                      new String JavaDoc[] {"", "alpha", "alpha/beta",
78                                    "alpha/beta/gamma"});
79     }
80
81     public void testFullPathMatchesCaseSensitive() {
82         DirectoryScanner ds = new DirectoryScanner();
83         ds.setBasedir(new File JavaDoc(getProject().getBaseDir(), "tmp"));
84         ds.setIncludes(new String JavaDoc[] {"alpha/beta/gamma/GAMMA.XML"});
85         ds.scan();
86         compareFiles(ds, new String JavaDoc[] {}, new String JavaDoc[] {});
87     }
88
89     public void testFullPathMatchesCaseInsensitive() {
90         DirectoryScanner ds = new DirectoryScanner();
91         ds.setCaseSensitive(false);
92         ds.setBasedir(new File JavaDoc(getProject().getBaseDir(), "tmp"));
93         ds.setIncludes(new String JavaDoc[] {"alpha/beta/gamma/GAMMA.XML"});
94         ds.scan();
95         compareFiles(ds, new String JavaDoc[] {"alpha/beta/gamma/gamma.xml"},
96             new String JavaDoc[] {});
97     }
98
99     public void test2ButCaseInsensitive() {
100         DirectoryScanner ds = new DirectoryScanner();
101         ds.setBasedir(new File JavaDoc(getProject().getBaseDir(), "tmp"));
102         ds.setIncludes(new String JavaDoc[] {"ALPHA/"});
103         ds.setCaseSensitive(false);
104         ds.scan();
105         compareFiles(ds, new String JavaDoc[] {"alpha/beta/beta.xml",
106                                        "alpha/beta/gamma/gamma.xml"},
107                      new String JavaDoc[] {"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 JavaDoc(getProject().getBaseDir(), "tmp"));
118         ds.setIncludes(new String JavaDoc[] {"alpha/beta/gamma/"});
119         ds.scan();
120         compareFiles(ds, new String JavaDoc[] {"alpha/beta/gamma/gamma.xml"},
121                      new String JavaDoc[] {"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 JavaDoc(getProject().getBaseDir(), "tmp"));
132         ds.setIncludes(new String JavaDoc[] {"alpha/beta/gamma/"});
133         ds.setFollowSymlinks(false);
134         ds.scan();
135         compareFiles(ds, new String JavaDoc[] {}, new String JavaDoc[] {});
136     }
137
138     // father and child pattern test
139
public void testOrderOfIncludePatternsIrrelevant() {
140         String JavaDoc [] expectedFiles = {"alpha/beta/beta.xml",
141                                    "alpha/beta/gamma/gamma.xml"};
142         String JavaDoc [] expectedDirectories = {"alpha/beta", "alpha/beta/gamma" };
143         DirectoryScanner ds = new DirectoryScanner();
144         ds.setBasedir(new File JavaDoc(getProject().getBaseDir(), "tmp"));
145         ds.setIncludes(new String JavaDoc[] {"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 JavaDoc(getProject().getBaseDir(), "tmp"));
151         ds.setIncludes(new String JavaDoc[] {"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 JavaDoc(getProject().getBaseDir(), "tmp"));
159         ds.setIncludes(new String JavaDoc[] {"alpha/", "ALPHA/"});
160         ds.scan();
161         compareFiles(ds, new String JavaDoc[] {"alpha/beta/beta.xml",
162                                        "alpha/beta/gamma/gamma.xml"},
163                      new String JavaDoc[] {"alpha", "alpha/beta", "alpha/beta/gamma"});
164     }
165
166     public void testPatternsDifferInCaseScanningInsensitive() {
167         DirectoryScanner ds = new DirectoryScanner();
168         ds.setBasedir(new File JavaDoc(getProject().getBaseDir(), "tmp"));
169         ds.setIncludes(new String JavaDoc[] {"alpha/", "ALPHA/"});
170         ds.setCaseSensitive(false);
171         ds.scan();
172         compareFiles(ds, new String JavaDoc[] {"alpha/beta/beta.xml",
173                                        "alpha/beta/gamma/gamma.xml"},
174                      new String JavaDoc[] {"alpha", "alpha/beta", "alpha/beta/gamma"});
175     }
176
177     public void testFullpathDiffersInCaseScanningSensitive() {
178         DirectoryScanner ds = new DirectoryScanner();
179         ds.setBasedir(new File JavaDoc(getProject().getBaseDir(), "tmp"));
180         ds.setIncludes(new String JavaDoc[] {
181             "alpha/beta/gamma/gamma.xml",
182             "alpha/beta/gamma/GAMMA.XML"
183         });
184         ds.scan();
185         compareFiles(ds, new String JavaDoc[] {"alpha/beta/gamma/gamma.xml"},
186                      new String JavaDoc[] {});
187     }
188
189     public void testFullpathDiffersInCaseScanningInsensitive() {
190         DirectoryScanner ds = new DirectoryScanner();
191         ds.setBasedir(new File JavaDoc(getProject().getBaseDir(), "tmp"));
192         ds.setIncludes(new String JavaDoc[] {
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 JavaDoc[] {"alpha/beta/gamma/gamma.xml"},
199                      new String JavaDoc[] {});
200     }
201
202     public void testParentDiffersInCaseScanningSensitive() {
203         DirectoryScanner ds = new DirectoryScanner();
204         ds.setBasedir(new File JavaDoc(getProject().getBaseDir(), "tmp"));
205         ds.setIncludes(new String JavaDoc[] {"alpha/", "ALPHA/beta/"});
206         ds.scan();
207         compareFiles(ds, new String JavaDoc[] {"alpha/beta/beta.xml",
208                                        "alpha/beta/gamma/gamma.xml"},
209                      new String JavaDoc[] {"alpha", "alpha/beta", "alpha/beta/gamma"});
210     }
211
212     public void testParentDiffersInCaseScanningInsensitive() {
213         DirectoryScanner ds = new DirectoryScanner();
214         ds.setBasedir(new File JavaDoc(getProject().getBaseDir(), "tmp"));
215         ds.setIncludes(new String JavaDoc[] {"alpha/", "ALPHA/beta/"});
216         ds.setCaseSensitive(false);
217         ds.scan();
218         compareFiles(ds, new String JavaDoc[] {"alpha/beta/beta.xml",
219                                        "alpha/beta/gamma/gamma.xml"},
220                      new String JavaDoc[] {"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 JavaDoc {
230         if (supportsSymlinks) {
231             File JavaDoc linkFile = new File JavaDoc(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 JavaDoc[] command = new String JavaDoc[] {
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 JavaDoc ioe) {
246                     fail("IOException making link "+ioe);
247                 } catch (InterruptedException JavaDoc ie) {
248                 }
249
250                 File JavaDoc dir = new File JavaDoc(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 JavaDoc[] {"ant/**"});
262                 ds.scan();
263
264                 boolean haveZipPackage = false;
265                 boolean haveTaskdefsPackage = false;
266
267                 String JavaDoc[] 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 JavaDoc[] {"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 JavaDoc("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 JavaDoc(getProject().getBaseDir(), "tmp"));
322         ds.setIncludes(new String JavaDoc[] {
323             "**/*.xml"
324         });
325         ds.setExcludes(new String JavaDoc[] {
326             "alpha/beta/b*xml"
327         });
328         ds.scan();
329         compareFiles(ds, new String JavaDoc[] {"alpha/beta/gamma/gamma.xml"},
330                      new String JavaDoc[] {});
331     }
332
333     public void testExcludeHasPrecedence() {
334         DirectoryScanner ds = new DirectoryScanner();
335         ds.setBasedir(new File JavaDoc(getProject().getBaseDir(), "tmp"));
336         ds.setIncludes(new String JavaDoc[] {
337             "alpha/**"
338         });
339         ds.setExcludes(new String JavaDoc[] {
340             "alpha/**"
341         });
342         ds.scan();
343         compareFiles(ds, new String JavaDoc[] {},
344                      new String JavaDoc[] {});
345
346     }
347
348     public void testAlternateIncludeExclude() {
349         DirectoryScanner ds = new DirectoryScanner();
350         ds.setBasedir(new File JavaDoc(getProject().getBaseDir(), "tmp"));
351         ds.setIncludes(new String JavaDoc[] {
352             "alpha/**",
353             "alpha/beta/gamma/**"
354         });
355         ds.setExcludes(new String JavaDoc[] {
356             "alpha/beta/**"
357         });
358         ds.scan();
359         compareFiles(ds, new String JavaDoc[] {},
360                      new String JavaDoc[] {"alpha"});
361
362     }
363
364     public void testAlternateExcludeInclude() {
365         DirectoryScanner ds = new DirectoryScanner();
366         ds.setBasedir(new File JavaDoc(getProject().getBaseDir(), "tmp"));
367         ds.setExcludes(new String JavaDoc[] {
368             "alpha/**",
369             "alpha/beta/gamma/**"
370         });
371         ds.setIncludes(new String JavaDoc[] {
372             "alpha/beta/**"
373         });
374         ds.scan();
375         compareFiles(ds, new String JavaDoc[] {},
376                      new String JavaDoc[] {});
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 JavaDoc(getProject().getBaseDir(), "tmp"));
387         ds.setExcludes(new String JavaDoc[] {"alpha/**"});
388         ds.setFollowSymlinks(false);
389         ds.scan();
390         compareFiles(ds, new String JavaDoc[] {"delta/delta.xml"},
391                     new String JavaDoc[] {"", "delta"});
392
393         ds = new DirectoryScanner();
394         ds.setBasedir(new File JavaDoc(getProject().getBaseDir(), "tmp"));
395         ds.setExcludes(new String JavaDoc[] {"alpha"});
396         ds.setFollowSymlinks(false);
397         ds.scan();
398         compareFiles(ds, new String JavaDoc[] {"alpha/beta/beta.xml",
399                                        "alpha/beta/gamma/gamma.xml",
400                                         "delta/delta.xml"},
401                      new String JavaDoc[] {"", "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 JavaDoc(getProject().getBaseDir(), "tmp"));
409         ds.setExcludes(new String JavaDoc[] {"**/gamma/**"});
410         ds.setFollowSymlinks(false);
411         ds.scan();
412         Set JavaDoc set = ds.getScannedDirs();
413         assertFalse("empty set", set.isEmpty());
414         String JavaDoc s = "alpha/beta/gamma/".replace('/', File.separatorChar);
415         assertFalse("scanned " + s, set.contains(s));
416     }
417
418     private void compareFiles(DirectoryScanner ds, String JavaDoc[] expectedFiles,
419                               String JavaDoc[] expectedDirectories) {
420         String JavaDoc includedFiles[] = ds.getIncludedFiles();
421         String JavaDoc includedDirectories[] = ds.getIncludedDirectories();
422         assertEquals("file present: ", expectedFiles.length,
423                      includedFiles.length);
424         assertEquals("directories present: ", expectedDirectories.length,
425                      includedDirectories.length);
426
427         TreeSet JavaDoc files = new TreeSet JavaDoc();
428         for (int counter=0; counter < includedFiles.length; counter++) {
429             files.add(includedFiles[counter].replace(File.separatorChar, '/'));
430         }
431         TreeSet JavaDoc directories = new TreeSet JavaDoc();
432         for (int counter=0; counter < includedDirectories.length; counter++) {
433             directories.add(includedDirectories[counter]
434                             .replace(File.separatorChar, '/'));
435         }
436
437         String JavaDoc currentfile;
438         Iterator JavaDoc i = files.iterator();
439         int counter = 0;
440         while (i.hasNext()) {
441             currentfile = (String JavaDoc) i.next();
442             assertEquals(expectedFiles[counter], currentfile);
443             counter++;
444         }
445         String JavaDoc currentdirectory;
446         Iterator JavaDoc dirit = directories.iterator();
447         counter = 0;
448         while (dirit.hasNext()) {
449             currentdirectory = (String JavaDoc) dirit.next();
450             assertEquals(expectedDirectories[counter], currentdirectory);
451             counter++;
452         }
453     }
454
455 }
456
Popular Tags