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 
  35 import java.io.*;
  36 import java.lang.ref.Reference;
  37 import java.lang.ref.ReferenceQueue;
  38 import java.lang.ref.WeakReference;
  39 import java.lang.reflect.Constructor;
  40 import java.net.URL;
  41 import java.net.URLClassLoader;
  42 
  43 public 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                 ClassLoader loader = new URLClassLoader(((URLClassLoader) systemLoader).getURLs(),

  54                                                                                         systemLoader.getParent());
  55                 Class<? extends ObjectOutputStream> cl =
  56                         Class.forName(SubclassOfOOS.class.getName(), false,
  57                                                   loader).asSubclass(ObjectOutputStream.class);
  58 
  59                 Constructor<? extends ObjectOutputStream> cons =
  60                         cl.getConstructor(OutputStream.class);
  61 
  62                 OutputStream os = new ByteArrayOutputStream();
  63                 ObjectOutputStream obj = cons.newInstance(os);
  64 
  65                 final ReferenceQueue<Class<?>> queue = new ReferenceQueue<Class<?>>();
  66                 WeakReference<Class<?>> ref = new WeakReference<Class<?>>(cl, queue);
  67 
  68                 cl = null;
  69                 obj = null;
  70                 loader = null;
  71                 cons = null;
  72                 systemLoader = null;
  73 
  74                 System.err.println("\nStart Garbage Collection right now");
  75                 System.gc();
  76 
  77                 Reference<? extends Class<?>> dequeued = queue.remove(TIMEOUT);
  78                 if (dequeued == ref) {
  79                         System.err.println("\nTEST PASSED");
  80                 } else {
  81                         throw new Error();
  82                 }
  83         }
  84 }
--- EOF ---