Replace static TemporaryFolders with instances ones, so that tests are truly independent.
MODIFY
src/test/java/org/apache/commons/io/FileUtilsCleanDirectoryTestCase.java
src/test/java/org/apache/commons/io/FileUtilsCleanDirectoryTestCase.java
import java.util.ArrayList;
import java.util.List;
-import org.junit.After;
import org.junit.Before;
-import org.junit.ClassRule;
+import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
*/
public class FileUtilsCleanDirectoryTestCase {
- @ClassRule
- public static TemporaryFolder temporaryFolder = new TemporaryFolder();
+ @Rule
+ public TemporaryFolder temporaryFolder = new TemporaryFolder();
- private static File getTestDirectory() {
- return temporaryFolder.getRoot();
- }
-
- final File top = getLocalTestDirectory();
+ private File top;
- private File getLocalTestDirectory() {
- return new File(getTestDirectory(), "list-files");
- }
-
- /**
- * @see junit.framework.TestCase#setUp()
- */
@Before
public void setUp() throws Exception {
- top.mkdirs();
- }
-
- /**
- * @see junit.framework.TestCase#tearDown()
- */
- @After
- public void tearDown() throws Exception {
- chmod(top, 775, true);
- FileUtils.deleteDirectory(top);
- FileUtils.deleteDirectory(getTestDirectory());
+ top = temporaryFolder.getRoot();
}
//-----------------------------------------------------------------------

MODIFY
src/test/java/org/apache/commons/io/FileUtilsCleanSymlinksTestCase.java
src/test/java/org/apache/commons/io/FileUtilsCleanSymlinksTestCase.java
import java.util.ArrayList;
import java.util.List;
-import org.junit.After;
import org.junit.Before;
-import org.junit.ClassRule;
+import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
*/
public class FileUtilsCleanSymlinksTestCase {
- @ClassRule
- public static TemporaryFolder temporaryFolder = new TemporaryFolder();
+ @Rule
+ public TemporaryFolder temporaryFolder = new TemporaryFolder();
- private static File getTestDirectory() {
- return temporaryFolder.getRoot();
- }
-
- final File top = getTestDirectory();
+ private File top;
@Before
public void setUp() throws Exception {
- top.mkdirs();
- }
-
- @After
- public void tearDown() throws Exception {
- FileUtils.deleteDirectory(top);
+ top = temporaryFolder.getRoot();
}
@Test

MODIFY
src/test/java/org/apache/commons/io/FileUtilsDirectoryContainsTestCase.java
src/test/java/org/apache/commons/io/FileUtilsDirectoryContainsTestCase.java
import java.io.File;
import java.io.IOException;
-import org.junit.After;
import org.junit.Before;
-import org.junit.ClassRule;
+import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
*
* @see FileUtils#directoryContains(File, File)
* @since 2.2
- * @version $Id$
*/
public class FileUtilsDirectoryContainsTestCase {
- @ClassRule
- public static TemporaryFolder temporaryFolder = new TemporaryFolder();
-
- private static File getTestDirectory() {
- return temporaryFolder.getRoot();
- }
+ @Rule
+ public TemporaryFolder temporaryFolder = new TemporaryFolder();
private File directory1;
private File directory2;
@@ -53,12 +47,12 @@ private static File getTestDirectory() {
private File file2;
private File file2ByRelativeDirectory1;
private File file3;
- final File top = getTestDirectory();
+ private File top;
@SuppressWarnings("ResultOfMethodCallIgnored")
@Before
public void setUp() throws Exception {
- top.mkdirs();
+ top = temporaryFolder.getRoot();
directory1 = new File(top, "directory1");
directory2 = new File(top, "directory2");
@@ -73,19 +67,14 @@ public void setUp() throws Exception {
file3 = new File(top, "file3");
// Tests case with relative path
- file1ByRelativeDirectory2 = new File(getTestDirectory(), "directory2/../directory1/file1");
- file2ByRelativeDirectory1 = new File(getTestDirectory(), "directory1/../directory2/file2");
+ file1ByRelativeDirectory2 = new File(top, "directory2/../directory1/file1");
+ file2ByRelativeDirectory1 = new File(top, "directory1/../directory2/file2");
FileUtils.touch(file1);
FileUtils.touch(file2);
FileUtils.touch(file3);
}
- @After
- public void tearDown() throws Exception {
- FileUtils.deleteDirectory(top);
- }
-
@Test
public void testCanonicalPath() throws IOException {
assertTrue(FileUtils.directoryContains(directory1, file1ByRelativeDirectory2));

MODIFY
src/test/java/org/apache/commons/io/FileUtilsFileNewerTestCase.java
src/test/java/org/apache/commons/io/FileUtilsFileNewerTestCase.java
import java.util.Date;
import org.apache.commons.io.testtools.TestUtils;
-import org.junit.After;
import org.junit.Before;
-import org.junit.ClassRule;
+import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
*/
public class FileUtilsFileNewerTestCase {
- @ClassRule
- public static TemporaryFolder temporaryFolder = new TemporaryFolder();
+ @Rule
+ public TemporaryFolder temporaryFolder = new TemporaryFolder();
- private static File getTestDirectory() {
+ private File getTestDirectory() {
return temporaryFolder.getRoot();
}
@@ -48,18 +47,13 @@ private static File getTestDirectory() {
private static final int FILE1_SIZE = 1;
private static final int FILE2_SIZE = 1024 * 4 + 1;
- private final File m_testFile1;
- private final File m_testFile2;
+ private File m_testFile1;
+ private File m_testFile2;
- public FileUtilsFileNewerTestCase() {
- m_testFile1 = new File(getTestDirectory(), "file1-test.txt");
- m_testFile2 = new File(getTestDirectory(), "file2-test.txt");
- }
-
- /** @see junit.framework.TestCase#setUp() */
@Before
public void setUp() throws Exception {
- getTestDirectory().mkdirs();
+ m_testFile1 = new File(getTestDirectory(), "file1-test.txt");
+ m_testFile2 = new File(getTestDirectory(), "file2-test.txt");
if (!m_testFile1.getParentFile().exists()) {
throw new IOException("Cannot create file " + m_testFile1
+ " as the parent directory does not exist");
@@ -78,14 +72,6 @@ public void setUp() throws Exception {
}
}
- /** @see junit.framework.TestCase#tearDown() */
- @After
- public void tearDown() throws Exception {
- m_testFile1.delete();
- m_testFile2.delete();
- FileUtils.deleteDirectory(getTestDirectory());
- }
-
/**
* Tests the <code>isFileNewer(File, *)</code> methods which a "normal" file.
*

MODIFY
src/test/java/org/apache/commons/io/FileUtilsListFilesTestCase.java
src/test/java/org/apache/commons/io/FileUtilsListFilesTestCase.java
import org.apache.commons.io.filefilter.FileFilterUtils;
import org.apache.commons.io.filefilter.IOFileFilter;
-import org.junit.After;
import org.junit.Before;
-import org.junit.ClassRule;
+import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
*/
public class FileUtilsListFilesTestCase {
- @ClassRule
- public static TemporaryFolder temporaryFolder = new TemporaryFolder();
-
- private static File getTestDirectory() {
- return temporaryFolder.getRoot();
- }
+ @Rule
+ public TemporaryFolder temporaryFolder = new TemporaryFolder();
private File getLocalTestDirectory() {
- return new File(getTestDirectory(), "list-files");
+ return temporaryFolder.getRoot();
}
/**
@@ -57,10 +52,6 @@ private File getLocalTestDirectory() {
@Before
public void setUp() throws Exception {
File dir = getLocalTestDirectory();
- if (dir.exists()) {
- FileUtils.deleteDirectory(dir);
- }
- dir.mkdirs();
File file = new File(dir, "dummy-build.xml");
FileUtils.touch(file);
file = new File(dir, "README");
@@ -89,16 +80,6 @@ public void setUp() throws Exception {
FileUtils.touch(file);
}
- /**
- * @see junit.framework.TestCase#tearDown()
- */
- @After
- public void tearDown() throws Exception {
- final File dir = getLocalTestDirectory();
- FileUtils.deleteDirectory(dir);
- FileUtils.deleteDirectory(getTestDirectory());
- }
-
private Collection<String> filesToFilenames(final Collection<File> files) {
final Collection<String> filenames = new ArrayList<>(files.size());
for (final File file : files) {

MODIFY
src/test/java/org/apache/commons/io/FileUtilsTestCase.java
src/test/java/org/apache/commons/io/FileUtilsTestCase.java
import org.apache.commons.io.filefilter.NameFileFilter;
import org.apache.commons.io.filefilter.WildcardFileFilter;
import org.apache.commons.io.testtools.TestUtils;
-import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
-import org.junit.ClassRule;
import org.junit.Ignore;
+import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
@SuppressWarnings({"deprecation", "ResultOfMethodCallIgnored"}) // unit tests include tests of many deprecated methods
public class FileUtilsTestCase {
- @ClassRule
- public static TemporaryFolder temporaryFolder = new TemporaryFolder();
+ @Rule
+ public TemporaryFolder temporaryFolder = new TemporaryFolder();
- private static File getTestDirectory() {
+ private File getTestDirectory() {
return temporaryFolder.getRoot();
}
@@ -101,23 +100,19 @@ private static File getTestDirectory() {
*/
//private static final int LAST_MODIFIED_DELAY = 600;
- private final File testFile1;
- private final File testFile2;
+ private File testFile1;
+ private File testFile2;
- private final int testFile1Size;
- private final int testFile2Size;
+ private int testFile1Size;
+ private int testFile2Size;
- public FileUtilsTestCase() {
+ @Before
+ public void setUp() throws Exception {
testFile1 = new File(getTestDirectory(), "file1-test.txt");
testFile2 = new File(getTestDirectory(), "file1a-test.txt");
testFile1Size = (int) testFile1.length();
testFile2Size = (int) testFile2.length();
- }
-
- @Before
- public void setUp() throws Exception {
- getTestDirectory().mkdirs();
if (!testFile1.getParentFile().exists()) {
throw new IOException("Cannot create file " + testFile1
+ " as the parent directory does not exist");
@@ -154,11 +149,6 @@ public void setUp() throws Exception {
}
}
- @After
- public void tearDown() throws Exception {
- FileUtils.deleteDirectory(getTestDirectory());
- }
-
private String getName() {
return this.getClass().getSimpleName();
}

MODIFY
src/test/java/org/apache/commons/io/IOUtilsTestCase.java
src/test/java/org/apache/commons/io/IOUtilsTestCase.java
import java.util.List;
import org.apache.commons.io.testtools.TestUtils;
-import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
-import org.junit.ClassRule;
+import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
@SuppressWarnings("deprecation") // deliberately testing deprecated code
public class IOUtilsTestCase {
- @ClassRule
- public static TemporaryFolder temporaryFolder = new TemporaryFolder();
+ @Rule
+ public TemporaryFolder temporaryFolder = new TemporaryFolder();
- private static File getTestDirectory() {
+ private File getTestDirectory() {
return temporaryFolder.getRoot();
}
@@ -110,7 +109,6 @@ private void assertEqualContent(final byte[] b0, final byte[] b1) {
@Before
public void setUp() {
try {
- getTestDirectory().mkdirs();
m_testFile = new File(getTestDirectory(), "file2-test.txt");
if (!m_testFile.getParentFile().exists()) {
@@ -141,17 +139,6 @@ public void setUp() {
}
}
- @After
- public void tearDown() {
- carr = null;
- iarr = null;
- try {
- FileUtils.deleteDirectory(getTestDirectory());
- } catch (final IOException e) {
- throw new RuntimeException("Could not clear up " + getTestDirectory() + ": " + e);
- }
- }
-
@Test public void testCloseQuietly_AllCloseableIOException() {
final Closeable closeable = new Closeable() {
@Override

MODIFY
src/test/java/org/apache/commons/io/LineIteratorTestCase.java
src/test/java/org/apache/commons/io/LineIteratorTestCase.java
import java.util.List;
import java.util.NoSuchElementException;
-import org.junit.After;
import org.junit.Before;
-import org.junit.ClassRule;
+import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
*/
public class LineIteratorTestCase {
- @ClassRule
- public static TemporaryFolder temporaryFolder = new TemporaryFolder();
+ @Rule
+ public TemporaryFolder temporaryFolder = new TemporaryFolder();
- private static File getTestDirectory() {
+ private File getTestDirectory() {
return temporaryFolder.getRoot();
}
@@ -118,11 +117,6 @@ public void setUp() throws Exception {
}
- @After
- public void tearDown() throws Exception {
- FileUtils.deleteDirectory(getTestDirectory());
- }
-
// -----------------------------------------------------------------------
@Test

MODIFY
src/test/java/org/apache/commons/io/filefilter/RegexFileFilterTestCase.java
src/test/java/org/apache/commons/io/filefilter/RegexFileFilterTestCase.java
import java.io.File;
import java.util.regex.Pattern;
-import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOCase;
-import org.junit.After;
-import org.junit.Before;
-import org.junit.ClassRule;
+import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
*/
public class RegexFileFilterTestCase {
- @ClassRule
- public static TemporaryFolder temporaryFolder = new TemporaryFolder();
-
- private static File getTestDirectory() {
- return temporaryFolder.getRoot();
- }
-
- @Before
- public void setUp() {
- getTestDirectory().mkdirs();
- }
-
- @After
- public void tearDown() throws Exception {
- FileUtils.deleteDirectory(getTestDirectory());
- }
+ @Rule
+ public TemporaryFolder temporaryFolder = new TemporaryFolder();
public void assertFiltering(final IOFileFilter filter, final File file, final boolean expected) throws Exception {
// Note. This only tests the (File, String) version if the parent of

Replace static TemporaryFolders with instances ones, so that tests are truly independent.