Browse Source

Use java.io.tmpdir as location for temporary files

in FileUtils#createTempFile (when the directory parameter is null)


git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@274706 13f79535-47bb-0310-9956-ffa450edef68
master
Antoine Levy-Lambert 22 years ago
parent
commit
b4cde9dabb
3 changed files with 69 additions and 64 deletions
  1. +3
    -0
      WHATSNEW
  2. +5
    -4
      src/main/org/apache/tools/ant/util/FileUtils.java
  3. +61
    -60
      src/testcases/org/apache/tools/ant/util/FileUtilsTest.java

+ 3
- 0
WHATSNEW View File

@@ -445,6 +445,9 @@ Other changes:


* <checksum> has two new properties: totalproperty and todir. * <checksum> has two new properties: totalproperty and todir.


* FileUtils#createTempFile will now create temporary files in the
directory pointed to by the property java.io.tmpdir

Changes from Ant 1.5.2 to Ant 1.5.3 Changes from Ant 1.5.2 to Ant 1.5.3
=================================== ===================================




+ 5
- 4
src/main/org/apache/tools/ant/util/FileUtils.java View File

@@ -783,20 +783,21 @@ public class FileUtils {
* of this method will yield a different file name.</p> * of this method will yield a different file name.</p>
* *
* <p>This method is different to File.createTempFile of JDK 1.2 * <p>This method is different to File.createTempFile of JDK 1.2
* as it doesn't create the file itself and doesn't use platform
* specific temporary directory when the parentDir attribute is
* as it doesn't create the file itself.
* It uses the location pointed to by java.io.tmpdir
* when the parentDir attribute is
* null.</p> * null.</p>
* *
* @param parentDir Directory to create the temporary file in - * @param parentDir Directory to create the temporary file in -
* current working directory will be assumed if this parameter is * current working directory will be assumed if this parameter is
* null. * null.
* *
* @since 1.8
* @since ant 1.5
*/ */
public File createTempFile(String prefix, String suffix, File parentDir) { public File createTempFile(String prefix, String suffix, File parentDir) {


File result = null; File result = null;
String parent = null;
String parent = System.getProperty("java.io.tmpdir");
if (parentDir != null) { if (parentDir != null) {
parent = parentDir.getPath(); parent = parentDir.getPath();
} }


+ 61
- 60
src/testcases/org/apache/tools/ant/util/FileUtilsTest.java View File

@@ -64,8 +64,8 @@ import org.apache.tools.ant.taskdefs.condition.Os;
/** /**
* Tests for org.apache.tools.ant.util.FileUtils. * Tests for org.apache.tools.ant.util.FileUtils.
* *
* @author Stefan Bodewig
* @author <a href="mailto:jtulley@novell.com">Jeff Tulley</a>
* @author Stefan Bodewig
* @author <a href="mailto:jtulley@novell.com">Jeff Tulley</a>
*/ */
public class FileUtilsTest extends TestCase { public class FileUtilsTest extends TestCase {


@@ -117,7 +117,7 @@ public class FileUtilsTest extends TestCase {
// JDK 1.1 // JDK 1.1
assertEquals(modTime, secondModTime); assertEquals(modTime, secondModTime);
} }


// number of milliseconds in a day // number of milliseconds in a day
final int millisperday=24 * 3600 * 1000; final int millisperday=24 * 3600 * 1000;
@@ -145,30 +145,30 @@ public class FileUtilsTest extends TestCase {
/* /*
* Start with simple absolute file names. * Start with simple absolute file names.
*/ */
assertEquals(File.separator,
assertEquals(File.separator,
fu.resolveFile(null, "/").getPath()); fu.resolveFile(null, "/").getPath());
assertEquals(File.separator,
assertEquals(File.separator,
fu.resolveFile(null, "\\").getPath()); fu.resolveFile(null, "\\").getPath());


/* /*
* throw in drive letters * throw in drive letters
*/ */
String driveSpec = "C:"; String driveSpec = "C:";
assertEquals(driveSpec + "\\",
assertEquals(driveSpec + "\\",
fu.resolveFile(null, driveSpec + "/").getPath()); fu.resolveFile(null, driveSpec + "/").getPath());
assertEquals(driveSpec + "\\",
assertEquals(driveSpec + "\\",
fu.resolveFile(null, driveSpec + "\\").getPath()); fu.resolveFile(null, driveSpec + "\\").getPath());
String driveSpecLower = "c:"; String driveSpecLower = "c:";
assertEquals(driveSpec + "\\",
assertEquals(driveSpec + "\\",
fu.resolveFile(null, driveSpecLower + "/").getPath()); fu.resolveFile(null, driveSpecLower + "/").getPath());
assertEquals(driveSpec + "\\",
assertEquals(driveSpec + "\\",
fu.resolveFile(null, driveSpecLower + "\\").getPath()); fu.resolveFile(null, driveSpecLower + "\\").getPath());
/* /*
* promised to eliminate consecutive slashes after drive letter. * promised to eliminate consecutive slashes after drive letter.
*/ */
assertEquals(driveSpec + "\\",
assertEquals(driveSpec + "\\",
fu.resolveFile(null, driveSpec + "/////").getPath()); fu.resolveFile(null, driveSpec + "/////").getPath());
assertEquals(driveSpec + "\\",
assertEquals(driveSpec + "\\",
fu.resolveFile(null, driveSpec + "\\\\\\\\\\\\").getPath()); fu.resolveFile(null, driveSpec + "\\\\\\\\\\\\").getPath());


if (Os.isFamily("netware")) { if (Os.isFamily("netware")) {
@@ -176,21 +176,21 @@ public class FileUtilsTest extends TestCase {
* throw in NetWare volume names * throw in NetWare volume names
*/ */
driveSpec = "SYS:"; driveSpec = "SYS:";
assertEquals(driveSpec,
assertEquals(driveSpec,
fu.resolveFile(null, driveSpec + "/").getPath()); fu.resolveFile(null, driveSpec + "/").getPath());
assertEquals(driveSpec,
assertEquals(driveSpec,
fu.resolveFile(null, driveSpec + "\\").getPath()); fu.resolveFile(null, driveSpec + "\\").getPath());
driveSpecLower = "sys:"; driveSpecLower = "sys:";
assertEquals(driveSpec,
assertEquals(driveSpec,
fu.resolveFile(null, driveSpecLower + "/").getPath()); fu.resolveFile(null, driveSpecLower + "/").getPath());
assertEquals(driveSpec,
assertEquals(driveSpec,
fu.resolveFile(null, driveSpecLower + "\\").getPath()); fu.resolveFile(null, driveSpecLower + "\\").getPath());
/* /*
* promised to eliminate consecutive slashes after drive letter. * promised to eliminate consecutive slashes after drive letter.
*/ */
assertEquals(driveSpec,
assertEquals(driveSpec,
fu.resolveFile(null, driveSpec + "/////").getPath()); fu.resolveFile(null, driveSpec + "/////").getPath());
assertEquals(driveSpec,
assertEquals(driveSpec,
fu.resolveFile(null, driveSpec + "\\\\\\\\\\\\").getPath()); fu.resolveFile(null, driveSpec + "\\\\\\\\\\\\").getPath());
} }


@@ -227,60 +227,60 @@ public class FileUtilsTest extends TestCase {
/* /*
* Start with simple absolute file names. * Start with simple absolute file names.
*/ */
assertEquals(File.separator,
assertEquals(File.separator,
fu.normalize("/").getPath()); fu.normalize("/").getPath());
assertEquals(File.separator,
assertEquals(File.separator,
fu.normalize("\\").getPath()); fu.normalize("\\").getPath());


/* /*
* throw in drive letters * throw in drive letters
*/ */
String driveSpec = "C:"; String driveSpec = "C:";
assertEquals(driveSpec,
assertEquals(driveSpec,
fu.normalize(driveSpec).getPath()); fu.normalize(driveSpec).getPath());
assertEquals(driveSpec + "\\",
assertEquals(driveSpec + "\\",
fu.normalize(driveSpec + "/").getPath()); fu.normalize(driveSpec + "/").getPath());
assertEquals(driveSpec + "\\",
assertEquals(driveSpec + "\\",
fu.normalize(driveSpec + "\\").getPath()); fu.normalize(driveSpec + "\\").getPath());
String driveSpecLower = "c:"; String driveSpecLower = "c:";
assertEquals(driveSpec + "\\",
assertEquals(driveSpec + "\\",
fu.normalize(driveSpecLower + "/").getPath()); fu.normalize(driveSpecLower + "/").getPath());
assertEquals(driveSpec + "\\",
assertEquals(driveSpec + "\\",
fu.normalize(driveSpecLower + "\\").getPath()); fu.normalize(driveSpecLower + "\\").getPath());
/* /*
* promised to eliminate consecutive slashes after drive letter. * promised to eliminate consecutive slashes after drive letter.
*/ */
assertEquals(driveSpec + "\\",
assertEquals(driveSpec + "\\",
fu.normalize(driveSpec + "/////").getPath()); fu.normalize(driveSpec + "/////").getPath());
assertEquals(driveSpec + "\\",
assertEquals(driveSpec + "\\",
fu.normalize(driveSpec + "\\\\\\\\\\\\").getPath()); fu.normalize(driveSpec + "\\\\\\\\\\\\").getPath());


if (Os.isFamily("netware")) { if (Os.isFamily("netware")) {
/* /*
* throw in NetWare volume names
* throw in NetWare volume names
*/ */
driveSpec = "SYS:"; driveSpec = "SYS:";
assertEquals(driveSpec,
assertEquals(driveSpec,
fu.normalize(driveSpec).getPath()); fu.normalize(driveSpec).getPath());
assertEquals(driveSpec,
assertEquals(driveSpec,
fu.normalize(driveSpec + "/").getPath()); fu.normalize(driveSpec + "/").getPath());
assertEquals(driveSpec,
assertEquals(driveSpec,
fu.normalize(driveSpec + "\\").getPath()); fu.normalize(driveSpec + "\\").getPath());
driveSpecLower = "sys:"; driveSpecLower = "sys:";
assertEquals(driveSpec,
assertEquals(driveSpec,
fu.normalize(driveSpecLower).getPath()); fu.normalize(driveSpecLower).getPath());
assertEquals(driveSpec,
assertEquals(driveSpec,
fu.normalize(driveSpecLower + "/").getPath()); fu.normalize(driveSpecLower + "/").getPath());
assertEquals(driveSpec,
assertEquals(driveSpec,
fu.normalize(driveSpecLower + "\\").getPath()); fu.normalize(driveSpecLower + "\\").getPath());
assertEquals(driveSpec + "\\junk",
assertEquals(driveSpec + "\\junk",
fu.normalize(driveSpecLower + "\\junk").getPath()); fu.normalize(driveSpecLower + "\\junk").getPath());
/* /*
* promised to eliminate consecutive slashes after drive letter. * promised to eliminate consecutive slashes after drive letter.
*/ */
assertEquals(driveSpec,
assertEquals(driveSpec,
fu.normalize(driveSpec + "/////").getPath()); fu.normalize(driveSpec + "/////").getPath());
assertEquals(driveSpec,
assertEquals(driveSpec,
fu.normalize(driveSpec + "\\\\\\\\\\\\").getPath()); fu.normalize(driveSpec + "\\\\\\\\\\\\").getPath());
} }


@@ -310,7 +310,7 @@ public class FileUtilsTest extends TestCase {
} catch (BuildException e) { } catch (BuildException e) {
// Expected exception caught // Expected exception caught
} }
try { try {
fu.normalize(localize("/1/../../b")); fu.normalize(localize("/1/../../b"));
fail("successfully crawled beyond the filesystem root"); fail("successfully crawled beyond the filesystem root");
@@ -329,7 +329,7 @@ public class FileUtilsTest extends TestCase {
} catch (NullPointerException npe) { } catch (NullPointerException npe) {
// Expected exception caught // Expected exception caught
} }
File f = fu.resolveFile(null, "a"); File f = fu.resolveFile(null, "a");
assertEquals(f, new File("a")); assertEquals(f, new File("a"));
} }
@@ -345,17 +345,18 @@ public class FileUtilsTest extends TestCase {
String name = tmp1.getName(); String name = tmp1.getName();
assertTrue("starts with pre", name.startsWith("pre")); assertTrue("starts with pre", name.startsWith("pre"));
assertTrue("ends with .suf", name.endsWith(".suf")); assertTrue("ends with .suf", name.endsWith(".suf"));
assertEquals("is inside parent dir",
assertEquals("is inside parent dir",
parent.getAbsolutePath(), parent.getAbsolutePath(),
tmp1.getParent()); tmp1.getParent());


File tmp2 = fu.createTempFile("pre", ".suf", parent); File tmp2 = fu.createTempFile("pre", ".suf", parent);
assertTrue("files are different",
assertTrue("files are different",
!tmp1.getAbsolutePath().equals(tmp2.getAbsolutePath())); !tmp1.getAbsolutePath().equals(tmp2.getAbsolutePath()));


// null parent dir // null parent dir
File tmp3 = fu.createTempFile("pre", ".suf", null); File tmp3 = fu.createTempFile("pre", ".suf", null);
assertEquals((new File(tmp3.getName())).getAbsolutePath(),
String tmploc = System.getProperty("java.io.tmpdir");
assertEquals((new File(tmploc, tmp3.getName())).getAbsolutePath(),
tmp3.getAbsolutePath()); tmp3.getAbsolutePath());
} }


@@ -363,17 +364,17 @@ public class FileUtilsTest extends TestCase {
* Test contentEquals * Test contentEquals
*/ */
public void testContentEquals() throws IOException { public void testContentEquals() throws IOException {
assertTrue("Non existing files", fu.contentEquals(new File("foo"),
assertTrue("Non existing files", fu.contentEquals(new File("foo"),
new File("bar"))); new File("bar")));
assertTrue("One exists, the other one doesn\'t",
assertTrue("One exists, the other one doesn\'t",
!fu.contentEquals(new File("foo"), new File("build.xml"))); !fu.contentEquals(new File("foo"), new File("build.xml")));
assertTrue("Don\'t compare directories", assertTrue("Don\'t compare directories",
!fu.contentEquals(new File("src"), new File("src"))); !fu.contentEquals(new File("src"), new File("src")));
assertTrue("File equals itself", assertTrue("File equals itself",
fu.contentEquals(new File("build.xml"),
fu.contentEquals(new File("build.xml"),
new File("build.xml"))); new File("build.xml")));
assertTrue("Files are different", assertTrue("Files are different",
!fu.contentEquals(new File("build.xml"),
!fu.contentEquals(new File("build.xml"),
new File("docs.xml"))); new File("docs.xml")));
} }


@@ -391,34 +392,34 @@ public class FileUtilsTest extends TestCase {
* Test removeLeadingPath. * Test removeLeadingPath.
*/ */
public void testRemoveLeadingPath() { public void testRemoveLeadingPath() {
assertEquals("bar", fu.removeLeadingPath(new File("/foo"),
assertEquals("bar", fu.removeLeadingPath(new File("/foo"),
new File("/foo/bar"))); new File("/foo/bar")));
assertEquals("bar", fu.removeLeadingPath(new File("/foo/"),
assertEquals("bar", fu.removeLeadingPath(new File("/foo/"),
new File("/foo/bar"))); new File("/foo/bar")));
assertEquals("bar", fu.removeLeadingPath(new File("\\foo"),
assertEquals("bar", fu.removeLeadingPath(new File("\\foo"),
new File("\\foo\\bar"))); new File("\\foo\\bar")));
assertEquals("bar", fu.removeLeadingPath(new File("\\foo\\"),
assertEquals("bar", fu.removeLeadingPath(new File("\\foo\\"),
new File("\\foo\\bar"))); new File("\\foo\\bar")));
assertEquals("bar", fu.removeLeadingPath(new File("c:/foo"),
assertEquals("bar", fu.removeLeadingPath(new File("c:/foo"),
new File("c:/foo/bar"))); new File("c:/foo/bar")));
assertEquals("bar", fu.removeLeadingPath(new File("c:/foo/"),
assertEquals("bar", fu.removeLeadingPath(new File("c:/foo/"),
new File("c:/foo/bar"))); new File("c:/foo/bar")));
assertEquals("bar", fu.removeLeadingPath(new File("c:\\foo"),
assertEquals("bar", fu.removeLeadingPath(new File("c:\\foo"),
new File("c:\\foo\\bar"))); new File("c:\\foo\\bar")));
assertEquals("bar", fu.removeLeadingPath(new File("c:\\foo\\"),
assertEquals("bar", fu.removeLeadingPath(new File("c:\\foo\\"),
new File("c:\\foo\\bar"))); new File("c:\\foo\\bar")));
assertEquals(fu.normalize("/bar").getAbsolutePath(),
assertEquals(fu.normalize("/bar").getAbsolutePath(),
fu.removeLeadingPath(new File("/foo"), new File("/bar"))); fu.removeLeadingPath(new File("/foo"), new File("/bar")));
assertEquals(fu.normalize("/foobar").getAbsolutePath(),
assertEquals(fu.normalize("/foobar").getAbsolutePath(),
fu.removeLeadingPath(new File("/foo"), new File("/foobar"))); fu.removeLeadingPath(new File("/foo"), new File("/foobar")));
// bugzilla report 19979 // bugzilla report 19979
assertEquals("", fu.removeLeadingPath(new File("/foo/bar"),
assertEquals("", fu.removeLeadingPath(new File("/foo/bar"),
new File("/foo/bar"))); new File("/foo/bar")));
assertEquals("", fu.removeLeadingPath(new File("/foo/bar"),
assertEquals("", fu.removeLeadingPath(new File("/foo/bar"),
new File("/foo/bar/"))); new File("/foo/bar/")));
assertEquals("", fu.removeLeadingPath(new File("/foo/bar/"),
assertEquals("", fu.removeLeadingPath(new File("/foo/bar/"),
new File("/foo/bar/"))); new File("/foo/bar/")));
assertEquals("", fu.removeLeadingPath(new File("/foo/bar/"),
assertEquals("", fu.removeLeadingPath(new File("/foo/bar/"),
new File("/foo/bar"))); new File("/foo/bar")));
} }


@@ -455,7 +456,7 @@ public class FileUtilsTest extends TestCase {
assertEquals("C:\\foo", fu.fromURI("file:///c:/foo")); assertEquals("C:\\foo", fu.fromURI("file:///c:/foo"));
} }
assertEquals(localize("/foo"), fu.fromURI("file:///foo")); assertEquals(localize("/foo"), fu.fromURI("file:///foo"));
assertEquals("." + File.separator + "foo",
assertEquals("." + File.separator + "foo",
fu.fromURI("file:./foo")); fu.fromURI("file:./foo"));
assertEquals(localize("/foo bar"), fu.fromURI("file:///foo%20bar")); assertEquals(localize("/foo bar"), fu.fromURI("file:///foo%20bar"));
assertEquals(localize("/foo#bar"), fu.fromURI("file:///foo%23bar")); assertEquals(localize("/foo#bar"), fu.fromURI("file:///foo%23bar"));


Loading…
Cancel
Save