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 static org.junit.Assert.*;
22  
23  import java.io.BufferedInputStream;
24  import java.io.File;
25  import java.io.FileInputStream;
26  import java.io.IOException;
27  
28  import org.apache.commons.compress.archivers.ArchiveException;
29  import org.apache.commons.compress.archivers.ArchiveInputStream;
30  import org.apache.commons.compress.archivers.ar.ArArchiveInputStream;
31  import org.apache.commons.compress.archivers.arj.ArjArchiveInputStream;
32  import org.apache.commons.compress.archivers.cpio.CpioArchiveInputStream;
33  import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
34  import org.apache.commons.compress.archivers.zip.ZipArchiveInputStream;
35  import org.junit.Test;
36  
37  public final class DetectArchiverTestCase extends AbstractTestCase {
38  
39      final ClassLoader classLoader = getClass().getClassLoader();
40  
41      @Test
42      public void testDetectionNotArchive() throws IOException {
43          try {
44              getStreamFor("test.txt");
45              fail("Expected ArchiveException");
46          } catch (ArchiveException e) {
47              // expected
48          }
49      }
50  
51      @Test
52      public void testCOMPRESS117() throws Exception {
53          final ArchiveInputStream tar = getStreamFor("COMPRESS-117.tar");
54          assertNotNull(tar);
55          assertTrue(tar instanceof TarArchiveInputStream);
56      }
57  
58      @Test
59      public void testDetection() throws Exception {
60  
61          final ArchiveInputStream ar = getStreamFor("bla.ar"); 
62          assertNotNull(ar);
63          assertTrue(ar instanceof ArArchiveInputStream);
64  
65          final ArchiveInputStream tar = getStreamFor("bla.tar");
66          assertNotNull(tar);
67          assertTrue(tar instanceof TarArchiveInputStream);
68  
69          final ArchiveInputStream zip = getStreamFor("bla.zip");
70          assertNotNull(zip);
71          assertTrue(zip instanceof ZipArchiveInputStream);
72  
73          final ArchiveInputStream jar = getStreamFor("bla.jar");
74          assertNotNull(jar);
75          assertTrue(jar instanceof ZipArchiveInputStream);
76  
77          final ArchiveInputStream cpio = getStreamFor("bla.cpio");
78          assertNotNull(cpio);
79          assertTrue(cpio instanceof CpioArchiveInputStream);
80          
81          final ArchiveInputStream arj = getStreamFor("bla.arj");
82          assertNotNull(arj);
83          assertTrue(arj instanceof ArjArchiveInputStream);
84  
85  // Not yet implemented
86  //        final ArchiveInputStream tgz = getStreamFor("bla.tgz");
87  //        assertNotNull(tgz);
88  //        assertTrue(tgz instanceof TarArchiveInputStream);
89  
90      }
91  
92      private ArchiveInputStream getStreamFor(String resource)
93              throws ArchiveException, IOException {
94          return factory.createArchiveInputStream(
95                     new BufferedInputStream(new FileInputStream(
96                         getFile(resource))));
97      }
98  
99      // Check that the empty archives created by the code are readable
100 
101     // Not possible to detect empty "ar" archive as it is completely empty
102 //    public void testEmptyArArchive() throws Exception {
103 //        emptyArchive("ar");
104 //    }
105 
106     @Test
107     public void testEmptyCpioArchive() throws Exception {
108         checkEmptyArchive("cpio");
109     }
110 
111     @Test
112     public void testEmptyJarArchive() throws Exception {
113         checkEmptyArchive("jar");
114     }
115 
116     // empty tar archives just have 512 null bytes
117 //    public void testEmptyTarArchive() throws Exception {
118 //        checkEmptyArchive("tar");
119 //    }
120     @Test
121     public void testEmptyZipArchive() throws Exception {
122         checkEmptyArchive("zip");
123     }
124 
125     private void checkEmptyArchive(String type) throws Exception{
126         File ar = createEmptyArchive(type); // will be deleted by tearDown()
127         ar.deleteOnExit(); // Just in case file cannot be deleted
128         ArchiveInputStream ais = null;
129         BufferedInputStream in = null;
130         try {
131             in = new BufferedInputStream(new FileInputStream(ar));
132             ais = factory.createArchiveInputStream(in);
133         } catch (ArchiveException ae) {
134             fail("Should have recognised empty archive for "+type);
135         } finally {
136             if (ais != null) {
137                 ais.close(); // will close input as well
138             } else if (in != null){
139                 in.close();
140             }
141         }
142     }
143 }