You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

DirectoryScannerTest.java 17 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. /*
  2. * Copyright 2001-2004 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. package org.apache.tools.ant;
  18. import org.apache.tools.ant.taskdefs.condition.Os;
  19. import org.apache.tools.ant.types.Resource;
  20. import org.apache.tools.ant.util.JavaEnvUtils;
  21. import junit.framework.TestCase;
  22. import junit.framework.AssertionFailedError;
  23. import java.io.File;
  24. import java.io.IOException;
  25. import java.util.TreeSet;
  26. import java.util.Iterator;
  27. /**
  28. * JUnit 3 testcases for org.apache.tools.ant.DirectoryScanner
  29. *
  30. */
  31. public class DirectoryScannerTest extends BuildFileTest {
  32. public DirectoryScannerTest(String name) {super(name);}
  33. // keep track of what operating systems are supported here.
  34. private boolean supportsSymlinks = Os.isFamily("unix");
  35. public void setUp() {
  36. configureProject("src/etc/testcases/core/directoryscanner.xml");
  37. getProject().executeTarget("setup");
  38. }
  39. public void tearDown() {
  40. getProject().executeTarget("cleanup");
  41. }
  42. public void test1() {
  43. DirectoryScanner ds = new DirectoryScanner();
  44. ds.setBasedir(new File(getProject().getBaseDir(), "tmp"));
  45. ds.setIncludes(new String[] {"alpha"});
  46. ds.scan();
  47. compareFiles(ds, new String[] {} ,new String[] {"alpha"});
  48. }
  49. public void test2() {
  50. DirectoryScanner ds = new DirectoryScanner();
  51. ds.setBasedir(new File(getProject().getBaseDir(), "tmp"));
  52. ds.setIncludes(new String[] {"alpha/"});
  53. ds.scan();
  54. compareFiles(ds, new String[] {"alpha/beta/beta.xml",
  55. "alpha/beta/gamma/gamma.xml"},
  56. new String[] {"alpha", "alpha/beta", "alpha/beta/gamma"});
  57. }
  58. public void test3() {
  59. DirectoryScanner ds = new DirectoryScanner();
  60. ds.setBasedir(new File(getProject().getBaseDir(), "tmp"));
  61. ds.scan();
  62. compareFiles(ds, new String[] {"alpha/beta/beta.xml",
  63. "alpha/beta/gamma/gamma.xml"},
  64. new String[] {"", "alpha", "alpha/beta",
  65. "alpha/beta/gamma"});
  66. }
  67. public void testFullPathMatchesCaseSensitive() {
  68. DirectoryScanner ds = new DirectoryScanner();
  69. ds.setBasedir(new File(getProject().getBaseDir(), "tmp"));
  70. ds.setIncludes(new String[] {"alpha/beta/gamma/GAMMA.XML"});
  71. ds.scan();
  72. compareFiles(ds, new String[] {}, new String[] {});
  73. }
  74. public void testFullPathMatchesCaseInsensitive() {
  75. DirectoryScanner ds = new DirectoryScanner();
  76. ds.setCaseSensitive(false);
  77. ds.setBasedir(new File(getProject().getBaseDir(), "tmp"));
  78. ds.setIncludes(new String[] {"alpha/beta/gamma/GAMMA.XML"});
  79. ds.scan();
  80. compareFiles(ds, new String[] {"alpha/beta/gamma/gamma.xml"},
  81. new String[] {});
  82. }
  83. public void test2ButCaseInsensitive() {
  84. DirectoryScanner ds = new DirectoryScanner();
  85. ds.setBasedir(new File(getProject().getBaseDir(), "tmp"));
  86. ds.setIncludes(new String[] {"ALPHA/"});
  87. ds.setCaseSensitive(false);
  88. ds.scan();
  89. compareFiles(ds, new String[] {"alpha/beta/beta.xml",
  90. "alpha/beta/gamma/gamma.xml"},
  91. new String[] {"alpha", "alpha/beta", "alpha/beta/gamma"});
  92. }
  93. public void testAllowSymlinks() {
  94. if (!supportsSymlinks) {
  95. return;
  96. }
  97. getProject().executeTarget("symlink-setup");
  98. DirectoryScanner ds = new DirectoryScanner();
  99. ds.setBasedir(new File(getProject().getBaseDir(), "tmp"));
  100. ds.setIncludes(new String[] {"alpha/beta/gamma/"});
  101. ds.scan();
  102. compareFiles(ds, new String[] {"alpha/beta/gamma/gamma.xml"},
  103. new String[] {"alpha/beta/gamma"});
  104. }
  105. public void testProhibitSymlinks() {
  106. if (!supportsSymlinks) {
  107. return;
  108. }
  109. getProject().executeTarget("symlink-setup");
  110. DirectoryScanner ds = new DirectoryScanner();
  111. ds.setBasedir(new File(getProject().getBaseDir(), "tmp"));
  112. ds.setIncludes(new String[] {"alpha/beta/gamma/"});
  113. ds.setFollowSymlinks(false);
  114. ds.scan();
  115. compareFiles(ds, new String[] {}, new String[] {});
  116. }
  117. // father and child pattern test
  118. public void testOrderOfIncludePatternsIrrelevant() {
  119. String [] expectedFiles = {"alpha/beta/beta.xml",
  120. "alpha/beta/gamma/gamma.xml"};
  121. String [] expectedDirectories = {"alpha/beta", "alpha/beta/gamma" };
  122. DirectoryScanner ds = new DirectoryScanner();
  123. ds.setBasedir(new File(getProject().getBaseDir(), "tmp"));
  124. ds.setIncludes(new String[] {"alpha/be?a/**", "alpha/beta/gamma/"});
  125. ds.scan();
  126. compareFiles(ds, expectedFiles, expectedDirectories);
  127. // redo the test, but the 2 include patterns are inverted
  128. ds = new DirectoryScanner();
  129. ds.setBasedir(new File(getProject().getBaseDir(), "tmp"));
  130. ds.setIncludes(new String[] {"alpha/beta/gamma/", "alpha/be?a/**"});
  131. ds.scan();
  132. compareFiles(ds, expectedFiles, expectedDirectories);
  133. }
  134. public void testPatternsDifferInCaseScanningSensitive() {
  135. DirectoryScanner ds = new DirectoryScanner();
  136. ds.setBasedir(new File(getProject().getBaseDir(), "tmp"));
  137. ds.setIncludes(new String[] {"alpha/", "ALPHA/"});
  138. ds.scan();
  139. compareFiles(ds, new String[] {"alpha/beta/beta.xml",
  140. "alpha/beta/gamma/gamma.xml"},
  141. new String[] {"alpha", "alpha/beta", "alpha/beta/gamma"});
  142. }
  143. public void testPatternsDifferInCaseScanningInsensitive() {
  144. DirectoryScanner ds = new DirectoryScanner();
  145. ds.setBasedir(new File(getProject().getBaseDir(), "tmp"));
  146. ds.setIncludes(new String[] {"alpha/", "ALPHA/"});
  147. ds.setCaseSensitive(false);
  148. ds.scan();
  149. compareFiles(ds, new String[] {"alpha/beta/beta.xml",
  150. "alpha/beta/gamma/gamma.xml"},
  151. new String[] {"alpha", "alpha/beta", "alpha/beta/gamma"});
  152. }
  153. public void testFullpathDiffersInCaseScanningSensitive() {
  154. DirectoryScanner ds = new DirectoryScanner();
  155. ds.setBasedir(new File(getProject().getBaseDir(), "tmp"));
  156. ds.setIncludes(new String[] {
  157. "alpha/beta/gamma/gamma.xml",
  158. "alpha/beta/gamma/GAMMA.XML"
  159. });
  160. ds.scan();
  161. compareFiles(ds, new String[] {"alpha/beta/gamma/gamma.xml"},
  162. new String[] {});
  163. }
  164. public void testFullpathDiffersInCaseScanningInsensitive() {
  165. DirectoryScanner ds = new DirectoryScanner();
  166. ds.setBasedir(new File(getProject().getBaseDir(), "tmp"));
  167. ds.setIncludes(new String[] {
  168. "alpha/beta/gamma/gamma.xml",
  169. "alpha/beta/gamma/GAMMA.XML"
  170. });
  171. ds.setCaseSensitive(false);
  172. ds.scan();
  173. compareFiles(ds, new String[] {"alpha/beta/gamma/gamma.xml"},
  174. new String[] {});
  175. }
  176. public void testParentDiffersInCaseScanningSensitive() {
  177. DirectoryScanner ds = new DirectoryScanner();
  178. ds.setBasedir(new File(getProject().getBaseDir(), "tmp"));
  179. ds.setIncludes(new String[] {"alpha/", "ALPHA/beta/"});
  180. ds.scan();
  181. compareFiles(ds, new String[] {"alpha/beta/beta.xml",
  182. "alpha/beta/gamma/gamma.xml"},
  183. new String[] {"alpha", "alpha/beta", "alpha/beta/gamma"});
  184. }
  185. public void testParentDiffersInCaseScanningInsensitive() {
  186. DirectoryScanner ds = new DirectoryScanner();
  187. ds.setBasedir(new File(getProject().getBaseDir(), "tmp"));
  188. ds.setIncludes(new String[] {"alpha/", "ALPHA/beta/"});
  189. ds.setCaseSensitive(false);
  190. ds.scan();
  191. compareFiles(ds, new String[] {"alpha/beta/beta.xml",
  192. "alpha/beta/gamma/gamma.xml"},
  193. new String[] {"alpha", "alpha/beta", "alpha/beta/gamma"});
  194. }
  195. /**
  196. * Test case for setFollowLinks() and associated funtionality.
  197. * Only supports test on linux, at the moment because Java has
  198. * no real notion of symlinks built in, so an os-specfic call
  199. * to Runtime.exec() must be made to create a link to test against.
  200. */
  201. public void testSetFollowLinks() {
  202. if (supportsSymlinks) {
  203. File linkFile = new File(System.getProperty("root"), "src/main/org/apache/tools/ThisIsALink");
  204. try {
  205. // add conditions and more commands as soon as the need arises
  206. String[] command = new String[] {
  207. "ln", "-s", "ant", linkFile.getAbsolutePath()
  208. };
  209. try {
  210. Runtime.getRuntime().exec(command);
  211. // give ourselves some time for the system call
  212. // to execute... tweak if you have a really over
  213. // loaded system.
  214. Thread.sleep(1000);
  215. } catch (IOException ioe) {
  216. fail("IOException making link "+ioe);
  217. } catch (InterruptedException ie) {
  218. }
  219. File dir = new File(System.getProperty("root"), "src/main/org/apache/tools");
  220. DirectoryScanner ds = new DirectoryScanner();
  221. // followLinks should be true by default, but if this ever
  222. // changes we will need this line.
  223. ds.setFollowSymlinks(true);
  224. ds.setBasedir(dir);
  225. ds.setExcludes(new String[] {"ant/**"});
  226. ds.scan();
  227. boolean haveZipPackage = false;
  228. boolean haveTaskdefsPackage = false;
  229. String[] included = ds.getIncludedDirectories();
  230. for (int i=0; i<included.length; i++) {
  231. if (included[i].equals("zip")) {
  232. haveZipPackage = true;
  233. } else if (included[i].equals("ThisIsALink"
  234. + File.separator
  235. + "taskdefs")) {
  236. haveTaskdefsPackage = true;
  237. }
  238. }
  239. // if we followed the symlink we just made we should
  240. // bypass the excludes.
  241. assertTrue("(1) zip package included", haveZipPackage);
  242. assertTrue("(1) taskdefs package included",
  243. haveTaskdefsPackage);
  244. ds = new DirectoryScanner();
  245. ds.setFollowSymlinks(false);
  246. ds.setBasedir(dir);
  247. ds.setExcludes(new String[] {"ant/**"});
  248. ds.scan();
  249. haveZipPackage = false;
  250. haveTaskdefsPackage = false;
  251. included = ds.getIncludedDirectories();
  252. for (int i=0; i<included.length; i++) {
  253. if (included[i].equals("zip")) {
  254. haveZipPackage = true;
  255. } else if (included[i].equals("ThisIsALink"
  256. + File.separator
  257. + "taskdefs")) {
  258. haveTaskdefsPackage = true;
  259. }
  260. }
  261. assertTrue("(2) zip package included", haveZipPackage);
  262. assertTrue("(2) taskdefs package not included",
  263. !haveTaskdefsPackage);
  264. } finally {
  265. if (!linkFile.delete()) {
  266. throw new RuntimeException("Failed to delete " + linkFile);
  267. }
  268. }
  269. }
  270. }
  271. public void testExcludeOneFile() {
  272. DirectoryScanner ds = new DirectoryScanner();
  273. ds.setBasedir(new File(getProject().getBaseDir(), "tmp"));
  274. ds.setIncludes(new String[] {
  275. "**/*.xml"
  276. });
  277. ds.setExcludes(new String[] {
  278. "alpha/beta/b*xml"
  279. });
  280. ds.scan();
  281. compareFiles(ds, new String[] {"alpha/beta/gamma/gamma.xml"},
  282. new String[] {});
  283. }
  284. public void testExcludeHasPrecedence() {
  285. DirectoryScanner ds = new DirectoryScanner();
  286. ds.setBasedir(new File(getProject().getBaseDir(), "tmp"));
  287. ds.setIncludes(new String[] {
  288. "alpha/**"
  289. });
  290. ds.setExcludes(new String[] {
  291. "alpha/**"
  292. });
  293. ds.scan();
  294. compareFiles(ds, new String[] {},
  295. new String[] {});
  296. }
  297. public void testAlternateIncludeExclude() {
  298. DirectoryScanner ds = new DirectoryScanner();
  299. ds.setBasedir(new File(getProject().getBaseDir(), "tmp"));
  300. ds.setIncludes(new String[] {
  301. "alpha/**",
  302. "alpha/beta/gamma/**"
  303. });
  304. ds.setExcludes(new String[] {
  305. "alpha/beta/**"
  306. });
  307. ds.scan();
  308. compareFiles(ds, new String[] {},
  309. new String[] {"alpha"});
  310. }
  311. public void testAlternateExcludeInclude() {
  312. DirectoryScanner ds = new DirectoryScanner();
  313. ds.setBasedir(new File(getProject().getBaseDir(), "tmp"));
  314. ds.setExcludes(new String[] {
  315. "alpha/**",
  316. "alpha/beta/gamma/**"
  317. });
  318. ds.setIncludes(new String[] {
  319. "alpha/beta/**"
  320. });
  321. ds.scan();
  322. compareFiles(ds, new String[] {},
  323. new String[] {});
  324. }
  325. /**
  326. * Test inspired by Bug#1415.
  327. */
  328. public void testChildrenOfExcludedDirectory() {
  329. getProject().executeTarget("children-of-excluded-dir-setup");
  330. DirectoryScanner ds = new DirectoryScanner();
  331. ds.setBasedir(new File(getProject().getBaseDir(), "tmp"));
  332. ds.setExcludes(new String[] {"alpha/**"});
  333. ds.setFollowSymlinks(false);
  334. ds.scan();
  335. compareFiles(ds, new String[] {"delta/delta.xml"},
  336. new String[] {"", "delta"});
  337. ds = new DirectoryScanner();
  338. ds.setBasedir(new File(getProject().getBaseDir(), "tmp"));
  339. ds.setExcludes(new String[] {"alpha"});
  340. ds.setFollowSymlinks(false);
  341. ds.scan();
  342. compareFiles(ds, new String[] {"alpha/beta/beta.xml",
  343. "alpha/beta/gamma/gamma.xml",
  344. "delta/delta.xml"},
  345. new String[] {"", "alpha/beta", "alpha/beta/gamma", "delta"});
  346. }
  347. private void compareFiles(DirectoryScanner ds, String[] expectedFiles,
  348. String[] expectedDirectories) {
  349. String includedFiles[] = ds.getIncludedFiles();
  350. String includedDirectories[] = ds.getIncludedDirectories();
  351. assertEquals("file present: ", expectedFiles.length,
  352. includedFiles.length);
  353. assertEquals("directories present: ", expectedDirectories.length,
  354. includedDirectories.length);
  355. TreeSet files = new TreeSet();
  356. for (int counter=0; counter < includedFiles.length; counter++) {
  357. files.add(includedFiles[counter].replace(File.separatorChar, '/'));
  358. }
  359. TreeSet directories = new TreeSet();
  360. for (int counter=0; counter < includedDirectories.length; counter++) {
  361. directories.add(includedDirectories[counter]
  362. .replace(File.separatorChar, '/'));
  363. }
  364. String currentfile;
  365. Iterator i = files.iterator();
  366. int counter = 0;
  367. while (i.hasNext()) {
  368. currentfile = (String) i.next();
  369. assertEquals(expectedFiles[counter], currentfile);
  370. counter++;
  371. }
  372. String currentdirectory;
  373. Iterator dirit = directories.iterator();
  374. counter = 0;
  375. while (dirit.hasNext()) {
  376. currentdirectory = (String) dirit.next();
  377. assertEquals(expectedDirectories[counter], currentdirectory);
  378. counter++;
  379. }
  380. }
  381. }