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.archivers;
20  
21  import static org.junit.Assert.assertEquals;
22  import static org.junit.Assert.assertTrue;
23  import static org.junit.Assert.fail;
24  
25  import java.io.BufferedInputStream;
26  import java.io.ByteArrayInputStream;
27  import java.io.ByteArrayOutputStream;
28  import java.io.FileInputStream;
29  import java.io.InputStream;
30  
31  import org.apache.commons.compress.archivers.zip.ZipArchiveInputStream;
32  import org.junit.Test;
33  
34  public class ArchiveStreamFactoryTest {
35  
36      /**
37       * see https://issues.apache.org/jira/browse/COMPRESS-171
38       */
39      @Test
40      public void shortTextFilesAreNoTARs() throws Exception {
41          try {
42              new ArchiveStreamFactory()
43                  .createArchiveInputStream(new ByteArrayInputStream("This certainly is not a tar archive, really, no kidding".getBytes()));
44              fail("created an input stream for a non-archive");
45          } catch (ArchiveException ae) {
46              assertTrue(ae.getMessage().startsWith("No Archiver found"));
47          }
48      }
49  
50      /**
51       * see https://issues.apache.org/jira/browse/COMPRESS-191
52       */
53      @Test
54      public void aiffFilesAreNoTARs() throws Exception {
55      	FileInputStream fis = new FileInputStream("src/test/resources/testAIFF.aif");
56      	try {
57              InputStream is = new BufferedInputStream(fis);
58              try {
59                  new ArchiveStreamFactory().createArchiveInputStream(is);
60                  fail("created an input stream for a non-archive");
61              } catch (ArchiveException ae) {
62                  assertTrue(ae.getMessage().startsWith("No Archiver found"));
63              } finally {
64                  is.close();
65              }
66      	} finally {
67              fis.close();
68      	}
69      }
70  
71      @Test
72      public void testCOMPRESS209() throws Exception {
73      	FileInputStream fis = new FileInputStream("src/test/resources/testCompress209.doc");
74      	try {
75              InputStream bis = new BufferedInputStream(fis);
76              try {
77                  new ArchiveStreamFactory().createArchiveInputStream(bis);
78                  fail("created an input stream for a non-archive");
79              } catch (ArchiveException ae) {
80                  assertTrue(ae.getMessage().startsWith("No Archiver found"));
81              } finally {
82                  bis.close();
83              }
84      	} finally {
85              fis.close();
86      	}
87      }
88  
89      @Test(expected = StreamingNotSupportedException.class)
90      public void cantRead7zFromStream() throws Exception {
91          new ArchiveStreamFactory()
92              .createArchiveInputStream(ArchiveStreamFactory.SEVEN_Z,
93                                        new ByteArrayInputStream(new byte[0]));
94      }
95  
96      @Test(expected = StreamingNotSupportedException.class)
97      public void cantWrite7zToStream() throws Exception {
98          new ArchiveStreamFactory()
99              .createArchiveOutputStream(ArchiveStreamFactory.SEVEN_Z,
100                                        new ByteArrayOutputStream());
101     }
102 
103     /**
104      * Test case for 
105      * <a href="https://issues.apache.org/jira/browse/COMPRESS-267"
106      * >COMPRESS-267</a>.
107      */
108     @Test
109     public void detectsAndThrowsFor7z() throws Exception {
110     	FileInputStream fis = new FileInputStream("src/test/resources/bla.7z");
111     	try {
112             InputStream bis = new BufferedInputStream(fis);
113             try {
114                 new ArchiveStreamFactory().createArchiveInputStream(bis);
115                 fail("Expected a StreamingNotSupportedException");
116             } catch (StreamingNotSupportedException ex) {
117                 assertEquals(ArchiveStreamFactory.SEVEN_Z, ex.getFormat());
118             } finally {
119                 bis.close();
120             }
121     	} finally {
122             fis.close();
123     	}
124     }
125 
126     /**
127      * Test case for 
128      * <a href="https://issues.apache.org/jira/browse/COMPRESS-208"
129      * >COMPRESS-208</a>.
130      */
131     @Test
132     public void skipsPK00Prefix() throws Exception {
133     	FileInputStream fis = new FileInputStream("src/test/resources/COMPRESS-208.zip");
134     	try {
135             InputStream bis = new BufferedInputStream(fis);
136             try {
137                 ArchiveInputStream ais = new ArchiveStreamFactory().createArchiveInputStream(bis);
138                 try {
139                     assertTrue(ais instanceof ZipArchiveInputStream);
140                 } finally {
141                     ais.close();
142                 }
143             } finally {
144                 bis.close();
145             }
146     	} finally {
147             fis.close();
148     	}
149     }
150 }