@@ -1,5 +1,5 @@
/*
* Copyright 2001-2004 The Apache Software Foundation
* Copyright 2001-2005 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -17,7 +17,9 @@
package org.apache.tools.ant.util;
import java.io.*;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import junit.framework.TestCase;
@@ -30,7 +32,7 @@ import org.apache.tools.ant.taskdefs.condition.Os;
*/
public class FileUtilsTest extends TestCase {
private FileUtils fu ;
private static final FileUtils FILE_UTILS = FileUtils.getFileUtils() ;
private File removeThis;
private String root;
@@ -39,7 +41,6 @@ public class FileUtilsTest extends TestCase {
}
public void setUp() {
fu = FileUtils.newFileUtils();
// Windows adds the drive letter in uppercase, unless you run Cygwin
root = new File(File.separator).getAbsolutePath().toUpperCase();
}
@@ -79,7 +80,7 @@ public class FileUtilsTest extends TestCase {
fail(ie.getMessage());
}
fu .setFileLastModified(removeThis, -1);
FILE_UTILS .setFileLastModified(removeThis, -1);
long secondModTime = removeThis.lastModified();
assertTrue(secondModTime > modTime);
@@ -88,7 +89,7 @@ public class FileUtilsTest extends TestCase {
// in a previous version, the date of the file was set to 123456
// milliseconds since 01.01.1970
// it did not work on a computer running JDK 1.4.1_02 + Windows 2000
fu .setFileLastModified(removeThis, secondModTime + millisperday);
FILE_UTILS .setFileLastModified(removeThis, secondModTime + millisperday);
long thirdModTime = removeThis.lastModified();
/*
* I would love to compare this with 123456, but depending on
@@ -104,30 +105,30 @@ public class FileUtilsTest extends TestCase {
* Start with simple absolute file names.
*/
assertEquals(File.separator,
fu .resolveFile(null, "/").getPath());
FILE_UTILS .resolveFile(null, "/").getPath());
assertEquals(File.separator,
fu .resolveFile(null, "\\").getPath());
FILE_UTILS .resolveFile(null, "\\").getPath());
/*
* throw in drive letters
*/
String driveSpec = "C:";
assertEquals(driveSpec + "\\",
fu .resolveFile(null, driveSpec + "/").getPath());
FILE_UTILS .resolveFile(null, driveSpec + "/").getPath());
assertEquals(driveSpec + "\\",
fu .resolveFile(null, driveSpec + "\\").getPath());
FILE_UTILS .resolveFile(null, driveSpec + "\\").getPath());
String driveSpecLower = "c:";
assertEquals(driveSpec + "\\",
fu .resolveFile(null, driveSpecLower + "/").getPath());
FILE_UTILS .resolveFile(null, driveSpecLower + "/").getPath());
assertEquals(driveSpec + "\\",
fu .resolveFile(null, driveSpecLower + "\\").getPath());
FILE_UTILS .resolveFile(null, driveSpecLower + "\\").getPath());
/*
* promised to eliminate consecutive slashes after drive letter.
*/
assertEquals(driveSpec + "\\",
fu .resolveFile(null, driveSpec + "/////").getPath());
FILE_UTILS .resolveFile(null, driveSpec + "/////").getPath());
assertEquals(driveSpec + "\\",
fu .resolveFile(null, driveSpec + "\\\\\\\\\\\\").getPath());
FILE_UTILS .resolveFile(null, driveSpec + "\\\\\\\\\\\\").getPath());
if (Os.isFamily("netware")) {
/*
@@ -135,45 +136,45 @@ public class FileUtilsTest extends TestCase {
*/
driveSpec = "SYS:";
assertEquals(driveSpec,
fu .resolveFile(null, driveSpec + "/").getPath());
FILE_UTILS .resolveFile(null, driveSpec + "/").getPath());
assertEquals(driveSpec,
fu .resolveFile(null, driveSpec + "\\").getPath());
FILE_UTILS .resolveFile(null, driveSpec + "\\").getPath());
driveSpecLower = "sys:";
assertEquals(driveSpec,
fu .resolveFile(null, driveSpecLower + "/").getPath());
FILE_UTILS .resolveFile(null, driveSpecLower + "/").getPath());
assertEquals(driveSpec,
fu .resolveFile(null, driveSpecLower + "\\").getPath());
FILE_UTILS .resolveFile(null, driveSpecLower + "\\").getPath());
/*
* promised to eliminate consecutive slashes after drive letter.
*/
assertEquals(driveSpec,
fu .resolveFile(null, driveSpec + "/////").getPath());
FILE_UTILS .resolveFile(null, driveSpec + "/////").getPath());
assertEquals(driveSpec,
fu .resolveFile(null, driveSpec + "\\\\\\\\\\\\").getPath());
FILE_UTILS .resolveFile(null, driveSpec + "\\\\\\\\\\\\").getPath());
}
/*
* Now test some relative file name magic.
*/
assertEquals(localize("/1/2/3/4"),
fu .resolveFile(new File(localize("/1/2/3")), "4").getPath());
FILE_UTILS .resolveFile(new File(localize("/1/2/3")), "4").getPath());
assertEquals(localize("/1/2/3/4"),
fu .resolveFile(new File(localize("/1/2/3")), "./4").getPath());
FILE_UTILS .resolveFile(new File(localize("/1/2/3")), "./4").getPath());
assertEquals(localize("/1/2/3/4"),
fu .resolveFile(new File(localize("/1/2/3")), ".\\4").getPath());
FILE_UTILS .resolveFile(new File(localize("/1/2/3")), ".\\4").getPath());
assertEquals(localize("/1/2/3/4"),
fu .resolveFile(new File(localize("/1/2/3")), "./.\\4").getPath());
FILE_UTILS .resolveFile(new File(localize("/1/2/3")), "./.\\4").getPath());
assertEquals(localize("/1/2/3/4"),
fu .resolveFile(new File(localize("/1/2/3")), "../3/4").getPath());
FILE_UTILS .resolveFile(new File(localize("/1/2/3")), "../3/4").getPath());
assertEquals(localize("/1/2/3/4"),
fu .resolveFile(new File(localize("/1/2/3")), "..\\3\\4").getPath());
FILE_UTILS .resolveFile(new File(localize("/1/2/3")), "..\\3\\4").getPath());
assertEquals(localize("/1/2/3/4"),
fu .resolveFile(new File(localize("/1/2/3")), "../../5/.././2/./3/6/../4").getPath());
FILE_UTILS .resolveFile(new File(localize("/1/2/3")), "../../5/.././2/./3/6/../4").getPath());
assertEquals(localize("/1/2/3/4"),
fu .resolveFile(new File(localize("/1/2/3")), "..\\../5/..\\./2/./3/6\\../4").getPath());
FILE_UTILS .resolveFile(new File(localize("/1/2/3")), "..\\../5/..\\./2/./3/6\\../4").getPath());
try {
fu .resolveFile(new File(localize("/1")), "../../b");
FILE_UTILS .resolveFile(new File(localize("/1")), "../../b");
fail("successfully crawled beyond the filesystem root");
} catch (BuildException e) {
// Expected Exception caught
@@ -186,32 +187,32 @@ public class FileUtilsTest extends TestCase {
* Start with simple absolute file names.
*/
assertEquals(File.separator,
fu .normalize("/").getPath());
FILE_UTILS .normalize("/").getPath());
assertEquals(File.separator,
fu .normalize("\\").getPath());
FILE_UTILS .normalize("\\").getPath());
/*
* throw in drive letters
*/
String driveSpec = "C:";
assertEquals(driveSpec,
fu .normalize(driveSpec).getPath());
FILE_UTILS .normalize(driveSpec).getPath());
assertEquals(driveSpec + "\\",
fu .normalize(driveSpec + "/").getPath());
FILE_UTILS .normalize(driveSpec + "/").getPath());
assertEquals(driveSpec + "\\",
fu .normalize(driveSpec + "\\").getPath());
FILE_UTILS .normalize(driveSpec + "\\").getPath());
String driveSpecLower = "c:";
assertEquals(driveSpec + "\\",
fu .normalize(driveSpecLower + "/").getPath());
FILE_UTILS .normalize(driveSpecLower + "/").getPath());
assertEquals(driveSpec + "\\",
fu .normalize(driveSpecLower + "\\").getPath());
FILE_UTILS .normalize(driveSpecLower + "\\").getPath());
/*
* promised to eliminate consecutive slashes after drive letter.
*/
assertEquals(driveSpec + "\\",
fu .normalize(driveSpec + "/////").getPath());
FILE_UTILS .normalize(driveSpec + "/////").getPath());
assertEquals(driveSpec + "\\",
fu .normalize(driveSpec + "\\\\\\\\\\\\").getPath());
FILE_UTILS .normalize(driveSpec + "\\\\\\\\\\\\").getPath());
if (Os.isFamily("netware")) {
/*
@@ -219,58 +220,58 @@ public class FileUtilsTest extends TestCase {
*/
driveSpec = "SYS:";
assertEquals(driveSpec,
fu .normalize(driveSpec).getPath());
FILE_UTILS .normalize(driveSpec).getPath());
assertEquals(driveSpec,
fu .normalize(driveSpec + "/").getPath());
FILE_UTILS .normalize(driveSpec + "/").getPath());
assertEquals(driveSpec,
fu .normalize(driveSpec + "\\").getPath());
FILE_UTILS .normalize(driveSpec + "\\").getPath());
driveSpecLower = "sys:";
assertEquals(driveSpec,
fu .normalize(driveSpecLower).getPath());
FILE_UTILS .normalize(driveSpecLower).getPath());
assertEquals(driveSpec,
fu .normalize(driveSpecLower + "/").getPath());
FILE_UTILS .normalize(driveSpecLower + "/").getPath());
assertEquals(driveSpec,
fu .normalize(driveSpecLower + "\\").getPath());
FILE_UTILS .normalize(driveSpecLower + "\\").getPath());
assertEquals(driveSpec + "\\junk",
fu .normalize(driveSpecLower + "\\junk").getPath());
FILE_UTILS .normalize(driveSpecLower + "\\junk").getPath());
/*
* promised to eliminate consecutive slashes after drive letter.
*/
assertEquals(driveSpec,
fu .normalize(driveSpec + "/////").getPath());
FILE_UTILS .normalize(driveSpec + "/////").getPath());
assertEquals(driveSpec,
fu .normalize(driveSpec + "\\\\\\\\\\\\").getPath());
FILE_UTILS .normalize(driveSpec + "\\\\\\\\\\\\").getPath());
}
/*
* Now test some relative file name magic.
*/
assertEquals(localize("/1/2/3/4"),
fu .normalize(localize("/1/2/3/4")).getPath());
FILE_UTILS .normalize(localize("/1/2/3/4")).getPath());
assertEquals(localize("/1/2/3/4"),
fu .normalize(localize("/1/2/3/./4")).getPath());
FILE_UTILS .normalize(localize("/1/2/3/./4")).getPath());
assertEquals(localize("/1/2/3/4"),
fu .normalize(localize("/1/2/3/.\\4")).getPath());
FILE_UTILS .normalize(localize("/1/2/3/.\\4")).getPath());
assertEquals(localize("/1/2/3/4"),
fu .normalize(localize("/1/2/3/./.\\4")).getPath());
FILE_UTILS .normalize(localize("/1/2/3/./.\\4")).getPath());
assertEquals(localize("/1/2/3/4"),
fu .normalize(localize("/1/2/3/../3/4")).getPath());
FILE_UTILS .normalize(localize("/1/2/3/../3/4")).getPath());
assertEquals(localize("/1/2/3/4"),
fu .normalize(localize("/1/2/3/..\\3\\4")).getPath());
FILE_UTILS .normalize(localize("/1/2/3/..\\3\\4")).getPath());
assertEquals(localize("/1/2/3/4"),
fu .normalize(localize("/1/2/3/../../5/.././2/./3/6/../4")).getPath());
FILE_UTILS .normalize(localize("/1/2/3/../../5/.././2/./3/6/../4")).getPath());
assertEquals(localize("/1/2/3/4"),
fu .normalize(localize("/1/2/3/..\\../5/..\\./2/./3/6\\../4")).getPath());
FILE_UTILS .normalize(localize("/1/2/3/..\\../5/..\\./2/./3/6\\../4")).getPath());
try {
fu .normalize("foo");
FILE_UTILS .normalize("foo");
fail("foo is not an absolute path");
} catch (BuildException e) {
// Expected exception caught
}
try {
fu .normalize(localize("/1/../../b"));
FILE_UTILS .normalize(localize("/1/../../b"));
fail("successfully crawled beyond the filesystem root");
} catch (BuildException e) {
// Expected exception caught
@@ -282,13 +283,13 @@ public class FileUtilsTest extends TestCase {
*/
public void testNullArgs() {
try {
fu .normalize(null);
FILE_UTILS .normalize(null);
fail("successfully normalized a null-file");
} catch (NullPointerException npe) {
// Expected exception caught
}
File f = fu .resolveFile(null, "a");
File f = FILE_UTILS .resolveFile(null, "a");
assertEquals(f, new File("a"));
}
@@ -297,7 +298,7 @@ public class FileUtilsTest extends TestCase {
*/
public void testCreateTempFile() {
File parent = new File((new File("/tmp")).getAbsolutePath());
File tmp1 = fu .createTempFile("pre", ".suf", parent);
File tmp1 = FILE_UTILS .createTempFile("pre", ".suf", parent);
assertTrue("new file", !tmp1.exists());
String name = tmp1.getName();
@@ -307,12 +308,12 @@ public class FileUtilsTest extends TestCase {
parent.getAbsolutePath(),
tmp1.getParent());
File tmp2 = fu .createTempFile("pre", ".suf", parent);
File tmp2 = FILE_UTILS .createTempFile("pre", ".suf", parent);
assertTrue("files are different",
!tmp1.getAbsolutePath().equals(tmp2.getAbsolutePath()));
// null parent dir
File tmp3 = fu .createTempFile("pre", ".suf", null);
File tmp3 = FILE_UTILS .createTempFile("pre", ".suf", null);
String tmploc = System.getProperty("java.io.tmpdir");
assertEquals((new File(tmploc, tmp3.getName())).getAbsolutePath(),
tmp3.getAbsolutePath());
@@ -322,17 +323,17 @@ public class FileUtilsTest extends TestCase {
* Test contentEquals
*/
public void testContentEquals() throws IOException {
assertTrue("Non existing files", fu .contentEquals(new File(System.getProperty("root"), "foo"),
assertTrue("Non existing files", FILE_UTILS .contentEquals(new File(System.getProperty("root"), "foo"),
new File(System.getProperty("root"), "bar")));
assertTrue("One exists, the other one doesn\'t",
!fu .contentEquals(new File(System.getProperty("root"), "foo"), new File(System.getProperty("root"), "build.xml")));
!FILE_UTILS .contentEquals(new File(System.getProperty("root"), "foo"), new File(System.getProperty("root"), "build.xml")));
assertTrue("Don\'t compare directories",
!fu .contentEquals(new File(System.getProperty("root"), "src"), new File(System.getProperty("root"), "src")));
!FILE_UTILS .contentEquals(new File(System.getProperty("root"), "src"), new File(System.getProperty("root"), "src")));
assertTrue("File equals itself",
fu .contentEquals(new File(System.getProperty("root"), "build.xml"),
FILE_UTILS .contentEquals(new File(System.getProperty("root"), "build.xml"),
new File(System.getProperty("root"), "build.xml")));
assertTrue("Files are different",
!fu .contentEquals(new File(System.getProperty("root"), "build.xml"),
!FILE_UTILS .contentEquals(new File(System.getProperty("root"), "build.xml"),
new File(System.getProperty("root"), "docs.xml")));
}
@@ -342,7 +343,7 @@ public class FileUtilsTest extends TestCase {
public void testCreateNewFile() throws IOException {
removeThis = new File("dummy");
assertTrue(!removeThis.exists());
fu .createNewFile(removeThis);
FILE_UTILS .createNewFile(removeThis);
assertTrue(removeThis.exists());
}
@@ -350,43 +351,43 @@ public class FileUtilsTest extends TestCase {
* Test removeLeadingPath.
*/
public void testRemoveLeadingPath() {
assertEquals("bar", fu .removeLeadingPath(new File("/foo"),
assertEquals("bar", FILE_UTILS .removeLeadingPath(new File("/foo"),
new File("/foo/bar")));
assertEquals("bar", fu .removeLeadingPath(new File("/foo/"),
assertEquals("bar", FILE_UTILS .removeLeadingPath(new File("/foo/"),
new File("/foo/bar")));
assertEquals("bar", fu .removeLeadingPath(new File("\\foo"),
assertEquals("bar", FILE_UTILS .removeLeadingPath(new File("\\foo"),
new File("\\foo\\bar")));
assertEquals("bar", fu .removeLeadingPath(new File("\\foo\\"),
assertEquals("bar", FILE_UTILS .removeLeadingPath(new File("\\foo\\"),
new File("\\foo\\bar")));
assertEquals("bar", fu .removeLeadingPath(new File("c:/foo"),
assertEquals("bar", FILE_UTILS .removeLeadingPath(new File("c:/foo"),
new File("c:/foo/bar")));
assertEquals("bar", fu .removeLeadingPath(new File("c:/foo/"),
assertEquals("bar", FILE_UTILS .removeLeadingPath(new File("c:/foo/"),
new File("c:/foo/bar")));
assertEquals("bar", fu .removeLeadingPath(new File("c:\\foo"),
assertEquals("bar", FILE_UTILS .removeLeadingPath(new File("c:\\foo"),
new File("c:\\foo\\bar")));
assertEquals("bar", fu .removeLeadingPath(new File("c:\\foo\\"),
assertEquals("bar", FILE_UTILS .removeLeadingPath(new File("c:\\foo\\"),
new File("c:\\foo\\bar")));
assertEqualsIgnoreDriveCase(fu .normalize("/bar").getAbsolutePath(),
fu .removeLeadingPath(new File("/foo"), new File("/bar")));
assertEqualsIgnoreDriveCase(fu .normalize("/foobar").getAbsolutePath(),
fu .removeLeadingPath(new File("/foo"), new File("/foobar")));
assertEqualsIgnoreDriveCase(FILE_UTILS .normalize("/bar").getAbsolutePath(),
FILE_UTILS .removeLeadingPath(new File("/foo"), new File("/bar")));
assertEqualsIgnoreDriveCase(FILE_UTILS .normalize("/foobar").getAbsolutePath(),
FILE_UTILS .removeLeadingPath(new File("/foo"), new File("/foobar")));
// bugzilla report 19979
assertEquals("", fu .removeLeadingPath(new File("/foo/bar"),
assertEquals("", FILE_UTILS .removeLeadingPath(new File("/foo/bar"),
new File("/foo/bar")));
assertEquals("", fu .removeLeadingPath(new File("/foo/bar"),
assertEquals("", FILE_UTILS .removeLeadingPath(new File("/foo/bar"),
new File("/foo/bar/")));
assertEquals("", fu .removeLeadingPath(new File("/foo/bar/"),
assertEquals("", FILE_UTILS .removeLeadingPath(new File("/foo/bar/"),
new File("/foo/bar/")));
assertEquals("", fu .removeLeadingPath(new File("/foo/bar/"),
assertEquals("", FILE_UTILS .removeLeadingPath(new File("/foo/bar/"),
new File("/foo/bar")));
String expected = "foo/bar".replace('\\', File.separatorChar)
.replace('/', File.separatorChar);
assertEquals(expected, fu .removeLeadingPath(new File("/"),
assertEquals(expected, FILE_UTILS .removeLeadingPath(new File("/"),
new File("/foo/bar")));
assertEquals(expected, fu .removeLeadingPath(new File("c:/"),
assertEquals(expected, FILE_UTILS .removeLeadingPath(new File("c:/"),
new File("c:/foo/bar")));
assertEquals(expected, fu .removeLeadingPath(new File("c:\\"),
assertEquals(expected, FILE_UTILS .removeLeadingPath(new File("c:\\"),
new File("c:\\foo\\bar")));
}
@@ -403,24 +404,24 @@ public class FileUtilsTest extends TestCase {
dosRoot = "";
}
if (Os.isFamily("dos")) {
assertEquals("file:///C:/foo", fu .toURI("c:\\foo"));
assertEquals("file:///C:/foo", FILE_UTILS .toURI("c:\\foo"));
}
if (Os.isFamily("netware")) {
assertEquals("file:///SYS:/foo", fu .toURI("sys:\\foo"));
assertEquals("file:///SYS:/foo", FILE_UTILS .toURI("sys:\\foo"));
}
assertEquals("file:///" + dosRoot + "foo", fu .toURI("/foo"));
assertEquals("file:///" + dosRoot + "foo", FILE_UTILS .toURI("/foo"));
/* May fail if the directory ${user.dir}/foo/ exists
* (and anyway is the tested behavior actually desirable?):
assertEquals("file:./foo", fu.toURI("./foo"));
*/
assertEquals("file:///" + dosRoot + "foo", fu .toURI("\\foo"));
assertEquals("file:///" + dosRoot + "foo", FILE_UTILS .toURI("\\foo"));
/* See above:
assertEquals("file:./foo", fu.toURI(".\\foo"));
*/
assertEquals("file:///" + dosRoot + "foo%20bar", fu .toURI("/foo bar"));
assertEquals("file:///" + dosRoot + "foo%20bar", fu .toURI("\\foo bar"));
assertEquals("file:///" + dosRoot + "foo%23bar", fu .toURI("/foo#bar"));
assertEquals("file:///" + dosRoot + "foo%23bar", fu .toURI("\\foo#bar"));
assertEquals("file:///" + dosRoot + "foo%20bar", FILE_UTILS .toURI("/foo bar"));
assertEquals("file:///" + dosRoot + "foo%20bar", FILE_UTILS .toURI("\\foo bar"));
assertEquals("file:///" + dosRoot + "foo%23bar", FILE_UTILS .toURI("/foo#bar"));
assertEquals("file:///" + dosRoot + "foo%23bar", FILE_UTILS .toURI("\\foo#bar"));
}
/**
@@ -428,16 +429,16 @@ public class FileUtilsTest extends TestCase {
*/
public void testFromURI() {
if (Os.isFamily("netware")) {
assertEqualsIgnoreDriveCase("SYS:\\foo", fu .fromURI("file:///sys:/foo"));
assertEqualsIgnoreDriveCase("SYS:\\foo", FILE_UTILS .fromURI("file:///sys:/foo"));
}
if (Os.isFamily("dos")) {
assertEqualsIgnoreDriveCase("C:\\foo", fu .fromURI("file:///c:/foo"));
assertEqualsIgnoreDriveCase("C:\\foo", FILE_UTILS .fromURI("file:///c:/foo"));
}
assertEqualsIgnoreDriveCase(localize("/foo"), fu .fromURI("file:///foo"));
assertEqualsIgnoreDriveCase(localize("/foo"), FILE_UTILS .fromURI("file:///foo"));
assertEquals("." + File.separator + "foo",
fu .fromURI("file:./foo"));
assertEqualsIgnoreDriveCase(localize("/foo bar"), fu .fromURI("file:///foo%20bar"));
assertEqualsIgnoreDriveCase(localize("/foo#bar"), fu .fromURI("file:///foo%23bar"));
FILE_UTILS .fromURI("file:./foo"));
assertEqualsIgnoreDriveCase(localize("/foo bar"), FILE_UTILS .fromURI("file:///foo%20bar"));
assertEqualsIgnoreDriveCase(localize("/foo#bar"), FILE_UTILS .fromURI("file:///foo%23bar"));
}
public void testModificationTests() {
@@ -453,13 +454,13 @@ public class FileUtilsTest extends TestCase {
//check that older is up to date with a newer dest
assertTrue("older source files are up to date",
fu .isUpToDate(firstTime,secondTime));
FILE_UTILS .isUpToDate(firstTime,secondTime));
//check that older is up to date with a newer dest
assertFalse("newer source files are no up to date",
fu .isUpToDate(secondTime, firstTime));
FILE_UTILS .isUpToDate(secondTime, firstTime));
assertTrue("-1 dest timestamp implies nonexistence",
!fu .isUpToDate(firstTime,-1L));
!FILE_UTILS .isUpToDate(firstTime,-1L));
}
/**