Home | History | Annotate | Download | only in compress
      1 /*
      2  * Licensed to the Apache Software Foundation (ASF) under one or more
      3  * contributor license agreements.  See the NOTICE file distributed with
      4  * this work for additional information regarding copyright ownership.
      5  * The ASF licenses this file to You under the Apache License, Version 2.0
      6  * (the "License"); you may not use this file except in compliance with
      7  * the License.  You may obtain a copy of the License at
      8  *
      9  *   http://www.apache.org/licenses/LICENSE-2.0
     10  *
     11  * Unless required by applicable law or agreed to in writing, software
     12  * distributed under the License is distributed on an "AS IS" BASIS,
     13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14  * See the License for the specific language governing permissions and
     15  * limitations under the License.
     16  *
     17  */
     18 
     19 package org.apache.commons.compress;
     20 
     21 import static org.junit.Assert.*;
     22 
     23 import java.io.ByteArrayOutputStream;
     24 import java.io.File;
     25 import java.io.FileInputStream;
     26 import java.io.InputStream;
     27 import java.io.OutputStream;
     28 
     29 import org.apache.commons.compress.archivers.ArchiveEntry;
     30 import org.apache.commons.compress.archivers.ArchiveInputStream;
     31 import org.apache.commons.compress.archivers.ArchiveOutputStream;
     32 import org.apache.commons.compress.archivers.ar.ArArchiveEntry;
     33 import org.apache.commons.compress.archivers.cpio.CpioArchiveEntry;
     34 import org.apache.commons.compress.archivers.jar.JarArchiveEntry;
     35 import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
     36 import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
     37 import org.junit.Test;
     38 
     39 /**
     40  * Check that the different write methods create the same output.
     41  * TODO perform the same checks for reads.
     42  */
     43 public class IOMethodsTest extends AbstractTestCase {
     44 
     45     private static final int bytesToTest = 50;
     46     private static final byte[] byteTest = new byte[bytesToTest];
     47     static {
     48         for(int i=0; i < byteTest.length ;) {
     49             byteTest[i]=(byte) i;
     50             byteTest[i+1]=(byte) -i;
     51             i += 2;
     52         }
     53     }
     54 
     55     @Test
     56     public void testWriteAr() throws Exception {
     57         final ArchiveEntry entry = new ArArchiveEntry("dummy", bytesToTest);
     58         compareWrites("ar", entry);
     59     }
     60 
     61     @Test
     62     public void testWriteCpio() throws Exception {
     63         final ArchiveEntry entry = new CpioArchiveEntry("dummy", bytesToTest);
     64         compareWrites("cpio", entry);
     65     }
     66 
     67     @Test
     68     public void testWriteJar() throws Exception {
     69         final ArchiveEntry entry = new JarArchiveEntry("dummy");
     70         compareWrites("jar", entry);
     71     }
     72 
     73     @Test
     74     public void testWriteTar() throws Exception {
     75         final TarArchiveEntry entry = new TarArchiveEntry("dummy");
     76         entry.setSize(bytesToTest);
     77         compareWrites("tar", entry);
     78     }
     79 
     80     @Test
     81     public void testWriteZip() throws Exception {
     82         final ArchiveEntry entry = new ZipArchiveEntry("dummy");
     83         compareWrites("zip", entry);
     84     }
     85 
     86     @Test
     87     public void testReadAr() throws Exception {
     88         compareReads("ar");
     89     }
     90 
     91     @Test
     92     public void testReadCpio() throws Exception {
     93         compareReads("cpio");
     94     }
     95 
     96     @Test
     97     public void testReadJar() throws Exception {
     98         compareReads("jar");
     99     }
    100 
    101     @Test
    102     public void testReadTar() throws Exception {
    103         compareReads("tar");
    104     }
    105 
    106     @Test
    107     public void testReadZip() throws Exception {
    108         compareReads("zip");
    109     }
    110 
    111     private void compareWrites(final String archiverName, final ArchiveEntry entry) throws Exception {
    112         final OutputStream out1 = new ByteArrayOutputStream();
    113         final OutputStream out2 = new ByteArrayOutputStream();
    114         final OutputStream out3 = new ByteArrayOutputStream();
    115         final ArchiveOutputStream aos1 = factory.createArchiveOutputStream(archiverName, out1);
    116         aos1.putArchiveEntry(entry);
    117         final ArchiveOutputStream aos2 = factory.createArchiveOutputStream(archiverName, out2);
    118         aos2.putArchiveEntry(entry);
    119         final ArchiveOutputStream aos3 = factory.createArchiveOutputStream(archiverName, out3);
    120         aos3.putArchiveEntry(entry);
    121         for (final byte element : byteTest) {
    122             aos1.write(element);
    123         }
    124         aos1.closeArchiveEntry();
    125         aos1.close();
    126 
    127         aos2.write(byteTest);
    128         aos2.closeArchiveEntry();
    129         aos2.close();
    130 
    131         aos3.write(byteTest, 0, byteTest.length);
    132         aos3.closeArchiveEntry();
    133         aos3.close();
    134         assertEquals("aos1Bytes!=aos2Bytes",aos1.getBytesWritten(),aos2.getBytesWritten());
    135         assertEquals("aos1Bytes!=aos3Bytes",aos1.getBytesWritten(),aos3.getBytesWritten());
    136         assertEquals("out1Len!=out2Len",out1.toString().length(),out2.toString().length());
    137         assertEquals("out1Len!=out2Len",out1.toString().length(),out3.toString().length());
    138         assertEquals("out1!=out2",out1.toString(),out2.toString());
    139         assertEquals("out1!=out3",out1.toString(),out3.toString());
    140     }
    141 
    142     private void compareReads(final String archiverName) throws Exception {
    143         final OutputStream out1 = new ByteArrayOutputStream();
    144         final OutputStream out2 = new ByteArrayOutputStream();
    145         final OutputStream out3 = new ByteArrayOutputStream();
    146         final File file = createSingleEntryArchive(archiverName);
    147         file.deleteOnExit();
    148 
    149         final InputStream is1 = new FileInputStream(file);
    150         final ArchiveInputStream ais1 = factory.createArchiveInputStream(archiverName, is1);
    151         final ArchiveEntry nextEntry = ais1.getNextEntry();
    152         assertNotNull(nextEntry);
    153 
    154         final byte [] buff = new byte[10]; // small so multiple reads are needed;
    155         final long size = nextEntry.getSize();
    156         if (size != ArchiveEntry.SIZE_UNKNOWN) {
    157             assertTrue("Size should be > 0, found: "+size, size > 0);
    158         }
    159 
    160         final InputStream is2 = new FileInputStream(file);
    161         final ArchiveInputStream ais2 = factory.createArchiveInputStream(archiverName, is2);
    162         final ArchiveEntry nextEntry2 = ais2.getNextEntry();
    163         assertNotNull(nextEntry2);
    164         assertEquals("Expected same entry size", size, nextEntry2.getSize());
    165 
    166         final InputStream is3 = new FileInputStream(file);
    167         final ArchiveInputStream ais3 = factory.createArchiveInputStream(archiverName, is3);
    168         final ArchiveEntry nextEntry3 = ais3.getNextEntry();
    169         assertNotNull(nextEntry3);
    170         assertEquals("Expected same entry size", size, nextEntry3.getSize());
    171 
    172         int b;
    173         while((b=ais1.read()) != -1){
    174             out1.write(b);
    175         }
    176         ais1.close();
    177 
    178         int bytes;
    179         while((bytes = ais2.read(buff)) > 0){
    180             out2.write(buff, 0, bytes);
    181         }
    182         ais2.close();
    183 
    184         while((bytes=ais3.read(buff, 0 , buff.length)) > 0){
    185             out3.write(buff, 0, bytes);
    186         }
    187         ais3.close();
    188 
    189         assertEquals("out1Len!=out2Len",out1.toString().length(),out2.toString().length());
    190         assertEquals("out1Len!=out3Len",out1.toString().length(),out3.toString().length());
    191         assertEquals("out1!=out2",out1.toString(),out2.toString());
    192         assertEquals("out1!=out3",out1.toString(),out3.toString());
    193     }
    194 }
    195