commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From pascalschumac...@apache.org
Subject [3/3] commons-io git commit: Replace static TemporaryFolders with instances ones, so that tests are truly independent.
Date Wed, 27 Sep 2017 20:23:16 GMT
Replace static TemporaryFolders with instances ones, so that tests are truly independent.


Project: http://git-wip-us.apache.org/repos/asf/commons-io/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-io/commit/a43d2fa2
Tree: http://git-wip-us.apache.org/repos/asf/commons-io/tree/a43d2fa2
Diff: http://git-wip-us.apache.org/repos/asf/commons-io/diff/a43d2fa2

Branch: refs/heads/master
Commit: a43d2fa2f74211881eaeb4188dfe330f905c8342
Parents: 68374b2
Author: pascalschumacher <pascalschumacher@gmx.net>
Authored: Wed Sep 27 22:21:55 2017 +0200
Committer: pascalschumacher <pascalschumacher@gmx.net>
Committed: Wed Sep 27 22:22:53 2017 +0200

----------------------------------------------------------------------
 .../io/FileUtilsCleanDirectoryTestCase.java     | 32 +++-----------------
 .../io/FileUtilsCleanSymlinksTestCase.java      | 20 +++---------
 .../io/FileUtilsDirectoryContainsTestCase.java  | 25 +++++----------
 .../commons/io/FileUtilsFileNewerTestCase.java  | 30 +++++-------------
 .../commons/io/FileUtilsListFilesTestCase.java  | 27 +++--------------
 .../apache/commons/io/FileUtilsTestCase.java    | 30 ++++++------------
 .../org/apache/commons/io/IOUtilsTestCase.java  | 21 +++----------
 .../apache/commons/io/LineIteratorTestCase.java | 14 +++------
 .../io/filefilter/RegexFileFilterTestCase.java  | 23 ++------------
 9 files changed, 50 insertions(+), 172 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/commons-io/blob/a43d2fa2/src/test/java/org/apache/commons/io/FileUtilsCleanDirectoryTestCase.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/io/FileUtilsCleanDirectoryTestCase.java b/src/test/java/org/apache/commons/io/FileUtilsCleanDirectoryTestCase.java
index e6ad4cc..f8966d8 100644
--- a/src/test/java/org/apache/commons/io/FileUtilsCleanDirectoryTestCase.java
+++ b/src/test/java/org/apache/commons/io/FileUtilsCleanDirectoryTestCase.java
@@ -25,9 +25,8 @@ import java.io.IOException;
 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;
 
@@ -37,35 +36,14 @@ 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 getLocalTestDirectory() {
-        return new File(getTestDirectory(), "list-files");
-    }
+    private File top;
 
-    /**
-     * @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();
     }
 
     //-----------------------------------------------------------------------

http://git-wip-us.apache.org/repos/asf/commons-io/blob/a43d2fa2/src/test/java/org/apache/commons/io/FileUtilsCleanSymlinksTestCase.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/io/FileUtilsCleanSymlinksTestCase.java b/src/test/java/org/apache/commons/io/FileUtilsCleanSymlinksTestCase.java
index 660c3cd..be1945b 100644
--- a/src/test/java/org/apache/commons/io/FileUtilsCleanSymlinksTestCase.java
+++ b/src/test/java/org/apache/commons/io/FileUtilsCleanSymlinksTestCase.java
@@ -24,9 +24,8 @@ import java.io.File;
 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;
 
@@ -36,23 +35,14 @@ 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

http://git-wip-us.apache.org/repos/asf/commons-io/blob/a43d2fa2/src/test/java/org/apache/commons/io/FileUtilsDirectoryContainsTestCase.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/io/FileUtilsDirectoryContainsTestCase.java b/src/test/java/org/apache/commons/io/FileUtilsDirectoryContainsTestCase.java
index bbc9695..4e4e88b 100644
--- a/src/test/java/org/apache/commons/io/FileUtilsDirectoryContainsTestCase.java
+++ b/src/test/java/org/apache/commons/io/FileUtilsDirectoryContainsTestCase.java
@@ -23,9 +23,8 @@ import static org.junit.Assert.fail;
 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;
 
@@ -34,16 +33,11 @@ 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 @@ public class FileUtilsDirectoryContainsTestCase {
     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 class FileUtilsDirectoryContainsTestCase {
         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));

http://git-wip-us.apache.org/repos/asf/commons-io/blob/a43d2fa2/src/test/java/org/apache/commons/io/FileUtilsFileNewerTestCase.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/io/FileUtilsFileNewerTestCase.java b/src/test/java/org/apache/commons/io/FileUtilsFileNewerTestCase.java
index 53f81ff..d55434e 100644
--- a/src/test/java/org/apache/commons/io/FileUtilsFileNewerTestCase.java
+++ b/src/test/java/org/apache/commons/io/FileUtilsFileNewerTestCase.java
@@ -26,9 +26,8 @@ import java.io.IOException;
 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;
 
@@ -37,10 +36,10 @@ 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 @@ public class FileUtilsFileNewerTestCase {
     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 class FileUtilsFileNewerTestCase {
         }
     }
 
-    /** @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.
      *

http://git-wip-us.apache.org/repos/asf/commons-io/blob/a43d2fa2/src/test/java/org/apache/commons/io/FileUtilsListFilesTestCase.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/io/FileUtilsListFilesTestCase.java b/src/test/java/org/apache/commons/io/FileUtilsListFilesTestCase.java
index e3c216c..f7e5034 100644
--- a/src/test/java/org/apache/commons/io/FileUtilsListFilesTestCase.java
+++ b/src/test/java/org/apache/commons/io/FileUtilsListFilesTestCase.java
@@ -28,9 +28,8 @@ import java.util.Iterator;
 
 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;
 
@@ -39,15 +38,11 @@ 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 @@ public class FileUtilsListFilesTestCase {
     @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 class FileUtilsListFilesTestCase {
         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) {

http://git-wip-us.apache.org/repos/asf/commons-io/blob/a43d2fa2/src/test/java/org/apache/commons/io/FileUtilsTestCase.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/io/FileUtilsTestCase.java b/src/test/java/org/apache/commons/io/FileUtilsTestCase.java
index 07a49dc..200eeb3 100644
--- a/src/test/java/org/apache/commons/io/FileUtilsTestCase.java
+++ b/src/test/java/org/apache/commons/io/FileUtilsTestCase.java
@@ -51,11 +51,10 @@ import java.util.zip.Checksum;
 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;
 
@@ -67,10 +66,10 @@ 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 @@ public class FileUtilsTestCase {
      */
     //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 class FileUtilsTestCase {
         }
     }
 
-    @After
-    public void tearDown() throws Exception {
-        FileUtils.deleteDirectory(getTestDirectory());
-    }
-
     private String getName() {
         return this.getClass().getSimpleName();
     }

http://git-wip-us.apache.org/repos/asf/commons-io/blob/a43d2fa2/src/test/java/org/apache/commons/io/IOUtilsTestCase.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/io/IOUtilsTestCase.java b/src/test/java/org/apache/commons/io/IOUtilsTestCase.java
index 228e028..c5c34db 100644
--- a/src/test/java/org/apache/commons/io/IOUtilsTestCase.java
+++ b/src/test/java/org/apache/commons/io/IOUtilsTestCase.java
@@ -59,10 +59,9 @@ import java.util.Arrays;
 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;
 
@@ -79,10 +78,10 @@ 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 @@ public class IOUtilsTestCase {
     @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 class IOUtilsTestCase {
         }
     }
 
-    @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

http://git-wip-us.apache.org/repos/asf/commons-io/blob/a43d2fa2/src/test/java/org/apache/commons/io/LineIteratorTestCase.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/io/LineIteratorTestCase.java b/src/test/java/org/apache/commons/io/LineIteratorTestCase.java
index 7243fbd..52a8901 100644
--- a/src/test/java/org/apache/commons/io/LineIteratorTestCase.java
+++ b/src/test/java/org/apache/commons/io/LineIteratorTestCase.java
@@ -34,9 +34,8 @@ import java.util.ArrayList;
 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;
 
@@ -46,10 +45,10 @@ 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 class LineIteratorTestCase {
 
     }
 
-    @After
-    public void tearDown() throws Exception {
-        FileUtils.deleteDirectory(getTestDirectory());
-    }
-
     // -----------------------------------------------------------------------
 
     @Test

http://git-wip-us.apache.org/repos/asf/commons-io/blob/a43d2fa2/src/test/java/org/apache/commons/io/filefilter/RegexFileFilterTestCase.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/io/filefilter/RegexFileFilterTestCase.java b/src/test/java/org/apache/commons/io/filefilter/RegexFileFilterTestCase.java
index 1cce1b1..120c857 100644
--- a/src/test/java/org/apache/commons/io/filefilter/RegexFileFilterTestCase.java
+++ b/src/test/java/org/apache/commons/io/filefilter/RegexFileFilterTestCase.java
@@ -22,11 +22,8 @@ import static org.junit.Assert.fail;
 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;
 
@@ -35,22 +32,8 @@ 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


Mime
View raw message