001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018:
019: package org.apache.tools.ant;
020:
021: import org.apache.tools.ant.taskdefs.condition.Os;
022: import org.apache.tools.ant.util.FileUtils;
023:
024: import java.io.File;
025: import java.io.IOException;
026: import java.util.Set;
027: import java.util.TreeSet;
028: import java.util.Iterator;
029:
030: /**
031: * JUnit 3 testcases for org.apache.tools.ant.DirectoryScanner
032: *
033: */
034: public class DirectoryScannerTest extends BuildFileTest {
035:
036: public DirectoryScannerTest(String name) {
037: super (name);
038: }
039:
040: // keep track of what operating systems are supported here.
041: private boolean supportsSymlinks = Os.isFamily("unix");
042:
043: public void setUp() {
044: configureProject("src/etc/testcases/core/directoryscanner.xml");
045: getProject().executeTarget("setup");
046: }
047:
048: public void tearDown() {
049: getProject().executeTarget("cleanup");
050: }
051:
052: public void test1() {
053: DirectoryScanner ds = new DirectoryScanner();
054: ds.setBasedir(new File(getProject().getBaseDir(), "tmp"));
055: ds.setIncludes(new String[] { "alpha" });
056: ds.scan();
057: compareFiles(ds, new String[] {}, new String[] { "alpha" });
058: }
059:
060: public void test2() {
061: DirectoryScanner ds = new DirectoryScanner();
062: ds.setBasedir(new File(getProject().getBaseDir(), "tmp"));
063: ds.setIncludes(new String[] { "alpha/" });
064: ds.scan();
065: compareFiles(ds, new String[] { "alpha/beta/beta.xml",
066: "alpha/beta/gamma/gamma.xml" }, new String[] { "alpha",
067: "alpha/beta", "alpha/beta/gamma" });
068: }
069:
070: public void test3() {
071: DirectoryScanner ds = new DirectoryScanner();
072: ds.setBasedir(new File(getProject().getBaseDir(), "tmp"));
073: ds.scan();
074: compareFiles(ds, new String[] { "alpha/beta/beta.xml",
075: "alpha/beta/gamma/gamma.xml" }, new String[] { "",
076: "alpha", "alpha/beta", "alpha/beta/gamma" });
077: }
078:
079: public void testFullPathMatchesCaseSensitive() {
080: DirectoryScanner ds = new DirectoryScanner();
081: ds.setBasedir(new File(getProject().getBaseDir(), "tmp"));
082: ds.setIncludes(new String[] { "alpha/beta/gamma/GAMMA.XML" });
083: ds.scan();
084: compareFiles(ds, new String[] {}, new String[] {});
085: }
086:
087: public void testFullPathMatchesCaseInsensitive() {
088: DirectoryScanner ds = new DirectoryScanner();
089: ds.setCaseSensitive(false);
090: ds.setBasedir(new File(getProject().getBaseDir(), "tmp"));
091: ds.setIncludes(new String[] { "alpha/beta/gamma/GAMMA.XML" });
092: ds.scan();
093: compareFiles(ds, new String[] { "alpha/beta/gamma/gamma.xml" },
094: new String[] {});
095: }
096:
097: public void test2ButCaseInsensitive() {
098: DirectoryScanner ds = new DirectoryScanner();
099: ds.setBasedir(new File(getProject().getBaseDir(), "tmp"));
100: ds.setIncludes(new String[] { "ALPHA/" });
101: ds.setCaseSensitive(false);
102: ds.scan();
103: compareFiles(ds, new String[] { "alpha/beta/beta.xml",
104: "alpha/beta/gamma/gamma.xml" }, new String[] { "alpha",
105: "alpha/beta", "alpha/beta/gamma" });
106: }
107:
108: public void testAllowSymlinks() {
109: if (!supportsSymlinks) {
110: return;
111: }
112:
113: getProject().executeTarget("symlink-setup");
114: DirectoryScanner ds = new DirectoryScanner();
115: ds.setBasedir(new File(getProject().getBaseDir(), "tmp"));
116: ds.setIncludes(new String[] { "alpha/beta/gamma/" });
117: ds.scan();
118: compareFiles(ds, new String[] { "alpha/beta/gamma/gamma.xml" },
119: new String[] { "alpha/beta/gamma" });
120: }
121:
122: public void testProhibitSymlinks() {
123: if (!supportsSymlinks) {
124: return;
125: }
126:
127: getProject().executeTarget("symlink-setup");
128: DirectoryScanner ds = new DirectoryScanner();
129: ds.setBasedir(new File(getProject().getBaseDir(), "tmp"));
130: ds.setIncludes(new String[] { "alpha/beta/gamma/" });
131: ds.setFollowSymlinks(false);
132: ds.scan();
133: compareFiles(ds, new String[] {}, new String[] {});
134: }
135:
136: // father and child pattern test
137: public void testOrderOfIncludePatternsIrrelevant() {
138: String[] expectedFiles = { "alpha/beta/beta.xml",
139: "alpha/beta/gamma/gamma.xml" };
140: String[] expectedDirectories = { "alpha/beta",
141: "alpha/beta/gamma" };
142: DirectoryScanner ds = new DirectoryScanner();
143: ds.setBasedir(new File(getProject().getBaseDir(), "tmp"));
144: ds.setIncludes(new String[] { "alpha/be?a/**",
145: "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/",
152: "alpha/be?a/**" });
153: ds.scan();
154: compareFiles(ds, expectedFiles, expectedDirectories);
155: }
156:
157: public void testPatternsDifferInCaseScanningSensitive() {
158: DirectoryScanner ds = new DirectoryScanner();
159: ds.setBasedir(new File(getProject().getBaseDir(), "tmp"));
160: ds.setIncludes(new String[] { "alpha/", "ALPHA/" });
161: ds.scan();
162: compareFiles(ds, new String[] { "alpha/beta/beta.xml",
163: "alpha/beta/gamma/gamma.xml" }, new String[] { "alpha",
164: "alpha/beta", "alpha/beta/gamma" });
165: }
166:
167: public void testPatternsDifferInCaseScanningInsensitive() {
168: DirectoryScanner ds = new DirectoryScanner();
169: ds.setBasedir(new File(getProject().getBaseDir(), "tmp"));
170: ds.setIncludes(new String[] { "alpha/", "ALPHA/" });
171: ds.setCaseSensitive(false);
172: ds.scan();
173: compareFiles(ds, new String[] { "alpha/beta/beta.xml",
174: "alpha/beta/gamma/gamma.xml" }, new String[] { "alpha",
175: "alpha/beta", "alpha/beta/gamma" });
176: }
177:
178: public void testFullpathDiffersInCaseScanningSensitive() {
179: DirectoryScanner ds = new DirectoryScanner();
180: ds.setBasedir(new File(getProject().getBaseDir(), "tmp"));
181: ds.setIncludes(new String[] { "alpha/beta/gamma/gamma.xml",
182: "alpha/beta/gamma/GAMMA.XML" });
183: ds.scan();
184: compareFiles(ds, new String[] { "alpha/beta/gamma/gamma.xml" },
185: new String[] {});
186: }
187:
188: public void testFullpathDiffersInCaseScanningInsensitive() {
189: DirectoryScanner ds = new DirectoryScanner();
190: ds.setBasedir(new File(getProject().getBaseDir(), "tmp"));
191: ds.setIncludes(new String[] { "alpha/beta/gamma/gamma.xml",
192: "alpha/beta/gamma/GAMMA.XML" });
193: ds.setCaseSensitive(false);
194: ds.scan();
195: compareFiles(ds, new String[] { "alpha/beta/gamma/gamma.xml" },
196: new String[] {});
197: }
198:
199: public void testParentDiffersInCaseScanningSensitive() {
200: DirectoryScanner ds = new DirectoryScanner();
201: ds.setBasedir(new File(getProject().getBaseDir(), "tmp"));
202: ds.setIncludes(new String[] { "alpha/", "ALPHA/beta/" });
203: ds.scan();
204: compareFiles(ds, new String[] { "alpha/beta/beta.xml",
205: "alpha/beta/gamma/gamma.xml" }, new String[] { "alpha",
206: "alpha/beta", "alpha/beta/gamma" });
207: }
208:
209: public void testParentDiffersInCaseScanningInsensitive() {
210: DirectoryScanner ds = new DirectoryScanner();
211: ds.setBasedir(new File(getProject().getBaseDir(), "tmp"));
212: ds.setIncludes(new String[] { "alpha/", "ALPHA/beta/" });
213: ds.setCaseSensitive(false);
214: ds.scan();
215: compareFiles(ds, new String[] { "alpha/beta/beta.xml",
216: "alpha/beta/gamma/gamma.xml" }, new String[] { "alpha",
217: "alpha/beta", "alpha/beta/gamma" });
218: }
219:
220: /**
221: * Test case for setFollowLinks() and associated functionality.
222: * Only supports test on Linux at the moment because Java has
223: * no real notion of symlinks built in, so an os-specfic call
224: * to Runtime.exec() must be made to create a link to test against.
225: */
226: public void testSetFollowLinks() throws IOException {
227: if (supportsSymlinks) {
228: File linkFile = new File(System.getProperty("root"),
229: "src/main/org/apache/tools/ThisIsALink");
230: System.err.println("link exists pre-test? "
231: + linkFile.exists());
232:
233: try {
234: // add conditions and more commands as soon as the need arises
235: String[] command = new String[] { "ln", "-s", "ant",
236: linkFile.getAbsolutePath() };
237: try {
238: Runtime.getRuntime().exec(command);
239: // give ourselves some time for the system call
240: // to execute... tweak if you have a really over
241: // loaded system.
242: Thread.sleep(1000);
243: } catch (IOException ioe) {
244: fail("IOException making link " + ioe);
245: } catch (InterruptedException ie) {
246: }
247:
248: File dir = new File(System.getProperty("root"),
249: "src/main/org/apache/tools");
250: System.err.println("link exists after exec? "
251: + linkFile.exists());
252: System.err.println("Ant knows it is a link? "
253: + FileUtils.getFileUtils().isSymbolicLink(dir,
254: "ThisIsALink"));
255:
256: DirectoryScanner ds = new DirectoryScanner();
257:
258: // followLinks should be true by default, but if this ever
259: // changes we will need this line.
260: ds.setFollowSymlinks(true);
261:
262: ds.setBasedir(dir);
263: ds.setExcludes(new String[] { "ant/**" });
264: ds.scan();
265:
266: boolean haveZipPackage = false;
267: boolean haveTaskdefsPackage = false;
268:
269: String[] included = ds.getIncludedDirectories();
270: for (int i = 0; i < included.length; i++) {
271: if (included[i].equals("zip")) {
272: haveZipPackage = true;
273: } else if (included[i].equals("ThisIsALink"
274: + File.separator + "taskdefs")) {
275: haveTaskdefsPackage = true;
276: }
277: }
278:
279: // if we followed the symlink we just made we should
280: // bypass the excludes.
281:
282: assertTrue("(1) zip package included", haveZipPackage);
283: assertTrue("(1) taskdefs package included",
284: haveTaskdefsPackage);
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 + "taskdefs")) {
301: haveTaskdefsPackage = true;
302: }
303: }
304: assertTrue("(2) zip package included", haveZipPackage);
305: assertTrue("(2) taskdefs package not included",
306: !haveTaskdefsPackage);
307:
308: } finally {
309: System.err.println("link exists pre-delete? "
310: + linkFile.exists());
311: if (!linkFile.delete()) {
312: throw new RuntimeException("Failed to delete "
313: + linkFile);
314: }
315: System.err.println("link exists post-delete? "
316: + linkFile.exists());
317: }
318: }
319: }
320:
321: public void testExcludeOneFile() {
322: DirectoryScanner ds = new DirectoryScanner();
323: ds.setBasedir(new File(getProject().getBaseDir(), "tmp"));
324: ds.setIncludes(new String[] { "**/*.xml" });
325: ds.setExcludes(new String[] { "alpha/beta/b*xml" });
326: ds.scan();
327: compareFiles(ds, new String[] { "alpha/beta/gamma/gamma.xml" },
328: new String[] {});
329: }
330:
331: public void testExcludeHasPrecedence() {
332: DirectoryScanner ds = new DirectoryScanner();
333: ds.setBasedir(new File(getProject().getBaseDir(), "tmp"));
334: ds.setIncludes(new String[] { "alpha/**" });
335: ds.setExcludes(new String[] { "alpha/**" });
336: ds.scan();
337: compareFiles(ds, new String[] {}, new String[] {});
338:
339: }
340:
341: public void testAlternateIncludeExclude() {
342: DirectoryScanner ds = new DirectoryScanner();
343: ds.setBasedir(new File(getProject().getBaseDir(), "tmp"));
344: ds
345: .setIncludes(new String[] { "alpha/**",
346: "alpha/beta/gamma/**" });
347: ds.setExcludes(new String[] { "alpha/beta/**" });
348: ds.scan();
349: compareFiles(ds, new String[] {}, new String[] { "alpha" });
350:
351: }
352:
353: public void testAlternateExcludeInclude() {
354: DirectoryScanner ds = new DirectoryScanner();
355: ds.setBasedir(new File(getProject().getBaseDir(), "tmp"));
356: ds
357: .setExcludes(new String[] { "alpha/**",
358: "alpha/beta/gamma/**" });
359: ds.setIncludes(new String[] { "alpha/beta/**" });
360: ds.scan();
361: compareFiles(ds, new String[] {}, new String[] {});
362:
363: }
364:
365: /**
366: * Test inspired by Bug#1415.
367: */
368: public void testChildrenOfExcludedDirectory() {
369: getProject().executeTarget("children-of-excluded-dir-setup");
370: DirectoryScanner ds = new DirectoryScanner();
371: ds.setBasedir(new File(getProject().getBaseDir(), "tmp"));
372: ds.setExcludes(new String[] { "alpha/**" });
373: ds.setFollowSymlinks(false);
374: ds.scan();
375: compareFiles(ds, new String[] { "delta/delta.xml" },
376: new String[] { "", "delta" });
377:
378: ds = new DirectoryScanner();
379: ds.setBasedir(new File(getProject().getBaseDir(), "tmp"));
380: ds.setExcludes(new String[] { "alpha" });
381: ds.setFollowSymlinks(false);
382: ds.scan();
383: compareFiles(ds, new String[] { "alpha/beta/beta.xml",
384: "alpha/beta/gamma/gamma.xml", "delta/delta.xml" },
385: new String[] { "", "alpha/beta", "alpha/beta/gamma",
386: "delta" });
387:
388: }
389:
390: public void testIsExcludedDirectoryScanned() {
391: String shareclassloader = getProject().getProperty(
392: "tests.and.ant.share.classloader");
393: // when the test is started by the build.xml of ant
394: // if the property tests.and.ant.share.classloader is not set in the build.xml
395: // a sysproperty with name tests.and.ant.share.classloader and value
396: // ${tests.and.ant.share.classloader} will be set
397: // we are trying to catch this here.
398: if (shareclassloader == null
399: || (shareclassloader != null && shareclassloader
400: .indexOf("${") == 0)) {
401: System.out
402: .println("cannot execute testIsExcludedDirectoryScanned when tests are forked, "
403: + "package private method called");
404: return;
405: }
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: public void testAbsolute1() {
419: getProject().executeTarget("extended-setup");
420: DirectoryScanner ds = new DirectoryScanner();
421: String tmpdir = getProject().getBaseDir().getAbsolutePath()
422: .replace(File.separatorChar, '/')
423: + "/tmp";
424: ds.setIncludes(new String[] { tmpdir + "/**/*" });
425: ds.scan();
426: compareFiles(ds, new String[] {
427: tmpdir + "/alpha/beta/beta.xml",
428: tmpdir + "/alpha/beta/gamma/gamma.xml",
429: tmpdir + "/delta/delta.xml" }, new String[] {
430: tmpdir + "/alpha", tmpdir + "/alpha/beta",
431: tmpdir + "/alpha/beta/gamma", tmpdir + "/delta" });
432: }
433:
434: public void testAbsolute2() {
435: getProject().executeTarget("setup");
436: DirectoryScanner ds = new DirectoryScanner();
437: ds
438: .setIncludes(new String[] { "alpha/**",
439: "alpha/beta/gamma/**" });
440: ds.scan();
441: String[] mt = new String[0];
442: compareFiles(ds, mt, mt);
443: }
444:
445: public void testAbsolute3() {
446: getProject().executeTarget("extended-setup");
447: DirectoryScanner ds = new DirectoryScanner();
448: String tmpdir = getProject().getBaseDir().getAbsolutePath()
449: .replace(File.separatorChar, '/')
450: + "/tmp";
451: ds.setIncludes(new String[] { tmpdir + "/**/*" });
452: ds.setExcludes(new String[] { "**/alpha", "**/delta/*" });
453: ds.scan();
454: compareFiles(ds, new String[] {
455: tmpdir + "/alpha/beta/beta.xml",
456: tmpdir + "/alpha/beta/gamma/gamma.xml" }, new String[] {
457: tmpdir + "/alpha/beta", tmpdir + "/alpha/beta/gamma",
458: tmpdir + "/delta" });
459: }
460:
461: public void testAbsolute4() {
462: getProject().executeTarget("extended-setup");
463: DirectoryScanner ds = new DirectoryScanner();
464: String tmpdir = getProject().getBaseDir().getAbsolutePath()
465: .replace(File.separatorChar, '/')
466: + "/tmp";
467: ds.setIncludes(new String[] { tmpdir + "/alpha/beta/**/*",
468: tmpdir + "/delta/*" });
469: ds.setExcludes(new String[] { "**/beta.xml" });
470: ds.scan();
471: compareFiles(ds, new String[] {
472: tmpdir + "/alpha/beta/gamma/gamma.xml",
473: tmpdir + "/delta/delta.xml" }, new String[] { tmpdir
474: + "/alpha/beta/gamma" });
475: }
476:
477: public void testAbsolute5() {
478: //testing drive letter search from root:
479: if (!(Os.isFamily("dos") || Os.isFamily("netware"))) {
480: return;
481: }
482: DirectoryScanner ds = new DirectoryScanner();
483: String pattern = new File(File.separator).getAbsolutePath()
484: .toUpperCase()
485: + "*";
486: ds.setIncludes(new String[] { pattern });
487: ds.scan();
488: //if this is our context we assume there must be something here:
489: assertTrue("should have at least one resident file", ds
490: .getIncludedFilesCount()
491: + ds.getIncludedDirsCount() > 0);
492: }
493:
494: private void compareFiles(DirectoryScanner ds,
495: String[] expectedFiles, String[] expectedDirectories) {
496: String includedFiles[] = ds.getIncludedFiles();
497: String includedDirectories[] = ds.getIncludedDirectories();
498: assertEquals("file present: ", expectedFiles.length,
499: includedFiles.length);
500: assertEquals("directories present: ",
501: expectedDirectories.length, includedDirectories.length);
502:
503: TreeSet files = new TreeSet();
504: for (int counter = 0; counter < includedFiles.length; counter++) {
505: files.add(includedFiles[counter].replace(
506: File.separatorChar, '/'));
507: }
508: TreeSet directories = new TreeSet();
509: for (int counter = 0; counter < includedDirectories.length; counter++) {
510: directories.add(includedDirectories[counter].replace(
511: File.separatorChar, '/'));
512: }
513:
514: String currentfile;
515: Iterator i = files.iterator();
516: int counter = 0;
517: while (i.hasNext()) {
518: currentfile = (String) i.next();
519: assertEquals(expectedFiles[counter], currentfile);
520: counter++;
521: }
522: String currentdirectory;
523: Iterator dirit = directories.iterator();
524: counter = 0;
525: while (dirit.hasNext()) {
526: currentdirectory = (String) dirit.next();
527: assertEquals(expectedDirectories[counter], currentdirectory);
528: counter++;
529: }
530: }
531:
532: }
|