1 /*
   2  * Copyright (c) 2010, 2014, 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 /* @test
  25  * @bug 7007609 7009618
  26  * @summary Check that ZipFile objects are always collected
  27  * @key randomness
  28  */
  29 
  30 import java.io.*;
  31 import java.util.Random;
  32 import java.util.zip.*;
  33 import java.util.concurrent.CountDownLatch;
  34 
  35 public class FinalizeZipFile {
  36 
  37     private final static CountDownLatch finalizersDone = new CountDownLatch(3);
  38 
  39     private static class InstrumentedZipFile extends ZipFile {
  40 
  41         public InstrumentedZipFile(File f) throws Exception {
  42             super(f);
  43             System.out.printf("Using %s%n", f.getPath());
  44         }
  45         @Override
  46         protected void finalize() throws IOException {
  47             System.out.printf("Killing %s%n", getName());
  48             super.finalize();
  49             finalizersDone.countDown();
  50         }
  51     }
  52 
  53     private static void makeGarbage() throws Throwable {
  54         final Random rnd = new Random();
  55         // Create some ZipFiles.
  56         // Find some .jar files in test directory.
  57         final File testdir = new File(System.getProperty("test.src", "."));
  58         check(testdir.isDirectory());
  59         final File[] jars = testdir.listFiles(
  60             new FilenameFilter() {
  61                 public boolean accept(File dir, String name) {
  62                     return name.endsWith(".jar");}});
  63         check(jars.length > 1);
  64 
  65         new InstrumentedZipFile(jars[rnd.nextInt(jars.length)]).close();
  66         new InstrumentedZipFile(jars[rnd.nextInt(jars.length)]).close();
  67 
  68         // Create a ZipFile and get an input stream from it
  69         for (int i = 0; i < jars.length + 10; i++) {
  70             ZipFile zf = new InstrumentedZipFile(jars[rnd.nextInt(jars.length)]);
  71             ZipEntry ze = zf.getEntry("META-INF/MANIFEST.MF");
  72             if (ze != null) {
  73                 InputStream is = zf.getInputStream(ze);
  74                 break;
  75             }
  76         }
  77     }
  78 
  79     public static void realMain(String[] args) throws Throwable {
  80         makeGarbage();
  81 
  82         System.gc();
  83         finalizersDone.await();
  84 
  85         // Not all ZipFiles were collected?
  86         equal(finalizersDone.getCount(), 0L);
  87     }
  88 
  89     //--------------------- Infrastructure ---------------------------
  90     static volatile int passed = 0, failed = 0;
  91     static void pass() {passed++;}
  92     static void fail() {failed++; Thread.dumpStack();}
  93     static void fail(String msg) {System.out.println(msg); fail();}
  94     static void unexpected(Throwable t) {failed++; t.printStackTrace();}
  95     static void check(boolean cond) {if (cond) pass(); else fail();}
  96     static void equal(Object x, Object y) {
  97         if (x == null ? y == null : x.equals(y)) pass();
  98         else fail(x + " not equal to " + y);}
  99     public static void main(String[] args) throws Throwable {
 100         try {realMain(args);} catch (Throwable t) {unexpected(t);}
 101         System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
 102         if (failed > 0) throw new AssertionError("Some tests failed");}
 103 }