You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@ant.apache.org by ke...@apache.org on 2007/11/08 09:18:21 UTC
svn commit: r593055 - in /ant/core/branches/ANT_17_BRANCH/src:
main/org/apache/tools/ant/util/FileUtils.java
tests/junit/org/apache/tools/ant/util/FileUtilsTest.java
Author: kevj
Date: Thu Nov 8 00:18:18 2007
New Revision: 593055
URL: http://svn.apache.org/viewvc?rev=593055&view=rev
Log:
-createTempFile changes to fix error in hasErrorInCase test
Modified:
ant/core/branches/ANT_17_BRANCH/src/main/org/apache/tools/ant/util/FileUtils.java
ant/core/branches/ANT_17_BRANCH/src/tests/junit/org/apache/tools/ant/util/FileUtilsTest.java
Modified: ant/core/branches/ANT_17_BRANCH/src/main/org/apache/tools/ant/util/FileUtils.java
URL: http://svn.apache.org/viewvc/ant/core/branches/ANT_17_BRANCH/src/main/org/apache/tools/ant/util/FileUtils.java?rev=593055&r1=593054&r2=593055&view=diff
==============================================================================
--- ant/core/branches/ANT_17_BRANCH/src/main/org/apache/tools/ant/util/FileUtils.java (original)
+++ ant/core/branches/ANT_17_BRANCH/src/main/org/apache/tools/ant/util/FileUtils.java Thu Nov 8 00:18:18 2007
@@ -789,12 +789,8 @@
* <p>The file denoted by the returned abstract pathname did not
* exist before this method was invoked, any subsequent invocation
* of this method will yield a different file name.</p>
- * <p>
- * The filename is prefixNNNNNsuffix where NNNN is a random number.
- * </p>
- * <p>This method is different from File.createTempFile() of JDK 1.2
- * 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>
+ *
+ * <p>As of ant 1.8 the file is actually created.</p>
*
* @param prefix prefix before the random number.
* @param suffix file extension; include the '.'.
@@ -814,19 +810,15 @@
* <p>The file denoted by the returned abstract pathname did not
* exist before this method was invoked, any subsequent invocation
* of this method will yield a different file name.</p>
- * <p>
- * The filename is prefixNNNNNsuffix where NNNN is a random number.
- * </p>
- * <p>This method is different from File.createTempFile() of JDK 1.2
- * 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>
+ *
+ * <p>As of ant 1.8 the file is actually created.</p>
*
* @param prefix prefix before the random number.
* @param suffix file extension; include the '.'.
* @param parentDir Directory to create the temporary file in;
+ * java.io.tmpdir used if not specified.
* @param deleteOnExit whether to set the tempfile for deletion on
* normal VM exit.
- * java.io.tmpdir used if not specified.
*
* @return a File reference to the new temporary file.
* @since Ant 1.7
@@ -837,11 +829,49 @@
String parent = (parentDir == null)
? System.getProperty("java.io.tmpdir")
: parentDir.getPath();
+ try {
+ result = File.createTempFile(prefix, suffix, new File(parent));
+ } catch (IOException e) {
+ throw new BuildException("Could not create tempfile in " + parent, e);
+ }
+ if (deleteOnExit) {
+ result.deleteOnExit();
+ }
+ return result;
+ }
+
+ /**
+ * Create a File object for a temporary file in a given directory. Without
+ * actually creating the file.
+ *
+ * <p>The file denoted by the returned abstract pathname did not
+ * exist before this method was invoked, any subsequent invocation
+ * of this method will yield a different file name.</p>
+ * <p>
+ * The filename is prefixNNNNNsuffix where NNNN is a random number.
+ * </p>
+ *
+ * @param prefix prefix before the random number.
+ * @param suffix file extension; include the '.'.
+ * @param parentDir Directory to create the temporary file in;
+ * java.io.tmpdir used if not specified.
+ * @param deleteOnExit whether to set the tempfile for deletion on
+ * normal VM exit.
+ *
+ * @return a File reference to the new, nonexistent temporary file.
+ * @since Ant 1.8
+ */
+ public File createTempFileName(String prefix, String suffix,
+ File parentDir, boolean deleteOnExit) {
+ File result = null;
+ String parent = (parentDir == null) ? System
+ .getProperty("java.io.tmpdir") : parentDir.getPath();
DecimalFormat fmt = new DecimalFormat("#####");
synchronized (rand) {
do {
- result = new File(parent, prefix + fmt.format(Math.abs(rand.nextInt())) + suffix);
+ result = new File(parent, prefix
+ + fmt.format(Math.abs(rand.nextInt())) + suffix);
} while (result.exists());
}
if (deleteOnExit) {
Modified: ant/core/branches/ANT_17_BRANCH/src/tests/junit/org/apache/tools/ant/util/FileUtilsTest.java
URL: http://svn.apache.org/viewvc/ant/core/branches/ANT_17_BRANCH/src/tests/junit/org/apache/tools/ant/util/FileUtilsTest.java?rev=593055&r1=593054&r2=593055&view=diff
==============================================================================
--- ant/core/branches/ANT_17_BRANCH/src/tests/junit/org/apache/tools/ant/util/FileUtilsTest.java (original)
+++ ant/core/branches/ANT_17_BRANCH/src/tests/junit/org/apache/tools/ant/util/FileUtilsTest.java Thu Nov 8 00:18:18 2007
@@ -48,7 +48,9 @@
public void tearDown() {
if (removeThis != null && removeThis.exists()) {
- removeThis.delete();
+ if (!removeThis.delete()) {
+ removeThis.deleteOnExit();
+ }
}
}
@@ -333,12 +335,38 @@
assertEquals(f, new File("a").getAbsoluteFile());
}
+ public void testCreateTempFile() {
+ // null parent dir
+ File tmp1 = FILE_UTILS.createTempFile("pre", ".suf", null, false);
+ String tmploc = System.getProperty("java.io.tmpdir");
+ String name = tmp1.getName();
+ assertTrue("starts with pre", name.startsWith("pre"));
+ assertTrue("ends with .suf", name.endsWith(".suf"));
+ assertTrue("File was created", tmp1.exists());
+ assertEquals((new File(tmploc, tmp1.getName())).getAbsolutePath(),
+ tmp1.getAbsolutePath());
+ tmp1.delete();
+
+ File dir2 = new File(tmploc+"/ant-test");
+ dir2.mkdir();
+ removeThis = dir2;
+
+ File tmp2 = FILE_UTILS.createTempFile("pre", ".suf", dir2, true);
+ String name2 = tmp2.getName();
+ assertTrue("starts with pre", name2.startsWith("pre"));
+ assertTrue("ends with .suf", name2.endsWith(".suf"));
+ assertTrue("File was created", tmp2.exists());
+ assertEquals((new File(dir2, tmp2.getName())).getAbsolutePath(),
+ tmp2.getAbsolutePath());
+ tmp2.delete();
+ dir2.delete();
+ }
/**
- * Test createTempFile
+ * Test createTempFileName
*/
- public void testCreateTempFile() {
+ public void testCreateTempFileName() {
File parent = new File((new File("/tmp")).getAbsolutePath());
- File tmp1 = FILE_UTILS.createTempFile("pre", ".suf", parent);
+ File tmp1 = FILE_UTILS.createTempFileName("pre", ".suf", parent, false);
assertTrue("new file", !tmp1.exists());
String name = tmp1.getName();
@@ -348,12 +376,12 @@
parent.getAbsolutePath(),
tmp1.getParent());
- File tmp2 = FILE_UTILS.createTempFile("pre", ".suf", parent);
+ File tmp2 = FILE_UTILS.createTempFileName("pre", ".suf", parent, false);
assertTrue("files are different",
!tmp1.getAbsolutePath().equals(tmp2.getAbsolutePath()));
// null parent dir
- File tmp3 = FILE_UTILS.createTempFile("pre", ".suf", null);
+ File tmp3 = FILE_UTILS.createTempFileName("pre", ".suf", null, false);
String tmploc = System.getProperty("java.io.tmpdir");
assertEquals((new File(tmploc, tmp3.getName())).getAbsolutePath(),
tmp3.getAbsolutePath());
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@ant.apache.org
For additional commands, e-mail: dev-help@ant.apache.org