jdk/test/java/io/Serializable/subclassGC/SubclassGC.java
author bchristi
Thu, 29 Jan 2015 14:49:15 -0800
changeset 28682 6c7c7cad5e64
parent 5506 202f599c92aa
permissions -rw-r--r--
8068578: test/java/io/Serializable/subclassGC/SubclassGC.java assumes app class loader is a URLClassLoader Reviewed-by: alanb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 2005, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/* @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @bug 6232010
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @summary this test checks that replacing SoftCache class with ConcurrentMap
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 *          in ObjectInputStream/ObjectOutputStream gives an opportunity to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 *          classes which are inherited from OIS and OOS and loaded through
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 *          separete ClassLoaders be available for garbage collection
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * @author Andrey Ozerov
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * @run main/othervm/policy=security.policy SubclassGC
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.lang.ref.Reference;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.lang.ref.ReferenceQueue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.lang.ref.WeakReference;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.lang.reflect.Constructor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.net.URL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.net.URLClassLoader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
public class SubclassGC {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
        private static final long TIMEOUT = 1000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
        public static final void main(String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
                System.err.println("\n Regression test for bug 6232010\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
                if (System.getSecurityManager() == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
                        System.setSecurityManager(new SecurityManager());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
                ClassLoader systemLoader = ClassLoader.getSystemClassLoader();
28682
6c7c7cad5e64 8068578: test/java/io/Serializable/subclassGC/SubclassGC.java assumes app class loader is a URLClassLoader
bchristi
parents: 5506
diff changeset
    53
                URL testClassesURL = new File(System.getProperty("test.classes")).toURI().toURL();
6c7c7cad5e64 8068578: test/java/io/Serializable/subclassGC/SubclassGC.java assumes app class loader is a URLClassLoader
bchristi
parents: 5506
diff changeset
    54
                ClassLoader loader = new URLClassLoader(new URL[] { testClassesURL } ,
6c7c7cad5e64 8068578: test/java/io/Serializable/subclassGC/SubclassGC.java assumes app class loader is a URLClassLoader
bchristi
parents: 5506
diff changeset
    55
                                                        systemLoader.getParent());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
                Class<? extends ObjectOutputStream> cl =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
                        Class.forName(SubclassOfOOS.class.getName(), false,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
                                                  loader).asSubclass(ObjectOutputStream.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
                Constructor<? extends ObjectOutputStream> cons =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
                        cl.getConstructor(OutputStream.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
                OutputStream os = new ByteArrayOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
                ObjectOutputStream obj = cons.newInstance(os);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
                final ReferenceQueue<Class<?>> queue = new ReferenceQueue<Class<?>>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
                WeakReference<Class<?>> ref = new WeakReference<Class<?>>(cl, queue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
                cl = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
                obj = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
                loader = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
                cons = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
                systemLoader = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
                System.err.println("\nStart Garbage Collection right now");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
                System.gc();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
                Reference<? extends Class<?>> dequeued = queue.remove(TIMEOUT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
                if (dequeued == ref) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
                        System.err.println("\nTEST PASSED");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                        throw new Error();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
}