Path: blob/master/test/jdk/javax/management/Introspector/ClassLeakTest.java
41152 views
/*1* Copyright (c) 2003, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 490953626* @summary Ensure that the Introspector does not retain refs to classes27* @requires vm.opt.final.ClassUnloading28* @author Eamonn McManus29*30* @run clean ClassLeakTest31* @run build ClassLeakTest32* @run main ClassLeakTest33*/3435import java.lang.ref.WeakReference;36import java.io.File;37import java.nio.file.Paths;38import java.net.*;39import java.util.*;4041import javax.management.*;42import javax.management.loading.*;4344public class ClassLeakTest {45public static void main(String[] args) throws Exception {46System.out.println("Testing that registering and unregistering a " +47"Standard MBean does not retain a reference to " +48"the MBean's class");495051String[] cpaths = System.getProperty("test.classes", ".")52.split(File.pathSeparator);53URL[] urls = new URL[cpaths.length];54for (int i=0; i < cpaths.length; i++) {55urls[i] = Paths.get(cpaths[i]).toUri().toURL();56}5758PrivateMLet mlet = new PrivateMLet(urls, null, false);59Class<?> shadowClass = mlet.loadClass(TestMBean.class.getName());60if (shadowClass == TestMBean.class) {61System.out.println("TEST INVALID: MLet got original " +62"TestMBean not shadow");63System.exit(1);64}65shadowClass = null;6667MBeanServer mbs = MBeanServerFactory.createMBeanServer();68ObjectName mletName = new ObjectName("x:type=mlet");69mbs.registerMBean(mlet, mletName);7071ObjectName testName = new ObjectName("x:type=test");72mbs.createMBean(Test.class.getName(), testName, mletName);7374ClassLoader testLoader = mbs.getClassLoaderFor(testName);75if (testLoader != mlet) {76System.out.println("TEST INVALID: MBean's class loader is not " +77"MLet: " + testLoader);78System.exit(1);79}80testLoader = null;8182MBeanInfo info = mbs.getMBeanInfo(testName);83MBeanAttributeInfo[] attrs = info.getAttributes();84if (attrs.length != 1 || !attrs[0].getName().equals("A")85|| !attrs[0].isReadable() || !attrs[0].isWritable()86|| attrs[0].isIs() || !attrs[0].getType().equals("int")) {87System.out.println("TEST FAILED: unexpected MBeanInfo attrs");88System.exit(1);89}90MBeanOperationInfo[] ops = info.getOperations();91if (ops.length != 1 || !ops[0].getName().equals("bogus")92|| ops[0].getSignature().length > 093|| ops[0].getImpact() != MBeanOperationInfo.UNKNOWN94|| !ops[0].getReturnType().equals("void")) {95System.out.println("TEST FAILED: unexpected MBeanInfo ops");96System.exit(1);97}98if (info.getConstructors().length != 2) {99System.out.println("TEST FAILED: wrong number of constructors " +100"in introspected bean: " +101Arrays.asList(info.getConstructors()));102System.exit(1);103}104if (!info.getClassName().endsWith("Test")) {105System.out.println("TEST FAILED: wrong info class name: " +106info.getClassName());107System.exit(1);108}109110mbs.unregisterMBean(testName);111mbs.unregisterMBean(mletName);112113WeakReference mletRef = new WeakReference(mlet);114mlet = null;115116System.out.println("MBean registered and unregistered, waiting for " +117"garbage collector to collect class loader");118for (int i = 0; i < 10000 && mletRef.get() != null; i++) {119System.gc();120Thread.sleep(1);121}122123if (mletRef.get() == null)124System.out.println("Test passed: class loader was GC'd");125else {126System.out.println("TEST FAILED: class loader was not GC'd");127System.exit(1);128}129}130131public static interface TestMBean {132public void bogus();133public int getA();134public void setA(int a);135}136137public static class Test implements TestMBean {138public Test() {}139public Test(int x) {}140141public void bogus() {}142public int getA() {return 0;}143public void setA(int a) {}144}145}146147148