0N/A/*
2362N/A * Copyright (c) 2003, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/*
0N/A * @test
0N/A * @bug 4909536
0N/A * @summary Ensure that the Introspector does not retain refs to classes
0N/A * @author Eamonn McManus
0N/A * @run clean ClassLeakTest
0N/A * @run build ClassLeakTest
0N/A * @run main ClassLeakTest
0N/A */
0N/A
0N/Aimport java.lang.ref.WeakReference;
0N/Aimport java.net.*;
0N/Aimport java.util.*;
0N/A
0N/Aimport javax.management.*;
0N/Aimport javax.management.loading.*;
0N/A
0N/Apublic class ClassLeakTest {
0N/A public static void main(String[] args) throws Exception {
0N/A System.out.println("Testing that registering and unregistering a " +
0N/A "Standard MBean does not retain a reference to " +
0N/A "the MBean's class");
0N/A
0N/A ClassLoader myClassLoader = ClassLeakTest.class.getClassLoader();
0N/A if (!(myClassLoader instanceof URLClassLoader)) {
0N/A System.out.println("TEST INVALID: test's class loader is not " +
0N/A "a URLClassLoader");
0N/A System.exit(1);
0N/A }
0N/A
0N/A URLClassLoader myURLClassLoader = (URLClassLoader) myClassLoader;
0N/A URL[] urls = myURLClassLoader.getURLs();
0N/A PrivateMLet mlet = new PrivateMLet(urls, null, false);
0N/A Class shadowClass = mlet.loadClass(TestMBean.class.getName());
0N/A if (shadowClass == TestMBean.class) {
0N/A System.out.println("TEST INVALID: MLet got original " +
0N/A "TestMBean not shadow");
0N/A System.exit(1);
0N/A }
0N/A shadowClass = null;
0N/A
0N/A MBeanServer mbs = MBeanServerFactory.createMBeanServer();
0N/A ObjectName mletName = new ObjectName("x:type=mlet");
0N/A mbs.registerMBean(mlet, mletName);
0N/A
0N/A ObjectName testName = new ObjectName("x:type=test");
0N/A mbs.createMBean(Test.class.getName(), testName, mletName);
0N/A
0N/A ClassLoader testLoader = mbs.getClassLoaderFor(testName);
0N/A if (testLoader != mlet) {
0N/A System.out.println("TEST INVALID: MBean's class loader is not " +
0N/A "MLet: " + testLoader);
0N/A System.exit(1);
0N/A }
0N/A testLoader = null;
0N/A
0N/A MBeanInfo info = mbs.getMBeanInfo(testName);
0N/A MBeanAttributeInfo[] attrs = info.getAttributes();
0N/A if (attrs.length != 1 || !attrs[0].getName().equals("A")
0N/A || !attrs[0].isReadable() || !attrs[0].isWritable()
0N/A || attrs[0].isIs() || !attrs[0].getType().equals("int")) {
0N/A System.out.println("TEST FAILED: unexpected MBeanInfo attrs");
0N/A System.exit(1);
0N/A }
0N/A MBeanOperationInfo[] ops = info.getOperations();
0N/A if (ops.length != 1 || !ops[0].getName().equals("bogus")
0N/A || ops[0].getSignature().length > 0
0N/A || ops[0].getImpact() != MBeanOperationInfo.UNKNOWN
0N/A || !ops[0].getReturnType().equals("void")) {
0N/A System.out.println("TEST FAILED: unexpected MBeanInfo ops");
0N/A System.exit(1);
0N/A }
0N/A if (info.getConstructors().length != 2) {
0N/A System.out.println("TEST FAILED: wrong number of constructors " +
0N/A "in introspected bean: " +
0N/A Arrays.asList(info.getConstructors()));
0N/A System.exit(1);
0N/A }
0N/A if (!info.getClassName().endsWith("Test")) {
0N/A System.out.println("TEST FAILED: wrong info class name: " +
0N/A info.getClassName());
0N/A System.exit(1);
0N/A }
0N/A
0N/A mbs.unregisterMBean(testName);
0N/A mbs.unregisterMBean(mletName);
0N/A
0N/A WeakReference mletRef = new WeakReference(mlet);
0N/A mlet = null;
0N/A
0N/A System.out.println("MBean registered and unregistered, waiting for " +
0N/A "garbage collector to collect class loader");
0N/A for (int i = 0; i < 10000 && mletRef.get() != null; i++) {
0N/A System.gc();
0N/A Thread.sleep(1);
0N/A }
0N/A
0N/A if (mletRef.get() == null)
0N/A System.out.println("Test passed: class loader was GC'd");
0N/A else {
0N/A System.out.println("TEST FAILED: class loader was not GC'd");
0N/A System.exit(1);
0N/A }
0N/A }
0N/A
0N/A public static interface TestMBean {
0N/A public void bogus();
0N/A public int getA();
0N/A public void setA(int a);
0N/A }
0N/A
0N/A public static class Test implements TestMBean {
0N/A public Test() {}
0N/A public Test(int x) {}
0N/A
0N/A public void bogus() {}
0N/A public int getA() {return 0;}
0N/A public void setA(int a) {}
0N/A }
0N/A}