Source Code Cross Referenced for FileUtilsTest.java in  » Build » ANT » org » apache » tools » ant » util » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Build » ANT » org.apache.tools.ant.util 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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.util;
020:
021:        import java.io.File;
022:        import java.io.FileOutputStream;
023:        import java.io.IOException;
024:
025:        import junit.framework.TestCase;
026:
027:        import org.apache.tools.ant.BuildException;
028:        import org.apache.tools.ant.taskdefs.condition.Os;
029:
030:        /**
031:         * Tests for org.apache.tools.ant.util.FileUtils.
032:         *
033:         */
034:        public class FileUtilsTest extends TestCase {
035:
036:            private static final FileUtils FILE_UTILS = FileUtils
037:                    .getFileUtils();
038:            private File removeThis;
039:            private String root;
040:
041:            public FileUtilsTest(String name) {
042:                super (name);
043:            }
044:
045:            public void setUp() {
046:                // Windows adds the drive letter in uppercase, unless you run Cygwin
047:                root = new File(File.separator).getAbsolutePath().toUpperCase();
048:            }
049:
050:            public void tearDown() {
051:                if (removeThis != null && removeThis.exists()) {
052:                    removeThis.delete();
053:                }
054:            }
055:
056:            /**
057:             * test modification.
058:             * Since Ant1.7, the method being tested no longer uses
059:             * reflection to provide backwards support to Java1.1, so this
060:             * test is not so critical. But it does explore file system
061:             * behaviour and will help catch any regression in Java itself,
062:             * so is worth retaining.
063:             * @see FileUtils#setFileLastModified(java.io.File, long)
064:             * @throws IOException
065:             */
066:            public void testSetLastModified() throws IOException {
067:                removeThis = new File("dummy");
068:                FileOutputStream fos = new FileOutputStream(removeThis);
069:                fos.write(new byte[0]);
070:                fos.close();
071:                long modTime = removeThis.lastModified();
072:                assertTrue(modTime != 0);
073:
074:                /*
075:                 * Sleep for some time to make sure a touched file would get a
076:                 * more recent timestamp according to the file system's
077:                 * granularity (should be > 2s to account for Windows FAT).
078:                 */
079:                try {
080:                    Thread.sleep(5000);
081:                } catch (InterruptedException ie) {
082:                    fail(ie.getMessage());
083:                }
084:
085:                FILE_UTILS.setFileLastModified(removeThis, -1);
086:                long secondModTime = removeThis.lastModified();
087:                assertTrue(secondModTime > modTime);
088:
089:                // number of milliseconds in a day
090:                final int millisperday = 24 * 3600 * 1000;
091:                // in a previous version, the date of the file was set to 123456
092:                // milliseconds since 01.01.1970
093:                // it did not work on a computer running JDK 1.4.1_02 + Windows 2000
094:                FILE_UTILS.setFileLastModified(removeThis, secondModTime
095:                        + millisperday);
096:                long thirdModTime = removeThis.lastModified();
097:                /*
098:                 * I would love to compare this with 123456, but depending on
099:                 * the filesystems granularity it can take an arbitrary value.
100:                 *
101:                 * Just assert the time has changed.
102:                 */
103:                assertTrue(thirdModTime != secondModTime);
104:            }
105:
106:            public void testResolveFile() {
107:                if (!(Os.isFamily("dos") || Os.isFamily("netware"))) {
108:                    /*
109:                     * Start with simple absolute file names.
110:                     */
111:                    assertEquals(File.separator, FILE_UTILS.resolveFile(null,
112:                            "/").getPath());
113:                    assertEquals(File.separator, FILE_UTILS.resolveFile(null,
114:                            "\\").getPath());
115:                } else {
116:                    assertEqualsIgnoreDriveCase(localize(File.separator),
117:                            FILE_UTILS.resolveFile(null, "/").getPath());
118:                    assertEqualsIgnoreDriveCase(localize(File.separator),
119:                            FILE_UTILS.resolveFile(null, "\\").getPath());
120:                    /*
121:                     * throw in drive letters
122:                     */
123:                    String driveSpec = "C:";
124:                    assertEquals(driveSpec + "\\", FILE_UTILS.resolveFile(null,
125:                            driveSpec + "/").getPath());
126:                    assertEquals(driveSpec + "\\", FILE_UTILS.resolveFile(null,
127:                            driveSpec + "\\").getPath());
128:                    String driveSpecLower = "c:";
129:                    assertEquals(driveSpecLower + "\\", FILE_UTILS.resolveFile(
130:                            null, driveSpecLower + "/").getPath());
131:                    assertEquals(driveSpecLower + "\\", FILE_UTILS.resolveFile(
132:                            null, driveSpecLower + "\\").getPath());
133:                    /*
134:                     * promised to eliminate consecutive slashes after drive letter.
135:                     */
136:                    assertEquals(driveSpec + "\\", FILE_UTILS.resolveFile(null,
137:                            driveSpec + "/////").getPath());
138:                    assertEquals(driveSpec + "\\", FILE_UTILS.resolveFile(null,
139:                            driveSpec + "\\\\\\\\\\\\").getPath());
140:                }
141:                if (Os.isFamily("netware")) {
142:                    /*
143:                     * throw in NetWare volume names
144:                     */
145:                    String driveSpec = "SYS:";
146:                    assertEquals(driveSpec, FILE_UTILS.resolveFile(null,
147:                            driveSpec + "/").getPath());
148:                    assertEquals(driveSpec, FILE_UTILS.resolveFile(null,
149:                            driveSpec + "\\").getPath());
150:                    String driveSpecLower = "sys:";
151:                    assertEquals(driveSpec, FILE_UTILS.resolveFile(null,
152:                            driveSpecLower + "/").getPath());
153:                    assertEquals(driveSpec, FILE_UTILS.resolveFile(null,
154:                            driveSpecLower + "\\").getPath());
155:                    /*
156:                     * promised to eliminate consecutive slashes after drive letter.
157:                     */
158:                    assertEquals(driveSpec, FILE_UTILS.resolveFile(null,
159:                            driveSpec + "/////").getPath());
160:                    assertEquals(driveSpec, FILE_UTILS.resolveFile(null,
161:                            driveSpec + "\\\\\\\\\\\\").getPath());
162:                } else if (!(Os.isFamily("dos"))) {
163:                    /*
164:                     * drive letters must be considered just normal filenames.
165:                     */
166:                    String driveSpec = "C:";
167:                    String udir = System.getProperty("user.dir");
168:                    assertEquals(udir + File.separator + driveSpec, FILE_UTILS
169:                            .resolveFile(null, driveSpec + "/").getPath());
170:                    assertEquals(udir + File.separator + driveSpec, FILE_UTILS
171:                            .resolveFile(null, driveSpec + "\\").getPath());
172:                    String driveSpecLower = "c:";
173:                    assertEquals(udir + File.separator + driveSpecLower,
174:                            FILE_UTILS.resolveFile(null, driveSpecLower + "/")
175:                                    .getPath());
176:                    assertEquals(udir + File.separator + driveSpecLower,
177:                            FILE_UTILS.resolveFile(null, driveSpecLower + "\\")
178:                                    .getPath());
179:                }
180:
181:                /*
182:                 * Now test some relative file name magic.
183:                 */
184:                assertEquals(localize("/1/2/3/4"), FILE_UTILS.resolveFile(
185:                        new File(localize("/1/2/3")), "4").getPath());
186:                assertEquals(localize("/1/2/3/4"), FILE_UTILS.resolveFile(
187:                        new File(localize("/1/2/3")), "./4").getPath());
188:                assertEquals(localize("/1/2/3/4"), FILE_UTILS.resolveFile(
189:                        new File(localize("/1/2/3")), ".\\4").getPath());
190:                assertEquals(localize("/1/2/3/4"), FILE_UTILS.resolveFile(
191:                        new File(localize("/1/2/3")), "./.\\4").getPath());
192:                assertEquals(localize("/1/2/3/4"), FILE_UTILS.resolveFile(
193:                        new File(localize("/1/2/3")), "../3/4").getPath());
194:                assertEquals(localize("/1/2/3/4"), FILE_UTILS.resolveFile(
195:                        new File(localize("/1/2/3")), "..\\3\\4").getPath());
196:                assertEquals(localize("/1/2/3/4"), FILE_UTILS.resolveFile(
197:                        new File(localize("/1/2/3")),
198:                        "../../5/.././2/./3/6/../4").getPath());
199:                assertEquals(localize("/1/2/3/4"), FILE_UTILS.resolveFile(
200:                        new File(localize("/1/2/3")),
201:                        "..\\../5/..\\./2/./3/6\\../4").getPath());
202:
203:                assertEquals("meaningless result but no exception", new File(
204:                        localize("/1/../../b")), FILE_UTILS.resolveFile(
205:                        new File(localize("/1")), "../../b"));
206:
207:            }
208:
209:            public void testNormalize() {
210:                if (!(Os.isFamily("dos") || Os.isFamily("netware"))) {
211:                    /*
212:                     * Start with simple absolute file names.
213:                     */
214:                    assertEquals(File.separator, FILE_UTILS.normalize("/")
215:                            .getPath());
216:                    assertEquals(File.separator, FILE_UTILS.normalize("\\")
217:                            .getPath());
218:                } else {
219:                    try {
220:                        FILE_UTILS.normalize("/").getPath();
221:                        fail("normalized \"/\" on dos or netware");
222:                    } catch (Exception e) {
223:                    }
224:                    try {
225:                        FILE_UTILS.normalize("\\").getPath();
226:                        fail("normalized \"\\\" on dos or netware");
227:                    } catch (Exception e) {
228:                    }
229:                }
230:
231:                if (Os.isFamily("dos")) {
232:                    /*
233:                     * throw in drive letters
234:                     */
235:                    String driveSpec = "C:";
236:                    try {
237:                        FILE_UTILS.normalize(driveSpec).getPath();
238:                        fail(driveSpec + " is not an absolute path");
239:                    } catch (Exception e) {
240:                    }
241:                    assertEquals(driveSpec + "\\", FILE_UTILS.normalize(
242:                            driveSpec + "/").getPath());
243:                    assertEquals(driveSpec + "\\", FILE_UTILS.normalize(
244:                            driveSpec + "\\").getPath());
245:                    String driveSpecLower = "c:";
246:                    assertEquals(driveSpecLower + "\\", FILE_UTILS.normalize(
247:                            driveSpecLower + "/").getPath());
248:                    assertEquals(driveSpecLower + "\\", FILE_UTILS.normalize(
249:                            driveSpecLower + "\\").getPath());
250:                    /*
251:                     * promised to eliminate consecutive slashes after drive letter.
252:                     */
253:                    assertEquals(driveSpec + "\\", FILE_UTILS.normalize(
254:                            driveSpec + "/////").getPath());
255:                    assertEquals(driveSpec + "\\", FILE_UTILS.normalize(
256:                            driveSpec + "\\\\\\\\\\\\").getPath());
257:                } else if (Os.isFamily("netware")) {
258:                    /*
259:                     * throw in NetWare volume names
260:                     */
261:                    String driveSpec = "SYS:";
262:                    assertEquals(driveSpec, FILE_UTILS.normalize(driveSpec)
263:                            .getPath());
264:                    assertEquals(driveSpec, FILE_UTILS.normalize(
265:                            driveSpec + "/").getPath());
266:                    assertEquals(driveSpec, FILE_UTILS.normalize(
267:                            driveSpec + "\\").getPath());
268:                    String driveSpecLower = "sys:";
269:                    assertEquals(driveSpec, FILE_UTILS
270:                            .normalize(driveSpecLower).getPath());
271:                    assertEquals(driveSpec, FILE_UTILS.normalize(
272:                            driveSpecLower + "/").getPath());
273:                    assertEquals(driveSpec, FILE_UTILS.normalize(
274:                            driveSpecLower + "\\").getPath());
275:                    assertEquals(driveSpec + "\\junk", FILE_UTILS.normalize(
276:                            driveSpecLower + "\\junk").getPath());
277:                    /*
278:                     * promised to eliminate consecutive slashes after drive letter.
279:                     */
280:                    assertEquals(driveSpec, FILE_UTILS.normalize(
281:                            driveSpec + "/////").getPath());
282:                    assertEquals(driveSpec, FILE_UTILS.normalize(
283:                            driveSpec + "\\\\\\\\\\\\").getPath());
284:                } else {
285:                    try {
286:                        String driveSpec = "C:";
287:                        assertEquals(driveSpec, FILE_UTILS.normalize(driveSpec)
288:                                .getPath());
289:                        fail("Expected failure, C: isn't an absolute path on other os's");
290:                    } catch (BuildException e) {
291:                        // Passed test 
292:                    }
293:                }
294:
295:                /*
296:                 * Now test some relative file name magic.
297:                 */
298:                assertEquals(localize("/1/2/3/4"), FILE_UTILS.normalize(
299:                        localize("/1/2/3/4")).getPath());
300:                assertEquals(localize("/1/2/3/4"), FILE_UTILS.normalize(
301:                        localize("/1/2/3/./4")).getPath());
302:                assertEquals(localize("/1/2/3/4"), FILE_UTILS.normalize(
303:                        localize("/1/2/3/.\\4")).getPath());
304:                assertEquals(localize("/1/2/3/4"), FILE_UTILS.normalize(
305:                        localize("/1/2/3/./.\\4")).getPath());
306:                assertEquals(localize("/1/2/3/4"), FILE_UTILS.normalize(
307:                        localize("/1/2/3/../3/4")).getPath());
308:                assertEquals(localize("/1/2/3/4"), FILE_UTILS.normalize(
309:                        localize("/1/2/3/..\\3\\4")).getPath());
310:                assertEquals(localize("/1/2/3/4"), FILE_UTILS.normalize(
311:                        localize("/1/2/3/../../5/.././2/./3/6/../4")).getPath());
312:                assertEquals(localize("/1/2/3/4"), FILE_UTILS.normalize(
313:                        localize("/1/2/3/..\\../5/..\\./2/./3/6\\../4"))
314:                        .getPath());
315:
316:                try {
317:                    FILE_UTILS.normalize("foo");
318:                    fail("foo is not an absolute path");
319:                } catch (BuildException e) {
320:                    // Expected exception caught
321:                }
322:
323:                assertEquals(
324:                        "will not go outside FS root (but will not throw an exception either)",
325:                        new File(localize("/1/../../b")), FILE_UTILS
326:                                .normalize(localize("/1/../../b")));
327:            }
328:
329:            /**
330:             * Test handling of null arguments.
331:             */
332:            public void testNullArgs() {
333:                try {
334:                    FILE_UTILS.normalize(null);
335:                    fail("successfully normalized a null-file");
336:                } catch (NullPointerException npe) {
337:                    // Expected exception caught
338:                }
339:
340:                File f = FILE_UTILS.resolveFile(null, "a");
341:                assertEquals(f, new File("a").getAbsoluteFile());
342:            }
343:
344:            /**
345:             * Test createTempFile
346:             */
347:            public void testCreateTempFile() {
348:                File parent = new File((new File("/tmp")).getAbsolutePath());
349:                File tmp1 = FILE_UTILS.createTempFile("pre", ".suf", parent);
350:                assertTrue("new file", !tmp1.exists());
351:
352:                String name = tmp1.getName();
353:                assertTrue("starts with pre", name.startsWith("pre"));
354:                assertTrue("ends with .suf", name.endsWith(".suf"));
355:                assertEquals("is inside parent dir", parent.getAbsolutePath(),
356:                        tmp1.getParent());
357:
358:                File tmp2 = FILE_UTILS.createTempFile("pre", ".suf", parent);
359:                assertTrue("files are different", !tmp1.getAbsolutePath()
360:                        .equals(tmp2.getAbsolutePath()));
361:
362:                // null parent dir
363:                File tmp3 = FILE_UTILS.createTempFile("pre", ".suf", null);
364:                String tmploc = System.getProperty("java.io.tmpdir");
365:                assertEquals((new File(tmploc, tmp3.getName()))
366:                        .getAbsolutePath(), tmp3.getAbsolutePath());
367:            }
368:
369:            /**
370:             * Test contentEquals
371:             */
372:            public void testContentEquals() throws IOException {
373:                assertTrue("Non existing files", FILE_UTILS.contentEquals(
374:                        new File(System.getProperty("root"), "foo"), new File(
375:                                System.getProperty("root"), "bar")));
376:                assertTrue("One exists, the other one doesn\'t", !FILE_UTILS
377:                        .contentEquals(new File(System.getProperty("root"),
378:                                "foo"), new File(System.getProperty("root"),
379:                                "build.xml")));
380:                assertTrue("Don\'t compare directories", !FILE_UTILS
381:                        .contentEquals(new File(System.getProperty("root"),
382:                                "src"), new File(System.getProperty("root"),
383:                                "src")));
384:                assertTrue("File equals itself", FILE_UTILS.contentEquals(
385:                        new File(System.getProperty("root"), "build.xml"),
386:                        new File(System.getProperty("root"), "build.xml")));
387:                assertTrue("Files are different", !FILE_UTILS.contentEquals(
388:                        new File(System.getProperty("root"), "build.xml"),
389:                        new File(System.getProperty("root"), "docs.xml")));
390:            }
391:
392:            /**
393:             * Test createNewFile
394:             */
395:            public void testCreateNewFile() throws IOException {
396:                removeThis = new File("dummy");
397:                assertTrue(!removeThis.exists());
398:                FILE_UTILS.createNewFile(removeThis);
399:                assertTrue(removeThis.exists());
400:            }
401:
402:            /**
403:             * Test removeLeadingPath.
404:             */
405:            public void testRemoveLeadingPath() {
406:                assertEquals("bar", FILE_UTILS.removeLeadingPath(new File(
407:                        "/foo"), new File("/foo/bar")));
408:                assertEquals("bar", FILE_UTILS.removeLeadingPath(new File(
409:                        "/foo/"), new File("/foo/bar")));
410:                assertEquals("bar", FILE_UTILS.removeLeadingPath(new File(
411:                        "\\foo"), new File("\\foo\\bar")));
412:                assertEquals("bar", FILE_UTILS.removeLeadingPath(new File(
413:                        "\\foo\\"), new File("\\foo\\bar")));
414:                assertEquals("bar", FILE_UTILS.removeLeadingPath(new File(
415:                        "c:/foo"), new File("c:/foo/bar")));
416:                assertEquals("bar", FILE_UTILS.removeLeadingPath(new File(
417:                        "c:/foo/"), new File("c:/foo/bar")));
418:                assertEquals("bar", FILE_UTILS.removeLeadingPath(new File(
419:                        "c:\\foo"), new File("c:\\foo\\bar")));
420:                assertEquals("bar", FILE_UTILS.removeLeadingPath(new File(
421:                        "c:\\foo\\"), new File("c:\\foo\\bar")));
422:                if (!(Os.isFamily("dos") || Os.isFamily("netware"))) {
423:                    assertEquals(
424:                            FILE_UTILS.normalize("/bar").getAbsolutePath(),
425:                            FILE_UTILS.removeLeadingPath(new File("/foo"),
426:                                    new File("/bar")));
427:                    assertEquals(FILE_UTILS.normalize("/foobar")
428:                            .getAbsolutePath(), FILE_UTILS.removeLeadingPath(
429:                            new File("/foo"), new File("/foobar")));
430:                }
431:                // bugzilla report 19979
432:                assertEquals("", FILE_UTILS.removeLeadingPath(new File(
433:                        "/foo/bar"), new File("/foo/bar")));
434:                assertEquals("", FILE_UTILS.removeLeadingPath(new File(
435:                        "/foo/bar"), new File("/foo/bar/")));
436:                assertEquals("", FILE_UTILS.removeLeadingPath(new File(
437:                        "/foo/bar/"), new File("/foo/bar/")));
438:                assertEquals("", FILE_UTILS.removeLeadingPath(new File(
439:                        "/foo/bar/"), new File("/foo/bar")));
440:
441:                String expected = "foo/bar".replace('\\', File.separatorChar)
442:                        .replace('/', File.separatorChar);
443:                assertEquals(expected, FILE_UTILS.removeLeadingPath(new File(
444:                        "/"), new File("/foo/bar")));
445:                assertEquals(expected, FILE_UTILS.removeLeadingPath(new File(
446:                        "c:/"), new File("c:/foo/bar")));
447:                assertEquals(expected, FILE_UTILS.removeLeadingPath(new File(
448:                        "c:\\"), new File("c:\\foo\\bar")));
449:            }
450:
451:            /**
452:             * test toUri
453:             */
454:            public void testToURI() {
455:                String dosRoot = null;
456:                if (Os.isFamily("dos") || Os.isFamily("netware")) {
457:                    dosRoot = System.getProperty("user.dir").substring(0, 3)
458:                            .replace(File.separatorChar, '/');
459:
460:                    //preserve case on Cygwin when using 1.4 toURI:
461:                    Class uriClazz = null;
462:                    try {
463:                        uriClazz = Class.forName("java.net.URI");
464:                    } catch (ClassNotFoundException e) {
465:                        // OK, Java 1.3.
466:                        dosRoot = dosRoot.toUpperCase();
467:                    }
468:                } else {
469:                    dosRoot = "";
470:                }
471:                if (Os.isFamily("dos")) {
472:                    assertEquals("file:/c:/foo",
473:                            removeExtraneousAuthority(FILE_UTILS
474:                                    .toURI("c:\\foo")));
475:                }
476:                if (Os.isFamily("netware")) {
477:                    assertEquals("file:/SYS:/foo",
478:                            removeExtraneousAuthority(FILE_UTILS
479:                                    .toURI("sys:\\foo")));
480:                }
481:                if (File.pathSeparatorChar == '/') {
482:                    assertEquals("file:/foo",
483:                            removeExtraneousAuthority(FILE_UTILS.toURI("/foo")));
484:                    assertTrue("file: URIs must name absolute paths",
485:                            FILE_UTILS.toURI("./foo").startsWith("file:/"));
486:                    assertTrue(FILE_UTILS.toURI("./foo").endsWith("/foo"));
487:                    assertEquals("file:/" + dosRoot + "foo%20bar",
488:                            removeExtraneousAuthority(FILE_UTILS
489:                                    .toURI("/foo bar")));
490:                    assertEquals("file:/" + dosRoot + "foo%23bar",
491:                            removeExtraneousAuthority(FILE_UTILS
492:                                    .toURI("/foo#bar")));
493:                } else if (File.pathSeparatorChar == '\\') {
494:                    assertEquals(
495:                            "file:/" + dosRoot + "foo",
496:                            removeExtraneousAuthority(FILE_UTILS.toURI("\\foo")));
497:                    assertTrue("file: URIs must name absolute paths",
498:                            FILE_UTILS.toURI(".\\foo").startsWith("file:/"));
499:                    assertTrue(FILE_UTILS.toURI(".\\foo").endsWith("/foo"));
500:                    assertEquals("file:/" + dosRoot + "foo%20bar",
501:                            removeExtraneousAuthority(FILE_UTILS
502:                                    .toURI("\\foo bar")));
503:                    assertEquals("file:/" + dosRoot + "foo%23bar",
504:                            removeExtraneousAuthority(FILE_UTILS
505:                                    .toURI("\\foo#bar")));
506:                }
507:                // a test with ant for germans
508:                // the escaped character used for the test is the "a umlaut"
509:                // this is the fix for the bug 37348
510:                assertEquals(
511:                        "file:/" + dosRoot + "%C3%A4nt",
512:                        removeExtraneousAuthority(FILE_UTILS.toURI("/\u00E4nt")));
513:            }
514:
515:            /**
516:             * Authority field is unnecessary, but harmless, in file: URIs.
517:             * Java 1.4 does not produce it when using File.toURI.
518:             */
519:            private static String removeExtraneousAuthority(String uri) {
520:                String prefix = "file:///";
521:                if (uri.startsWith(prefix)) {
522:                    return "file:/" + uri.substring(prefix.length());
523:                } else {
524:                    return uri;
525:                }
526:            }
527:
528:            public void testIsContextRelativePath() {
529:                if (Os.isFamily("dos")) {
530:                    assertTrue(FileUtils.isContextRelativePath("/\u00E4nt"));
531:                    assertTrue(FileUtils.isContextRelativePath("\\foo"));
532:                }
533:            }
534:
535:            /**
536:             * test fromUri
537:             */
538:            public void testFromURI() {
539:                String dosRoot = null;
540:                if (Os.isFamily("dos") || Os.isFamily("netware")) {
541:                    dosRoot = System.getProperty("user.dir").substring(0, 2);
542:                } else {
543:                    dosRoot = "";
544:                }
545:                if (Os.isFamily("netware")) {
546:                    assertEqualsIgnoreDriveCase("SYS:\\foo", FILE_UTILS
547:                            .fromURI("file:///sys:/foo"));
548:                }
549:                if (Os.isFamily("dos")) {
550:                    assertEqualsIgnoreDriveCase("C:\\foo", FILE_UTILS
551:                            .fromURI("file:///c:/foo"));
552:                }
553:                assertEqualsIgnoreDriveCase(dosRoot + File.separator + "foo",
554:                        FILE_UTILS.fromURI("file:///foo"));
555:                assertEquals("." + File.separator + "foo", FILE_UTILS
556:                        .fromURI("file:./foo"));
557:                assertEquals(dosRoot + File.separator + "foo bar", FILE_UTILS
558:                        .fromURI("file:///foo%20bar"));
559:                assertEquals(dosRoot + File.separator + "foo#bar", FILE_UTILS
560:                        .fromURI("file:///foo%23bar"));
561:            }
562:
563:            public void testModificationTests() {
564:
565:                //get a time
566:                long firstTime = System.currentTimeMillis();
567:                //add some time. We assume no OS has a granularity this bad
568:                long secondTime = firstTime + 60000;
569:                /*
570:                 assertTrue("same timestamp is up to date",
571:                 fu.isUpToDate(firstTime, firstTime));
572:                 */
573:
574:                //check that older is up to date with a newer dest
575:                assertTrue("older source files are up to date", FILE_UTILS
576:                        .isUpToDate(firstTime, secondTime));
577:                //check that older is up to date with a newer dest
578:                assertFalse("newer source files are no up to date", FILE_UTILS
579:                        .isUpToDate(secondTime, firstTime));
580:
581:                assertTrue("-1 dest timestamp implies nonexistence",
582:                        !FILE_UTILS.isUpToDate(firstTime, -1L));
583:            }
584:
585:            public void testGetDefaultEncoding() {
586:                // This just tests that the function does not blow up
587:                FILE_UTILS.getDefaultEncoding();
588:            }
589:
590:            /**
591:             * adapt file separators to local conventions
592:             */
593:            private String localize(String path) {
594:                path = root + path.substring(1);
595:                return path.replace('\\', File.separatorChar).replace('/',
596:                        File.separatorChar);
597:            }
598:
599:            /**
600:             * convenience method
601:             * normalize brings the drive in uppercase
602:             * the drive letter is in lower case under cygwin
603:             * calling this method allows tests where normalize is called to pass under cygwin
604:             */
605:            private void assertEqualsIgnoreDriveCase(String s1, String s2) {
606:                if ((Os.isFamily("dos") || Os.isFamily("netware"))
607:                        && s1.length() > 0 && s2.length() > 0) {
608:                    StringBuffer sb1 = new StringBuffer(s1);
609:                    StringBuffer sb2 = new StringBuffer(s2);
610:                    sb1.setCharAt(0, Character.toUpperCase(s1.charAt(0)));
611:                    sb2.setCharAt(0, Character.toUpperCase(s2.charAt(0)));
612:                    assertEquals(sb1.toString(), sb2.toString());
613:                } else {
614:                    assertEquals(s1, s2);
615:                }
616:            }
617:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.