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.

FileUtilsTest.java 19 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  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.util;
  18. import java.io.*;
  19. import junit.framework.TestCase;
  20. import org.apache.tools.ant.BuildException;
  21. import org.apache.tools.ant.taskdefs.condition.Os;
  22. /**
  23. * Tests for org.apache.tools.ant.util.FileUtils.
  24. *
  25. */
  26. public class FileUtilsTest extends TestCase {
  27. private FileUtils fu;
  28. private File removeThis;
  29. private String root;
  30. public FileUtilsTest(String name) {
  31. super(name);
  32. }
  33. public void setUp() {
  34. fu = FileUtils.newFileUtils();
  35. // Windows adds the drive letter in uppercase, unless you run Cygwin
  36. root = new File(File.separator).getAbsolutePath().toUpperCase();
  37. }
  38. public void tearDown() {
  39. if (removeThis != null && removeThis.exists()) {
  40. removeThis.delete();
  41. }
  42. }
  43. /**
  44. * test modification.
  45. * Since Ant1.7, the method being tested no longer uses
  46. * reflection to provide backwards support to Java1.1, so this
  47. * test is not so critical. But it does explore file system
  48. * behaviour and will help catch any regression in Java itself,
  49. * so is worth retaining.
  50. * @see FileUtils#setFileLastModified(java.io.File, long)
  51. * @throws IOException
  52. */
  53. public void testSetLastModified() throws IOException {
  54. removeThis = new File("dummy");
  55. FileOutputStream fos = new FileOutputStream(removeThis);
  56. fos.write(new byte[0]);
  57. fos.close();
  58. long modTime = removeThis.lastModified();
  59. assertTrue(modTime != 0);
  60. /*
  61. * Sleep for some time to make sure a touched file would get a
  62. * more recent timestamp according to the file system's
  63. * granularity (should be > 2s to account for Windows FAT).
  64. */
  65. try {
  66. Thread.sleep(5000);
  67. } catch (InterruptedException ie) {
  68. fail(ie.getMessage());
  69. }
  70. fu.setFileLastModified(removeThis, -1);
  71. long secondModTime = removeThis.lastModified();
  72. assertTrue(secondModTime > modTime);
  73. //check that the isUpToDate logic works
  74. assertFalse(fu.isUpToDate(modTime,secondModTime));
  75. // number of milliseconds in a day
  76. final int millisperday=24 * 3600 * 1000;
  77. // in a previous version, the date of the file was set to 123456
  78. // milliseconds since 01.01.1970
  79. // it did not work on a computer running JDK 1.4.1_02 + Windows 2000
  80. fu.setFileLastModified(removeThis, secondModTime + millisperday);
  81. long thirdModTime = removeThis.lastModified();
  82. /*
  83. * I would love to compare this with 123456, but depending on
  84. * the filesystems granularity it can take an arbitrary value.
  85. *
  86. * Just assert the time has changed.
  87. */
  88. assertTrue(thirdModTime != secondModTime);
  89. }
  90. public void testResolveFile() {
  91. /*
  92. * Start with simple absolute file names.
  93. */
  94. assertEquals(File.separator,
  95. fu.resolveFile(null, "/").getPath());
  96. assertEquals(File.separator,
  97. fu.resolveFile(null, "\\").getPath());
  98. /*
  99. * throw in drive letters
  100. */
  101. String driveSpec = "C:";
  102. assertEquals(driveSpec + "\\",
  103. fu.resolveFile(null, driveSpec + "/").getPath());
  104. assertEquals(driveSpec + "\\",
  105. fu.resolveFile(null, driveSpec + "\\").getPath());
  106. String driveSpecLower = "c:";
  107. assertEquals(driveSpec + "\\",
  108. fu.resolveFile(null, driveSpecLower + "/").getPath());
  109. assertEquals(driveSpec + "\\",
  110. fu.resolveFile(null, driveSpecLower + "\\").getPath());
  111. /*
  112. * promised to eliminate consecutive slashes after drive letter.
  113. */
  114. assertEquals(driveSpec + "\\",
  115. fu.resolveFile(null, driveSpec + "/////").getPath());
  116. assertEquals(driveSpec + "\\",
  117. fu.resolveFile(null, driveSpec + "\\\\\\\\\\\\").getPath());
  118. if (Os.isFamily("netware")) {
  119. /*
  120. * throw in NetWare volume names
  121. */
  122. driveSpec = "SYS:";
  123. assertEquals(driveSpec,
  124. fu.resolveFile(null, driveSpec + "/").getPath());
  125. assertEquals(driveSpec,
  126. fu.resolveFile(null, driveSpec + "\\").getPath());
  127. driveSpecLower = "sys:";
  128. assertEquals(driveSpec,
  129. fu.resolveFile(null, driveSpecLower + "/").getPath());
  130. assertEquals(driveSpec,
  131. fu.resolveFile(null, driveSpecLower + "\\").getPath());
  132. /*
  133. * promised to eliminate consecutive slashes after drive letter.
  134. */
  135. assertEquals(driveSpec,
  136. fu.resolveFile(null, driveSpec + "/////").getPath());
  137. assertEquals(driveSpec,
  138. fu.resolveFile(null, driveSpec + "\\\\\\\\\\\\").getPath());
  139. }
  140. /*
  141. * Now test some relative file name magic.
  142. */
  143. assertEquals(localize("/1/2/3/4"),
  144. fu.resolveFile(new File(localize("/1/2/3")), "4").getPath());
  145. assertEquals(localize("/1/2/3/4"),
  146. fu.resolveFile(new File(localize("/1/2/3")), "./4").getPath());
  147. assertEquals(localize("/1/2/3/4"),
  148. fu.resolveFile(new File(localize("/1/2/3")), ".\\4").getPath());
  149. assertEquals(localize("/1/2/3/4"),
  150. fu.resolveFile(new File(localize("/1/2/3")), "./.\\4").getPath());
  151. assertEquals(localize("/1/2/3/4"),
  152. fu.resolveFile(new File(localize("/1/2/3")), "../3/4").getPath());
  153. assertEquals(localize("/1/2/3/4"),
  154. fu.resolveFile(new File(localize("/1/2/3")), "..\\3\\4").getPath());
  155. assertEquals(localize("/1/2/3/4"),
  156. fu.resolveFile(new File(localize("/1/2/3")), "../../5/.././2/./3/6/../4").getPath());
  157. assertEquals(localize("/1/2/3/4"),
  158. fu.resolveFile(new File(localize("/1/2/3")), "..\\../5/..\\./2/./3/6\\../4").getPath());
  159. try {
  160. fu.resolveFile(new File(localize("/1")), "../../b");
  161. fail("successfully crawled beyond the filesystem root");
  162. } catch (BuildException e) {
  163. // Expected Exception caught
  164. }
  165. }
  166. public void testNormalize() {
  167. /*
  168. * Start with simple absolute file names.
  169. */
  170. assertEquals(File.separator,
  171. fu.normalize("/").getPath());
  172. assertEquals(File.separator,
  173. fu.normalize("\\").getPath());
  174. /*
  175. * throw in drive letters
  176. */
  177. String driveSpec = "C:";
  178. assertEquals(driveSpec,
  179. fu.normalize(driveSpec).getPath());
  180. assertEquals(driveSpec + "\\",
  181. fu.normalize(driveSpec + "/").getPath());
  182. assertEquals(driveSpec + "\\",
  183. fu.normalize(driveSpec + "\\").getPath());
  184. String driveSpecLower = "c:";
  185. assertEquals(driveSpec + "\\",
  186. fu.normalize(driveSpecLower + "/").getPath());
  187. assertEquals(driveSpec + "\\",
  188. fu.normalize(driveSpecLower + "\\").getPath());
  189. /*
  190. * promised to eliminate consecutive slashes after drive letter.
  191. */
  192. assertEquals(driveSpec + "\\",
  193. fu.normalize(driveSpec + "/////").getPath());
  194. assertEquals(driveSpec + "\\",
  195. fu.normalize(driveSpec + "\\\\\\\\\\\\").getPath());
  196. if (Os.isFamily("netware")) {
  197. /*
  198. * throw in NetWare volume names
  199. */
  200. driveSpec = "SYS:";
  201. assertEquals(driveSpec,
  202. fu.normalize(driveSpec).getPath());
  203. assertEquals(driveSpec,
  204. fu.normalize(driveSpec + "/").getPath());
  205. assertEquals(driveSpec,
  206. fu.normalize(driveSpec + "\\").getPath());
  207. driveSpecLower = "sys:";
  208. assertEquals(driveSpec,
  209. fu.normalize(driveSpecLower).getPath());
  210. assertEquals(driveSpec,
  211. fu.normalize(driveSpecLower + "/").getPath());
  212. assertEquals(driveSpec,
  213. fu.normalize(driveSpecLower + "\\").getPath());
  214. assertEquals(driveSpec + "\\junk",
  215. fu.normalize(driveSpecLower + "\\junk").getPath());
  216. /*
  217. * promised to eliminate consecutive slashes after drive letter.
  218. */
  219. assertEquals(driveSpec,
  220. fu.normalize(driveSpec + "/////").getPath());
  221. assertEquals(driveSpec,
  222. fu.normalize(driveSpec + "\\\\\\\\\\\\").getPath());
  223. }
  224. /*
  225. * Now test some relative file name magic.
  226. */
  227. assertEquals(localize("/1/2/3/4"),
  228. fu.normalize(localize("/1/2/3/4")).getPath());
  229. assertEquals(localize("/1/2/3/4"),
  230. fu.normalize(localize("/1/2/3/./4")).getPath());
  231. assertEquals(localize("/1/2/3/4"),
  232. fu.normalize(localize("/1/2/3/.\\4")).getPath());
  233. assertEquals(localize("/1/2/3/4"),
  234. fu.normalize(localize("/1/2/3/./.\\4")).getPath());
  235. assertEquals(localize("/1/2/3/4"),
  236. fu.normalize(localize("/1/2/3/../3/4")).getPath());
  237. assertEquals(localize("/1/2/3/4"),
  238. fu.normalize(localize("/1/2/3/..\\3\\4")).getPath());
  239. assertEquals(localize("/1/2/3/4"),
  240. fu.normalize(localize("/1/2/3/../../5/.././2/./3/6/../4")).getPath());
  241. assertEquals(localize("/1/2/3/4"),
  242. fu.normalize(localize("/1/2/3/..\\../5/..\\./2/./3/6\\../4")).getPath());
  243. try {
  244. fu.normalize("foo");
  245. fail("foo is not an absolute path");
  246. } catch (BuildException e) {
  247. // Expected exception caught
  248. }
  249. try {
  250. fu.normalize(localize("/1/../../b"));
  251. fail("successfully crawled beyond the filesystem root");
  252. } catch (BuildException e) {
  253. // Expected exception caught
  254. }
  255. }
  256. /**
  257. * Test handling of null arguments.
  258. */
  259. public void testNullArgs() {
  260. try {
  261. fu.normalize(null);
  262. fail("successfully normalized a null-file");
  263. } catch (NullPointerException npe) {
  264. // Expected exception caught
  265. }
  266. File f = fu.resolveFile(null, "a");
  267. assertEquals(f, new File("a"));
  268. }
  269. /**
  270. * Test createTempFile
  271. */
  272. public void testCreateTempFile() {
  273. File parent = new File((new File("/tmp")).getAbsolutePath());
  274. File tmp1 = fu.createTempFile("pre", ".suf", parent);
  275. assertTrue("new file", !tmp1.exists());
  276. String name = tmp1.getName();
  277. assertTrue("starts with pre", name.startsWith("pre"));
  278. assertTrue("ends with .suf", name.endsWith(".suf"));
  279. assertEquals("is inside parent dir",
  280. parent.getAbsolutePath(),
  281. tmp1.getParent());
  282. File tmp2 = fu.createTempFile("pre", ".suf", parent);
  283. assertTrue("files are different",
  284. !tmp1.getAbsolutePath().equals(tmp2.getAbsolutePath()));
  285. // null parent dir
  286. File tmp3 = fu.createTempFile("pre", ".suf", null);
  287. String tmploc = System.getProperty("java.io.tmpdir");
  288. assertEquals((new File(tmploc, tmp3.getName())).getAbsolutePath(),
  289. tmp3.getAbsolutePath());
  290. }
  291. /**
  292. * Test contentEquals
  293. */
  294. public void testContentEquals() throws IOException {
  295. assertTrue("Non existing files", fu.contentEquals(new File("foo"),
  296. new File("bar")));
  297. assertTrue("One exists, the other one doesn\'t",
  298. !fu.contentEquals(new File("foo"), new File("build.xml")));
  299. assertTrue("Don\'t compare directories",
  300. !fu.contentEquals(new File("src"), new File("src")));
  301. assertTrue("File equals itself",
  302. fu.contentEquals(new File("build.xml"),
  303. new File("build.xml")));
  304. assertTrue("Files are different",
  305. !fu.contentEquals(new File("build.xml"),
  306. new File("docs.xml")));
  307. }
  308. /**
  309. * Test createNewFile
  310. */
  311. public void testCreateNewFile() throws IOException {
  312. removeThis = new File("dummy");
  313. assertTrue(!removeThis.exists());
  314. fu.createNewFile(removeThis);
  315. assertTrue(removeThis.exists());
  316. }
  317. /**
  318. * Test removeLeadingPath.
  319. */
  320. public void testRemoveLeadingPath() {
  321. assertEquals("bar", fu.removeLeadingPath(new File("/foo"),
  322. new File("/foo/bar")));
  323. assertEquals("bar", fu.removeLeadingPath(new File("/foo/"),
  324. new File("/foo/bar")));
  325. assertEquals("bar", fu.removeLeadingPath(new File("\\foo"),
  326. new File("\\foo\\bar")));
  327. assertEquals("bar", fu.removeLeadingPath(new File("\\foo\\"),
  328. new File("\\foo\\bar")));
  329. assertEquals("bar", fu.removeLeadingPath(new File("c:/foo"),
  330. new File("c:/foo/bar")));
  331. assertEquals("bar", fu.removeLeadingPath(new File("c:/foo/"),
  332. new File("c:/foo/bar")));
  333. assertEquals("bar", fu.removeLeadingPath(new File("c:\\foo"),
  334. new File("c:\\foo\\bar")));
  335. assertEquals("bar", fu.removeLeadingPath(new File("c:\\foo\\"),
  336. new File("c:\\foo\\bar")));
  337. assertEqualsIgnoreDriveCase(fu.normalize("/bar").getAbsolutePath(),
  338. fu.removeLeadingPath(new File("/foo"), new File("/bar")));
  339. assertEqualsIgnoreDriveCase(fu.normalize("/foobar").getAbsolutePath(),
  340. fu.removeLeadingPath(new File("/foo"), new File("/foobar")));
  341. // bugzilla report 19979
  342. assertEquals("", fu.removeLeadingPath(new File("/foo/bar"),
  343. new File("/foo/bar")));
  344. assertEquals("", fu.removeLeadingPath(new File("/foo/bar"),
  345. new File("/foo/bar/")));
  346. assertEquals("", fu.removeLeadingPath(new File("/foo/bar/"),
  347. new File("/foo/bar/")));
  348. assertEquals("", fu.removeLeadingPath(new File("/foo/bar/"),
  349. new File("/foo/bar")));
  350. String expected = "foo/bar".replace('\\', File.separatorChar)
  351. .replace('/', File.separatorChar);
  352. assertEquals(expected, fu.removeLeadingPath(new File("/"),
  353. new File("/foo/bar")));
  354. assertEquals(expected, fu.removeLeadingPath(new File("c:/"),
  355. new File("c:/foo/bar")));
  356. assertEquals(expected, fu.removeLeadingPath(new File("c:\\"),
  357. new File("c:\\foo\\bar")));
  358. }
  359. /**
  360. * test toUri
  361. */
  362. public void testToURI() {
  363. String dosRoot = null;
  364. if (Os.isFamily("dos")) {
  365. dosRoot = System.getProperty("user.dir").charAt(0) + ":/";
  366. }
  367. else
  368. {
  369. dosRoot = "";
  370. }
  371. if (Os.isFamily("dos")) {
  372. assertEquals("file:///C:/foo", fu.toURI("c:\\foo"));
  373. }
  374. if (Os.isFamily("netware")) {
  375. assertEquals("file:///SYS:/foo", fu.toURI("sys:\\foo"));
  376. }
  377. assertEquals("file:///" + dosRoot + "foo", fu.toURI("/foo"));
  378. assertEquals("file:./foo", fu.toURI("./foo"));
  379. assertEquals("file:///" + dosRoot + "foo", fu.toURI("\\foo"));
  380. assertEquals("file:./foo", fu.toURI(".\\foo"));
  381. assertEquals("file:///" + dosRoot + "foo%20bar", fu.toURI("/foo bar"));
  382. assertEquals("file:///" + dosRoot + "foo%20bar", fu.toURI("\\foo bar"));
  383. assertEquals("file:///" + dosRoot + "foo%23bar", fu.toURI("/foo#bar"));
  384. assertEquals("file:///" + dosRoot + "foo%23bar", fu.toURI("\\foo#bar"));
  385. }
  386. /**
  387. * test fromUri
  388. */
  389. public void testFromURI() {
  390. if (Os.isFamily("netware")) {
  391. assertEqualsIgnoreDriveCase("SYS:\\foo", fu.fromURI("file:///sys:/foo"));
  392. }
  393. if (Os.isFamily("dos")) {
  394. assertEqualsIgnoreDriveCase("C:\\foo", fu.fromURI("file:///c:/foo"));
  395. }
  396. assertEqualsIgnoreDriveCase(localize("/foo"), fu.fromURI("file:///foo"));
  397. assertEquals("." + File.separator + "foo",
  398. fu.fromURI("file:./foo"));
  399. assertEqualsIgnoreDriveCase(localize("/foo bar"), fu.fromURI("file:///foo%20bar"));
  400. assertEqualsIgnoreDriveCase(localize("/foo#bar"), fu.fromURI("file:///foo%23bar"));
  401. }
  402. /**
  403. * adapt file separators to local conventions
  404. */
  405. private String localize(String path) {
  406. path = root + path.substring(1);
  407. return path.replace('\\', File.separatorChar).replace('/', File.separatorChar);
  408. }
  409. /**
  410. * convenience method
  411. * normalize brings the drive in uppercase
  412. * the drive letter is in lower case under cygwin
  413. * calling this method allows tests where normalize is called to pass under cygwin
  414. */
  415. private void assertEqualsIgnoreDriveCase(String s1, String s2) {
  416. if (Os.isFamily("dos") && s1.length()>=1 && s2.length()>=1) {
  417. StringBuffer sb1= new StringBuffer(s1);
  418. StringBuffer sb2= new StringBuffer(s2);
  419. sb1.setCharAt(0,Character.toUpperCase(s1.charAt(0)));
  420. sb2.setCharAt(0,Character.toUpperCase(s2.charAt(0)));
  421. assertEquals(sb1.toString(),sb2.toString());
  422. } else {
  423. assertEquals(s1,s2);
  424. }
  425. }
  426. }