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.apache.commons.compress.AbstractTestCase.getFile;
22  
23  import java.io.BufferedInputStream;
24  import java.io.ByteArrayInputStream;
25  import java.io.FileInputStream;
26  import java.io.IOException;
27  import junit.framework.TestCase;
28  
29  import org.apache.commons.compress.compressors.CompressorException;
30  import org.apache.commons.compress.compressors.CompressorInputStream;
31  import org.apache.commons.compress.compressors.CompressorStreamFactory;
32  import org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream;
33  import org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream;
34  import org.apache.commons.compress.compressors.pack200.Pack200CompressorInputStream;
35  import org.apache.commons.compress.compressors.xz.XZCompressorInputStream;
36  
37  public final class DetectCompressorTestCase extends TestCase {
38  
39      public DetectCompressorTestCase(String name) {
40          super(name);
41      }
42  
43      final CompressorStreamFactory factory = new CompressorStreamFactory();
44  
45      public void testDetection() throws Exception {
46          CompressorInputStream bzip2 = getStreamFor("bla.txt.bz2"); 
47          assertNotNull(bzip2);
48          assertTrue(bzip2 instanceof BZip2CompressorInputStream);
49  
50          CompressorInputStream gzip = getStreamFor("bla.tgz");
51          assertNotNull(gzip);
52          assertTrue(gzip instanceof GzipCompressorInputStream);
53          
54          CompressorInputStream pack200 = getStreamFor("bla.pack");
55          assertNotNull(pack200);
56          assertTrue(pack200 instanceof Pack200CompressorInputStream);
57  
58          CompressorInputStream xz = getStreamFor("bla.tar.xz");
59          assertNotNull(xz);
60          assertTrue(xz instanceof XZCompressorInputStream);
61  
62          try {
63              factory.createCompressorInputStream(new ByteArrayInputStream(new byte[0]));
64              fail("No exception thrown for an empty input stream");
65          } catch (CompressorException e) {
66              // expected
67          }
68      }
69  
70      private CompressorInputStream getStreamFor(String resource)
71              throws CompressorException, IOException {
72          return factory.createCompressorInputStream(
73                     new BufferedInputStream(new FileInputStream(
74                         getFile(resource))));
75      }
76  
77  }