commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bode...@apache.org
Subject commons-compress git commit: reduce duplication in tests
Date Sat, 29 Apr 2017 18:07:46 GMT
Repository: commons-compress
Updated Branches:
  refs/heads/compress-2.0 b74e53866 -> 2b31d222b


reduce duplication in tests


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

Branch: refs/heads/compress-2.0
Commit: 2b31d222b26e0e22b5d4e1f1ae613b1925992a8c
Parents: b74e538
Author: Stefan Bodewig <bodewig@apache.org>
Authored: Sat Apr 29 20:07:27 2017 +0200
Committer: Stefan Bodewig <bodewig@apache.org>
Committed: Sat Apr 29 20:07:27 2017 +0200

----------------------------------------------------------------------
 .../apache/commons/compress2/TestSupport.java   | 96 ++++++++++++++++++++
 .../formats/AbstractFileSystemTest.java         | 40 ++++++++
 .../formats/ar/ArArchiveFormatTest.java         |  3 +-
 .../compress2/formats/ar/RoundTripTest.java     | 88 +-----------------
 .../deflate/DeflateCompressionFormatTest.java   |  9 +-
 .../formats/deflate/RoundTripTest.java          | 90 +-----------------
 6 files changed, 149 insertions(+), 177 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/commons-compress/blob/2b31d222/src/test/java/org/apache/commons/compress2/TestSupport.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/compress2/TestSupport.java b/src/test/java/org/apache/commons/compress2/TestSupport.java
new file mode 100644
index 0000000..22e0c62
--- /dev/null
+++ b/src/test/java/org/apache/commons/compress2/TestSupport.java
@@ -0,0 +1,96 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.commons.compress2;
+
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.net.URI;
+import java.net.URL;
+import java.util.Locale;
+
+public class TestSupport {
+    public static File mkdir(String name) throws IOException {
+        File f = File.createTempFile(name, "");
+        f.delete();
+        f.mkdir();
+        return f;
+    }
+
+    public static File getFile(String path) throws IOException {
+        URL url = TestSupport.class.getClassLoader().getResource(path);
+        if (url == null) {
+            throw new FileNotFoundException("couldn't find " + path);
+        }
+        URI uri = null;
+        try {
+            uri = url.toURI();
+        } catch (java.net.URISyntaxException ex) {
+            throw new IOException(ex);
+        }
+        return new File(uri);
+    }
+
+    public static void rmdir(File f) {
+        String[] s = f.list();
+        if (s != null) {
+            for (String element : s) {
+                final File file = new File(f, element);
+                if (file.isDirectory()){
+                    rmdir(file);
+                }
+                boolean ok = tryHardToDelete(file);
+                if (!ok && file.exists()){
+                    System.out.println("Failed to delete "+element+" in "+f.getPath());
+                }
+            }
+        }
+        tryHardToDelete(f); // safer to delete and check
+        if (f.exists()){
+            throw new Error("Failed to delete "+f.getPath());
+        }
+    }
+
+    private static final boolean ON_WINDOWS =
+        System.getProperty("os.name").toLowerCase(Locale.ENGLISH)
+        .indexOf("windows") > -1;
+
+    /**
+     * Accommodate Windows bug encountered in both Sun and IBM JDKs.
+     * Others possible. If the delete does not work, call System.gc(),
+     * wait a little and try again.
+     *
+     * @return whether deletion was successful
+     * @since Stolen from FileUtils in Ant 1.8.0
+     */
+    public static boolean tryHardToDelete(File f) {
+        if (f != null && f.exists() && !f.delete()) {
+            if (ON_WINDOWS) {
+                System.gc();
+            }
+            try {
+                Thread.sleep(10);
+            } catch (InterruptedException ex) {
+                // Ignore Exception
+            }
+            return f.delete();
+        }
+        return true;
+    }
+}

http://git-wip-us.apache.org/repos/asf/commons-compress/blob/2b31d222/src/test/java/org/apache/commons/compress2/formats/AbstractFileSystemTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/compress2/formats/AbstractFileSystemTest.java
b/src/test/java/org/apache/commons/compress2/formats/AbstractFileSystemTest.java
new file mode 100644
index 0000000..fd871b3
--- /dev/null
+++ b/src/test/java/org/apache/commons/compress2/formats/AbstractFileSystemTest.java
@@ -0,0 +1,40 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.commons.compress2.formats;
+
+import java.io.File;
+import org.apache.commons.compress2.TestSupport;
+import org.junit.After;
+import org.junit.Before;
+
+public abstract class AbstractFileSystemTest {
+
+    protected File dir;
+
+    @Before
+    public void createTempDir() throws Exception {
+        dir = TestSupport.mkdir("dir");
+    }
+
+    @After
+    public void removeTempDir() throws Exception {
+        TestSupport.rmdir(dir);
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/commons-compress/blob/2b31d222/src/test/java/org/apache/commons/compress2/formats/ar/ArArchiveFormatTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/compress2/formats/ar/ArArchiveFormatTest.java
b/src/test/java/org/apache/commons/compress2/formats/ar/ArArchiveFormatTest.java
index c7b4165..6a751ab 100644
--- a/src/test/java/org/apache/commons/compress2/formats/ar/ArArchiveFormatTest.java
+++ b/src/test/java/org/apache/commons/compress2/formats/ar/ArArchiveFormatTest.java
@@ -22,6 +22,7 @@ import java.io.File;
 import java.io.FileInputStream;
 import java.io.IOException;
 import java.nio.ByteBuffer;
+import org.apache.commons.compress2.TestSupport;
 import org.apache.commons.compress2.util.IOUtils;
 import org.junit.Assert;
 import org.junit.Test;
@@ -40,7 +41,7 @@ public class ArArchiveFormatTest {
 
 
     private boolean isAr(String file) throws IOException {
-        File f = RoundTripTest.getFile(file);
+        File f = TestSupport.getFile(file);
         FileInputStream c = new FileInputStream(f);
         try {
             byte[] b = new byte[10];

http://git-wip-us.apache.org/repos/asf/commons-compress/blob/2b31d222/src/test/java/org/apache/commons/compress2/formats/ar/RoundTripTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/compress2/formats/ar/RoundTripTest.java b/src/test/java/org/apache/commons/compress2/formats/ar/RoundTripTest.java
index 58c8038..37b3e6d 100644
--- a/src/test/java/org/apache/commons/compress2/formats/ar/RoundTripTest.java
+++ b/src/test/java/org/apache/commons/compress2/formats/ar/RoundTripTest.java
@@ -20,38 +20,23 @@ package org.apache.commons.compress2.formats.ar;
 
 import java.io.File;
 import java.io.FileInputStream;
-import java.io.FileNotFoundException;
 import java.io.FileOutputStream;
 import java.io.IOException;
-import java.net.URI;
-import java.net.URL;
 import java.nio.channels.ReadableByteChannel;
 import java.nio.channels.WritableByteChannel;
-import java.util.Locale;
 
-import org.junit.After;
 import org.junit.Assert;
-import org.junit.Before;
 import org.junit.Test;
 
 import org.apache.commons.compress2.archivers.ArchiveEntryParameters;
 import org.apache.commons.compress2.archivers.ArchiveInput;
 import org.apache.commons.compress2.archivers.ArchiveOutput;
+import org.apache.commons.compress2.formats.AbstractFileSystemTest;
 import org.apache.commons.compress2.util.IOUtils;
 
-public class RoundTripTest {
+import static org.apache.commons.compress2.TestSupport.getFile;
 
-    private File dir;
-
-    @Before
-    public void createTempDir() throws Exception {
-        dir = mkdir("dir");
-    }
-
-    @After
-    public void removeTempDir() throws Exception {
-        rmdir(dir);
-    }
+public class RoundTripTest extends AbstractFileSystemTest {
 
     @Test
     public void testRoundtripUsingConstructors() throws Exception {
@@ -171,71 +156,4 @@ public class RoundTripTest {
         in.close();
     }
 
-    public static File mkdir(String name) throws IOException {
-        File f = File.createTempFile(name, "");
-        f.delete();
-        f.mkdir();
-        return f;
-    }
-
-    public static File getFile(String path) throws IOException {
-        URL url = RoundTripTest.class.getClassLoader().getResource(path);
-        if (url == null) {
-            throw new FileNotFoundException("couldn't find " + path);
-        }
-        URI uri = null;
-        try {
-            uri = url.toURI();
-        } catch (java.net.URISyntaxException ex) {
-            throw new IOException(ex);
-        }
-        return new File(uri);
-    }
-
-    public static void rmdir(File f) {
-        String[] s = f.list();
-        if (s != null) {
-            for (String element : s) {
-                final File file = new File(f, element);
-                if (file.isDirectory()){
-                    rmdir(file);
-                }
-                boolean ok = tryHardToDelete(file);
-                if (!ok && file.exists()){
-                    System.out.println("Failed to delete "+element+" in "+f.getPath());
-                }
-            }
-        }
-        tryHardToDelete(f); // safer to delete and check
-        if (f.exists()){
-            throw new Error("Failed to delete "+f.getPath());
-        }
-    }
-
-    private static final boolean ON_WINDOWS =
-        System.getProperty("os.name").toLowerCase(Locale.ENGLISH)
-        .indexOf("windows") > -1;
-
-    /**
-     * Accommodate Windows bug encountered in both Sun and IBM JDKs.
-     * Others possible. If the delete does not work, call System.gc(),
-     * wait a little and try again.
-     *
-     * @return whether deletion was successful
-     * @since Stolen from FileUtils in Ant 1.8.0
-     */
-    public static boolean tryHardToDelete(File f) {
-        if (f != null && f.exists() && !f.delete()) {
-            if (ON_WINDOWS) {
-                System.gc();
-            }
-            try {
-                Thread.sleep(10);
-            } catch (InterruptedException ex) {
-                // Ignore Exception
-            }
-            return f.delete();
-        }
-        return true;
-    }
 }

http://git-wip-us.apache.org/repos/asf/commons-compress/blob/2b31d222/src/test/java/org/apache/commons/compress2/formats/deflate/DeflateCompressionFormatTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/compress2/formats/deflate/DeflateCompressionFormatTest.java
b/src/test/java/org/apache/commons/compress2/formats/deflate/DeflateCompressionFormatTest.java
index 837f31d..325b2db 100644
--- a/src/test/java/org/apache/commons/compress2/formats/deflate/DeflateCompressionFormatTest.java
+++ b/src/test/java/org/apache/commons/compress2/formats/deflate/DeflateCompressionFormatTest.java
@@ -22,6 +22,7 @@ import java.io.File;
 import java.io.FileInputStream;
 import java.io.IOException;
 import java.nio.ByteBuffer;
+import org.apache.commons.compress2.TestSupport;
 import org.apache.commons.compress2.util.IOUtils;
 import org.junit.Assert;
 import org.junit.Test;
@@ -30,17 +31,17 @@ public class DeflateCompressionFormatTest {
 
     @Test
     public void shouldDetectFormat() throws IOException {
-        Assert.assertTrue(isAr("test-archives/default.tar.deflatez"));
+        Assert.assertTrue(isDeflate("test-archives/default.tar.deflatez"));
     }
 
     @Test
     public void shouldRejectXMLFile() throws IOException {
-        Assert.assertFalse(isAr("test1.xml"));
+        Assert.assertFalse(isDeflate("test1.xml"));
     }
 
 
-    private boolean isAr(String file) throws IOException {
-        File f = RoundTripTest.getFile(file);
+    private boolean isDeflate(String file) throws IOException {
+        File f = TestSupport.getFile(file);
         FileInputStream c = new FileInputStream(f);
         try {
             byte[] b = new byte[10];

http://git-wip-us.apache.org/repos/asf/commons-compress/blob/2b31d222/src/test/java/org/apache/commons/compress2/formats/deflate/RoundTripTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/compress2/formats/deflate/RoundTripTest.java
b/src/test/java/org/apache/commons/compress2/formats/deflate/RoundTripTest.java
index 72f36d4..b74d532 100644
--- a/src/test/java/org/apache/commons/compress2/formats/deflate/RoundTripTest.java
+++ b/src/test/java/org/apache/commons/compress2/formats/deflate/RoundTripTest.java
@@ -20,37 +20,21 @@ package org.apache.commons.compress2.formats.deflate;
 
 import java.io.File;
 import java.io.FileInputStream;
-import java.io.FileNotFoundException;
 import java.io.FileOutputStream;
-import java.io.IOException;
-import java.net.URI;
-import java.net.URL;
 import java.nio.channels.ReadableByteChannel;
 import java.nio.channels.WritableByteChannel;
-import java.util.Locale;
 
-import org.junit.After;
 import org.junit.Assert;
-import org.junit.Before;
 import org.junit.Test;
 
 import org.apache.commons.compress2.compressors.CompressedInput;
 import org.apache.commons.compress2.compressors.CompressedOutput;
+import org.apache.commons.compress2.formats.AbstractFileSystemTest;
 import org.apache.commons.compress2.util.IOUtils;
 
-public class RoundTripTest {
+import static org.apache.commons.compress2.TestSupport.getFile;
 
-    private File dir;
-
-    @Before
-    public void createTempDir() throws Exception {
-        dir = mkdir("dir");
-    }
-
-    @After
-    public void removeTempDir() throws Exception {
-        rmdir(dir);
-    }
+public class RoundTripTest extends AbstractFileSystemTest {
 
     @Test
     public void testRoundtripUsingConstructors() throws Exception {
@@ -114,72 +98,4 @@ public class RoundTripTest {
             IOUtils.copy(r, out);
         }
     }
-
-    public static File mkdir(String name) throws IOException {
-        File f = File.createTempFile(name, "");
-        f.delete();
-        f.mkdir();
-        return f;
-    }
-
-    public static File getFile(String path) throws IOException {
-        URL url = RoundTripTest.class.getClassLoader().getResource(path);
-        if (url == null) {
-            throw new FileNotFoundException("couldn't find " + path);
-        }
-        URI uri = null;
-        try {
-            uri = url.toURI();
-        } catch (java.net.URISyntaxException ex) {
-            throw new IOException(ex);
-        }
-        return new File(uri);
-    }
-
-    public static void rmdir(File f) {
-        String[] s = f.list();
-        if (s != null) {
-            for (String element : s) {
-                final File file = new File(f, element);
-                if (file.isDirectory()){
-                    rmdir(file);
-                }
-                boolean ok = tryHardToDelete(file);
-                if (!ok && file.exists()){
-                    System.out.println("Failed to delete "+element+" in "+f.getPath());
-                }
-            }
-        }
-        tryHardToDelete(f); // safer to delete and check
-        if (f.exists()){
-            throw new Error("Failed to delete "+f.getPath());
-        }
-    }
-
-    private static final boolean ON_WINDOWS =
-        System.getProperty("os.name").toLowerCase(Locale.ENGLISH)
-        .indexOf("windows") > -1;
-
-    /**
-     * Accommodate Windows bug encountered in both Sun and IBM JDKs.
-     * Others possible. If the delete does not work, call System.gc(),
-     * wait a little and try again.
-     *
-     * @return whether deletion was successful
-     * @since Stolen from FileUtils in Ant 1.8.0
-     */
-    public static boolean tryHardToDelete(File f) {
-        if (f != null && f.exists() && !f.delete()) {
-            if (ON_WINDOWS) {
-                System.gc();
-            }
-            try {
-                Thread.sleep(10);
-            } catch (InterruptedException ex) {
-                // Ignore Exception
-            }
-            return f.delete();
-        }
-        return true;
-    }
 }


Mime
View raw message