View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   * http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.commons.compress;
20  
21  import java.io.BufferedInputStream;
22  import java.io.File;
23  import java.io.FileInputStream;
24  import java.io.IOException;
25  
26  import org.apache.commons.compress.archivers.ArchiveException;
27  import org.apache.commons.compress.archivers.ArchiveInputStream;
28  import org.apache.commons.compress.archivers.ar.ArArchiveInputStream;
29  import org.apache.commons.compress.archivers.arj.ArjArchiveInputStream;
30  import org.apache.commons.compress.archivers.cpio.CpioArchiveInputStream;
31  import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
32  import org.apache.commons.compress.archivers.zip.ZipArchiveInputStream;
33  
34  public final class DetectArchiverTestCase extends AbstractTestCase {
35      public DetectArchiverTestCase(String name) {
36          super(name);
37      }
38  
39      final ClassLoader classLoader = getClass().getClassLoader();
40  
41      public void testDetectionNotArchive() throws IOException {
42          try {
43              getStreamFor("test.txt");
44              fail("Expected ArchiveException");
45          } catch (ArchiveException e) {
46              // expected
47          }
48      }
49  
50      public void testCOMPRESS117() throws Exception {
51          final ArchiveInputStream tar = getStreamFor("COMPRESS-117.tar");
52          assertNotNull(tar);
53          assertTrue(tar instanceof TarArchiveInputStream);
54      }
55  
56      public void testDetection() throws Exception {
57  
58          final ArchiveInputStream ar = getStreamFor("bla.ar"); 
59          assertNotNull(ar);
60          assertTrue(ar instanceof ArArchiveInputStream);
61  
62          final ArchiveInputStream tar = getStreamFor("bla.tar");
63          assertNotNull(tar);
64          assertTrue(tar instanceof TarArchiveInputStream);
65  
66          final ArchiveInputStream zip = getStreamFor("bla.zip");
67          assertNotNull(zip);
68          assertTrue(zip instanceof ZipArchiveInputStream);
69  
70          final ArchiveInputStream jar = getStreamFor("bla.jar");
71          assertNotNull(jar);
72          assertTrue(jar instanceof ZipArchiveInputStream);
73  
74          final ArchiveInputStream cpio = getStreamFor("bla.cpio");
75          assertNotNull(cpio);
76          assertTrue(cpio instanceof CpioArchiveInputStream);
77          
78          final ArchiveInputStream arj = getStreamFor("bla.arj");
79          assertNotNull(arj);
80          assertTrue(arj instanceof ArjArchiveInputStream);
81  
82  // Not yet implemented
83  //        final ArchiveInputStream tgz = getStreamFor("bla.tgz");
84  //        assertNotNull(tgz);
85  //        assertTrue(tgz instanceof TarArchiveInputStream);
86  
87      }
88  
89      private ArchiveInputStream getStreamFor(String resource)
90              throws ArchiveException, IOException {
91          return factory.createArchiveInputStream(
92                     new BufferedInputStream(new FileInputStream(
93                         getFile(resource))));
94      }
95  
96      // Check that the empty archives created by the code are readable
97  
98      // Not possible to detect empty "ar" archive as it is completely empty
99  //    public void testEmptyArArchive() throws Exception {
100 //        emptyArchive("ar");
101 //    }
102 
103     public void testEmptyCpioArchive() throws Exception {
104         checkEmptyArchive("cpio");
105     }
106 
107     public void testEmptyJarArchive() throws Exception {
108         checkEmptyArchive("jar");
109     }
110 
111     // empty tar archives just have 512 null bytes
112 //    public void testEmptyTarArchive() throws Exception {
113 //        checkEmptyArchive("tar");
114 //    }
115     public void testEmptyZipArchive() throws Exception {
116         checkEmptyArchive("zip");
117     }
118 
119     private void checkEmptyArchive(String type) throws Exception{
120         File ar = createEmptyArchive(type); // will be deleted by tearDown()
121         ar.deleteOnExit(); // Just in case file cannot be deleted
122         ArchiveInputStream ais = null;
123         BufferedInputStream in = null;
124         try {
125             in = new BufferedInputStream(new FileInputStream(ar));
126             ais = factory.createArchiveInputStream(in);
127         } catch (ArchiveException ae) {
128             fail("Should have recognised empty archive for "+type);
129         } finally {
130             if (ais != null) {
131                 ais.close(); // will close input as well
132             } else if (in != null){
133                 in.close();
134             }
135         }
136     }
137 }