3335N/A/*
3909N/A * Copyright (c) 2010, 2011, Oracle and/or its affiliates. All rights reserved.
3335N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3335N/A *
3335N/A * This code is free software; you can redistribute it and/or modify it
3335N/A * under the terms of the GNU General Public License version 2 only, as
3335N/A * published by the Free Software Foundation.
3335N/A *
3335N/A * This code is distributed in the hope that it will be useful, but WITHOUT
3335N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
3335N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
3335N/A * version 2 for more details (a copy is included in the LICENSE file that
3335N/A * accompanied this code).
3335N/A *
3335N/A * You should have received a copy of the GNU General Public License version
3335N/A * 2 along with this work; if not, write to the Free Software Foundation,
3335N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
3335N/A *
3335N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
3335N/A * or visit www.oracle.com if you need additional information or have any
3335N/A * questions.
3335N/A */
3335N/A
3335N/A/* @test
3335N/A * @bug 7007609 7009618
3335N/A * @summary Check that ZipFile objects are always collected
3335N/A */
3335N/A
3335N/Aimport java.io.*;
3335N/Aimport java.nio.*;
3335N/Aimport java.util.Random;
3335N/Aimport java.util.zip.*;
3335N/Aimport java.util.concurrent.CountDownLatch;
3335N/Aimport java.util.concurrent.TimeUnit;
3335N/A
3335N/Apublic class FinalizeZipFile {
3335N/A
3335N/A private final static CountDownLatch finalizersDone = new CountDownLatch(3);
3335N/A
3335N/A private static class InstrumentedZipFile extends ZipFile {
3335N/A
3335N/A public InstrumentedZipFile(File f) throws Exception {
3335N/A super(f);
3335N/A System.out.printf("Using %s%n", f.getPath());
3335N/A }
3335N/A protected void finalize() throws IOException {
3335N/A System.out.printf("Killing %s%n", getName());
3335N/A super.finalize();
3335N/A finalizersDone.countDown();
3335N/A }
3335N/A }
3335N/A
3335N/A private static void makeGarbage() throws Throwable {
3335N/A final Random rnd = new Random();
3335N/A final String javaHome = System.getProperty("java.home");
3335N/A // Create some ZipFiles.
3335N/A // Find some .jar files in JDK's lib directory.
3335N/A final File lib = new File(javaHome, "lib");
3335N/A check(lib.isDirectory());
3335N/A final File[] jars = lib.listFiles(
3335N/A new FilenameFilter() {
3335N/A public boolean accept(File dir, String name) {
3335N/A return name.endsWith(".jar");}});
3335N/A check(jars.length > 1);
3335N/A
3335N/A new InstrumentedZipFile(jars[rnd.nextInt(jars.length)]).close();
3335N/A new InstrumentedZipFile(jars[rnd.nextInt(jars.length)]).close();
3335N/A
3335N/A // Create a ZipFile and get an input stream from it
3335N/A ZipFile zf = new InstrumentedZipFile(jars[rnd.nextInt(jars.length)]);
3335N/A ZipEntry ze = zf.getEntry("META-INF/MANIFEST.MF");
3335N/A InputStream is = zf.getInputStream(ze);
3335N/A }
3335N/A
3335N/A public static void realMain(String[] args) throws Throwable {
3335N/A makeGarbage();
3335N/A
3335N/A System.gc();
3335N/A finalizersDone.await(5, TimeUnit.SECONDS);
3335N/A
3335N/A // Not all ZipFiles were collected?
3335N/A equal(finalizersDone.getCount(), 0L);
3335N/A }
3335N/A
3335N/A //--------------------- Infrastructure ---------------------------
3335N/A static volatile int passed = 0, failed = 0;
3335N/A static void pass() {passed++;}
3335N/A static void fail() {failed++; Thread.dumpStack();}
3335N/A static void fail(String msg) {System.out.println(msg); fail();}
3335N/A static void unexpected(Throwable t) {failed++; t.printStackTrace();}
3335N/A static void check(boolean cond) {if (cond) pass(); else fail();}
3335N/A static void equal(Object x, Object y) {
3335N/A if (x == null ? y == null : x.equals(y)) pass();
3335N/A else fail(x + " not equal to " + y);}
3335N/A public static void main(String[] args) throws Throwable {
3335N/A try {realMain(args);} catch (Throwable t) {unexpected(t);}
3335N/A System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
3335N/A if (failed > 0) throw new AssertionError("Some tests failed");}
3335N/A}
3335N/A