jdk/test/java/util/zip/ZipFile/FinalizeZipFile.java
changeset 7815 12cc7f77e459
child 9035 1255eb81cc2f
equal deleted inserted replaced
7814:dff0b40f9ac8 7815:12cc7f77e459
       
     1 /*
       
     2  * Copyright (c) 2010, 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.nio.*;
       
    31 import java.util.Random;
       
    32 import java.util.zip.*;
       
    33 import java.util.concurrent.CountDownLatch;
       
    34 import java.util.concurrent.TimeUnit;
       
    35 
       
    36 public class FinalizeZipFile {
       
    37 
       
    38     private final static CountDownLatch finalizersDone = new CountDownLatch(3);
       
    39 
       
    40     private static class InstrumentedZipFile extends ZipFile {
       
    41 
       
    42         public InstrumentedZipFile(File f) throws Exception {
       
    43             super(f);
       
    44             System.out.printf("Using %s%n", f.getPath());
       
    45         }
       
    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         final String javaHome = System.getProperty("java.home");
       
    56         // Create some ZipFiles.
       
    57         // Find some .jar files in JDK's lib directory.
       
    58         final File lib = new File(javaHome, "lib");
       
    59         check(lib.isDirectory());
       
    60         final File[] jars = lib.listFiles(
       
    61             new FilenameFilter() {
       
    62                 public boolean accept(File dir, String name) {
       
    63                     return name.endsWith(".jar");}});
       
    64         check(jars.length > 1);
       
    65 
       
    66         new InstrumentedZipFile(jars[rnd.nextInt(jars.length)]).close();
       
    67         new InstrumentedZipFile(jars[rnd.nextInt(jars.length)]).close();
       
    68 
       
    69         // Create a ZipFile and get an input stream from it
       
    70         ZipFile zf = new InstrumentedZipFile(jars[rnd.nextInt(jars.length)]);
       
    71         ZipEntry ze = zf.getEntry("META-INF/MANIFEST.MF");
       
    72         InputStream is = zf.getInputStream(ze);
       
    73     }
       
    74 
       
    75     public static void realMain(String[] args) throws Throwable {
       
    76         makeGarbage();
       
    77 
       
    78         System.gc();
       
    79         finalizersDone.await(5, TimeUnit.SECONDS);
       
    80 
       
    81         // Not all ZipFiles were collected?
       
    82         equal(finalizersDone.getCount(), 0L);
       
    83     }
       
    84 
       
    85     //--------------------- Infrastructure ---------------------------
       
    86     static volatile int passed = 0, failed = 0;
       
    87     static void pass() {passed++;}
       
    88     static void fail() {failed++; Thread.dumpStack();}
       
    89     static void fail(String msg) {System.out.println(msg); fail();}
       
    90     static void unexpected(Throwable t) {failed++; t.printStackTrace();}
       
    91     static void check(boolean cond) {if (cond) pass(); else fail();}
       
    92     static void equal(Object x, Object y) {
       
    93         if (x == null ? y == null : x.equals(y)) pass();
       
    94         else fail(x + " not equal to " + y);}
       
    95     public static void main(String[] args) throws Throwable {
       
    96         try {realMain(args);} catch (Throwable t) {unexpected(t);}
       
    97         System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
       
    98         if (failed > 0) throw new AssertionError("Some tests failed");}
       
    99 }
       
   100