KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > util > FileUtilsTest


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.util;
19
20 import java.io.File JavaDoc;
21 import java.io.FileOutputStream JavaDoc;
22 import java.io.IOException JavaDoc;
23
24 import junit.framework.TestCase;
25
26 import org.apache.tools.ant.BuildException;
27 import org.apache.tools.ant.taskdefs.condition.Os;
28
29 /**
30  * Tests for org.apache.tools.ant.util.FileUtils.
31  *
32  */

33 public class FileUtilsTest extends TestCase {
34
35     private FileUtils fu;
36     private File JavaDoc removeThis;
37     private String JavaDoc root;
38
39     public FileUtilsTest(String JavaDoc name) {
40         super(name);
41     }
42
43     public void setUp() {
44         fu = FileUtils.newFileUtils();
45         // Windows adds the drive letter in uppercase, unless you run Cygwin
46
root = new File JavaDoc(File.separator).getAbsolutePath().toUpperCase();
47     }
48
49     public void tearDown() {
50         if (removeThis != null && removeThis.exists()) {
51             removeThis.delete();
52         }
53     }
54
55     public void testSetLastModified() throws IOException JavaDoc {
56         removeThis = new File JavaDoc("dummy");
57         FileOutputStream JavaDoc fos = new FileOutputStream JavaDoc(removeThis);
58         fos.write(new byte[0]);
59         fos.close();
60         long modTime = removeThis.lastModified();
61         assertTrue(modTime != 0);
62
63         /*
64          * Sleep for some time to make sure a touched file would get a
65          * more recent timestamp according to the file system's
66          * granularity (should be > 2s to account for Windows FAT).
67          */

68         try {
69             Thread.sleep(5000);
70         } catch (InterruptedException JavaDoc ie) {
71             fail(ie.getMessage());
72         }
73
74         fu.setFileLastModified(removeThis, -1);
75         long secondModTime = removeThis.lastModified();
76         assertTrue(secondModTime > modTime);
77
78         // number of milliseconds in a day
79
final int millisperday=24 * 3600 * 1000;
80         // in a previous version, the date of the file was set to 123456
81
// milliseconds since 01.01.1970
82
// it did not work on a computer running JDK 1.4.1_02 + Windows 2000
83
fu.setFileLastModified(removeThis, secondModTime + millisperday);
84         long thirdModTime = removeThis.lastModified();
85         /*
86          * I would love to compare this with 123456, but depending on
87          * the filesystems granularity it can take an arbitrary value.
88          *
89          * Just assert the time has changed.
90          */

91         assertTrue(thirdModTime != secondModTime);
92     }
93
94     public void testResolveFile() {
95         /*
96          * Start with simple absolute file names.
97          */

98         assertEquals(File.separator,
99                      fu.resolveFile(null, "/").getPath());
100         assertEquals(File.separator,
101                      fu.resolveFile(null, "\\").getPath());
102
103         if (!Os.isFamily("unix")) {
104         /*
105          * throw in drive letters
106          */

107         String JavaDoc driveSpec = "C:";
108         assertEquals(driveSpec + "\\",
109                      fu.resolveFile(null, driveSpec + "/").getPath());
110         assertEquals(driveSpec + "\\",
111                      fu.resolveFile(null, driveSpec + "\\").getPath());
112         String JavaDoc driveSpecLower = "c:";
113         assertEquals(driveSpec + "\\",
114                      fu.resolveFile(null, driveSpecLower + "/").getPath());
115         assertEquals(driveSpec + "\\",
116                      fu.resolveFile(null, driveSpecLower + "\\").getPath());
117         /*
118          * promised to eliminate consecutive slashes after drive letter.
119          */

120         assertEquals(driveSpec + "\\",
121                      fu.resolveFile(null, driveSpec + "/////").getPath());
122         assertEquals(driveSpec + "\\",
123                      fu.resolveFile(null, driveSpec + "\\\\\\\\\\\\").getPath());
124         }
125
126         if (Os.isFamily("netware")) {
127             /*
128              * throw in NetWare volume names
129              */

130             String JavaDoc driveSpec = "SYS:";
131             assertEquals(driveSpec,
132                          fu.resolveFile(null, driveSpec + "/").getPath());
133             assertEquals(driveSpec,
134                          fu.resolveFile(null, driveSpec + "\\").getPath());
135             String JavaDoc driveSpecLower = "sys:";
136             assertEquals(driveSpec,
137                          fu.resolveFile(null, driveSpecLower + "/").getPath());
138             assertEquals(driveSpec,
139                          fu.resolveFile(null, driveSpecLower + "\\").getPath());
140             /*
141              * promised to eliminate consecutive slashes after drive letter.
142              */

143             assertEquals(driveSpec,
144                          fu.resolveFile(null, driveSpec + "/////").getPath());
145             assertEquals(driveSpec,
146                          fu.resolveFile(null, driveSpec + "\\\\\\\\\\\\").getPath());
147         }
148
149         /*
150          * Now test some relative file name magic.
151          */

152         assertEquals(localize("/1/2/3/4"),
153                      fu.resolveFile(new File JavaDoc(localize("/1/2/3")), "4").getPath());
154         assertEquals(localize("/1/2/3/4"),
155                      fu.resolveFile(new File JavaDoc(localize("/1/2/3")), "./4").getPath());
156         assertEquals(localize("/1/2/3/4"),
157                      fu.resolveFile(new File JavaDoc(localize("/1/2/3")), ".\\4").getPath());
158         assertEquals(localize("/1/2/3/4"),
159                      fu.resolveFile(new File JavaDoc(localize("/1/2/3")), "./.\\4").getPath());
160         assertEquals(localize("/1/2/3/4"),
161                      fu.resolveFile(new File JavaDoc(localize("/1/2/3")), "../3/4").getPath());
162         assertEquals(localize("/1/2/3/4"),
163                      fu.resolveFile(new File JavaDoc(localize("/1/2/3")), "..\\3\\4").getPath());
164         assertEquals(localize("/1/2/3/4"),
165                      fu.resolveFile(new File JavaDoc(localize("/1/2/3")), "../../5/.././2/./3/6/../4").getPath());
166         assertEquals(localize("/1/2/3/4"),
167                      fu.resolveFile(new File JavaDoc(localize("/1/2/3")), "..\\../5/..\\./2/./3/6\\../4").getPath());
168
169         try {
170             fu.resolveFile(new File JavaDoc(localize("/1")), "../../b");
171             fail("successfully crawled beyond the filesystem root");
172         } catch (BuildException e) {
173             // Expected Exception caught
174
}
175
176     }
177
178     public void testNormalize() {
179         /*
180          * Start with simple absolute file names.
181          */

182         assertEquals(File.separator,
183                      fu.normalize("/").getPath());
184         assertEquals(File.separator,
185                      fu.normalize("\\").getPath());
186
187         if (!Os.isFamily("unix")) {
188         /*
189          * throw in drive letters
190          */

191         String JavaDoc driveSpec = "C:";
192         assertEquals(driveSpec,
193                      fu.normalize(driveSpec).getPath());
194         assertEquals(driveSpec + "\\",
195                      fu.normalize(driveSpec + "/").getPath());
196         assertEquals(driveSpec + "\\",
197                      fu.normalize(driveSpec + "\\").getPath());
198         String JavaDoc driveSpecLower = "c:";
199         assertEquals(driveSpec + "\\",
200                      fu.normalize(driveSpecLower + "/").getPath());
201         assertEquals(driveSpec + "\\",
202                      fu.normalize(driveSpecLower + "\\").getPath());
203         /*
204          * promised to eliminate consecutive slashes after drive letter.
205          */

206         assertEquals(driveSpec + "\\",
207                      fu.normalize(driveSpec + "/////").getPath());
208         assertEquals(driveSpec + "\\",
209                      fu.normalize(driveSpec + "\\\\\\\\\\\\").getPath());
210         }
211         if (Os.isFamily("netware")) {
212             /*
213              * throw in NetWare volume names
214              */

215             String JavaDoc driveSpec = "SYS:";
216             assertEquals(driveSpec,
217                          fu.normalize(driveSpec).getPath());
218             assertEquals(driveSpec,
219                          fu.normalize(driveSpec + "/").getPath());
220             assertEquals(driveSpec,
221                          fu.normalize(driveSpec + "\\").getPath());
222             String JavaDoc driveSpecLower = "sys:";
223             assertEquals(driveSpec,
224                          fu.normalize(driveSpecLower).getPath());
225             assertEquals(driveSpec,
226                          fu.normalize(driveSpecLower + "/").getPath());
227             assertEquals(driveSpec,
228                          fu.normalize(driveSpecLower + "\\").getPath());
229             assertEquals(driveSpec + "\\junk",
230                          fu.normalize(driveSpecLower + "\\junk").getPath());
231             /*
232              * promised to eliminate consecutive slashes after drive letter.
233              */

234             assertEquals(driveSpec,
235                          fu.normalize(driveSpec + "/////").getPath());
236             assertEquals(driveSpec,
237                          fu.normalize(driveSpec + "\\\\\\\\\\\\").getPath());
238         }
239
240         /*
241          * Now test some relative file name magic.
242          */

243         assertEquals(localize("/1/2/3/4"),
244                      fu.normalize(localize("/1/2/3/4")).getPath());
245         assertEquals(localize("/1/2/3/4"),
246                      fu.normalize(localize("/1/2/3/./4")).getPath());
247         assertEquals(localize("/1/2/3/4"),
248                      fu.normalize(localize("/1/2/3/.\\4")).getPath());
249         assertEquals(localize("/1/2/3/4"),
250                      fu.normalize(localize("/1/2/3/./.\\4")).getPath());
251         assertEquals(localize("/1/2/3/4"),
252                      fu.normalize(localize("/1/2/3/../3/4")).getPath());
253         assertEquals(localize("/1/2/3/4"),
254                      fu.normalize(localize("/1/2/3/..\\3\\4")).getPath());
255         assertEquals(localize("/1/2/3/4"),
256                      fu.normalize(localize("/1/2/3/../../5/.././2/./3/6/../4")).getPath());
257         assertEquals(localize("/1/2/3/4"),
258                      fu.normalize(localize("/1/2/3/..\\../5/..\\./2/./3/6\\../4")).getPath());
259
260         try {
261             fu.normalize("foo");
262             fail("foo is not an absolute path");
263         } catch (BuildException e) {
264             // Expected exception caught
265
}
266
267         try {
268             fu.normalize(localize("/1/../../b"));
269             fail("successfully crawled beyond the filesystem root");
270         } catch (BuildException e) {
271             // Expected exception caught
272
}
273     }
274
275     /**
276      * Test handling of null arguments.
277      */

278     public void testNullArgs() {
279         try {
280             fu.normalize(null);
281             fail("successfully normalized a null-file");
282         } catch (NullPointerException JavaDoc npe) {
283             // Expected exception caught
284
}
285
286         File JavaDoc f = fu.resolveFile(null, "a");
287         assertEquals(f, new File JavaDoc("a"));
288     }
289
290     /**
291      * Test createTempFile
292      */

293     public void testCreateTempFile() {
294         File JavaDoc parent = new File JavaDoc((new File JavaDoc("/tmp")).getAbsolutePath());
295         File JavaDoc tmp1 = fu.createTempFile("pre", ".suf", parent);
296         assertTrue("new file", !tmp1.exists());
297
298         String JavaDoc name = tmp1.getName();
299         assertTrue("starts with pre", name.startsWith("pre"));
300         assertTrue("ends with .suf", name.endsWith(".suf"));
301         assertEquals("is inside parent dir",
302                      parent.getAbsolutePath(),
303                      tmp1.getParent());
304
305         File JavaDoc tmp2 = fu.createTempFile("pre", ".suf", parent);
306         assertTrue("files are different",
307                    !tmp1.getAbsolutePath().equals(tmp2.getAbsolutePath()));
308
309         // null parent dir
310
File JavaDoc tmp3 = fu.createTempFile("pre", ".suf", null);
311         String JavaDoc tmploc = System.getProperty("java.io.tmpdir");
312         assertEquals((new File JavaDoc(tmploc, tmp3.getName())).getAbsolutePath(),
313                      tmp3.getAbsolutePath());
314     }
315
316     /**
317      * Test contentEquals
318      */

319     public void testContentEquals() throws IOException JavaDoc {
320         assertTrue("Non existing files", fu.contentEquals(new File JavaDoc("foo"),
321                                                           new File JavaDoc("bar")));
322         assertTrue("One exists, the other one doesn\'t",
323                    !fu.contentEquals(new File JavaDoc("foo"), new File JavaDoc("build.xml")));
324         assertTrue("Don\'t compare directories",
325                    !fu.contentEquals(new File JavaDoc("src"), new File JavaDoc("src")));
326         assertTrue("File equals itself",
327                    fu.contentEquals(new File JavaDoc("build.xml"),
328                                     new File JavaDoc("build.xml")));
329         assertTrue("Files are different",
330                    !fu.contentEquals(new File JavaDoc("build.xml"),
331                                      new File JavaDoc("docs.xml")));
332     }
333
334     /**
335      * Test createNewFile
336      */

337     public void testCreateNewFile() throws IOException JavaDoc {
338         removeThis = new File JavaDoc("dummy");
339         assertTrue(!removeThis.exists());
340         fu.createNewFile(removeThis);
341         assertTrue(removeThis.exists());
342     }
343
344     /**
345      * Test removeLeadingPath.
346      */

347     public void testRemoveLeadingPath() {
348         assertEquals("bar", fu.removeLeadingPath(new File JavaDoc("/foo"),
349                                                  new File JavaDoc("/foo/bar")));
350         assertEquals("bar", fu.removeLeadingPath(new File JavaDoc("/foo/"),
351                                                  new File JavaDoc("/foo/bar")));
352         assertEquals("bar", fu.removeLeadingPath(new File JavaDoc("\\foo"),
353                                                  new File JavaDoc("\\foo\\bar")));
354         assertEquals("bar", fu.removeLeadingPath(new File JavaDoc("\\foo\\"),
355                                                  new File JavaDoc("\\foo\\bar")));
356         assertEquals("bar", fu.removeLeadingPath(new File JavaDoc("c:/foo"),
357                                                  new File JavaDoc("c:/foo/bar")));
358         assertEquals("bar", fu.removeLeadingPath(new File JavaDoc("c:/foo/"),
359                                                  new File JavaDoc("c:/foo/bar")));
360         assertEquals("bar", fu.removeLeadingPath(new File JavaDoc("c:\\foo"),
361                                                  new File JavaDoc("c:\\foo\\bar")));
362         assertEquals("bar", fu.removeLeadingPath(new File JavaDoc("c:\\foo\\"),
363                                                  new File JavaDoc("c:\\foo\\bar")));
364         assertEqualsIgnoreDriveCase(fu.normalize("/bar").getAbsolutePath(),
365                      fu.removeLeadingPath(new File JavaDoc("/foo"), new File JavaDoc("/bar")));
366         assertEqualsIgnoreDriveCase(fu.normalize("/foobar").getAbsolutePath(),
367                      fu.removeLeadingPath(new File JavaDoc("/foo"), new File JavaDoc("/foobar")));
368         // bugzilla report 19979
369
assertEquals("", fu.removeLeadingPath(new File JavaDoc("/foo/bar"),
370                                               new File JavaDoc("/foo/bar")));
371         assertEquals("", fu.removeLeadingPath(new File JavaDoc("/foo/bar"),
372                                               new File JavaDoc("/foo/bar/")));
373         assertEquals("", fu.removeLeadingPath(new File JavaDoc("/foo/bar/"),
374                                               new File JavaDoc("/foo/bar/")));
375         assertEquals("", fu.removeLeadingPath(new File JavaDoc("/foo/bar/"),
376                                               new File JavaDoc("/foo/bar")));
377
378         String JavaDoc expected = "foo/bar".replace('\\', File.separatorChar)
379             .replace('/', File.separatorChar);
380         assertEquals(expected, fu.removeLeadingPath(new File JavaDoc("/"),
381                                                     new File JavaDoc("/foo/bar")));
382         assertEquals(expected, fu.removeLeadingPath(new File JavaDoc("c:/"),
383                                                     new File JavaDoc("c:/foo/bar")));
384         assertEquals(expected, fu.removeLeadingPath(new File JavaDoc("c:\\"),
385                                                     new File JavaDoc("c:\\foo\\bar")));
386     }
387
388     /**
389      * test toUri
390      */

391     public void testToURI() {
392         String JavaDoc dosRoot = null;
393         if (Os.isFamily("dos")) {
394             dosRoot = System.getProperty("user.dir").charAt(0) + ":/";
395         }
396         else
397         {
398             dosRoot = "";
399         }
400         if (Os.isFamily("dos")) {
401             assertEquals("file:///C:/foo", fu.toURI("c:\\foo"));
402         }
403         if (Os.isFamily("netware")) {
404             assertEquals("file:///SYS:/foo", fu.toURI("sys:\\foo"));
405         }
406         assertEquals("file:///" + dosRoot + "foo", fu.toURI("/foo"));
407         assertEquals("file:./foo", fu.toURI("./foo"));
408         assertEquals("file:///" + dosRoot + "foo", fu.toURI("\\foo"));
409         assertEquals("file:./foo", fu.toURI(".\\foo"));
410         assertEquals("file:///" + dosRoot + "foo%20bar", fu.toURI("/foo bar"));
411         assertEquals("file:///" + dosRoot + "foo%20bar", fu.toURI("\\foo bar"));
412         assertEquals("file:///" + dosRoot + "foo%23bar", fu.toURI("/foo#bar"));
413         assertEquals("file:///" + dosRoot + "foo%23bar", fu.toURI("\\foo#bar"));
414     }
415
416     /**
417      * test fromUri
418      */

419     public void testFromURI() {
420         if (Os.isFamily("netware")) {
421             assertEqualsIgnoreDriveCase("SYS:\\foo", fu.fromURI("file:///sys:/foo"));
422         }
423         if (Os.isFamily("dos")) {
424             assertEqualsIgnoreDriveCase("C:\\foo", fu.fromURI("file:///c:/foo"));
425         }
426         assertEqualsIgnoreDriveCase(localize("/foo"), fu.fromURI("file:///foo"));
427         assertEquals("." + File.separator + "foo",
428                      fu.fromURI("file:./foo"));
429         assertEqualsIgnoreDriveCase(localize("/foo bar"), fu.fromURI("file:///foo%20bar"));
430         assertEqualsIgnoreDriveCase(localize("/foo#bar"), fu.fromURI("file:///foo%23bar"));
431     }
432
433     /**
434      * adapt file separators to local conventions
435      */

436     private String JavaDoc localize(String JavaDoc path) {
437         path = root + path.substring(1);
438         return path.replace('\\', File.separatorChar).replace('/', File.separatorChar);
439     }
440     /**
441      * convenience method
442      * normalize brings the drive in uppercase
443      * the drive letter is in lower case under cygwin
444      * calling this method allows tests where normalize is called to pass under cygwin
445      */

446     private void assertEqualsIgnoreDriveCase(String JavaDoc s1, String JavaDoc s2) {
447         if (Os.isFamily("dos") && s1.length()>=1 && s2.length()>=1) {
448             StringBuffer JavaDoc sb1= new StringBuffer JavaDoc(s1);
449             StringBuffer JavaDoc sb2= new StringBuffer JavaDoc(s2);
450             sb1.setCharAt(0,Character.toUpperCase(s1.charAt(0)));
451             sb2.setCharAt(0,Character.toUpperCase(s2.charAt(0)));
452             assertEquals(sb1.toString(),sb2.toString());
453         } else {
454             assertEquals(s1,s2);
455         }
456     }
457 }
458
Popular Tags