diff --git a/src/tests/junit/org/apache/tools/ant/filters/TokenFilterTest.java b/src/tests/junit/org/apache/tools/ant/filters/TokenFilterTest.java index 928348b52..7305d4abd 100644 --- a/src/tests/junit/org/apache/tools/ant/filters/TokenFilterTest.java +++ b/src/tests/junit/org/apache/tools/ant/filters/TokenFilterTest.java @@ -18,6 +18,7 @@ package org.apache.tools.ant.filters; +import static org.apache.tools.ant.util.FileUtils.readFully; import static org.hamcrest.Matchers.both; import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.endsWith; @@ -32,7 +33,6 @@ import java.io.IOException; import java.io.Reader; import org.apache.tools.ant.BuildFileRule; -import org.apache.tools.ant.util.FileUtils; import org.junit.Before; import org.junit.Rule; import org.junit.Test; @@ -44,8 +44,6 @@ public class TokenFilterTest { @Rule public BuildFileRule buildRule = new BuildFileRule(); - private static final FileUtils FILE_UTILS = FileUtils.getFileUtils(); - @Before public void setUp() { buildRule.configureProject("src/etc/testcases/filters/tokenfilter.xml"); @@ -268,23 +266,15 @@ public class TokenFilterTest { // ----------------------------------------------------- private String getFileString(String filename) throws IOException { - Reader r = null; - try { - r = new FileReader(FILE_UTILS.resolveFile(buildRule.getProject().getBaseDir(),filename)); - return FileUtils.readFully(r); - } - finally { - FileUtils.close(r); + try (Reader r = new FileReader(buildRule.getProject().resolveFile(filename))) { + return readFully(r); } } public static class Capitalize implements TokenFilter.Filter { public String filter(String token) { - if (token.isEmpty()) { - return token; - } - return token.substring(0, 1).toUpperCase() + token.substring(1); + return token.isEmpty() ? token : token.substring(0, 1).toUpperCase() + token.substring(1); } } diff --git a/src/tests/junit/org/apache/tools/ant/taskdefs/ExecStreamRedirectorTest.java b/src/tests/junit/org/apache/tools/ant/taskdefs/ExecStreamRedirectorTest.java index f69e6e250..6ae5f1700 100644 --- a/src/tests/junit/org/apache/tools/ant/taskdefs/ExecStreamRedirectorTest.java +++ b/src/tests/junit/org/apache/tools/ant/taskdefs/ExecStreamRedirectorTest.java @@ -4,6 +4,7 @@ import org.apache.tools.ant.BuildFileRule; import org.junit.Before; import org.junit.Rule; import org.junit.Test; +import org.junit.rules.TemporaryFolder; import java.io.ByteArrayOutputStream; import java.io.File; @@ -23,10 +24,15 @@ public class ExecStreamRedirectorTest { @Rule public BuildFileRule buildRule = new BuildFileRule(); + @Rule + public TemporaryFolder folder = new TemporaryFolder(); + + private File outputDir; + @Before - public void setUp() { + public void setUp() throws IOException { buildRule.configureProject("src/etc/testcases/taskdefs/exec/exec-with-redirector.xml"); - final File outputDir = this.createTmpDir(); + outputDir = folder.newFolder(String.valueOf("temp-" + System.nanoTime())); buildRule.getProject().setUserProperty("output", outputDir.toString()); buildRule.executeTarget("setUp"); } @@ -47,10 +53,9 @@ public class ExecStreamRedirectorTest { buildRule.executeTarget("list-dir"); // verify the redirected output - final String outputDirPath = buildRule.getProject().getProperty("output"); byte[] dirListingOutput = null; for (int i = 1; i <= 16; i++) { - final File redirectedOutputFile = new File(outputDirPath, "ls" + i + ".txt"); + final File redirectedOutputFile = new File(outputDir, "ls" + i + ".txt"); assertTrue(redirectedOutputFile + " is missing or not a regular file", redirectedOutputFile.isFile()); final byte[] redirectedOutput = readAllBytes(redirectedOutputFile); @@ -68,14 +73,6 @@ public class ExecStreamRedirectorTest { } } - private File createTmpDir() { - final File tmpDir = new File(System.getProperty("java.io.tmpdir"), - String.valueOf("temp-" + System.nanoTime())); - tmpDir.mkdir(); - tmpDir.deleteOnExit(); - return tmpDir; - } - private static byte[] readAllBytes(final File file) throws IOException { final ByteArrayOutputStream bos = new ByteArrayOutputStream(); try (FileInputStream fis = new FileInputStream(file)) { diff --git a/src/tests/junit/org/apache/tools/ant/taskdefs/optional/junit/JUnitReportTest.java b/src/tests/junit/org/apache/tools/ant/taskdefs/optional/junit/JUnitReportTest.java index f4d111a77..0d39e4136 100644 --- a/src/tests/junit/org/apache/tools/ant/taskdefs/optional/junit/JUnitReportTest.java +++ b/src/tests/junit/org/apache/tools/ant/taskdefs/optional/junit/JUnitReportTest.java @@ -18,6 +18,7 @@ package org.apache.tools.ant.taskdefs.optional.junit; +import static org.apache.tools.ant.util.FileUtils.getFileUtils; import static org.hamcrest.Matchers.containsString; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; @@ -88,12 +89,9 @@ public class JUnitReportTest { assertTrue("Can't read the report file.", reportFile.canRead()); assertTrue("File shouldn't be empty.", reportFile.length() > 0); // conversion to URL via FileUtils like in XMLResultAggregator, not as suggested in the bug report - URL reportUrl = new URL(FileUtils.getFileUtils().toURI(reportFile.getAbsolutePath())); - InputStream reportStream = reportUrl.openStream(); - try { + URL reportUrl = new URL(getFileUtils().toURI(reportFile.getAbsolutePath())); + try (InputStream reportStream = reportUrl.openStream()) { assertTrue("This shouldn't be an empty stream.", reportStream.available() > 0); - } finally { - FileUtils.close(reportStream); } } @@ -126,16 +124,13 @@ public class JUnitReportTest { public void testStackTraceLineBreaks() throws Exception { buildRule.executeTarget("testStackTraceLineBreaks"); assertIndexCreated(); - FileReader r = null; - try { - r = new FileReader(new File(buildRule.getOutputDir(), "html/sampleproject/coins/0_CoinTest.html")); + try (FileReader r = new FileReader(new File(buildRule.getOutputDir(), + "html/sampleproject/coins/0_CoinTest.html"))) { String report = FileUtils.readFully(r); assertThat("output must contain
:\n" + report, report, containsString("junit.framework.AssertionFailedError: DOEG
")); assertThat("#51049: output must translate line breaks:\n" + report, report, containsString("cur['line.separator'] = '\\r\\n';")); - } finally { - FileUtils.close(r); } } diff --git a/src/tests/junit/org/apache/tools/ant/util/FileUtilsTest.java b/src/tests/junit/org/apache/tools/ant/util/FileUtilsTest.java index 6bd260d74..cef4d54a8 100644 --- a/src/tests/junit/org/apache/tools/ant/util/FileUtilsTest.java +++ b/src/tests/junit/org/apache/tools/ant/util/FileUtilsTest.java @@ -30,13 +30,15 @@ import java.util.Optional; import org.apache.tools.ant.BuildException; import org.apache.tools.ant.MagicTestNames; import org.apache.tools.ant.taskdefs.condition.Os; -import org.junit.After; -import org.junit.Assert; import org.junit.Before; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; +import org.junit.rules.TemporaryFolder; +import static org.apache.tools.ant.util.FileUtils.getFileUtils; +import static org.apache.tools.ant.util.FileUtils.isCaseSensitiveFileSystem; +import static org.apache.tools.ant.util.FileUtils.isContextRelativePath; import static org.hamcrest.Matchers.endsWith; import static org.hamcrest.Matchers.startsWith; import static org.junit.Assert.assertEquals; @@ -56,9 +58,10 @@ public class FileUtilsTest { @Rule public ExpectedException thrown = ExpectedException.none(); - private static final FileUtils FILE_UTILS = FileUtils.getFileUtils(); + @Rule + public TemporaryFolder folder = new TemporaryFolder(); + private static final String ROOT = System.getProperty(MagicTestNames.TEST_ROOT_DIRECTORY); - private File removeThis; private String root; @Before @@ -67,13 +70,6 @@ public class FileUtilsTest { root = new File(File.separator).getAbsolutePath().toUpperCase(); } - @After - public void tearDown() { - if (removeThis != null && removeThis.exists() && !removeThis.delete()) { - removeThis.deleteOnExit(); - } - } - /** * test modification. * Since Ant1.7, the method being tested no longer uses @@ -86,17 +82,16 @@ public class FileUtilsTest { */ @Test public void testSetLastModified() throws IOException { - removeThis = new File("dummy"); - FileOutputStream fos = new FileOutputStream(removeThis); - fos.write(new byte[0]); - fos.close(); + File removeThis = folder.newFile("dummy"); + try (FileOutputStream fos = new FileOutputStream(removeThis)) { + fos.write(new byte[0]); + } assumeTrue("Could not change file modified time", removeThis.setLastModified(removeThis.lastModified() - 2000)); long modTime = removeThis.lastModified(); assertNotEquals(0, modTime); - - FILE_UTILS.setFileLastModified(removeThis, -1); + getFileUtils().setFileLastModified(removeThis, -1); long secondModTime = removeThis.lastModified(); assertTrue(secondModTime > modTime); @@ -105,7 +100,7 @@ public class FileUtilsTest { // 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 - FILE_UTILS.setFileLastModified(removeThis, secondModTime + millisperday); + getFileUtils().setFileLastModified(removeThis, secondModTime + millisperday); long thirdModTime = removeThis.lastModified(); /* * I would love to compare this with 123456, but depending on @@ -122,37 +117,37 @@ public class FileUtilsTest { /* * Start with simple absolute file names. */ - assertEquals(File.separator, FILE_UTILS.resolveFile(null, "/").getPath()); - assertEquals(File.separator, FILE_UTILS.resolveFile(null, "\\").getPath()); + assertEquals(File.separator, getFileUtils().resolveFile(null, "/").getPath()); + assertEquals(File.separator, getFileUtils().resolveFile(null, "\\").getPath()); } @Test public void testResolveFileDosOrNetware() { assumeTrue("Not DOS or Netware", Os.isFamily("dos") || Os.isFamily("netware")); assertEqualsIgnoreDriveCase(localize(File.separator), - FILE_UTILS.resolveFile(null, "/").getPath()); + getFileUtils().resolveFile(null, "/").getPath()); assertEqualsIgnoreDriveCase(localize(File.separator), - FILE_UTILS.resolveFile(null, "\\").getPath()); + getFileUtils().resolveFile(null, "\\").getPath()); /* * throw in drive letters */ String driveSpec = "C:"; assertEquals(driveSpec + "\\", - FILE_UTILS.resolveFile(null, driveSpec + "/").getPath()); + getFileUtils().resolveFile(null, driveSpec + "/").getPath()); assertEquals(driveSpec + "\\", - FILE_UTILS.resolveFile(null, driveSpec + "\\").getPath()); + getFileUtils().resolveFile(null, driveSpec + "\\").getPath()); String driveSpecLower = "c:"; assertEquals(driveSpecLower + "\\", - FILE_UTILS.resolveFile(null, driveSpecLower + "/").getPath()); + getFileUtils().resolveFile(null, driveSpecLower + "/").getPath()); assertEquals(driveSpecLower + "\\", - FILE_UTILS.resolveFile(null, driveSpecLower + "\\").getPath()); + getFileUtils().resolveFile(null, driveSpecLower + "\\").getPath()); /* * promised to eliminate consecutive slashes after drive letter. */ assertEquals(driveSpec + "\\", - FILE_UTILS.resolveFile(null, driveSpec + "/////").getPath()); + getFileUtils().resolveFile(null, driveSpec + "/////").getPath()); assertEquals(driveSpec + "\\", - FILE_UTILS.resolveFile(null, driveSpec + "\\\\\\\\\\\\").getPath()); + getFileUtils().resolveFile(null, driveSpec + "\\\\\\\\\\\\").getPath()); if (Os.isFamily("netware")) { /* @@ -160,21 +155,21 @@ public class FileUtilsTest { */ driveSpec = "SYS:"; assertEquals(driveSpec, - FILE_UTILS.resolveFile(null, driveSpec + "/").getPath()); + getFileUtils().resolveFile(null, driveSpec + "/").getPath()); assertEquals(driveSpec, - FILE_UTILS.resolveFile(null, driveSpec + "\\").getPath()); + getFileUtils().resolveFile(null, driveSpec + "\\").getPath()); driveSpecLower = "sys:"; assertEquals(driveSpec, - FILE_UTILS.resolveFile(null, driveSpecLower + "/").getPath()); + getFileUtils().resolveFile(null, driveSpecLower + "/").getPath()); assertEquals(driveSpec, - FILE_UTILS.resolveFile(null, driveSpecLower + "\\").getPath()); + getFileUtils().resolveFile(null, driveSpecLower + "\\").getPath()); /* * promised to eliminate consecutive slashes after drive letter. */ assertEquals(driveSpec, - FILE_UTILS.resolveFile(null, driveSpec + "/////").getPath()); + getFileUtils().resolveFile(null, driveSpec + "/////").getPath()); assertEquals(driveSpec, - FILE_UTILS.resolveFile(null, driveSpec + "\\\\\\\\\\\\").getPath()); + getFileUtils().resolveFile(null, driveSpec + "\\\\\\\\\\\\").getPath()); } } @Test @@ -186,14 +181,14 @@ public class FileUtilsTest { String driveSpec = "C:"; String udir = System.getProperty("user.dir"); assertEquals(udir + File.separator + driveSpec, - FILE_UTILS.resolveFile(null, driveSpec + "/").getPath()); + getFileUtils().resolveFile(null, driveSpec + "/").getPath()); assertEquals(udir + File.separator + driveSpec, - FILE_UTILS.resolveFile(null, driveSpec + "\\").getPath()); + getFileUtils().resolveFile(null, driveSpec + "\\").getPath()); String driveSpecLower = "c:"; assertEquals(udir + File.separator + driveSpecLower, - FILE_UTILS.resolveFile(null, driveSpecLower + "/").getPath()); + getFileUtils().resolveFile(null, driveSpecLower + "/").getPath()); assertEquals(udir + File.separator + driveSpecLower, - FILE_UTILS.resolveFile(null, driveSpecLower + "\\").getPath()); + getFileUtils().resolveFile(null, driveSpecLower + "\\").getPath()); } /* @@ -202,25 +197,25 @@ public class FileUtilsTest { @Test public void testResolveRelativeFile() { assertEquals(localize("/1/2/3/4"), - FILE_UTILS.resolveFile(new File(localize("/1/2/3")), "4").getPath()); + getFileUtils().resolveFile(new File(localize("/1/2/3")), "4").getPath()); assertEquals(localize("/1/2/3/4"), - FILE_UTILS.resolveFile(new File(localize("/1/2/3")), "./4").getPath()); + getFileUtils().resolveFile(new File(localize("/1/2/3")), "./4").getPath()); assertEquals(localize("/1/2/3/4"), - FILE_UTILS.resolveFile(new File(localize("/1/2/3")), ".\\4").getPath()); + getFileUtils().resolveFile(new File(localize("/1/2/3")), ".\\4").getPath()); assertEquals(localize("/1/2/3/4"), - FILE_UTILS.resolveFile(new File(localize("/1/2/3")), "./.\\4").getPath()); + getFileUtils().resolveFile(new File(localize("/1/2/3")), "./.\\4").getPath()); assertEquals(localize("/1/2/3/4"), - FILE_UTILS.resolveFile(new File(localize("/1/2/3")), "../3/4").getPath()); + getFileUtils().resolveFile(new File(localize("/1/2/3")), "../3/4").getPath()); assertEquals(localize("/1/2/3/4"), - FILE_UTILS.resolveFile(new File(localize("/1/2/3")), "..\\3\\4").getPath()); + getFileUtils().resolveFile(new File(localize("/1/2/3")), "..\\3\\4").getPath()); assertEquals(localize("/1/2/3/4"), - FILE_UTILS.resolveFile(new File(localize("/1/2/3")), "../../5/.././2/./3/6/../4").getPath()); + getFileUtils().resolveFile(new File(localize("/1/2/3")), "../../5/.././2/./3/6/../4").getPath()); assertEquals(localize("/1/2/3/4"), - FILE_UTILS.resolveFile(new File(localize("/1/2/3")), "..\\../5/..\\./2/./3/6\\../4").getPath()); + getFileUtils().resolveFile(new File(localize("/1/2/3")), "..\\../5/..\\./2/./3/6\\../4").getPath()); assertEquals("meaningless result but no exception", new File(localize("/1/../../b")), - FILE_UTILS.resolveFile(new File(localize("/1")), "../../b")); + getFileUtils().resolveFile(new File(localize("/1")), "../../b")); } @@ -230,27 +225,27 @@ public class FileUtilsTest { /* * Start with simple absolute file names. */ - assertEquals(File.separator, FILE_UTILS.normalize("/").getPath()); - assertEquals(File.separator, FILE_UTILS.normalize("\\").getPath()); + assertEquals(File.separator, getFileUtils().normalize("/").getPath()); + assertEquals(File.separator, getFileUtils().normalize("\\").getPath()); // Expected exception caught thrown.expect(BuildException.class); String driveSpec = "C:"; - assertEquals(driveSpec, FILE_UTILS.normalize(driveSpec).getPath()); + assertEquals(driveSpec, getFileUtils().normalize(driveSpec).getPath()); } @Test public void testNormalizeSlashDosOrNetware() { assumeTrue("Not DOS or Netware", Os.isFamily("dos") || Os.isFamily("netware")); thrown.expect(BuildException.class); - FILE_UTILS.normalize("/").getPath(); + getFileUtils().normalize("/").getPath(); } @Test public void testNormalizeBackSlashDosOrNetware() { assumeTrue("Not DOS or Netware", Os.isFamily("dos") || Os.isFamily("netware")); thrown.expect(BuildException.class); - FILE_UTILS.normalize("\\").getPath(); + getFileUtils().normalize("\\").getPath(); } @Test @@ -262,52 +257,52 @@ public class FileUtilsTest { */ String driveSpec = "C:"; assertEquals(driveSpec + "\\", - FILE_UTILS.normalize(driveSpec + "/").getPath()); + getFileUtils().normalize(driveSpec + "/").getPath()); assertEquals(driveSpec + "\\", - FILE_UTILS.normalize(driveSpec + "\\").getPath()); + getFileUtils().normalize(driveSpec + "\\").getPath()); String driveSpecLower = "c:"; assertEquals(driveSpecLower + "\\", - FILE_UTILS.normalize(driveSpecLower + "/").getPath()); + getFileUtils().normalize(driveSpecLower + "/").getPath()); assertEquals(driveSpecLower + "\\", - FILE_UTILS.normalize(driveSpecLower + "\\").getPath()); + getFileUtils().normalize(driveSpecLower + "\\").getPath()); /* * promised to eliminate consecutive slashes after drive letter. */ assertEquals(driveSpec + "\\", - FILE_UTILS.normalize(driveSpec + "/////").getPath()); + getFileUtils().normalize(driveSpec + "/////").getPath()); assertEquals(driveSpec + "\\", - FILE_UTILS.normalize(driveSpec + "\\\\\\\\\\\\").getPath()); + getFileUtils().normalize(driveSpec + "\\\\\\\\\\\\").getPath()); // Expected exception caught thrown.expect(BuildException.class); - FILE_UTILS.normalize(driveSpec).getPath(); + getFileUtils().normalize(driveSpec).getPath(); } else if (Os.isFamily("netware")) { /* * throw in NetWare volume names */ String driveSpec = "SYS:"; assertEquals(driveSpec, - FILE_UTILS.normalize(driveSpec).getPath()); + getFileUtils().normalize(driveSpec).getPath()); assertEquals(driveSpec, - FILE_UTILS.normalize(driveSpec + "/").getPath()); + getFileUtils().normalize(driveSpec + "/").getPath()); assertEquals(driveSpec, - FILE_UTILS.normalize(driveSpec + "\\").getPath()); + getFileUtils().normalize(driveSpec + "\\").getPath()); String driveSpecLower = "sys:"; assertEquals(driveSpec, - FILE_UTILS.normalize(driveSpecLower).getPath()); + getFileUtils().normalize(driveSpecLower).getPath()); assertEquals(driveSpec, - FILE_UTILS.normalize(driveSpecLower + "/").getPath()); + getFileUtils().normalize(driveSpecLower + "/").getPath()); assertEquals(driveSpec, - FILE_UTILS.normalize(driveSpecLower + "\\").getPath()); + getFileUtils().normalize(driveSpecLower + "\\").getPath()); assertEquals(driveSpec + "\\junk", - FILE_UTILS.normalize(driveSpecLower + "\\junk").getPath()); + getFileUtils().normalize(driveSpecLower + "\\junk").getPath()); /* * promised to eliminate consecutive slashes after drive letter. */ assertEquals(driveSpec, - FILE_UTILS.normalize(driveSpec + "/////").getPath()); + getFileUtils().normalize(driveSpec + "/////").getPath()); assertEquals(driveSpec, - FILE_UTILS.normalize(driveSpec + "\\\\\\\\\\\\").getPath()); + getFileUtils().normalize(driveSpec + "\\\\\\\\\\\\").getPath()); } } @@ -317,29 +312,29 @@ public class FileUtilsTest { @Test public void testNormalizeRelativeFile() { assertEquals(localize("/1/2/3/4"), - FILE_UTILS.normalize(localize("/1/2/3/4")).getPath()); + getFileUtils().normalize(localize("/1/2/3/4")).getPath()); assertEquals(localize("/1/2/3/4"), - FILE_UTILS.normalize(localize("/1/2/3/./4")).getPath()); + getFileUtils().normalize(localize("/1/2/3/./4")).getPath()); assertEquals(localize("/1/2/3/4"), - FILE_UTILS.normalize(localize("/1/2/3/.\\4")).getPath()); + getFileUtils().normalize(localize("/1/2/3/.\\4")).getPath()); assertEquals(localize("/1/2/3/4"), - FILE_UTILS.normalize(localize("/1/2/3/./.\\4")).getPath()); + getFileUtils().normalize(localize("/1/2/3/./.\\4")).getPath()); assertEquals(localize("/1/2/3/4"), - FILE_UTILS.normalize(localize("/1/2/3/../3/4")).getPath()); + getFileUtils().normalize(localize("/1/2/3/../3/4")).getPath()); assertEquals(localize("/1/2/3/4"), - FILE_UTILS.normalize(localize("/1/2/3/..\\3\\4")).getPath()); + getFileUtils().normalize(localize("/1/2/3/..\\3\\4")).getPath()); assertEquals(localize("/1/2/3/4"), - FILE_UTILS.normalize(localize("/1/2/3/../../5/.././2/./3/6/../4")).getPath()); + getFileUtils().normalize(localize("/1/2/3/../../5/.././2/./3/6/../4")).getPath()); assertEquals(localize("/1/2/3/4"), - FILE_UTILS.normalize(localize("/1/2/3/..\\../5/..\\./2/./3/6\\../4")).getPath()); + getFileUtils().normalize(localize("/1/2/3/..\\../5/..\\./2/./3/6\\../4")).getPath()); assertEquals("will not go outside FS root (but will not throw an exception either)", new File(localize("/1/../../b")), - FILE_UTILS.normalize(localize("/1/../../b"))); + getFileUtils().normalize(localize("/1/../../b"))); // Expected exception caught thrown.expect(BuildException.class); - FILE_UTILS.normalize("foo"); + getFileUtils().normalize("foo"); } /** @@ -347,22 +342,21 @@ public class FileUtilsTest { */ @Test public void testNullArgs() { - File f = FILE_UTILS.resolveFile(null, "a"); + File f = getFileUtils().resolveFile(null, "a"); assertEquals(f, new File("a").getAbsoluteFile()); // Expected exception caught thrown.expect(NullPointerException.class); - FILE_UTILS.normalize(null); + getFileUtils().normalize(null); } - /** * Test createTempFile */ @Test - public void testCreateTempFile() { + public void testCreateTempFile() throws IOException { // null parent dir - File tmp1 = FILE_UTILS.createTempFile("pre", ".suf", null, false, true); + File tmp1 = getFileUtils().createTempFile("pre", ".suf", null, false, true); String tmploc = System.getProperty("java.io.tmpdir"); String name = tmp1.getName(); assertThat("starts with pre", name, startsWith("pre")); @@ -372,11 +366,9 @@ public class FileUtilsTest { tmp1.getAbsolutePath()); tmp1.delete(); - File dir2 = new File(tmploc + "/ant-test"); - dir2.mkdir(); - removeThis = dir2; + File dir2 = folder.newFolder("ant-test"); - File tmp2 = FILE_UTILS.createTempFile("pre", ".suf", dir2, true, true); + File tmp2 = getFileUtils().createTempFile("pre", ".suf", dir2, true, true); String name2 = tmp2.getName(); assertThat("starts with pre", name2, startsWith("pre")); assertThat("ends with .suf", name2, endsWith(".suf")); @@ -384,10 +376,9 @@ public class FileUtilsTest { assertEquals((new File(dir2, tmp2.getName())).getAbsolutePath(), tmp2.getAbsolutePath()); tmp2.delete(); - dir2.delete(); File parent = new File((new File("/tmp")).getAbsolutePath()); - tmp1 = FILE_UTILS.createTempFile("pre", ".suf", parent, false); + tmp1 = getFileUtils().createTempFile("pre", ".suf", parent, false); assertFalse("new file", tmp1.exists()); name = tmp1.getName(); @@ -396,11 +387,11 @@ public class FileUtilsTest { assertEquals("is inside parent dir", parent.getAbsolutePath(), tmp1 .getParent()); - tmp2 = FILE_UTILS.createTempFile("pre", ".suf", parent, false); + tmp2 = getFileUtils().createTempFile("pre", ".suf", parent, false); assertNotEquals("files are different", tmp1.getAbsolutePath(), tmp2.getAbsolutePath()); // null parent dir - File tmp3 = FILE_UTILS.createTempFile("pre", ".suf", null, false); + File tmp3 = getFileUtils().createTempFile("pre", ".suf", null, false); tmploc = System.getProperty("java.io.tmpdir"); assertEquals((new File(tmploc, tmp3.getName())).getAbsolutePath(), tmp3.getAbsolutePath()); @@ -412,19 +403,19 @@ public class FileUtilsTest { @Test public void testContentEquals() throws IOException { assertTrue("Non existing files", - FILE_UTILS.contentEquals(new File(ROOT, "foo"), + getFileUtils().contentEquals(new File(ROOT, "foo"), new File(ROOT, "bar"))); assertFalse("One exists, the other one doesn\'t", - FILE_UTILS.contentEquals(new File(ROOT, "foo"), + getFileUtils().contentEquals(new File(ROOT, "foo"), new File(ROOT, "build.xml"))); assertFalse("Don\'t compare directories", - FILE_UTILS.contentEquals(new File(ROOT, "src"), + getFileUtils().contentEquals(new File(ROOT, "src"), new File(ROOT, "src"))); assertTrue("File equals itself", - FILE_UTILS.contentEquals(new File(ROOT, "build.xml"), + getFileUtils().contentEquals(new File(ROOT, "build.xml"), new File(ROOT, "build.xml"))); assertFalse("Files are different", - FILE_UTILS.contentEquals(new File(ROOT, "build.xml"), + getFileUtils().contentEquals(new File(ROOT, "build.xml"), new File(ROOT, "docs.xml"))); } @@ -433,9 +424,10 @@ public class FileUtilsTest { */ @Test public void testCreateNewFile() throws IOException { - removeThis = new File("dummy"); + File removeThis = new File("dummy"); + removeThis.deleteOnExit(); assertFalse(removeThis.exists()); - FILE_UTILS.createNewFile(removeThis); + getFileUtils().createNewFile(removeThis); assertTrue(removeThis.exists()); } @@ -444,45 +436,45 @@ public class FileUtilsTest { */ @Test public void testRemoveLeadingPath() { - assertEquals("bar", FILE_UTILS.removeLeadingPath(new File("/foo"), + assertEquals("bar", getFileUtils().removeLeadingPath(new File("/foo"), new File("/foo/bar"))); - assertEquals("bar", FILE_UTILS.removeLeadingPath(new File("/foo/"), + assertEquals("bar", getFileUtils().removeLeadingPath(new File("/foo/"), new File("/foo/bar"))); - assertEquals("bar", FILE_UTILS.removeLeadingPath(new File("\\foo"), + assertEquals("bar", getFileUtils().removeLeadingPath(new File("\\foo"), new File("\\foo\\bar"))); - assertEquals("bar", FILE_UTILS.removeLeadingPath(new File("\\foo\\"), + assertEquals("bar", getFileUtils().removeLeadingPath(new File("\\foo\\"), new File("\\foo\\bar"))); - assertEquals("bar", FILE_UTILS.removeLeadingPath(new File("c:/foo"), + assertEquals("bar", getFileUtils().removeLeadingPath(new File("c:/foo"), new File("c:/foo/bar"))); - assertEquals("bar", FILE_UTILS.removeLeadingPath(new File("c:/foo/"), + assertEquals("bar", getFileUtils().removeLeadingPath(new File("c:/foo/"), new File("c:/foo/bar"))); - assertEquals("bar", FILE_UTILS.removeLeadingPath(new File("c:\\foo"), + assertEquals("bar", getFileUtils().removeLeadingPath(new File("c:\\foo"), new File("c:\\foo\\bar"))); - assertEquals("bar", FILE_UTILS.removeLeadingPath(new File("c:\\foo\\"), + assertEquals("bar", getFileUtils().removeLeadingPath(new File("c:\\foo\\"), new File("c:\\foo\\bar"))); if (!Os.isFamily("dos") && !Os.isFamily("netware")) { - assertEquals(FILE_UTILS.normalize("/bar").getAbsolutePath(), - FILE_UTILS.removeLeadingPath(new File("/foo"), new File("/bar"))); - assertEquals(FILE_UTILS.normalize("/foobar").getAbsolutePath(), - FILE_UTILS.removeLeadingPath(new File("/foo"), new File("/foobar"))); + assertEquals(getFileUtils().normalize("/bar").getAbsolutePath(), + getFileUtils().removeLeadingPath(new File("/foo"), new File("/bar"))); + assertEquals(getFileUtils().normalize("/foobar").getAbsolutePath(), + getFileUtils().removeLeadingPath(new File("/foo"), new File("/foobar"))); } // bugzilla report 19979 - assertEquals("", FILE_UTILS.removeLeadingPath(new File("/foo/bar"), + assertEquals("", getFileUtils().removeLeadingPath(new File("/foo/bar"), new File("/foo/bar"))); - assertEquals("", FILE_UTILS.removeLeadingPath(new File("/foo/bar"), + assertEquals("", getFileUtils().removeLeadingPath(new File("/foo/bar"), new File("/foo/bar/"))); - assertEquals("", FILE_UTILS.removeLeadingPath(new File("/foo/bar/"), + assertEquals("", getFileUtils().removeLeadingPath(new File("/foo/bar/"), new File("/foo/bar/"))); - assertEquals("", FILE_UTILS.removeLeadingPath(new File("/foo/bar/"), + assertEquals("", getFileUtils().removeLeadingPath(new File("/foo/bar/"), new File("/foo/bar"))); String expected = "foo/bar".replace('\\', File.separatorChar) .replace('/', File.separatorChar); - assertEquals(expected, FILE_UTILS.removeLeadingPath(new File("/"), + assertEquals(expected, getFileUtils().removeLeadingPath(new File("/"), new File("/foo/bar"))); - assertEquals(expected, FILE_UTILS.removeLeadingPath(new File("c:/"), + assertEquals(expected, getFileUtils().removeLeadingPath(new File("c:/"), new File("c:/foo/bar"))); - assertEquals(expected, FILE_UTILS.removeLeadingPath(new File("c:\\"), + assertEquals(expected, getFileUtils().removeLeadingPath(new File("c:\\"), new File("c:\\foo\\bar"))); } @@ -499,28 +491,28 @@ public class FileUtilsTest { dosRoot = ""; } if (Os.isFamily("dos")) { - assertEquals("file:/c:/foo", removeExtraneousAuthority(FILE_UTILS.toURI("c:\\foo"))); + assertEquals("file:/c:/foo", removeExtraneousAuthority(getFileUtils().toURI("c:\\foo"))); } if (Os.isFamily("netware")) { - assertEquals("file:/SYS:/foo", removeExtraneousAuthority(FILE_UTILS.toURI("sys:\\foo"))); + assertEquals("file:/SYS:/foo", removeExtraneousAuthority(getFileUtils().toURI("sys:\\foo"))); } if (File.pathSeparatorChar == '/') { - assertEquals("file:/foo", removeExtraneousAuthority(FILE_UTILS.toURI("/foo"))); - assertThat("file: URIs must name absolute paths", FILE_UTILS.toURI("./foo"), startsWith("file:/")); - assertThat(FILE_UTILS.toURI("./foo"), endsWith("/foo")); - assertEquals("file:/" + dosRoot + "foo%20bar", removeExtraneousAuthority(FILE_UTILS.toURI("/foo bar"))); - assertEquals("file:/" + dosRoot + "foo%23bar", removeExtraneousAuthority(FILE_UTILS.toURI("/foo#bar"))); + assertEquals("file:/foo", removeExtraneousAuthority(getFileUtils().toURI("/foo"))); + assertThat("file: URIs must name absolute paths", getFileUtils().toURI("./foo"), startsWith("file:/")); + assertThat(getFileUtils().toURI("./foo"), endsWith("/foo")); + assertEquals("file:/" + dosRoot + "foo%20bar", removeExtraneousAuthority(getFileUtils().toURI("/foo bar"))); + assertEquals("file:/" + dosRoot + "foo%23bar", removeExtraneousAuthority(getFileUtils().toURI("/foo#bar"))); } else if (File.pathSeparatorChar == '\\') { - assertEquals("file:/" + dosRoot + "foo", removeExtraneousAuthority(FILE_UTILS.toURI("\\foo"))); - assertThat("file: URIs must name absolute paths", FILE_UTILS.toURI(".\\foo"), startsWith("file:/")); - assertThat(FILE_UTILS.toURI(".\\foo"), endsWith("/foo")); - assertEquals("file:/" + dosRoot + "foo%20bar", removeExtraneousAuthority(FILE_UTILS.toURI("\\foo bar"))); - assertEquals("file:/" + dosRoot + "foo%23bar", removeExtraneousAuthority(FILE_UTILS.toURI("\\foo#bar"))); + assertEquals("file:/" + dosRoot + "foo", removeExtraneousAuthority(getFileUtils().toURI("\\foo"))); + assertThat("file: URIs must name absolute paths", getFileUtils().toURI(".\\foo"), startsWith("file:/")); + assertThat(getFileUtils().toURI(".\\foo"), endsWith("/foo")); + assertEquals("file:/" + dosRoot + "foo%20bar", removeExtraneousAuthority(getFileUtils().toURI("\\foo bar"))); + assertEquals("file:/" + dosRoot + "foo%23bar", removeExtraneousAuthority(getFileUtils().toURI("\\foo#bar"))); } // a test with ant for germans // the escaped character used for the test is the "a umlaut" // this is the fix for the bug 37348 - assertEquals("file:/" + dosRoot + "%C3%A4nt", removeExtraneousAuthority(FILE_UTILS.toURI("/\u00E4nt"))); + assertEquals("file:/" + dosRoot + "%C3%A4nt", removeExtraneousAuthority(getFileUtils().toURI("/\u00E4nt"))); } /** @@ -539,8 +531,8 @@ public class FileUtilsTest { @Test public void testIsContextRelativePath() { assumeTrue("Test only runs on DOS", Os.isFamily("dos")); - assertTrue(FileUtils.isContextRelativePath("/\u00E4nt")); - assertTrue(FileUtils.isContextRelativePath("\\foo")); + assertTrue(isContextRelativePath("/\u00E4nt")); + assertTrue(isContextRelativePath("\\foo")); } /** @@ -555,16 +547,16 @@ public class FileUtilsTest { dosRoot = ""; } if (Os.isFamily("netware")) { - assertEqualsIgnoreDriveCase("SYS:\\foo", FILE_UTILS.fromURI("file:///sys:/foo")); + assertEqualsIgnoreDriveCase("SYS:\\foo", getFileUtils().fromURI("file:///sys:/foo")); } if (Os.isFamily("dos")) { - assertEqualsIgnoreDriveCase("C:\\foo", FILE_UTILS.fromURI("file:///c:/foo")); + assertEqualsIgnoreDriveCase("C:\\foo", getFileUtils().fromURI("file:///c:/foo")); } - assertEqualsIgnoreDriveCase(dosRoot + File.separator + "foo", FILE_UTILS.fromURI("file:///foo")); + assertEqualsIgnoreDriveCase(dosRoot + File.separator + "foo", getFileUtils().fromURI("file:///foo")); assertEquals("." + File.separator + "foo", - FILE_UTILS.fromURI("file:./foo")); - assertEquals(dosRoot + File.separator + "foo bar", FILE_UTILS.fromURI("file:///foo%20bar")); - assertEquals(dosRoot + File.separator + "foo#bar", FILE_UTILS.fromURI("file:///foo%23bar")); + getFileUtils().fromURI("file:./foo")); + assertEquals(dosRoot + File.separator + "foo bar", getFileUtils().fromURI("file:///foo%20bar")); + assertEquals(dosRoot + File.separator + "foo#bar", getFileUtils().fromURI("file:///foo%23bar")); } @Test @@ -581,30 +573,30 @@ public class FileUtilsTest { //check that older is up to date with a newer dest assertTrue("older source files are up to date", - FILE_UTILS.isUpToDate(firstTime,secondTime)); + getFileUtils().isUpToDate(firstTime,secondTime)); //check that older is up to date with a newer dest assertFalse("newer source files are no up to date", - FILE_UTILS.isUpToDate(secondTime, firstTime)); + getFileUtils().isUpToDate(secondTime, firstTime)); assertFalse("-1 dest timestamp implies nonexistence", - FILE_UTILS.isUpToDate(firstTime, -1L)); + getFileUtils().isUpToDate(firstTime, -1L)); } @Test public void testHasErrorInCase() { File tempFolder = new File(System.getProperty("java.io.tmpdir")); - File wellcased = FILE_UTILS.createTempFile("alpha", "beta", tempFolder, + File wellcased = getFileUtils().createTempFile("alpha", "beta", tempFolder, true, true); String s = wellcased.getName().toUpperCase(); File wrongcased = new File(tempFolder, s); if (Os.isFamily("mac") && Os.isFamily("unix")) { //no guarantees on filesystem case-sensitivity } else if (Os.isFamily("dos")) { - assertTrue(FILE_UTILS.hasErrorInCase(wrongcased)); - assertFalse(FILE_UTILS.hasErrorInCase(wellcased)); + assertTrue(getFileUtils().hasErrorInCase(wrongcased)); + assertFalse(getFileUtils().hasErrorInCase(wellcased)); } else { - assertFalse(FILE_UTILS.hasErrorInCase(wrongcased)); - assertFalse(FILE_UTILS.hasErrorInCase(wellcased)); + assertFalse(getFileUtils().hasErrorInCase(wrongcased)); + assertFalse(getFileUtils().hasErrorInCase(wellcased)); } } @@ -612,7 +604,7 @@ public class FileUtilsTest { @Test public void testGetDefaultEncoding() { // This just tests that the function does not blow up - FILE_UTILS.getDefaultEncoding(); + getFileUtils().getDefaultEncoding(); } /** @@ -620,9 +612,9 @@ public class FileUtilsTest { */ @Test public void isLeadingPathCannotBeFooledByTooManyDoubleDots() { - assertFalse(FILE_UTILS.isLeadingPath(new File("/foo"), new File("/foo/../../bar"))); - assertFalse(FILE_UTILS.isLeadingPath(new File("c:\\foo"), new File("c:\\foo\\..\\..\\bar"))); - assertFalse(FILE_UTILS.isLeadingPath(new File("/foo"), new File("/foo/../.."))); + assertFalse(getFileUtils().isLeadingPath(new File("/foo"), new File("/foo/../../bar"))); + assertFalse(getFileUtils().isLeadingPath(new File("c:\\foo"), new File("c:\\foo\\..\\..\\bar"))); + assertFalse(getFileUtils().isLeadingPath(new File("/foo"), new File("/foo/../.."))); } /** @@ -630,61 +622,61 @@ public class FileUtilsTest { */ @Test public void isLeadingPathCanonicalVersionCannotBeFooledByTooManyDoubleDots() throws IOException { - assertFalse(FILE_UTILS.isLeadingPath(new File("/foo"), new File("/foo/../../bar"), true)); - assertFalse(FILE_UTILS.isLeadingPath(new File("c:\\foo"), new File("c:\\foo\\..\\..\\bar"), true)); - assertFalse(FILE_UTILS.isLeadingPath(new File("/foo"), new File("/foo/../.."), true)); + assertFalse(getFileUtils().isLeadingPath(new File("/foo"), new File("/foo/../../bar"), true)); + assertFalse(getFileUtils().isLeadingPath(new File("c:\\foo"), new File("c:\\foo\\..\\..\\bar"), true)); + assertFalse(getFileUtils().isLeadingPath(new File("/foo"), new File("/foo/../.."), true)); } @Test public void isLeadingPathCanonicalVersionWorksAsExpectedOnUnix() throws IOException { assumeFalse("Test doesn't run on DOS", Os.isFamily("dos")); - assertTrue(FILE_UTILS.isLeadingPath(new File("/foo"), new File("/foo/bar"), true)); - assertTrue(FILE_UTILS.isLeadingPath(new File("/foo"), new File("/foo/baz/../bar"), true)); - assertTrue(FILE_UTILS.isLeadingPath(new File("/foo"), new File("/foo/../foo/bar"), true)); - assertFalse(FILE_UTILS.isLeadingPath(new File("/foo"), new File("/foobar"), true)); - assertFalse(FILE_UTILS.isLeadingPath(new File("/foo"), new File("/bar"), true)); + assertTrue(getFileUtils().isLeadingPath(new File("/foo"), new File("/foo/bar"), true)); + assertTrue(getFileUtils().isLeadingPath(new File("/foo"), new File("/foo/baz/../bar"), true)); + assertTrue(getFileUtils().isLeadingPath(new File("/foo"), new File("/foo/../foo/bar"), true)); + assertFalse(getFileUtils().isLeadingPath(new File("/foo"), new File("/foobar"), true)); + assertFalse(getFileUtils().isLeadingPath(new File("/foo"), new File("/bar"), true)); } @Test public void isLeadingPathAndTrailingSlashesOnUnix() throws IOException { assumeFalse("Test doesn't run on DOS", Os.isFamily("dos")); - assertTrue(FILE_UTILS.isLeadingPath(new File("/foo/"), new File("/foo/bar"), true)); - assertTrue(FILE_UTILS.isLeadingPath(new File("/foo/"), new File("/foo/bar/"), true)); - assertTrue(FILE_UTILS.isLeadingPath(new File("/foo/"), new File("/foo/"), true)); - assertTrue(FILE_UTILS.isLeadingPath(new File("/foo/"), new File("/foo"), true)); - assertTrue(FILE_UTILS.isLeadingPath(new File("/foo"), new File("/foo/"), true)); + assertTrue(getFileUtils().isLeadingPath(new File("/foo/"), new File("/foo/bar"), true)); + assertTrue(getFileUtils().isLeadingPath(new File("/foo/"), new File("/foo/bar/"), true)); + assertTrue(getFileUtils().isLeadingPath(new File("/foo/"), new File("/foo/"), true)); + assertTrue(getFileUtils().isLeadingPath(new File("/foo/"), new File("/foo"), true)); + assertTrue(getFileUtils().isLeadingPath(new File("/foo"), new File("/foo/"), true)); - assertTrue(FILE_UTILS.isLeadingPath(new File("/foo/"), new File("/foo/bar"), false)); - assertTrue(FILE_UTILS.isLeadingPath(new File("/foo/"), new File("/foo/bar/"), false)); - assertTrue(FILE_UTILS.isLeadingPath(new File("/foo/"), new File("/foo/"), false)); - assertTrue(FILE_UTILS.isLeadingPath(new File("/foo/"), new File("/foo"), false)); - assertTrue(FILE_UTILS.isLeadingPath(new File("/foo"), new File("/foo/"), false)); + assertTrue(getFileUtils().isLeadingPath(new File("/foo/"), new File("/foo/bar"), false)); + assertTrue(getFileUtils().isLeadingPath(new File("/foo/"), new File("/foo/bar/"), false)); + assertTrue(getFileUtils().isLeadingPath(new File("/foo/"), new File("/foo/"), false)); + assertTrue(getFileUtils().isLeadingPath(new File("/foo/"), new File("/foo"), false)); + assertTrue(getFileUtils().isLeadingPath(new File("/foo"), new File("/foo/"), false)); } @Test public void isLeadingPathCanonicalVersionWorksAsExpectedOnDos() throws IOException { assumeTrue("Test only runs on DOS", Os.isFamily("dos")); - assertTrue(FILE_UTILS.isLeadingPath(new File("C:\\foo"), new File("C:\\foo\\bar"), true)); - assertTrue(FILE_UTILS.isLeadingPath(new File("C:\\foo"), new File("C:\\foo\\baz\\..\\bar"), true)); - assertTrue(FILE_UTILS.isLeadingPath(new File("C:\\foo"), new File("C:\\foo\\..\\foo\\bar"), true)); - assertFalse(FILE_UTILS.isLeadingPath(new File("C:\\foo"), new File("C:\\foobar"), true)); - assertFalse(FILE_UTILS.isLeadingPath(new File("C:\\foo"), new File("C:\\bar"), true)); + assertTrue(getFileUtils().isLeadingPath(new File("C:\\foo"), new File("C:\\foo\\bar"), true)); + assertTrue(getFileUtils().isLeadingPath(new File("C:\\foo"), new File("C:\\foo\\baz\\..\\bar"), true)); + assertTrue(getFileUtils().isLeadingPath(new File("C:\\foo"), new File("C:\\foo\\..\\foo\\bar"), true)); + assertFalse(getFileUtils().isLeadingPath(new File("C:\\foo"), new File("C:\\foobar"), true)); + assertFalse(getFileUtils().isLeadingPath(new File("C:\\foo"), new File("C:\\bar"), true)); } @Test public void isLeadingPathAndTrailingSlashesOnDos() throws IOException { assumeTrue("Test only runs on DOS", Os.isFamily("dos")); - assertTrue(FILE_UTILS.isLeadingPath(new File("c:\\foo\\"), new File("c:\\foo\\bar"), true)); - assertTrue(FILE_UTILS.isLeadingPath(new File("c:\\foo\\"), new File("c:\\foo\\bar\\"), true)); - assertTrue(FILE_UTILS.isLeadingPath(new File("c:\\foo\\"), new File("c:\\foo\\"), true)); - assertTrue(FILE_UTILS.isLeadingPath(new File("c:\\foo\\"), new File("c:\\foo"), true)); - assertTrue(FILE_UTILS.isLeadingPath(new File("c:\\foo"), new File("c:\\foo\\"), true)); + assertTrue(getFileUtils().isLeadingPath(new File("c:\\foo\\"), new File("c:\\foo\\bar"), true)); + assertTrue(getFileUtils().isLeadingPath(new File("c:\\foo\\"), new File("c:\\foo\\bar\\"), true)); + assertTrue(getFileUtils().isLeadingPath(new File("c:\\foo\\"), new File("c:\\foo\\"), true)); + assertTrue(getFileUtils().isLeadingPath(new File("c:\\foo\\"), new File("c:\\foo"), true)); + assertTrue(getFileUtils().isLeadingPath(new File("c:\\foo"), new File("c:\\foo\\"), true)); - assertTrue(FILE_UTILS.isLeadingPath(new File("c:\\foo\\"), new File("c:\\foo\\bar"), false)); - assertTrue(FILE_UTILS.isLeadingPath(new File("c:\\foo\\"), new File("c:\\foo\\bar\\"), false)); - assertTrue(FILE_UTILS.isLeadingPath(new File("c:\\foo\\"), new File("c:\\foo\\"), false)); - assertTrue(FILE_UTILS.isLeadingPath(new File("c:\\foo\\"), new File("c:\\foo"), false)); - assertTrue(FILE_UTILS.isLeadingPath(new File("c:\\foo"), new File("c:\\foo\\"), false)); + assertTrue(getFileUtils().isLeadingPath(new File("c:\\foo\\"), new File("c:\\foo\\bar"), false)); + assertTrue(getFileUtils().isLeadingPath(new File("c:\\foo\\"), new File("c:\\foo\\bar\\"), false)); + assertTrue(getFileUtils().isLeadingPath(new File("c:\\foo\\"), new File("c:\\foo\\"), false)); + assertTrue(getFileUtils().isLeadingPath(new File("c:\\foo\\"), new File("c:\\foo"), false)); + assertTrue(getFileUtils().isLeadingPath(new File("c:\\foo"), new File("c:\\foo\\"), false)); } /** @@ -707,17 +699,18 @@ public class FileUtilsTest { final Boolean expectedCaseSensitivity = !existsAsLowerCase || !existsAsUpperCase; // call the method and pass it a directory - Optional actualCaseSensitivity = FileUtils.isCaseSensitiveFileSystem(tmpDir); - Assert.assertTrue("Filesystem case sensitivity was expected to be determined", actualCaseSensitivity.isPresent()); - Assert.assertEquals("Filesystem was expected to be case " + (expectedCaseSensitivity + Optional actualCaseSensitivity = isCaseSensitiveFileSystem(tmpDir); + assertTrue("Filesystem case sensitivity was expected to be determined", actualCaseSensitivity.isPresent()); + assertEquals("Filesystem was expected to be case " + (expectedCaseSensitivity ? "sensitive" : "insensitive"), expectedCaseSensitivity, actualCaseSensitivity.get()); // now test it out by passing it a file - actualCaseSensitivity = FileUtils.isCaseSensitiveFileSystem(tmpFile); - Assert.assertTrue("Filesystem case sensitivity was expected to be determined", actualCaseSensitivity.isPresent()); - Assert.assertEquals("Filesystem was expected to be case " + (expectedCaseSensitivity + actualCaseSensitivity = isCaseSensitiveFileSystem(tmpFile); + assertTrue("Filesystem case sensitivity was expected to be determined", actualCaseSensitivity.isPresent()); + assertEquals("Filesystem was expected to be case " + (expectedCaseSensitivity ? "sensitive" : "insensitive"), expectedCaseSensitivity, actualCaseSensitivity.get()); } + /** * adapt file separators to local conventions */ diff --git a/src/tests/junit/org/apache/tools/ant/util/LayoutPreservingPropertiesTest.java b/src/tests/junit/org/apache/tools/ant/util/LayoutPreservingPropertiesTest.java index 361f28586..00462b53c 100644 --- a/src/tests/junit/org/apache/tools/ant/util/LayoutPreservingPropertiesTest.java +++ b/src/tests/junit/org/apache/tools/ant/util/LayoutPreservingPropertiesTest.java @@ -26,8 +26,11 @@ import java.util.Properties; import org.apache.tools.ant.MagicTestNames; import org.junit.Before; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.TemporaryFolder; +import static org.apache.tools.ant.util.FileUtils.readFully; import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.not; import static org.hamcrest.Matchers.startsWith; @@ -36,13 +39,19 @@ import static org.junit.Assert.assertThat; public class LayoutPreservingPropertiesTest { + @Rule + public TemporaryFolder folder = new TemporaryFolder(); + private static final String ROOT = System.getProperty(MagicTestNames.TEST_ROOT_DIRECTORY); private LayoutPreservingProperties lpf; + private File tmp; + @Before - public void setUp() { + public void setUp() throws IOException { lpf = new LayoutPreservingProperties(); + tmp = folder.newFile("tmp.properties"); } /** * Tests that a properties file read by the @@ -55,8 +64,6 @@ public class LayoutPreservingPropertiesTest { FileInputStream fis = new FileInputStream(simple); lpf.load(fis); - File tmp = File.createTempFile("tmp", "props"); - tmp.deleteOnExit(); lpf.saveAs(tmp); // now compare original and tmp for property equivalence @@ -91,8 +98,6 @@ public class LayoutPreservingPropertiesTest { lpf.setProperty("prop#nine", "contains#hash"); lpf.setProperty("prop!ten", "contains!exclamation"); - File tmp = File.createTempFile("tmp", "props"); - tmp.deleteOnExit(); lpf.saveAs(tmp); // and check that the resulting file looks okay @@ -124,8 +129,6 @@ public class LayoutPreservingPropertiesTest { lpf.setProperty("prop\ttwo", "new two"); lpf.setProperty("prop\nthree", "new three"); - File tmp = File.createTempFile("tmp", "props"); - tmp.deleteOnExit(); lpf.saveAs(tmp); // and check that the resulting file looks okay @@ -145,11 +148,9 @@ public class LayoutPreservingPropertiesTest { FileInputStream fis = new FileInputStream(simple); lpf.load(fis); - File tmp = File.createTempFile("tmp", "props"); - tmp.deleteOnExit(); - FileOutputStream fos = new FileOutputStream(tmp); - lpf.store(fos, "file-header"); - fos.close(); + try (FileOutputStream fos = new FileOutputStream(tmp)) { + lpf.store(fos, "file-header"); + } // and check that the resulting file looks okay assertThat("should have had header ", readFile(tmp), startsWith("#file-header")); @@ -163,8 +164,6 @@ public class LayoutPreservingPropertiesTest { lpf.clear(); - File tmp = File.createTempFile("tmp", "props"); - tmp.deleteOnExit(); lpf.saveAs(tmp); // and check that the resulting file looks okay @@ -187,8 +186,6 @@ public class LayoutPreservingPropertiesTest { lpf.remove("prop.beta"); - File tmp = File.createTempFile("tmp", "props"); - tmp.deleteOnExit(); lpf.saveAs(tmp); // and check that the resulting file looks okay @@ -208,8 +205,6 @@ public class LayoutPreservingPropertiesTest { lpf.remove("prop.beta"); - File tmp = File.createTempFile("tmp", "props"); - tmp.deleteOnExit(); lpf.saveAs(tmp); // and check that the resulting file looks okay @@ -233,15 +228,12 @@ public class LayoutPreservingPropertiesTest { assertEquals("size of original is wrong", 3, lpf.size()); assertEquals("size of clone is wrong", 4, lpfClone.size()); - File tmp1 = File.createTempFile("tmp", "props"); - tmp1.deleteOnExit(); - lpf.saveAs(tmp1); - String s1 = readFile(tmp1); + lpf.saveAs(tmp); + String s1 = readFile(tmp); - File tmp2 = File.createTempFile("tmp", "props"); - tmp2.deleteOnExit(); - lpfClone.saveAs(tmp2); - String s2 = readFile(tmp2); + File tmpClone = folder.newFile("tmp-clone.properties"); + lpfClone.saveAs(tmpClone); + String s2 = readFile(tmpClone); // check original is untouched assertThat("should have had 'simple'", s1, containsString(("simple"))); @@ -265,8 +257,6 @@ public class LayoutPreservingPropertiesTest { lpf.setProperty("alpha", "new value for alpha"); lpf.setProperty("beta", "new value for beta"); - File tmp = File.createTempFile("tmp", "props"); - tmp.deleteOnExit(); lpf.saveAs(tmp); // and check that the resulting file looks okay @@ -285,11 +275,8 @@ public class LayoutPreservingPropertiesTest { } private static String readFile(File f) throws IOException { - FileInputStream fis = new FileInputStream(f); - InputStreamReader isr = new InputStreamReader(fis); - String s = FileUtils.readFully(isr); - isr.close(); - fis.close(); - return s; + try (InputStreamReader isr = new InputStreamReader(new FileInputStream(f))) { + return readFully(isr); + } } } diff --git a/src/tests/junit/org/apache/tools/ant/util/PermissionUtilsTest.java b/src/tests/junit/org/apache/tools/ant/util/PermissionUtilsTest.java index 7ea50b44b..5fc2a1ad0 100644 --- a/src/tests/junit/org/apache/tools/ant/util/PermissionUtilsTest.java +++ b/src/tests/junit/org/apache/tools/ant/util/PermissionUtilsTest.java @@ -37,10 +37,15 @@ import org.apache.tools.tar.TarEntry; import org.apache.tools.tar.TarOutputStream; import org.apache.tools.zip.ZipEntry; import org.apache.tools.zip.ZipOutputStream; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.TemporaryFolder; public class PermissionUtilsTest { + @Rule + public TemporaryFolder folder = new TemporaryFolder(); + @Test public void modeFromPermissionsReturnsExpectedResult() { int mode = PermissionUtils.modeFromPermissions(EnumSet.of(PosixFilePermission.OWNER_READ, @@ -65,44 +70,31 @@ public class PermissionUtilsTest { @Test public void detectsFileTypeOfRegularFileFromPath() throws IOException { - File f = File.createTempFile("ant", ".tst"); - f.deleteOnExit(); assertEquals(PermissionUtils.FileType.REGULAR_FILE, - PermissionUtils.FileType.of(f.toPath())); + PermissionUtils.FileType.of(folder.newFile("ant.tst").toPath())); } @Test public void detectsFileTypeOfRegularFileFromResource() throws IOException { - File f = File.createTempFile("ant", ".tst"); - f.deleteOnExit(); assertEquals(PermissionUtils.FileType.REGULAR_FILE, - PermissionUtils.FileType.of(new FileResource(f))); + PermissionUtils.FileType.of(new FileResource(folder.newFile("ant.tst")))); } @Test public void detectsFileTypeOfDirectoryFromPath() throws IOException { - File f = File.createTempFile("ant", ".dir"); - f.delete(); - f.mkdirs(); - f.deleteOnExit(); assertEquals(PermissionUtils.FileType.DIR, - PermissionUtils.FileType.of(f.toPath())); + PermissionUtils.FileType.of(folder.newFolder("ant.tst").toPath())); } @Test public void detectsFileTypeOfDirectoryFromResource() throws IOException { - File f = File.createTempFile("ant", ".tst"); - f.delete(); - f.mkdirs(); - f.deleteOnExit(); assertEquals(PermissionUtils.FileType.DIR, - PermissionUtils.FileType.of(new FileResource(f))); + PermissionUtils.FileType.of(new FileResource(folder.newFolder("ant.tst")))); } @Test public void getSetPermissionsWorksForFiles() throws IOException { - File f = File.createTempFile("ant", ".tst"); - f.deleteOnExit(); + File f = folder.newFile("ant.tst"); assumeNotNull(Files.getFileAttributeView(f.toPath(), PosixFileAttributeView.class)); Set s = @@ -116,8 +108,7 @@ public class PermissionUtilsTest { @Test public void getSetPermissionsWorksForZipResources() throws IOException { - File f = File.createTempFile("ant", ".zip"); - f.deleteOnExit(); + File f = folder.newFile("ant.zip"); try (ZipOutputStream os = new ZipOutputStream(f)) { ZipEntry e = new ZipEntry("foo"); os.putNextEntry(e); @@ -138,8 +129,7 @@ public class PermissionUtilsTest { @Test public void getSetPermissionsWorksForTarResources() throws IOException { - File f = File.createTempFile("ant", ".zip"); - f.deleteOnExit(); + File f = folder.newFile("ant.tar"); try (TarOutputStream os = new TarOutputStream(new FileOutputStream(f))) { TarEntry e = new TarEntry("foo"); os.putNextEntry(e);