2
|
1 |
/*
|
5506
|
2 |
* Copyright (c) 2003, Oracle and/or its affiliates. All rights reserved.
|
2
|
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 |
*
|
5506
|
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.
|
2
|
22 |
*/
|
|
23 |
|
|
24 |
/*
|
|
25 |
* @test
|
|
26 |
* @bug 4909536
|
|
27 |
* @summary Ensure that the Introspector does not retain refs to classes
|
|
28 |
* @author Eamonn McManus
|
|
29 |
* @run clean ClassLeakTest
|
|
30 |
* @run build ClassLeakTest
|
|
31 |
* @run main ClassLeakTest
|
|
32 |
*/
|
|
33 |
|
|
34 |
import java.lang.ref.WeakReference;
|
|
35 |
import java.net.*;
|
|
36 |
import java.util.*;
|
|
37 |
|
|
38 |
import javax.management.*;
|
|
39 |
import javax.management.loading.*;
|
|
40 |
|
|
41 |
public class ClassLeakTest {
|
|
42 |
public static void main(String[] args) throws Exception {
|
|
43 |
System.out.println("Testing that registering and unregistering a " +
|
|
44 |
"Standard MBean does not retain a reference to " +
|
|
45 |
"the MBean's class");
|
|
46 |
|
|
47 |
ClassLoader myClassLoader = ClassLeakTest.class.getClassLoader();
|
|
48 |
if (!(myClassLoader instanceof URLClassLoader)) {
|
|
49 |
System.out.println("TEST INVALID: test's class loader is not " +
|
|
50 |
"a URLClassLoader");
|
|
51 |
System.exit(1);
|
|
52 |
}
|
|
53 |
|
|
54 |
URLClassLoader myURLClassLoader = (URLClassLoader) myClassLoader;
|
|
55 |
URL[] urls = myURLClassLoader.getURLs();
|
|
56 |
PrivateMLet mlet = new PrivateMLet(urls, null, false);
|
|
57 |
Class shadowClass = mlet.loadClass(TestMBean.class.getName());
|
|
58 |
if (shadowClass == TestMBean.class) {
|
|
59 |
System.out.println("TEST INVALID: MLet got original " +
|
|
60 |
"TestMBean not shadow");
|
|
61 |
System.exit(1);
|
|
62 |
}
|
|
63 |
shadowClass = null;
|
|
64 |
|
|
65 |
MBeanServer mbs = MBeanServerFactory.createMBeanServer();
|
|
66 |
ObjectName mletName = new ObjectName("x:type=mlet");
|
|
67 |
mbs.registerMBean(mlet, mletName);
|
|
68 |
|
|
69 |
ObjectName testName = new ObjectName("x:type=test");
|
|
70 |
mbs.createMBean(Test.class.getName(), testName, mletName);
|
|
71 |
|
|
72 |
ClassLoader testLoader = mbs.getClassLoaderFor(testName);
|
|
73 |
if (testLoader != mlet) {
|
|
74 |
System.out.println("TEST INVALID: MBean's class loader is not " +
|
|
75 |
"MLet: " + testLoader);
|
|
76 |
System.exit(1);
|
|
77 |
}
|
|
78 |
testLoader = null;
|
|
79 |
|
|
80 |
MBeanInfo info = mbs.getMBeanInfo(testName);
|
|
81 |
MBeanAttributeInfo[] attrs = info.getAttributes();
|
|
82 |
if (attrs.length != 1 || !attrs[0].getName().equals("A")
|
|
83 |
|| !attrs[0].isReadable() || !attrs[0].isWritable()
|
|
84 |
|| attrs[0].isIs() || !attrs[0].getType().equals("int")) {
|
|
85 |
System.out.println("TEST FAILED: unexpected MBeanInfo attrs");
|
|
86 |
System.exit(1);
|
|
87 |
}
|
|
88 |
MBeanOperationInfo[] ops = info.getOperations();
|
|
89 |
if (ops.length != 1 || !ops[0].getName().equals("bogus")
|
|
90 |
|| ops[0].getSignature().length > 0
|
|
91 |
|| ops[0].getImpact() != MBeanOperationInfo.UNKNOWN
|
|
92 |
|| !ops[0].getReturnType().equals("void")) {
|
|
93 |
System.out.println("TEST FAILED: unexpected MBeanInfo ops");
|
|
94 |
System.exit(1);
|
|
95 |
}
|
|
96 |
if (info.getConstructors().length != 2) {
|
|
97 |
System.out.println("TEST FAILED: wrong number of constructors " +
|
|
98 |
"in introspected bean: " +
|
|
99 |
Arrays.asList(info.getConstructors()));
|
|
100 |
System.exit(1);
|
|
101 |
}
|
|
102 |
if (!info.getClassName().endsWith("Test")) {
|
|
103 |
System.out.println("TEST FAILED: wrong info class name: " +
|
|
104 |
info.getClassName());
|
|
105 |
System.exit(1);
|
|
106 |
}
|
|
107 |
|
|
108 |
mbs.unregisterMBean(testName);
|
|
109 |
mbs.unregisterMBean(mletName);
|
|
110 |
|
|
111 |
WeakReference mletRef = new WeakReference(mlet);
|
|
112 |
mlet = null;
|
|
113 |
|
|
114 |
System.out.println("MBean registered and unregistered, waiting for " +
|
|
115 |
"garbage collector to collect class loader");
|
|
116 |
for (int i = 0; i < 10000 && mletRef.get() != null; i++) {
|
|
117 |
System.gc();
|
|
118 |
Thread.sleep(1);
|
|
119 |
}
|
|
120 |
|
|
121 |
if (mletRef.get() == null)
|
|
122 |
System.out.println("Test passed: class loader was GC'd");
|
|
123 |
else {
|
|
124 |
System.out.println("TEST FAILED: class loader was not GC'd");
|
|
125 |
System.exit(1);
|
|
126 |
}
|
|
127 |
}
|
|
128 |
|
|
129 |
public static interface TestMBean {
|
|
130 |
public void bogus();
|
|
131 |
public int getA();
|
|
132 |
public void setA(int a);
|
|
133 |
}
|
|
134 |
|
|
135 |
public static class Test implements TestMBean {
|
|
136 |
public Test() {}
|
|
137 |
public Test(int x) {}
|
|
138 |
|
|
139 |
public void bogus() {}
|
|
140 |
public int getA() {return 0;}
|
|
141 |
public void setA(int a) {}
|
|
142 |
}
|
|
143 |
}
|