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 import java.util.concurrent.TimeUnit;
35 
36 public class FinalizeZipFile {
37 
38     private static final CountDownLatch finalizersDone = new CountDownLatch(3);
39 
40     private static class InstrumentedZipFile extends ZipFile {
41 
InstrumentedZipFile(File f)42         public InstrumentedZipFile(File f) throws Exception {
43             super(f);
44             System.out.printf("Using %s%n", f.getPath());
45         }
46         @Override
finalize()47         protected void finalize() throws IOException {
48             System.out.printf("Killing %s%n", getName());
49             super.finalize();
50             finalizersDone.countDown();
51         }
52     }
53 
makeGarbage()54     private static void makeGarbage() throws Throwable {
55         final Random rnd = new Random();
56         // Create some ZipFiles.
57         // Find some .jar files in test directory.
58         final File testdir = new File(System.getProperty("test.src", "."));
59         check(testdir.isDirectory());
60         final File[] jars = testdir.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         for (int i = 0; i < jars.length + 10; i++) {
71             ZipFile zf = new InstrumentedZipFile(jars[rnd.nextInt(jars.length)]);
72             ZipEntry ze = zf.getEntry("META-INF/MANIFEST.MF");
73             if (ze != null) {
74                 InputStream is = zf.getInputStream(ze);
75                 break;
76             }
77         }
78     }
79 
realMain(String[] args)80     public static void realMain(String[] args) throws Throwable {
81         makeGarbage();
82         while (!finalizersDone.await(10, TimeUnit.MILLISECONDS)) {
83             System.gc();
84         }
85         // Not all ZipFiles were collected?
86         equal(finalizersDone.getCount(), 0L);
87     }
88 
89     //--------------------- Infrastructure ---------------------------
90     static volatile int passed = 0, failed = 0;
pass()91     static void pass() {passed++;}
fail()92     static void fail() {failed++; Thread.dumpStack();}
fail(String msg)93     static void fail(String msg) {System.out.println(msg); fail();}
unexpected(Throwable t)94     static void unexpected(Throwable t) {failed++; t.printStackTrace();}
check(boolean cond)95     static void check(boolean cond) {if (cond) pass(); else fail();}
equal(Object x, Object y)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);}
main(String[] args)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 }
104