jdk/test/java/util/zip/ZipFile/FinalizeZipFile.java
author ohair
Wed, 06 Apr 2011 22:06:11 -0700
changeset 9035 1255eb81cc2f
parent 7815 12cc7f77e459
child 15295 1d90347088c3
permissions -rw-r--r--
7033660: Update copyright year to 2011 on any files changed in 2011 Reviewed-by: dholmes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
7815
12cc7f77e459 7009618: regression test failed caused by the fix for 7003462
sherman
parents:
diff changeset
     1
/*
9035
1255eb81cc2f 7033660: Update copyright year to 2011 on any files changed in 2011
ohair
parents: 7815
diff changeset
     2
 * Copyright (c) 2010, 2011, Oracle and/or its affiliates. All rights reserved.
7815
12cc7f77e459 7009618: regression test failed caused by the fix for 7003462
sherman
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
12cc7f77e459 7009618: regression test failed caused by the fix for 7003462
sherman
parents:
diff changeset
     4
 *
12cc7f77e459 7009618: regression test failed caused by the fix for 7003462
sherman
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
12cc7f77e459 7009618: regression test failed caused by the fix for 7003462
sherman
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
12cc7f77e459 7009618: regression test failed caused by the fix for 7003462
sherman
parents:
diff changeset
     7
 * published by the Free Software Foundation.
12cc7f77e459 7009618: regression test failed caused by the fix for 7003462
sherman
parents:
diff changeset
     8
 *
12cc7f77e459 7009618: regression test failed caused by the fix for 7003462
sherman
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
12cc7f77e459 7009618: regression test failed caused by the fix for 7003462
sherman
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12cc7f77e459 7009618: regression test failed caused by the fix for 7003462
sherman
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12cc7f77e459 7009618: regression test failed caused by the fix for 7003462
sherman
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
12cc7f77e459 7009618: regression test failed caused by the fix for 7003462
sherman
parents:
diff changeset
    13
 * accompanied this code).
12cc7f77e459 7009618: regression test failed caused by the fix for 7003462
sherman
parents:
diff changeset
    14
 *
12cc7f77e459 7009618: regression test failed caused by the fix for 7003462
sherman
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
12cc7f77e459 7009618: regression test failed caused by the fix for 7003462
sherman
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
12cc7f77e459 7009618: regression test failed caused by the fix for 7003462
sherman
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
12cc7f77e459 7009618: regression test failed caused by the fix for 7003462
sherman
parents:
diff changeset
    18
 *
12cc7f77e459 7009618: regression test failed caused by the fix for 7003462
sherman
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
12cc7f77e459 7009618: regression test failed caused by the fix for 7003462
sherman
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
12cc7f77e459 7009618: regression test failed caused by the fix for 7003462
sherman
parents:
diff changeset
    21
 * questions.
12cc7f77e459 7009618: regression test failed caused by the fix for 7003462
sherman
parents:
diff changeset
    22
 */
12cc7f77e459 7009618: regression test failed caused by the fix for 7003462
sherman
parents:
diff changeset
    23
12cc7f77e459 7009618: regression test failed caused by the fix for 7003462
sherman
parents:
diff changeset
    24
/* @test
12cc7f77e459 7009618: regression test failed caused by the fix for 7003462
sherman
parents:
diff changeset
    25
 * @bug 7007609 7009618
12cc7f77e459 7009618: regression test failed caused by the fix for 7003462
sherman
parents:
diff changeset
    26
 * @summary Check that ZipFile objects are always collected
12cc7f77e459 7009618: regression test failed caused by the fix for 7003462
sherman
parents:
diff changeset
    27
 */
12cc7f77e459 7009618: regression test failed caused by the fix for 7003462
sherman
parents:
diff changeset
    28
12cc7f77e459 7009618: regression test failed caused by the fix for 7003462
sherman
parents:
diff changeset
    29
import java.io.*;
12cc7f77e459 7009618: regression test failed caused by the fix for 7003462
sherman
parents:
diff changeset
    30
import java.nio.*;
12cc7f77e459 7009618: regression test failed caused by the fix for 7003462
sherman
parents:
diff changeset
    31
import java.util.Random;
12cc7f77e459 7009618: regression test failed caused by the fix for 7003462
sherman
parents:
diff changeset
    32
import java.util.zip.*;
12cc7f77e459 7009618: regression test failed caused by the fix for 7003462
sherman
parents:
diff changeset
    33
import java.util.concurrent.CountDownLatch;
12cc7f77e459 7009618: regression test failed caused by the fix for 7003462
sherman
parents:
diff changeset
    34
import java.util.concurrent.TimeUnit;
12cc7f77e459 7009618: regression test failed caused by the fix for 7003462
sherman
parents:
diff changeset
    35
12cc7f77e459 7009618: regression test failed caused by the fix for 7003462
sherman
parents:
diff changeset
    36
public class FinalizeZipFile {
12cc7f77e459 7009618: regression test failed caused by the fix for 7003462
sherman
parents:
diff changeset
    37
12cc7f77e459 7009618: regression test failed caused by the fix for 7003462
sherman
parents:
diff changeset
    38
    private final static CountDownLatch finalizersDone = new CountDownLatch(3);
12cc7f77e459 7009618: regression test failed caused by the fix for 7003462
sherman
parents:
diff changeset
    39
12cc7f77e459 7009618: regression test failed caused by the fix for 7003462
sherman
parents:
diff changeset
    40
    private static class InstrumentedZipFile extends ZipFile {
12cc7f77e459 7009618: regression test failed caused by the fix for 7003462
sherman
parents:
diff changeset
    41
12cc7f77e459 7009618: regression test failed caused by the fix for 7003462
sherman
parents:
diff changeset
    42
        public InstrumentedZipFile(File f) throws Exception {
12cc7f77e459 7009618: regression test failed caused by the fix for 7003462
sherman
parents:
diff changeset
    43
            super(f);
12cc7f77e459 7009618: regression test failed caused by the fix for 7003462
sherman
parents:
diff changeset
    44
            System.out.printf("Using %s%n", f.getPath());
12cc7f77e459 7009618: regression test failed caused by the fix for 7003462
sherman
parents:
diff changeset
    45
        }
12cc7f77e459 7009618: regression test failed caused by the fix for 7003462
sherman
parents:
diff changeset
    46
        protected void finalize() throws IOException {
12cc7f77e459 7009618: regression test failed caused by the fix for 7003462
sherman
parents:
diff changeset
    47
            System.out.printf("Killing %s%n", getName());
12cc7f77e459 7009618: regression test failed caused by the fix for 7003462
sherman
parents:
diff changeset
    48
            super.finalize();
12cc7f77e459 7009618: regression test failed caused by the fix for 7003462
sherman
parents:
diff changeset
    49
            finalizersDone.countDown();
12cc7f77e459 7009618: regression test failed caused by the fix for 7003462
sherman
parents:
diff changeset
    50
        }
12cc7f77e459 7009618: regression test failed caused by the fix for 7003462
sherman
parents:
diff changeset
    51
    }
12cc7f77e459 7009618: regression test failed caused by the fix for 7003462
sherman
parents:
diff changeset
    52
12cc7f77e459 7009618: regression test failed caused by the fix for 7003462
sherman
parents:
diff changeset
    53
    private static void makeGarbage() throws Throwable {
12cc7f77e459 7009618: regression test failed caused by the fix for 7003462
sherman
parents:
diff changeset
    54
        final Random rnd = new Random();
12cc7f77e459 7009618: regression test failed caused by the fix for 7003462
sherman
parents:
diff changeset
    55
        final String javaHome = System.getProperty("java.home");
12cc7f77e459 7009618: regression test failed caused by the fix for 7003462
sherman
parents:
diff changeset
    56
        // Create some ZipFiles.
12cc7f77e459 7009618: regression test failed caused by the fix for 7003462
sherman
parents:
diff changeset
    57
        // Find some .jar files in JDK's lib directory.
12cc7f77e459 7009618: regression test failed caused by the fix for 7003462
sherman
parents:
diff changeset
    58
        final File lib = new File(javaHome, "lib");
12cc7f77e459 7009618: regression test failed caused by the fix for 7003462
sherman
parents:
diff changeset
    59
        check(lib.isDirectory());
12cc7f77e459 7009618: regression test failed caused by the fix for 7003462
sherman
parents:
diff changeset
    60
        final File[] jars = lib.listFiles(
12cc7f77e459 7009618: regression test failed caused by the fix for 7003462
sherman
parents:
diff changeset
    61
            new FilenameFilter() {
12cc7f77e459 7009618: regression test failed caused by the fix for 7003462
sherman
parents:
diff changeset
    62
                public boolean accept(File dir, String name) {
12cc7f77e459 7009618: regression test failed caused by the fix for 7003462
sherman
parents:
diff changeset
    63
                    return name.endsWith(".jar");}});
12cc7f77e459 7009618: regression test failed caused by the fix for 7003462
sherman
parents:
diff changeset
    64
        check(jars.length > 1);
12cc7f77e459 7009618: regression test failed caused by the fix for 7003462
sherman
parents:
diff changeset
    65
12cc7f77e459 7009618: regression test failed caused by the fix for 7003462
sherman
parents:
diff changeset
    66
        new InstrumentedZipFile(jars[rnd.nextInt(jars.length)]).close();
12cc7f77e459 7009618: regression test failed caused by the fix for 7003462
sherman
parents:
diff changeset
    67
        new InstrumentedZipFile(jars[rnd.nextInt(jars.length)]).close();
12cc7f77e459 7009618: regression test failed caused by the fix for 7003462
sherman
parents:
diff changeset
    68
12cc7f77e459 7009618: regression test failed caused by the fix for 7003462
sherman
parents:
diff changeset
    69
        // Create a ZipFile and get an input stream from it
12cc7f77e459 7009618: regression test failed caused by the fix for 7003462
sherman
parents:
diff changeset
    70
        ZipFile zf = new InstrumentedZipFile(jars[rnd.nextInt(jars.length)]);
12cc7f77e459 7009618: regression test failed caused by the fix for 7003462
sherman
parents:
diff changeset
    71
        ZipEntry ze = zf.getEntry("META-INF/MANIFEST.MF");
12cc7f77e459 7009618: regression test failed caused by the fix for 7003462
sherman
parents:
diff changeset
    72
        InputStream is = zf.getInputStream(ze);
12cc7f77e459 7009618: regression test failed caused by the fix for 7003462
sherman
parents:
diff changeset
    73
    }
12cc7f77e459 7009618: regression test failed caused by the fix for 7003462
sherman
parents:
diff changeset
    74
12cc7f77e459 7009618: regression test failed caused by the fix for 7003462
sherman
parents:
diff changeset
    75
    public static void realMain(String[] args) throws Throwable {
12cc7f77e459 7009618: regression test failed caused by the fix for 7003462
sherman
parents:
diff changeset
    76
        makeGarbage();
12cc7f77e459 7009618: regression test failed caused by the fix for 7003462
sherman
parents:
diff changeset
    77
12cc7f77e459 7009618: regression test failed caused by the fix for 7003462
sherman
parents:
diff changeset
    78
        System.gc();
12cc7f77e459 7009618: regression test failed caused by the fix for 7003462
sherman
parents:
diff changeset
    79
        finalizersDone.await(5, TimeUnit.SECONDS);
12cc7f77e459 7009618: regression test failed caused by the fix for 7003462
sherman
parents:
diff changeset
    80
12cc7f77e459 7009618: regression test failed caused by the fix for 7003462
sherman
parents:
diff changeset
    81
        // Not all ZipFiles were collected?
12cc7f77e459 7009618: regression test failed caused by the fix for 7003462
sherman
parents:
diff changeset
    82
        equal(finalizersDone.getCount(), 0L);
12cc7f77e459 7009618: regression test failed caused by the fix for 7003462
sherman
parents:
diff changeset
    83
    }
12cc7f77e459 7009618: regression test failed caused by the fix for 7003462
sherman
parents:
diff changeset
    84
12cc7f77e459 7009618: regression test failed caused by the fix for 7003462
sherman
parents:
diff changeset
    85
    //--------------------- Infrastructure ---------------------------
12cc7f77e459 7009618: regression test failed caused by the fix for 7003462
sherman
parents:
diff changeset
    86
    static volatile int passed = 0, failed = 0;
12cc7f77e459 7009618: regression test failed caused by the fix for 7003462
sherman
parents:
diff changeset
    87
    static void pass() {passed++;}
12cc7f77e459 7009618: regression test failed caused by the fix for 7003462
sherman
parents:
diff changeset
    88
    static void fail() {failed++; Thread.dumpStack();}
12cc7f77e459 7009618: regression test failed caused by the fix for 7003462
sherman
parents:
diff changeset
    89
    static void fail(String msg) {System.out.println(msg); fail();}
12cc7f77e459 7009618: regression test failed caused by the fix for 7003462
sherman
parents:
diff changeset
    90
    static void unexpected(Throwable t) {failed++; t.printStackTrace();}
12cc7f77e459 7009618: regression test failed caused by the fix for 7003462
sherman
parents:
diff changeset
    91
    static void check(boolean cond) {if (cond) pass(); else fail();}
12cc7f77e459 7009618: regression test failed caused by the fix for 7003462
sherman
parents:
diff changeset
    92
    static void equal(Object x, Object y) {
12cc7f77e459 7009618: regression test failed caused by the fix for 7003462
sherman
parents:
diff changeset
    93
        if (x == null ? y == null : x.equals(y)) pass();
12cc7f77e459 7009618: regression test failed caused by the fix for 7003462
sherman
parents:
diff changeset
    94
        else fail(x + " not equal to " + y);}
12cc7f77e459 7009618: regression test failed caused by the fix for 7003462
sherman
parents:
diff changeset
    95
    public static void main(String[] args) throws Throwable {
12cc7f77e459 7009618: regression test failed caused by the fix for 7003462
sherman
parents:
diff changeset
    96
        try {realMain(args);} catch (Throwable t) {unexpected(t);}
12cc7f77e459 7009618: regression test failed caused by the fix for 7003462
sherman
parents:
diff changeset
    97
        System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
12cc7f77e459 7009618: regression test failed caused by the fix for 7003462
sherman
parents:
diff changeset
    98
        if (failed > 0) throw new AssertionError("Some tests failed");}
12cc7f77e459 7009618: regression test failed caused by the fix for 7003462
sherman
parents:
diff changeset
    99
}
12cc7f77e459 7009618: regression test failed caused by the fix for 7003462
sherman
parents:
diff changeset
   100