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 testDetectionNotArchive()42 public void testDetectionNotArchive() throws IOException { 43 try { 44 getStreamFor("test.txt"); 45 fail("Expected ArchiveException"); 46 } catch (final ArchiveException e) { 47 // expected 48 } 49 } 50 51 @Test testCOMPRESS117()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 testCOMPRESS335()59 public void testCOMPRESS335() throws Exception { 60 final ArchiveInputStream tar = getStreamFor("COMPRESS-335.tar"); 61 assertNotNull(tar); 62 assertTrue(tar instanceof TarArchiveInputStream); 63 } 64 65 @Test testDetection()66 public void testDetection() throws Exception { 67 68 final ArchiveInputStream ar = getStreamFor("bla.ar"); 69 assertNotNull(ar); 70 assertTrue(ar instanceof ArArchiveInputStream); 71 72 final ArchiveInputStream tar = getStreamFor("bla.tar"); 73 assertNotNull(tar); 74 assertTrue(tar instanceof TarArchiveInputStream); 75 76 final ArchiveInputStream zip = getStreamFor("bla.zip"); 77 assertNotNull(zip); 78 assertTrue(zip instanceof ZipArchiveInputStream); 79 80 final ArchiveInputStream jar = getStreamFor("bla.jar"); 81 assertNotNull(jar); 82 assertTrue(jar instanceof ZipArchiveInputStream); 83 84 final ArchiveInputStream cpio = getStreamFor("bla.cpio"); 85 assertNotNull(cpio); 86 assertTrue(cpio instanceof CpioArchiveInputStream); 87 88 final ArchiveInputStream arj = getStreamFor("bla.arj"); 89 assertNotNull(arj); 90 assertTrue(arj instanceof ArjArchiveInputStream); 91 92 // Not yet implemented 93 // final ArchiveInputStream tgz = getStreamFor("bla.tgz"); 94 // assertNotNull(tgz); 95 // assertTrue(tgz instanceof TarArchiveInputStream); 96 97 } 98 getStreamFor(final String resource)99 private ArchiveInputStream getStreamFor(final String resource) 100 throws ArchiveException, IOException { 101 return factory.createArchiveInputStream( 102 new BufferedInputStream(new FileInputStream( 103 getFile(resource)))); 104 } 105 106 // Check that the empty archives created by the code are readable 107 108 // Not possible to detect empty "ar" archive as it is completely empty 109 // public void testEmptyArArchive() throws Exception { 110 // emptyArchive("ar"); 111 // } 112 113 @Test testEmptyCpioArchive()114 public void testEmptyCpioArchive() throws Exception { 115 checkEmptyArchive("cpio"); 116 } 117 118 @Test testEmptyJarArchive()119 public void testEmptyJarArchive() throws Exception { 120 checkEmptyArchive("jar"); 121 } 122 123 // empty tar archives just have 512 null bytes 124 // public void testEmptyTarArchive() throws Exception { 125 // checkEmptyArchive("tar"); 126 // } 127 @Test testEmptyZipArchive()128 public void testEmptyZipArchive() throws Exception { 129 checkEmptyArchive("zip"); 130 } 131 checkEmptyArchive(final String type)132 private void checkEmptyArchive(final String type) throws Exception{ 133 final File ar = createEmptyArchive(type); // will be deleted by tearDown() 134 ar.deleteOnExit(); // Just in case file cannot be deleted 135 ArchiveInputStream ais = null; 136 BufferedInputStream in = null; 137 try { 138 in = new BufferedInputStream(new FileInputStream(ar)); 139 ais = factory.createArchiveInputStream(in); 140 } catch (final ArchiveException ae) { 141 fail("Should have recognized empty archive for "+type); 142 } finally { 143 if (ais != null) { 144 ais.close(); // will close input as well 145 } else if (in != null){ 146 in.close(); 147 } 148 } 149 } 150 } 151