commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bode...@apache.org
Subject svn commit: r758755 - in /commons/proper/compress/trunk/src/test/java/org/apache/commons/compress: AbstractTestCase.java archivers/ZipTestCase.java changes/ChangeSetTestCase.java
Date Thu, 26 Mar 2009 17:20:30 GMT
Author: bodewig
Date: Thu Mar 26 17:19:47 2009
New Revision: 758755

URL: http://svn.apache.org/viewvc?rev=758755&view=rev
Log:
more resource savings

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/ZipTestCase.java
    commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/changes/ChangeSetTestCase.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=758755&r1=758754&r2=758755&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
Thu Mar 26 17:19:47 2009
@@ -22,6 +22,7 @@
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.FileOutputStream;
+import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
 import java.util.Iterator;
@@ -39,11 +40,18 @@
 public abstract class AbstractTestCase extends TestCase {
 
     protected File dir;
+    protected File resultDir;
 
     protected void setUp() throws Exception {
-        dir = File.createTempFile("dir", "");
-        dir.delete();
-        dir.mkdir();
+        dir = mkdir("dir");
+        resultDir = mkdir("dir-result");
+    }
+
+    protected static File mkdir(String name) throws IOException {
+        File f = File.createTempFile(name, "");
+        f.delete();
+        f.mkdir();
+        return f;
     }
 
     protected File getFile(String path) {
@@ -51,14 +59,19 @@
     }
 
     protected void tearDown() throws Exception {
-        String[] f = dir.list();
-        if (f != null) {
-            for (int i = 0; i < f.length; i++) {
-                new File(dir, f[i]).delete();
+        rmdir(dir);
+        rmdir(resultDir);
+        dir = resultDir = null;
+    }
+
+    protected static void rmdir(File f) throws IOException {
+        String[] s = f.list();
+        if (s != null) {
+            for (int i = 0; i < s.length; i++) {
+                new File(f, s[i]).delete();
             }
         }
-        dir.delete();
-        dir = null;
+        f.delete();
     }
 
     /**

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=758755&r1=758754&r2=758755&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
Thu Mar 26 17:19:47 2009
@@ -42,10 +42,11 @@
         final File file1 = getFile("test1.xml");
         final File file2 = getFile("test2.xml");
 
-        {
-            final OutputStream out = new FileOutputStream(output);
-            final ArchiveOutputStream os = new ArchiveStreamFactory().createArchiveOutputStream("zip",
out);
-
+        final OutputStream out = new FileOutputStream(output);
+        ArchiveOutputStream os = null;
+        try {
+            os = new ArchiveStreamFactory()
+                .createArchiveOutputStream("zip", out);
             os.putArchiveEntry(new ZipArchiveEntry("testdata/test1.xml"));
             IOUtils.copy(new FileInputStream(file1), os);
             os.closeArchiveEntry();
@@ -53,30 +54,41 @@
             os.putArchiveEntry(new ZipArchiveEntry("testdata/test2.xml"));
             IOUtils.copy(new FileInputStream(file2), os);
             os.closeArchiveEntry();
-            os.close();
+        } finally {
+            if (os != null) {
+                os.close();
+            } else {
+                out.close();
+            }
         }
 
         // Unarchive the same
         List results = new ArrayList();
 
-        {
-            final InputStream is = new FileInputStream(output);
-            final ArchiveInputStream in = new ArchiveStreamFactory().createArchiveInputStream("zip",
is);
-
-            File result = File.createTempFile("dir-result", "");
-            result.delete();
-            result.mkdir();
+        final InputStream is = new FileInputStream(output);
+        ArchiveInputStream in = null;
+        try {
+            in = new ArchiveStreamFactory()
+                .createArchiveInputStream("zip", is);
 
             ZipArchiveEntry entry = null;
             while((entry = (ZipArchiveEntry)in.getNextEntry()) != null) {
-                File outfile = new File(result.getCanonicalPath() + "/result/" + entry.getName());
+                File outfile = new File(resultDir.getCanonicalPath() + "/result/" + entry.getName());
                 outfile.getParentFile().mkdirs();
-                OutputStream out = new FileOutputStream(outfile);
-                IOUtils.copy(in, out);
-                out.close();
+                OutputStream o = new FileOutputStream(outfile);
+                try {
+                    IOUtils.copy(in, o);
+                } finally {
+                    o.close();
+                }
                 results.add(outfile);
             }
-            in.close();
+        } finally {
+            if (in != null) {
+                in.close();
+            } else {
+                is.close();
+            }
         }
 
         assertEquals(results.size(), 2);

Modified: commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/changes/ChangeSetTestCase.java
URL: http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/changes/ChangeSetTestCase.java?rev=758755&r1=758754&r2=758755&view=diff
==============================================================================
--- commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/changes/ChangeSetTestCase.java
(original)
+++ commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/changes/ChangeSetTestCase.java
Thu Mar 26 17:19:47 2009
@@ -53,6 +53,7 @@
         ArchiveOutputStream out = null;
         ArchiveInputStream ais = null;
         File result = File.createTempFile("test", ".zip");
+        result.deleteOnExit();
         try {
 
             final InputStream is = new FileInputStream(input);
@@ -96,6 +97,7 @@
         ArchiveOutputStream out = null;
         ArchiveInputStream ais = null;
         File result = File.createTempFile("test", ".zip");
+        result.deleteOnExit();
         try {
 
             final InputStream is = new FileInputStream(input);
@@ -141,6 +143,7 @@
         ArchiveOutputStream out = null;
         ArchiveInputStream ais = null;
         File result = File.createTempFile("test", ".zip");
+        result.deleteOnExit();
         try {
 
             final InputStream is = new FileInputStream(input);
@@ -189,6 +192,7 @@
         ArchiveOutputStream out = null;
         ArchiveInputStream ais = null;
         File result = File.createTempFile("test", ".zip");
+        result.deleteOnExit();
         try {
 
             final InputStream is = new FileInputStream(input);
@@ -240,6 +244,7 @@
         ArchiveOutputStream out = null;
         ArchiveInputStream ais = null;
         File result = File.createTempFile("test", ".zip");
+        result.deleteOnExit();
         try {
 
             final InputStream is = new FileInputStream(input);
@@ -291,6 +296,7 @@
         ArchiveOutputStream out = null;
         ArchiveInputStream ais = null;
         File result = File.createTempFile("test", ".zip");
+        result.deleteOnExit();
         try {
 
             final InputStream is = new FileInputStream(input);
@@ -348,6 +354,7 @@
                     .createArchiveInputStream("zip", is);
 
             temp = File.createTempFile("test", ".zip");
+            temp.deleteOnExit();
             out = new ArchiveStreamFactory().createArchiveOutputStream("zip",
                     new FileOutputStream(temp));
 



Mime
View raw message