001: /*
002: * Copyright 2001-2004 The Apache Software Foundation
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: *
016: */
017:
018: package org.drftpd.org.apache.tools.ant;
019:
020: //import org.apache.tools.ant.taskdefs.condition.Os;
021: import net.sf.drftpd.FileExistsException;
022:
023: import org.apache.log4j.BasicConfigurator;
024: import org.drftpd.remotefile.LinkedRemoteFile;
025: import org.drftpd.remotefile.StaticRemoteFile; //import org.apache.tools.ant.util.JavaEnvUtils;
026:
027: import junit.framework.TestCase;
028: import junit.framework.AssertionFailedError;
029: import java.io.File;
030: import java.io.FileNotFoundException;
031: import java.io.IOException;
032: import java.util.Arrays;
033: import java.util.Collections;
034: import java.util.TreeSet;
035: import java.util.Iterator;
036:
037: /**
038: * JUnit 3 testcases for org.apache.tools.ant.DirectoryScanner
039: *
040: */
041: public class DirectoryScannerTest extends TestCase /*BuildFileTest*/{
042:
043: private LinkedRemoteFile _root;
044:
045: public DirectoryScannerTest(String name) {
046: super (name);
047: }
048:
049: // keep track of what operating systems are supported here.
050: //private boolean supportsSymlinks = Os.isFamily("unix")
051: // && !JavaEnvUtils.isJavaVersion(JavaEnvUtils.JAVA_1_1);
052:
053: public void setUp() throws FileExistsException {
054: BasicConfigurator.configure();
055: //configureProject("src/etc/testcases/core/directoryscanner.xml");
056: //getProject().executeTarget("setup");
057: _root = new LinkedRemoteFile(null);
058: LinkedRemoteFile beta = _root.createDirectories("alpha/beta");
059: beta.addFile(new StaticRemoteFile(Collections.EMPTY_LIST,
060: "beta.xml", 0));
061: LinkedRemoteFile gamma = beta.createDirectory("gamma");
062: gamma.addFile(new StaticRemoteFile(Collections.EMPTY_LIST,
063: "gamma.xml", 0));
064: }
065:
066: public void tearDown() {
067: //getProject().executeTarget("cleanup");
068: }
069:
070: public void test1() throws IllegalStateException,
071: FileNotFoundException {
072: DirectoryScanner ds = new DirectoryScanner();
073: ds.setBasedir(_root);
074: ds.setIncludes(new String[] { "alpha" });
075: ds.scan();
076: compareFiles(ds, new String[] {}, new String[] { "alpha" });
077: }
078:
079: public void test2() throws IllegalStateException,
080: FileNotFoundException {
081: DirectoryScanner ds = new DirectoryScanner();
082: ds.setBasedir(_root);
083: ds.setIncludes(new String[] { "alpha/" });
084: ds.scan();
085: compareFiles(ds, new String[] { "alpha/beta/beta.xml",
086: "alpha/beta/gamma/gamma.xml" }, new String[] { "alpha",
087: "alpha/beta", "alpha/beta/gamma" });
088: }
089:
090: public void test3() throws IllegalStateException,
091: FileNotFoundException {
092: DirectoryScanner ds = new DirectoryScanner();
093: ds.setBasedir(_root);
094: ds.scan();
095: compareFiles(ds, new String[] { "alpha/beta/beta.xml",
096: "alpha/beta/gamma/gamma.xml" }, new String[] { "",
097: "alpha", "alpha/beta", "alpha/beta/gamma" });
098: }
099:
100: // public void testFullPathMatchesCaseSensitive() throws IllegalStateException, FileNotFoundException {
101: // DirectoryScanner ds = new DirectoryScanner();
102: // ds.setBasedir(_root);
103: // ds.setIncludes(new String[] {"alpha/beta/gamma/GAMMA.XML"});
104: // ds.scan();
105: // compareFiles(ds, new String[] {}, new String[] {});
106: // }
107:
108: public void testFullPathMatchesCaseInsensitive()
109: throws IllegalStateException, FileNotFoundException {
110: DirectoryScanner ds = new DirectoryScanner();
111: //ds.setCaseSensitive(false);
112: ds.setBasedir(_root);
113: ds.setIncludes(new String[] { "alpha/beta/gamma/GAMMA.XML" });
114: ds.scan();
115: compareFiles(ds, new String[] { "alpha/beta/gamma/gamma.xml" },
116: new String[] {});
117: }
118:
119: public void test2ButCaseInsensitive() throws IllegalStateException,
120: FileNotFoundException {
121: DirectoryScanner ds = new DirectoryScanner();
122: ds.setBasedir(_root);
123: ds.setIncludes(new String[] { "ALPHA/" });
124: //ds.setCaseSensitive(false);
125: ds.scan();
126: compareFiles(ds, new String[] { "alpha/beta/beta.xml",
127: "alpha/beta/gamma/gamma.xml" }, new String[] { "alpha",
128: "alpha/beta", "alpha/beta/gamma" });
129: }
130:
131: // public void testAllowSymlinks() throws IllegalStateException, FileNotFoundException {
132: // //if (!supportsSymlinks) {
133: // // return;
134: // //}
135: //
136: // //getProject().executeTarget("symlink-setup");
137: // setUpSymlink();
138: // DirectoryScanner ds = new DirectoryScanner();
139: // ds.setBasedir(_root);
140: // ds.setIncludes(new String[] {"alpha/beta/gamma/"});
141: // ds.scan();
142: // compareFiles(ds, new String[] {"alpha/beta/gamma/gamma.xml"},
143: // new String[] {"alpha/beta/gamma"});
144: // }
145:
146: public void testProhibitSymlinks() throws IllegalStateException,
147: FileNotFoundException {
148: //if (!supportsSymlinks) {
149: // return;
150: //}
151:
152: //getProject().executeTarget("symlink-setup");
153: setUpSymlink();
154: DirectoryScanner ds = new DirectoryScanner();
155: ds.setBasedir(_root);
156: ds.setIncludes(new String[] { "alpha/beta/gamma/" });
157: ds.setFollowSymlinks(false);
158: ds.scan();
159: compareFiles(ds, new String[] {}, new String[] {});
160: }
161:
162: // father and child pattern test
163: public void testOrderOfIncludePatternsIrrelevant()
164: throws IllegalStateException, FileNotFoundException {
165: String[] expectedFiles = { "alpha/beta/beta.xml",
166: "alpha/beta/gamma/gamma.xml" };
167: String[] expectedDirectories = { "alpha/beta",
168: "alpha/beta/gamma" };
169: DirectoryScanner ds = new DirectoryScanner();
170: ds.setBasedir(_root);
171: ds.setIncludes(new String[] { "alpha/be?a/**",
172: "alpha/beta/gamma/" });
173: ds.scan();
174: compareFiles(ds, expectedFiles, expectedDirectories);
175: // redo the test, but the 2 include patterns are inverted
176: ds = new DirectoryScanner();
177: ds.setBasedir(_root);
178: ds.setIncludes(new String[] { "alpha/beta/gamma/",
179: "alpha/be?a/**" });
180: ds.scan();
181: compareFiles(ds, expectedFiles, expectedDirectories);
182: }
183:
184: public void testPatternsDifferInCaseScanningSensitive()
185: throws IllegalStateException, FileNotFoundException {
186: DirectoryScanner ds = new DirectoryScanner();
187: ds.setBasedir(_root);
188: ds.setIncludes(new String[] { "alpha/", "ALPHA/" });
189: ds.scan();
190: compareFiles(ds, new String[] { "alpha/beta/beta.xml",
191: "alpha/beta/gamma/gamma.xml" }, new String[] { "alpha",
192: "alpha/beta", "alpha/beta/gamma" });
193: }
194:
195: public void testPatternsDifferInCaseScanningInsensitive()
196: throws IllegalStateException, FileNotFoundException {
197: DirectoryScanner ds = new DirectoryScanner();
198: ds.setBasedir(_root);
199: ds.setIncludes(new String[] { "alpha/", "ALPHA/" });
200: //ds.setCaseSensitive(false);
201: ds.scan();
202: compareFiles(ds, new String[] { "alpha/beta/beta.xml",
203: "alpha/beta/gamma/gamma.xml" }, new String[] { "alpha",
204: "alpha/beta", "alpha/beta/gamma" });
205: }
206:
207: public void testFullpathDiffersInCaseScanningSensitive()
208: throws IllegalStateException, FileNotFoundException {
209: DirectoryScanner ds = new DirectoryScanner();
210: ds.setBasedir(_root);
211: ds.setIncludes(new String[] { "alpha/beta/gamma/gamma.xml",
212: "alpha/beta/gamma/GAMMA.XML" });
213: ds.scan();
214: compareFiles(ds, new String[] { "alpha/beta/gamma/gamma.xml" },
215: new String[] {});
216: }
217:
218: public void testFullpathDiffersInCaseScanningInsensitive()
219: throws IllegalStateException, FileNotFoundException {
220: DirectoryScanner ds = new DirectoryScanner();
221: ds.setBasedir(_root);
222: ds.setIncludes(new String[] { "alpha/beta/gamma/gamma.xml",
223: "alpha/beta/gamma/GAMMA.XML" });
224: //ds.setCaseSensitive(false);
225: ds.scan();
226: compareFiles(ds, new String[] { "alpha/beta/gamma/gamma.xml" },
227: new String[] {});
228: }
229:
230: public void testParentDiffersInCaseScanningSensitive()
231: throws IllegalStateException, FileNotFoundException {
232: DirectoryScanner ds = new DirectoryScanner();
233: ds.setBasedir(_root);
234: ds.setIncludes(new String[] { "alpha/", "ALPHA/beta/" });
235: ds.scan();
236: compareFiles(ds, new String[] { "alpha/beta/beta.xml",
237: "alpha/beta/gamma/gamma.xml" }, new String[] { "alpha",
238: "alpha/beta", "alpha/beta/gamma" });
239: }
240:
241: public void testParentDiffersInCaseScanningInsensitive()
242: throws IllegalStateException, FileNotFoundException {
243: DirectoryScanner ds = new DirectoryScanner();
244: ds.setBasedir(_root);
245: ds.setIncludes(new String[] { "alpha/", "ALPHA/beta/" });
246: //ds.setCaseSensitive(false);
247: ds.scan();
248: compareFiles(ds, new String[] { "alpha/beta/beta.xml",
249: "alpha/beta/gamma/gamma.xml" }, new String[] { "alpha",
250: "alpha/beta", "alpha/beta/gamma" });
251: }
252:
253: /**
254: * Test case for setFollowLinks() and associated funtionality.
255: * Only supports test on linux, at the moment because Java has
256: * no real notion of symlinks built in, so an os-specfic call
257: * to Runtime.exec() must be made to create a link to test against.
258: * @throws FileNotFoundException
259: * @throws IllegalStateException
260: */
261: // public void testSetFollowLinks() {
262: // /*if (supportsSymlinks)*/ {
263: // try {
264: // // add conditions and more commands as soon as the need arises
265: // String[] command = new String[] {
266: // "ln", "-s", "ant", "src/main/org/apache/tools/ThisIsALink"
267: // };
268: // try {
269: // Runtime.getRuntime().exec(command);
270: // // give ourselves some time for the system call
271: // // to execute... tweak if you have a really over
272: // // loaded system.
273: // Thread.sleep(1000);
274: // } catch (IOException ioe) {
275: // fail("IOException making link "+ioe);
276: // } catch (InterruptedException ie) {
277: // }
278: //
279: // File dir = new File("src/main/org/apache/tools");
280: // DirectoryScanner ds = new DirectoryScanner();
281: //
282: // // followLinks should be true by default, but if this ever
283: // // changes we will need this line.
284: // ds.setFollowSymlinks(true);
285: //
286: // ds.setBasedir(dir);
287: // ds.setExcludes(new String[] {"ant/**"});
288: // ds.scan();
289: //
290: // boolean haveZipPackage = false;
291: // boolean haveTaskdefsPackage = false;
292: //
293: // String[] included = ds.getIncludedDirectories();
294: // for (int i=0; i<included.length; i++) {
295: // if (included[i].equals("zip")) {
296: // haveZipPackage = true;
297: // } else if (included[i].equals("ThisIsALink"
298: // + File.separator
299: // + "taskdefs")) {
300: // haveTaskdefsPackage = true;
301: // }
302: // }
303: //
304: // // if we followed the symlink we just made we should
305: // // bypass the excludes.
306: //
307: // assertTrue("(1) zip package included", haveZipPackage);
308: // assertTrue("(1) taskdefs package included",
309: // haveTaskdefsPackage);
310: //
311: //
312: // ds = new DirectoryScanner();
313: // ds.setFollowSymlinks(false);
314: //
315: // ds.setBasedir(dir);
316: // ds.setExcludes(new String[] {"ant/**"});
317: // ds.scan();
318: //
319: // haveZipPackage = false;
320: // haveTaskdefsPackage = false;
321: // included = ds.getIncludedDirectories();
322: // for (int i=0; i<included.length; i++) {
323: // if (included[i].equals("zip")) {
324: // haveZipPackage = true;
325: // } else if (included[i].equals("ThisIsALink"
326: // + File.separator
327: // + "taskdefs")) {
328: // haveTaskdefsPackage = true;
329: // }
330: // }
331: // assertTrue("(2) zip package included", haveZipPackage);
332: // assertTrue("(2) taskdefs package not included",
333: // !haveTaskdefsPackage);
334: //
335: // } finally {
336: // File f = new File("src/main/org/apache/tools/ThisIsALink");
337: // if (!f.delete()) {
338: // throw new RuntimeException("Failed to delete " + f);
339: // }
340: // }
341: // }
342: // }
343: public void testExcludeOneFile() throws IllegalStateException,
344: FileNotFoundException {
345: DirectoryScanner ds = new DirectoryScanner();
346: ds.setBasedir(_root);
347: ds.setIncludes(new String[] { "**/*.xml" });
348: ds.setExcludes(new String[] { "alpha/beta/b*xml" });
349: ds.scan();
350: compareFiles(ds, new String[] { "alpha/beta/gamma/gamma.xml" },
351: new String[] {});
352: }
353:
354: public void testExcludeHasPrecedence()
355: throws IllegalStateException, FileNotFoundException {
356: DirectoryScanner ds = new DirectoryScanner();
357: ds.setBasedir(_root);
358: ds.setIncludes(new String[] { "alpha/**" });
359: ds.setExcludes(new String[] { "alpha/**" });
360: ds.scan();
361: compareFiles(ds, new String[] {}, new String[] {});
362:
363: }
364:
365: public void testAlternateIncludeExclude()
366: throws IllegalStateException, FileNotFoundException {
367: DirectoryScanner ds = new DirectoryScanner();
368: ds.setBasedir(_root);
369: ds
370: .setIncludes(new String[] { "alpha/**",
371: "alpha/beta/gamma/**" });
372: ds.setExcludes(new String[] { "alpha/beta/**" });
373: ds.scan();
374: compareFiles(ds, new String[] {}, new String[] { "alpha" });
375:
376: }
377:
378: public void testAlternateExcludeInclude()
379: throws IllegalStateException, FileNotFoundException {
380: DirectoryScanner ds = new DirectoryScanner();
381: ds.setBasedir(_root);
382: ds
383: .setExcludes(new String[] { "alpha/**",
384: "alpha/beta/gamma/**" });
385: ds.setIncludes(new String[] { "alpha/beta/**" });
386: ds.scan();
387: compareFiles(ds, new String[] {}, new String[] {});
388:
389: }
390:
391: /**
392: * Test inspired by Bug#1415.
393: * @throws FileNotFoundException
394: * @throws IllegalStateException
395: */
396: public void testChildrenOfExcludedDirectory()
397: throws IllegalStateException, FileNotFoundException {
398: //getProject().executeTarget("children-of-excluded-dir-setup");
399: setUpChildrenOfExcludedDir();
400: DirectoryScanner ds = new DirectoryScanner();
401: ds.setBasedir(_root);
402: ds.setExcludes(new String[] { "alpha/**" });
403: ds.setFollowSymlinks(false);
404: ds.scan();
405: compareFiles(ds, new String[] { "delta/delta.xml" },
406: new String[] { "", "delta" });
407:
408: ds = new DirectoryScanner();
409: ds.setBasedir(_root);
410: ds.setExcludes(new String[] { "alpha" });
411: ds.setFollowSymlinks(false);
412: ds.scan();
413: compareFiles(ds, new String[] { "alpha/beta/beta.xml",
414: "alpha/beta/gamma/gamma.xml", "delta/delta.xml" },
415: new String[] { "", "alpha/beta", "alpha/beta/gamma",
416: "delta" });
417:
418: }
419:
420: private void setUpChildrenOfExcludedDir() {
421: _root.createDirectories("delta").addFile(
422: new StaticRemoteFile(Collections.EMPTY_LIST,
423: "delta.xml", 0));
424: }
425:
426: private void setUpSymlink() throws FileNotFoundException {
427: _root.createDirectories("epsilon/gamma");
428: _root.lookupFile("alpha/beta").delete();
429: _root.lookupFile("alpha").addFile(
430: new StaticRemoteFile("beta", Collections.EMPTY_LIST,
431: "/epsilon"));
432: _root.lookupFile("alpha/beta/gamma").addFile(
433: new StaticRemoteFile("gamma.xml"));
434: }
435:
436: private void compareFiles(DirectoryScanner ds,
437: String[] expectedFiles, String[] expectedDirectories) {
438: String includedFiles[] = ds.getIncludedFiles();
439: String includedDirectories[] = ds.getIncludedDirectories();
440: assertEquals("file present: " + Arrays.asList(expectedFiles)
441: + ", " + Arrays.asList(includedFiles),
442: expectedFiles.length, includedFiles.length);
443: assertEquals("directories present: ",
444: expectedDirectories.length, includedDirectories.length);
445:
446: TreeSet files = new TreeSet();
447: for (int counter = 0; counter < includedFiles.length; counter++) {
448: files.add(includedFiles[counter].replace(
449: File.separatorChar, '/'));
450: }
451: TreeSet directories = new TreeSet();
452: for (int counter = 0; counter < includedDirectories.length; counter++) {
453: directories.add(includedDirectories[counter].replace(
454: File.separatorChar, '/'));
455: }
456:
457: String currentfile;
458: Iterator i = files.iterator();
459: int counter = 0;
460: while (i.hasNext()) {
461: currentfile = (String) i.next();
462: assertEquals(expectedFiles[counter], currentfile);
463: counter++;
464: }
465: String currentdirectory;
466: Iterator dirit = directories.iterator();
467: counter = 0;
468: while (dirit.hasNext()) {
469: currentdirectory = (String) dirit.next();
470: assertEquals(expectedDirectories[counter], currentdirectory);
471: counter++;
472: }
473: }
474:
475: }
|