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