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
35 public class FinalizeZipFile {
36
37 private static final CountDownLatch finalizersDone = new CountDownLatch(3);
38
39 private static class InstrumentedZipFile extends ZipFile {
40
41 public InstrumentedZipFile(File f) throws Exception {
42 super(f);
43 System.out.printf("Using %s%n", f.getPath());
44 }
45 @Override
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 {
61 public boolean accept(File dir, String name) {
62 return name.endsWith(".jar");}});
63 check(jars.length > 1);
64
65 new InstrumentedZipFile(jars[rnd.nextInt(jars.length)]).close();
66 new InstrumentedZipFile(jars[rnd.nextInt(jars.length)]).close();
67
68 // Create a ZipFile and get an input stream from it
69 for (int i = 0; i < jars.length + 10; i++) {
70 ZipFile zf = new InstrumentedZipFile(jars[rnd.nextInt(jars.length)]);
71 ZipEntry ze = zf.getEntry("META-INF/MANIFEST.MF");
72 if (ze != null) {
73 InputStream is = zf.getInputStream(ze);
74 break;
75 }
76 }
77 }
78
79 public static void realMain(String[] args) throws Throwable {
80 makeGarbage();
81
82 System.gc();
83 finalizersDone.await();
84
85 // Not all ZipFiles were collected?
86 equal(finalizersDone.getCount(), 0L);
87 }
88
89 //--------------------- Infrastructure ---------------------------
90 static volatile int passed = 0, failed = 0;
91 static void pass() {passed++;}
92 static void fail() {failed++; Thread.dumpStack();}
93 static void fail(String msg) {System.out.println(msg); fail();}
94 static void unexpected(Throwable t) {failed++; t.printStackTrace();}
95 static void check(boolean cond) {if (cond) pass(); else fail();}
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);}
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 }
|
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
42 public InstrumentedZipFile(File f) throws Exception {
43 super(f);
44 System.out.printf("Using %s%n", f.getPath());
45 }
46 @Override
47 protected void finalize() throws IOException {
48 System.out.printf("Killing %s%n", getName());
49 super.finalize();
50 finalizersDone.countDown();
51 }
52 }
53
54 private static void makeGarbage() throws Throwable {
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
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;
91 static void pass() {passed++;}
92 static void fail() {failed++; Thread.dumpStack();}
93 static void fail(String msg) {System.out.println(msg); fail();}
94 static void unexpected(Throwable t) {failed++; t.printStackTrace();}
95 static void check(boolean cond) {if (cond) pass(); else fail();}
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);}
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 }
|