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.assertTrue;
22  import static org.junit.Assert.fail;
23  
24  import java.io.BufferedInputStream;
25  import java.io.ByteArrayInputStream;
26  import java.io.FileInputStream;
27  import java.io.InputStream;
28  
29  import org.apache.commons.compress.archivers.zip.ZipArchiveInputStream;
30  import org.junit.Test;
31  
32  public class ArchiveStreamFactoryTest {
33  
34      /**
35       * see https://issues.apache.org/jira/browse/COMPRESS-171
36       */
37      @Test
38      public void shortTextFilesAreNoTARs() throws Exception {
39          try {
40              new ArchiveStreamFactory()
41                  .createArchiveInputStream(new ByteArrayInputStream("This certainly is not a tar archive, really, no kidding".getBytes()));
42              fail("created an input stream for a non-archive");
43          } catch (ArchiveException ae) {
44              assertTrue(ae.getMessage().startsWith("No Archiver found"));
45          }
46      }
47  
48      /**
49       * see https://issues.apache.org/jira/browse/COMPRESS-191
50       */
51      @Test
52      public void aiffFilesAreNoTARs() throws Exception {
53          InputStream is = null;
54          try {
55              is = new BufferedInputStream(new FileInputStream("src/test/resources/testAIFF.aif"));
56              new ArchiveStreamFactory().createArchiveInputStream(is);
57              fail("created an input stream for a non-archive");
58          } catch (ArchiveException ae) {
59              assertTrue(ae.getMessage().startsWith("No Archiver found"));
60          } finally {
61              if (is != null) {
62                  is.close();
63              }
64          }
65      }
66  
67      @Test
68      public void testCOMPRESS209() throws Exception {
69          InputStream is = null;
70          try {
71              is = new BufferedInputStream(new FileInputStream("src/test/resources/testCompress209.doc"));
72              new ArchiveStreamFactory().createArchiveInputStream(is);
73              fail("created an input stream for a non-archive");
74          } catch (ArchiveException ae) {
75              assertTrue(ae.getMessage().startsWith("No Archiver found"));
76          } finally {
77              if (is != null) {
78                  is.close();
79              }
80          }
81      }
82  
83      /**
84       * Test case for 
85       * <a href="https://issues.apache.org/jira/browse/COMPRESS-208"
86       * >COMPRESS-208</a>.
87       */
88      @Test
89      public void skipsPK00Prefix() throws Exception {
90          InputStream is = null;
91          try {
92              is = new BufferedInputStream(new FileInputStream("src/test/resources/COMPRESS-208.zip"));
93              ArchiveInputStream ais = new ArchiveStreamFactory().createArchiveInputStream(is);
94              assertTrue(ais instanceof ZipArchiveInputStream);
95          } finally {
96              if (is != null) {
97                  is.close();
98              }
99          }
100     }
101 }