commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From s...@apache.org
Subject svn commit: r759811 - /commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/DetectArchiverTestCase.java
Date Mon, 30 Mar 2009 00:47:07 GMT
Author: sebb
Date: Mon Mar 30 00:47:06 2009
New Revision: 759811

URL: http://svn.apache.org/viewvc?rev=759811&view=rev
Log:
Check that empty archives created by the code can be read back

Modified:
    commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/DetectArchiverTestCase.java

Modified: commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/DetectArchiverTestCase.java
URL: http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/DetectArchiverTestCase.java?rev=759811&r1=759810&r2=759811&view=diff
==============================================================================
--- commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/DetectArchiverTestCase.java
(original)
+++ commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/DetectArchiverTestCase.java
Mon Mar 30 00:47:06 2009
@@ -21,7 +21,14 @@
 import java.io.BufferedInputStream;
 import java.io.File;
 import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.net.URL;
+import java.util.ArrayList;
+import java.util.List;
 
+import org.apache.commons.compress.archivers.ArchiveEntry;
+import org.apache.commons.compress.archivers.ArchiveException;
 import org.apache.commons.compress.archivers.ArchiveInputStream;
 import org.apache.commons.compress.archivers.ArchiveStreamFactory;
 import org.apache.commons.compress.archivers.ar.ArArchiveInputStream;
@@ -30,44 +37,106 @@
 import org.apache.commons.compress.archivers.zip.ZipArchiveInputStream;
 
 public final class DetectArchiverTestCase extends AbstractTestCase {
+    final ArchiveStreamFactory factory = new ArchiveStreamFactory();
+    final ClassLoader classLoader = getClass().getClassLoader();
+
     public void testDetection() throws Exception {
-        final ArchiveStreamFactory factory = new ArchiveStreamFactory();
 
-        final ArchiveInputStream ar = factory.createArchiveInputStream(
-                                                                       new BufferedInputStream(new
FileInputStream(
-                                                                                        
                          new File(getClass().getClassLoader().getResource("bla.ar").getFile()))));

+        final ArchiveInputStream ar = getStreamFor("bla.ar"); 
         assertNotNull(ar);
         assertTrue(ar instanceof ArArchiveInputStream);
 
-        final ArchiveInputStream tar = factory.createArchiveInputStream(
-                                                                        new BufferedInputStream(new
FileInputStream(
-                                                                                        
                           new File(getClass().getClassLoader().getResource("bla.tar").getFile()))));
+        final ArchiveInputStream tar = getStreamFor("bla.tar");
         assertNotNull(tar);
         assertTrue(tar instanceof TarArchiveInputStream);
 
-        final ArchiveInputStream zip = factory.createArchiveInputStream(
-                                                                        new BufferedInputStream(new
FileInputStream(
-                                                                                        
                           new File(getClass().getClassLoader().getResource("bla.zip").getFile()))));
+        final ArchiveInputStream zip = getStreamFor("bla.zip");
         assertNotNull(zip);
         assertTrue(zip instanceof ZipArchiveInputStream);
 
-        final ArchiveInputStream jar = factory.createArchiveInputStream(
-                                                                        new BufferedInputStream(new
FileInputStream(
-                                                                                        
                           new File(getClass().getClassLoader().getResource("bla.jar").getFile()))));
+        final ArchiveInputStream jar = getStreamFor("bla.jar");
         assertNotNull(jar);
         assertTrue(jar instanceof ZipArchiveInputStream);
 
-        final ArchiveInputStream cpio = factory.createArchiveInputStream(
-                                                                         new BufferedInputStream(new
FileInputStream(
-                                                                                        
                            new File(getClass().getClassLoader().getResource("bla.cpio").getFile()))));
+        final ArchiveInputStream cpio = getStreamFor("bla.cpio");
         assertNotNull(cpio);
         assertTrue(cpio instanceof CpioArchiveInputStream);
 
-        //              final ArchiveInputStream tgz = factory.createArchiveInputStream(
-        //                              new BufferedInputStream(new FileInputStream(
-        //                                              new File(getClass().getClassLoader().getResource("bla.tgz").getFile()))));
-        //              assertTrue(tgz instanceof TarArchiveInputStream);
+// Not yet implemented        
+//        final ArchiveInputStream tgz = getStreamFor("bla.tgz");
+//        assertNotNull(tgz);
+//        assertTrue(tgz instanceof TarArchiveInputStream);
+
+    }
+
+    private ArchiveInputStream getStreamFor(String resource)
+            throws ArchiveException, FileNotFoundException {
+        final URL rsc = classLoader.getResource(resource);
+        assertNotNull("Could not find resource "+resource,rsc);
+        return factory.createArchiveInputStream(
+                   new BufferedInputStream(new FileInputStream(
+                       new File(rsc.getFile()))));
+    }
+    
+    // Scan list of archives in resources/archives directory
+    public void notyettestArchives() throws Exception{
+        File arcdir =new File(classLoader.getResource("archives").getFile());
+        assertTrue(arcdir.exists());
+        File[]files=arcdir.listFiles();
+        for (int i=0; i<files.length; i++){
+            final File file = files[i];
+            if (file.getName().endsWith(".txt")){
+                continue;
+            }
+           try {
+               // TODO - how to determine expected file contents
+               final ArrayList expected = new ArrayList();
+               checkArchiveContent(file, expected);
+            } catch (ArchiveException e) {
+                fail("Problem checking "+file);
+            }
+        }
+    }
+
+    // Check that the empty archives created by the code are readable
+    
+    // Not possible to detect empty "ar" archive as it is completely empty
+//    public void testEmptyArArchive() throws Exception {
+//        emptyArchive("ar");
+//    }
+
+    public void testEmptyCpioArchive() throws Exception {
+        checkEmptyArchive("cpio");
+    }
+
+    public void testEmptyJarArchive() throws Exception {
+        checkEmptyArchive("jar");
+    }
 
+    // empty tar archives just have 512 null bytes
+//    public void testEmptyTarArchive() throws Exception {
+//        checkEmptyArchive("tar");
+//    }
+    public void testEmptyZipArchive() throws Exception {
+        checkEmptyArchive("zip");
     }
 
+    private void checkEmptyArchive(String type) throws Exception{
+        File ar = createEmptyArchive(type); // will be deleted by tearDown()
+        ar.deleteOnExit(); // Just in case file cannot be deleted
+        ArchiveInputStream ais = null;
+        BufferedInputStream in = null;
+        try {
+            in = new BufferedInputStream(new FileInputStream(ar));
+            ais = factory.createArchiveInputStream(in);
+        } catch (ArchiveException ae) {
+            fail("Should have recognised empty archive for "+type);
+        } finally {
+            if (ais != null) {
+                ais.close(); // will close input as well
+            } else if (in != null){
+                in.close();
+            }
+        }
+    }
 }



Mime
View raw message