Home | History | Annotate | Download | only in zip
      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 package org.apache.harmony.tests.java.util.zip;
     18 
     19 import java.io.ByteArrayInputStream;
     20 import java.io.ByteArrayOutputStream;
     21 import java.io.File;
     22 import java.io.FileOutputStream;
     23 import java.io.IOException;
     24 import java.util.zip.CRC32;
     25 import java.util.zip.ZipEntry;
     26 import java.util.zip.ZipException;
     27 import java.util.zip.ZipInputStream;
     28 import java.util.zip.ZipOutputStream;
     29 
     30 public class ZipOutputStreamTest extends junit.framework.TestCase {
     31 
     32     ZipOutputStream zos;
     33 
     34     ByteArrayOutputStream bos;
     35 
     36     ZipInputStream zis;
     37 
     38     static final String data = "HelloWorldHelloWorldHelloWorldHelloWorldHelloWorldHelloWorldHelloWorldHelloWorldHelloWorldHelloWorldHelloWorldHelloWorldHelloWorld";
     39 
     40     /**
     41      * java.util.zip.ZipOutputStream#close()
     42      */
     43     public void test_close() throws Exception {
     44         zos = new ZipOutputStream(bos);
     45         zos.putNextEntry(new ZipEntry("XX"));
     46         zos.closeEntry();
     47         zos.close();
     48 
     49         // Regression for HARMONY-97
     50         ZipOutputStream zos = new ZipOutputStream(new ByteArrayOutputStream());
     51         zos.putNextEntry(new ZipEntry("myFile"));
     52         zos.close();
     53         zos.close(); // Should be a no-op
     54     }
     55 
     56     /**
     57      * java.util.zip.ZipOutputStream#closeEntry()
     58      */
     59     public void test_closeEntry() throws IOException {
     60         ZipEntry ze = new ZipEntry("testEntry");
     61         ze.setTime(System.currentTimeMillis());
     62         zos.putNextEntry(ze);
     63         zos.write("Hello World".getBytes("UTF-8"));
     64         zos.closeEntry();
     65         assertTrue("closeEntry failed to update required fields",
     66                 ze.getSize() == 11 && ze.getCompressedSize() == 13);
     67 
     68     }
     69 
     70     /**
     71      * java.util.zip.ZipOutputStream#finish()
     72      */
     73     public void test_finish() throws Exception {
     74         ZipEntry ze = new ZipEntry("test");
     75         zos.putNextEntry(ze);
     76         zos.write("Hello World".getBytes());
     77         zos.finish();
     78         assertEquals("Finish failed to closeCurrentEntry", 11, ze.getSize());
     79 
     80         ZipOutputStream zos = new ZipOutputStream(new ByteArrayOutputStream());
     81         zos.putNextEntry(new ZipEntry("myFile"));
     82         zos.finish();
     83         zos.close();
     84         try {
     85             zos.finish();
     86             fail("Assert 0: Expected IOException");
     87         } catch (IOException e) {
     88             // Expected
     89         }
     90     }
     91 
     92     /**
     93      * java.util.zip.ZipOutputStream#putNextEntry(java.util.zip.ZipEntry)
     94      */
     95     public void test_putNextEntryLjava_util_zip_ZipEntry() throws IOException {
     96         ZipEntry ze = new ZipEntry("testEntry");
     97         ze.setTime(System.currentTimeMillis());
     98         zos.putNextEntry(ze);
     99         zos.write("Hello World".getBytes());
    100         zos.closeEntry();
    101         zos.close();
    102         zis = new ZipInputStream(new ByteArrayInputStream(bos.toByteArray()));
    103         ZipEntry ze2 = zis.getNextEntry();
    104         zis.closeEntry();
    105         assertEquals("Failed to write correct entry", ze.getName(), ze2.getName());
    106         assertEquals("Failed to write correct entry", ze.getCrc(), ze2.getCrc());
    107         try {
    108             zos.putNextEntry(ze);
    109             fail("Entry with incorrect setting failed to throw exception");
    110         } catch (IOException e) {
    111             // expected
    112         }
    113     }
    114 
    115     /**
    116      * java.util.zip.ZipOutputStream#setComment(java.lang.String)
    117      */
    118     public void test_setCommentLjava_lang_String() {
    119         // There is no way to get the comment back, so no way to determine if
    120         // the comment is set correct
    121         zos.setComment("test setComment");
    122 
    123         try {
    124             zos.setComment(new String(new byte[0xFFFF + 1]));
    125             fail("Comment over 0xFFFF in length should throw exception");
    126         } catch (IllegalArgumentException e) {
    127             // Passed
    128         }
    129     }
    130 
    131     /**
    132      * java.util.zip.ZipOutputStream#setLevel(int)
    133      */
    134     public void test_setLevelI() throws IOException {
    135         ZipEntry ze = new ZipEntry("test");
    136         zos.putNextEntry(ze);
    137         zos.write(data.getBytes());
    138         zos.closeEntry();
    139         long csize = ze.getCompressedSize();
    140         zos.setLevel(9); // Max Compression
    141         zos.putNextEntry(ze = new ZipEntry("test2"));
    142         zos.write(data.getBytes());
    143         zos.closeEntry();
    144         assertTrue("setLevel failed", csize <= ze.getCompressedSize());
    145     }
    146 
    147     /**
    148      * java.util.zip.ZipOutputStream#setMethod(int)
    149      */
    150     public void test_setMethodI() throws IOException {
    151         ZipEntry ze = new ZipEntry("test");
    152         zos.setMethod(ZipOutputStream.STORED);
    153         CRC32 tempCrc = new CRC32();
    154         tempCrc.update(data.getBytes());
    155         ze.setCrc(tempCrc.getValue());
    156         ze.setSize(new String(data).length());
    157         zos.putNextEntry(ze);
    158         zos.write(data.getBytes());
    159         zos.closeEntry();
    160         long csize = ze.getCompressedSize();
    161         zos.setMethod(ZipOutputStream.DEFLATED);
    162         zos.putNextEntry(ze = new ZipEntry("test2"));
    163         zos.write(data.getBytes());
    164         zos.closeEntry();
    165         assertTrue("setLevel failed", csize >= ze.getCompressedSize());
    166     }
    167 
    168     /**
    169      * java.util.zip.ZipOutputStream#write(byte[], int, int)
    170      */
    171     public void test_write$BII() throws IOException {
    172         ZipEntry ze = new ZipEntry("test");
    173         zos.putNextEntry(ze);
    174         zos.write(data.getBytes());
    175         zos.closeEntry();
    176         zos.close();
    177         zos = null;
    178         zis = new ZipInputStream(new ByteArrayInputStream(bos.toByteArray()));
    179         zis.getNextEntry();
    180         byte[] b = new byte[data.length()];
    181         int r = 0;
    182         int count = 0;
    183         while (count != b.length && (r = zis.read(b, count, b.length)) != -1) {
    184             count += r;
    185         }
    186         zis.closeEntry();
    187         assertEquals("Write failed to write correct bytes", new String(b), data);
    188 
    189         File f = File.createTempFile("testZip", "tst");
    190         f.deleteOnExit();
    191         FileOutputStream stream = new FileOutputStream(f);
    192         ZipOutputStream zip = new ZipOutputStream(stream);
    193         zip.setMethod(ZipEntry.STORED);
    194 
    195         try {
    196             zip.putNextEntry(new ZipEntry("Second"));
    197             fail("Not set an entry. Should have thrown ZipException.");
    198         } catch (ZipException e) {
    199             // expected -- We have not set an entry
    200         }
    201 
    202         try {
    203             // We try to write data without entry
    204             zip.write(new byte[2]);
    205             fail("Writing data without an entry. Should have thrown IOException");
    206         } catch (IOException e) {
    207             // expected
    208         }
    209 
    210         try {
    211             // Try to write without an entry and with nonsense offset and
    212             // length
    213             zip.write(new byte[2], 0, 12);
    214             fail("Writing data without an entry. Should have thrown IndexOutOfBoundsException");
    215         } catch (IndexOutOfBoundsException e) {
    216             // expected
    217         }
    218 
    219         // Regression for HARMONY-4405
    220         try {
    221             zip.write(null, 0, -2);
    222             fail();
    223         } catch (NullPointerException expected) {
    224         } catch (IndexOutOfBoundsException expected) {
    225         }
    226         try {
    227             zip.write(null, 0, 2);
    228             fail();
    229         } catch (NullPointerException expected) {
    230         }
    231         try {
    232             zip.write(new byte[2], 0, -2);
    233             fail();
    234         } catch (IndexOutOfBoundsException expected) {
    235         }
    236 
    237         // Close stream because ZIP is invalid
    238         stream.close();
    239     }
    240 
    241     /**
    242      * java.util.zip.ZipOutputStream#write(byte[], int, int)
    243      */
    244     public void test_write$BII_2() throws IOException {
    245         // Regression for HARMONY-577
    246         File f1 = File.createTempFile("testZip1", "tst");
    247         f1.deleteOnExit();
    248         FileOutputStream stream1 = new FileOutputStream(f1);
    249         ZipOutputStream zip1 = new ZipOutputStream(stream1);
    250         zip1.putNextEntry(new ZipEntry("one"));
    251         zip1.setMethod(ZipOutputStream.STORED);
    252         zip1.setMethod(ZipEntry.STORED);
    253 
    254         zip1.write(new byte[2]);
    255 
    256         try {
    257             zip1.putNextEntry(new ZipEntry("Second"));
    258             fail("ZipException expected");
    259         } catch (ZipException e) {
    260             // expected - We have not set an entry
    261         }
    262 
    263         try {
    264             zip1.write(new byte[2]); // try to write data without entry
    265             fail("expected IOE there");
    266         } catch (IOException e2) {
    267             // expected
    268         }
    269 
    270         zip1.close();
    271     }
    272 
    273     @Override
    274     protected void setUp() throws Exception {
    275         super.setUp();
    276         zos = new ZipOutputStream(bos = new ByteArrayOutputStream());
    277     }
    278 
    279     @Override
    280     protected void tearDown() throws Exception {
    281         try {
    282             if (zos != null) {
    283                 zos.close();
    284             }
    285             if (zis != null) {
    286                 zis.close();
    287             }
    288         } catch (Exception e) {
    289         }
    290         super.tearDown();
    291     }
    292 }
    293