commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bode...@apache.org
Subject svn commit: r1157119 - in /commons/proper/compress/trunk/src/test/java/org/apache/commons/compress: ./ archivers/ archivers/jar/ archivers/tar/ archivers/zip/
Date Fri, 12 Aug 2011 14:21:44 GMT
Author: bodewig
Date: Fri Aug 12 14:21:44 2011
New Revision: 1157119

URL: http://svn.apache.org/viewvc?rev=1157119&view=rev
Log:
be more aggressive when closing/deleting temporary files in tests

Modified:
    commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/AbstractTestCase.java
    commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/ArTestCase.java
    commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/CpioTestCase.java
    commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/LongPathTest.java
    commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/TarTestCase.java
    commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/ZipTestCase.java
    commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/jar/JarArchiveOutputStreamTest.java
    commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/tar/TarArchiveEntryTest.java
    commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/zip/UTF8ZipFilesTest.java
    commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/zip/Zip64SupportIT.java

Modified: commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/AbstractTestCase.java
URL: http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/AbstractTestCase.java?rev=1157119&r1=1157118&r2=1157119&view=diff
==============================================================================
--- commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/AbstractTestCase.java
(original)
+++ commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/AbstractTestCase.java
Fri Aug 12 14:21:44 2011
@@ -29,6 +29,7 @@ import java.io.OutputStream;
 import java.util.ArrayList;
 import java.util.Iterator;
 import java.util.List;
+import java.util.Locale;
 
 import junit.framework.TestCase;
 
@@ -77,20 +78,13 @@ public abstract class AbstractTestCase e
         rmdir(dir);
         rmdir(resultDir);
         dir = resultDir = null;
-        if (archive != null && archive.exists()) {
-            if (!archive.delete()){
-                // Note: this exception won't be shown if the test has already failed
-                throw new Exception("Could not delete "+archive.getPath());
-            }
+        if (!tryHardToDelete(archive)) {
+            // Note: this exception won't be shown if the test has already failed
+            throw new Exception("Could not delete "+archive.getPath());
         }
     }
 
     protected static void rmdir(File f) {
-        // Sometimes fails without a pause - perhaps file close is partially asynchronous?
-        try {
-            Thread.sleep(20);
-        } catch (InterruptedException e) {
-        }
         String[] s = f.list();
         if (s != null) {
             for (int i = 0; i < s.length; i++) {
@@ -98,18 +92,45 @@ public abstract class AbstractTestCase e
                 if (file.isDirectory()){
                     rmdir(file);
                 }
-                boolean ok = file.delete();
+                boolean ok = tryHardToDelete(file);
                 if (!ok && file.exists()){
                     System.out.println("Failed to delete "+s[i]+" in "+f.getPath());
                 }
             }
         }
-        f.delete(); // safer to delete and check
+        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;
+    }
+
     /**
      * Creates an archive of textbased files in several directories. The
      * archivername is the factory identifier for the archiver, for example zip,
@@ -139,6 +160,7 @@ public abstract class AbstractTestCase e
         OutputStream stream = null;
         try {
             archive = File.createTempFile("test", "." + archivename);
+            archive.deleteOnExit();
             archiveList = new ArrayList<String>();
 
             stream = new FileOutputStream(archive);
@@ -201,6 +223,7 @@ public abstract class AbstractTestCase e
         archiveList = new ArrayList<String>();
         try {
             archive = File.createTempFile("empty", "." + archivename);
+            archive.deleteOnExit();
             stream = new FileOutputStream(archive);
             out = factory.createArchiveOutputStream(archivename, stream);
             out.finish();
@@ -227,6 +250,7 @@ public abstract class AbstractTestCase e
         archiveList = new ArrayList<String>();
         try {
             archive = File.createTempFile("empty", "." + archivename);
+            archive.deleteOnExit();
             stream = new FileOutputStream(archive);
             out = factory.createArchiveOutputStream(archivename, stream);
             // Use short file name so does not cause problems for ar
@@ -286,9 +310,8 @@ public abstract class AbstractTestCase e
      */
     protected File checkArchiveContent(ArchiveInputStream in, List<String> expected,
boolean cleanUp)
             throws Exception {
-        File result = File.createTempFile("dir-result", "");
-        result.delete();
-        result.mkdir();
+        File result = mkdir("dir-result");
+        result.deleteOnExit();
 
         try {
             ArchiveEntry entry = null;
@@ -354,9 +377,7 @@ public abstract class AbstractTestCase e
      * element of the two element array).
      */
     protected File[] createTempDirAndFile() throws IOException {
-        File tmpDir = File.createTempFile("testdir", "");
-        tmpDir.delete();
-        tmpDir.mkdir();
+        File tmpDir = mkdir("testdir");
         tmpDir.deleteOnExit();
         File tmpFile = File.createTempFile("testfile", "", tmpDir);
         tmpFile.deleteOnExit();

Modified: commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/ArTestCase.java
URL: http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/ArTestCase.java?rev=1157119&r1=1157118&r2=1157119&view=diff
==============================================================================
--- commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/ArTestCase.java
(original)
+++ commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/ArTestCase.java
Fri Aug 12 14:21:44 2011
@@ -215,11 +215,9 @@ public final class ArTestCase extends Ab
             if (aos != null) {
                 aos.close();
             }
-            if (archive != null) {
-                archive.delete();
-            }
-            tmp[1].delete();
-            tmp[0].delete();
+            tryHardToDelete(archive);
+            tryHardToDelete(tmp[1]);
+            rmdir(tmp[0]);
         }
     }
 
@@ -257,11 +255,9 @@ public final class ArTestCase extends Ab
             if (aos != null) {
                 aos.close();
             }
-            if (archive != null) {
-                archive.delete();
-            }
-            tmp[1].delete();
-            tmp[0].delete();
+            tryHardToDelete(archive);
+            tryHardToDelete(tmp[1]);
+            rmdir(tmp[0]);
         }
     }
 
@@ -305,14 +301,12 @@ public final class ArTestCase extends Ab
             if (aos != null) {
                 aos.close();
             }
-            if (archive != null) {
-                archive.delete();
-            }
+            tryHardToDelete(archive);
             if (fis != null) {
                 fis.close();
             }
-            tmp[1].delete();
-            tmp[0].delete();
+            tryHardToDelete(tmp[1]);
+            rmdir(tmp[0]);
         }
     }
 
@@ -357,14 +351,12 @@ public final class ArTestCase extends Ab
             if (aos != null) {
                 aos.close();
             }
-            if (archive != null) {
-                archive.delete();
-            }
+            tryHardToDelete(archive);
             if (fis != null) {
                 fis.close();
             }
-            tmp[1].delete();
-            tmp[0].delete();
+            tryHardToDelete(tmp[1]);
+            rmdir(tmp[0]);
         }
     }
 }

Modified: commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/CpioTestCase.java
URL: http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/CpioTestCase.java?rev=1157119&r1=1157118&r2=1157119&view=diff
==============================================================================
--- commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/CpioTestCase.java
(original)
+++ commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/CpioTestCase.java
Fri Aug 12 14:21:44 2011
@@ -141,11 +141,9 @@ public final class CpioTestCase extends 
             if (tos != null) {
                 tos.close();
             }
-            if (archive != null) {
-                archive.delete();
-            }
-            tmp[1].delete();
-            tmp[0].delete();
+            tryHardToDelete(archive);
+            tryHardToDelete(tmp[1]);
+            rmdir(tmp[0]);
         }
     }
 
@@ -183,11 +181,9 @@ public final class CpioTestCase extends 
             if (tos != null) {
                 tos.close();
             }
-            if (archive != null) {
-                archive.delete();
-            }
-            tmp[1].delete();
-            tmp[0].delete();
+            tryHardToDelete(archive);
+            tryHardToDelete(tmp[1]);
+            rmdir(tmp[0]);
         }
     }
 
@@ -230,14 +226,12 @@ public final class CpioTestCase extends 
             if (tos != null) {
                 tos.close();
             }
-            if (archive != null) {
-                archive.delete();
-            }
+            tryHardToDelete(archive);
             if (fis != null) {
                 fis.close();
             }
-            tmp[1].delete();
-            tmp[0].delete();
+            tryHardToDelete(tmp[1]);
+            rmdir(tmp[0]);
         }
     }
 
@@ -283,14 +277,12 @@ public final class CpioTestCase extends 
             if (tos != null) {
                 tos.close();
             }
-            if (archive != null) {
-                archive.delete();
-            }
+            tryHardToDelete(archive);
             if (fis != null) {
                 fis.close();
             }
-            tmp[1].delete();
-            tmp[0].delete();
+            tryHardToDelete(tmp[1]);
+            rmdir(tmp[0]);
         }
     }
 }

Modified: commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/LongPathTest.java
URL: http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/LongPathTest.java?rev=1157119&r1=1157118&r2=1157119&view=diff
==============================================================================
--- commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/LongPathTest.java
(original)
+++ commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/LongPathTest.java
Fri Aug 12 14:21:44 2011
@@ -136,6 +136,8 @@ public class LongPathTest extends Abstra
             checkArchiveContent(ais, expected);
         } catch (AssertionFailedError e) {
             fail("Error processing "+file.getName()+" "+e);
+        } finally {
+            ais.close();
         }
     }
 }

Modified: commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/TarTestCase.java
URL: http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/TarTestCase.java?rev=1157119&r1=1157118&r2=1157119&view=diff
==============================================================================
--- commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/TarTestCase.java
(original)
+++ commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/TarTestCase.java
Fri Aug 12 14:21:44 2011
@@ -158,11 +158,9 @@ public final class TarTestCase extends A
             if (tos != null) {
                 tos.close();
             }
-            if (archive != null) {
-                archive.delete();
-            }
-            tmp[1].delete();
-            tmp[0].delete();
+            tryHardToDelete(archive);
+            tryHardToDelete(tmp[1]);
+            rmdir(tmp[0]);
         }
     }
 
@@ -199,11 +197,9 @@ public final class TarTestCase extends A
             if (tos != null) {
                 tos.close();
             }
-            if (archive != null) {
-                archive.delete();
-            }
-            tmp[1].delete();
-            tmp[0].delete();
+            tryHardToDelete(archive);
+            tryHardToDelete(tmp[1]);
+            rmdir(tmp[0]);
         }
     }
 
@@ -246,14 +242,12 @@ public final class TarTestCase extends A
             if (tos != null) {
                 tos.close();
             }
-            if (archive != null) {
-                archive.delete();
-            }
+            tryHardToDelete(archive);
             if (fis != null) {
                 fis.close();
             }
-            tmp[1].delete();
-            tmp[0].delete();
+            tryHardToDelete(tmp[1]);
+            rmdir(tmp[0]);
         }
     }
 
@@ -298,14 +292,12 @@ public final class TarTestCase extends A
             if (tos != null) {
                 tos.close();
             }
-            if (archive != null) {
-                archive.delete();
-            }
+            tryHardToDelete(archive);
             if (fis != null) {
                 fis.close();
             }
-            tmp[1].delete();
-            tmp[0].delete();
+            tryHardToDelete(tmp[1]);
+            rmdir(tmp[0]);
         }
     }
 }

Modified: commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/ZipTestCase.java
URL: http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/ZipTestCase.java?rev=1157119&r1=1157118&r2=1157119&view=diff
==============================================================================
--- commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/ZipTestCase.java
(original)
+++ commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/ZipTestCase.java
Fri Aug 12 14:21:44 2011
@@ -228,11 +228,9 @@ public final class ZipTestCase extends A
             if (zos != null) {
                 zos.close();
             }
-            if (archive != null) {
-                archive.delete();
-            }
-            tmp[1].delete();
-            tmp[0].delete();
+            tryHardToDelete(archive);
+            tryHardToDelete(tmp[1]);
+            rmdir(tmp[0]);
         }
     }
 
@@ -265,11 +263,9 @@ public final class ZipTestCase extends A
             if (zos != null) {
                 zos.close();
             }
-            if (archive != null) {
-                archive.delete();
-            }
-            tmp[1].delete();
-            tmp[0].delete();
+            tryHardToDelete(archive);
+            tryHardToDelete(tmp[1]);
+            rmdir(tmp[0]);
         }
     }
 
@@ -308,14 +304,12 @@ public final class ZipTestCase extends A
             if (zos != null) {
                 zos.close();
             }
-            if (archive != null) {
-                archive.delete();
-            }
+            tryHardToDelete(archive);
             if (fis != null) {
                 fis.close();
             }
-            tmp[1].delete();
-            tmp[0].delete();
+            tryHardToDelete(tmp[1]);
+            rmdir(tmp[0]);
         }
     }
 
@@ -356,14 +350,12 @@ public final class ZipTestCase extends A
             if (zos != null) {
                 zos.close();
             }
-            if (archive != null) {
-                archive.delete();
-            }
+            tryHardToDelete(archive);
             if (fis != null) {
                 fis.close();
             }
-            tmp[1].delete();
-            tmp[0].delete();
+            tryHardToDelete(tmp[1]);
+            rmdir(tmp[0]);
         }
     }
 }

Modified: commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/jar/JarArchiveOutputStreamTest.java
URL: http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/jar/JarArchiveOutputStreamTest.java?rev=1157119&r1=1157118&r2=1157119&view=diff
==============================================================================
--- commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/jar/JarArchiveOutputStreamTest.java
(original)
+++ commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/jar/JarArchiveOutputStreamTest.java
Fri Aug 12 14:21:44 2011
@@ -23,6 +23,7 @@ import java.io.FileOutputStream;
 import java.io.IOException;
 import junit.framework.TestCase;
 
+import org.apache.commons.compress.AbstractTestCase;
 import org.apache.commons.compress.archivers.zip.JarMarker;
 import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
 import org.apache.commons.compress.archivers.zip.ZipExtraField;
@@ -32,6 +33,7 @@ public class JarArchiveOutputStreamTest 
 
     public void testJarMarker() throws IOException {
         File testArchive = File.createTempFile("jar-aostest", ".jar");
+        testArchive.deleteOnExit();
         JarArchiveOutputStream out = null;
         ZipFile zf = null;
         try {
@@ -63,9 +65,7 @@ public class JarArchiveOutputStreamTest 
                 } catch (IOException e) { /* swallow */ }
             }
             ZipFile.closeQuietly(zf);
-            if (testArchive.exists()) {
-                testArchive.delete();
-            }
+            AbstractTestCase.tryHardToDelete(testArchive);
         }
     }
 

Modified: commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/tar/TarArchiveEntryTest.java
URL: http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/tar/TarArchiveEntryTest.java?rev=1157119&r1=1157118&r2=1157119&view=diff
==============================================================================
--- commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/tar/TarArchiveEntryTest.java
(original)
+++ commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/tar/TarArchiveEntryTest.java
Fri Aug 12 14:21:44 2011
@@ -25,6 +25,8 @@ import java.io.IOException;
 import java.util.Locale;
 import junit.framework.TestCase;
 
+import org.apache.commons.compress.AbstractTestCase;
+
 public class TarArchiveEntryTest extends TestCase {
 
     private static final String OS =
@@ -93,6 +95,7 @@ public class TarArchiveEntryTest extends
             if (tout != null) {
                 tout.close();
             }
+            AbstractTestCase.tryHardToDelete(f);
         }
     }
     

Modified: commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/zip/UTF8ZipFilesTest.java
URL: http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/zip/UTF8ZipFilesTest.java?rev=1157119&r1=1157118&r2=1157119&view=diff
==============================================================================
--- commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/zip/UTF8ZipFilesTest.java
(original)
+++ commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/zip/UTF8ZipFilesTest.java
Fri Aug 12 14:21:44 2011
@@ -156,6 +156,7 @@ public class UTF8ZipFilesTest extends Ab
 
     public void testZipFileReadsUnicodeFields() throws IOException {
         File file = File.createTempFile("unicode-test", ".zip");
+        file.deleteOnExit();
         ZipArchiveInputStream zi = null;
         try {
             createTestFile(file, US_ASCII, false, true);
@@ -168,15 +169,14 @@ public class UTF8ZipFilesTest extends Ab
             if (zi != null) {
                 zi.close();
             }
-            if (file.exists()) {
-                file.delete();
-            }
+            tryHardToDelete(file);
         }
     }
 
     public void testZipArchiveInputStreamReadsUnicodeFields()
         throws IOException {
         File file = File.createTempFile("unicode-test", ".zip");
+        file.deleteOnExit();
         ZipFile zf = null;
         try {
             createTestFile(file, US_ASCII, false, true);
@@ -186,9 +186,7 @@ public class UTF8ZipFilesTest extends Ab
             assertNotNull(zf.getEntry(OIL_BARREL_TXT));
         } finally {
             ZipFile.closeQuietly(zf);
-            if (file.exists()) {
-                file.delete();
-            }
+            tryHardToDelete(file);
         }
     }
 
@@ -226,13 +224,12 @@ public class UTF8ZipFilesTest extends Ab
         throws IOException {
 
         File file = File.createTempFile(encoding + "-test", ".zip");
+        file.deleteOnExit();
         try {
             createTestFile(file, encoding, withEFS, withExplicitUnicodeExtra);
             testFile(file, encoding);
         } finally {
-            if (file.exists()) {
-                file.delete();
-            }
+            tryHardToDelete(file);
         }
     }
 

Modified: commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/zip/Zip64SupportIT.java
URL: http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/zip/Zip64SupportIT.java?rev=1157119&r1=1157118&r2=1157119&view=diff
==============================================================================
--- commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/zip/Zip64SupportIT.java
(original)
+++ commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/zip/Zip64SupportIT.java
Fri Aug 12 14:21:44 2011
@@ -30,6 +30,8 @@ import java.net.URL;
 import java.util.Enumeration;
 import java.util.Random;
 
+import org.apache.commons.compress.AbstractTestCase;
+
 import org.junit.Ignore;
 import org.junit.Test;
 import static org.junit.Assert.assertArrayEquals;
@@ -2270,7 +2272,7 @@ public class Zip64SupportIT {
                 if (os != null) {
                     os.close();
                 }
-                f.delete();
+                AbstractTestCase.tryHardToDelete(f);
             }
         }
     }



Mime
View raw message