1/*
2 * Copyright (c) 2005, 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 6232010
26 * @summary this test checks that replacing SoftCache class with ConcurrentMap
27 *          in ObjectInputStream/ObjectOutputStream gives an opportunity to
28 *          classes which are inherited from OIS and OOS and loaded through
29 *          separete ClassLoaders be available for garbage collection
30 *
31 * @author Andrey Ozerov
32 * @run main/othervm/policy=security.policy SubclassGC
33 */
34
35import java.io.*;
36import java.lang.ref.Reference;
37import java.lang.ref.ReferenceQueue;
38import java.lang.ref.WeakReference;
39import java.lang.reflect.Constructor;
40import java.net.URL;
41import java.net.URLClassLoader;
42
43public class SubclassGC {
44        private static final long TIMEOUT = 1000;
45
46        public static final void main(String[] args) throws Exception {
47                System.err.println("\n Regression test for bug 6232010\n");
48                if (System.getSecurityManager() == null) {
49                        System.setSecurityManager(new SecurityManager());
50                }
51
52                ClassLoader systemLoader = ClassLoader.getSystemClassLoader();
53                URL testClassesURL = new File(System.getProperty("test.classes")).toURI().toURL();
54                ClassLoader loader = new URLClassLoader(new URL[] { testClassesURL } ,
55                                                        systemLoader.getParent());
56                Class<? extends ObjectOutputStream> cl =
57                        Class.forName(SubclassOfOOS.class.getName(), false,
58                                                  loader).asSubclass(ObjectOutputStream.class);
59
60                Constructor<? extends ObjectOutputStream> cons =
61                        cl.getConstructor(OutputStream.class);
62
63                OutputStream os = new ByteArrayOutputStream();
64                ObjectOutputStream obj = cons.newInstance(os);
65
66                final ReferenceQueue<Class<?>> queue = new ReferenceQueue<Class<?>>();
67                WeakReference<Class<?>> ref = new WeakReference<Class<?>>(cl, queue);
68
69                cl = null;
70                obj = null;
71                loader = null;
72                cons = null;
73                systemLoader = null;
74
75                System.err.println("\nStart Garbage Collection right now");
76                System.gc();
77
78                Reference<? extends Class<?>> dequeued = queue.remove(TIMEOUT);
79                if (dequeued == ref) {
80                        System.err.println("\nTEST PASSED");
81                } else {
82                        throw new Error();
83                }
84        }
85}
86