1 /*
   2  * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /**
  25  * @test
  26  * @run testng StreamZipEntriesTest
  27  * @summary Make sure we can stream entries of a zip file.
  28  */
  29 
  30 import java.io.File;
  31 import java.io.IOException;
  32 import java.lang.Object;
  33 import java.lang.System;
  34 import java.util.jar.JarFile;
  35 import java.util.jar.JarEntry;
  36 import java.util.stream.Stream;
  37 import java.util.zip.ZipEntry;
  38 import java.util.zip.ZipFile;
  39 
  40 import org.testng.annotations.Test;
  41 
  42 import static org.testng.Assert.assertEquals;
  43 import static org.testng.Assert.assertTrue;
  44 import static org.testng.Assert.fail;
  45 
  46 public class StreamZipEntriesTest {
  47 
  48     @Test
  49     public void testStreamZip() throws IOException {
  50         try (ZipFile zf = new ZipFile(new File(System.getProperty("test.src", "."), "input.zip"))) {
  51             zf.stream().forEach(e -> assertTrue(e instanceof ZipEntry));
  52             zf.stream().forEach(e -> assertEquals(e.toString(), "ReadZip.java"));
  53 
  54             Object elements[] = zf.stream().toArray();
  55             assertEquals(1, elements.length);
  56             assertEquals(elements[0].toString(), "ReadZip.java");
  57         }
  58     }
  59 
  60     @Test
  61     public void testStreamJar() throws IOException {
  62         try (JarFile jf = new JarFile(new File(System.getProperty("test.src", "."), "input.jar"))) {
  63             jf.stream().forEach(e -> assertTrue(e instanceof JarEntry));
  64 
  65             Object elements[] = jf.stream().toArray();
  66             assertEquals(3, elements.length);
  67             assertEquals(elements[0].toString(), "META-INF/");
  68             assertEquals(elements[1].toString(), "META-INF/MANIFEST.MF");
  69             assertEquals(elements[2].toString(), "ReleaseInflater.java");
  70         }
  71     }
  72 
  73     @Test
  74     public void testClosedZipFile() throws IOException {
  75         ZipFile zf = new ZipFile(new File(System.getProperty("test.src", "."), "input.zip"));
  76         zf.close();
  77         try {
  78             Stream s = zf.stream();
  79             fail("Should have thrown IllegalStateException");
  80         } catch (IllegalStateException e) {
  81             // expected;
  82         }
  83     }
  84 
  85     @Test
  86     public void testClosedJarFile() throws IOException {
  87         JarFile jf = new JarFile(new File(System.getProperty("test.src", "."), "input.jar"));
  88         jf.close();
  89         try {
  90             Stream s = jf.stream();
  91             fail("Should have thrown IllegalStateException");
  92         } catch (IllegalStateException e) {
  93             // expected;
  94         }
  95     }
  96 }