Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/management/mxbean/MXBeanLoadingTest1.java
38841 views
/*1* Copyright (c) 2005, 2015, 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 805886526* @summary Checks correct collection of MXBean's class after unregistration27* @author Olivier Lagneau28* @modules java.management29* @library /lib/testlibrary30* @run main/othervm/timeout=300 MXBeanLoadingTest131*/3233import java.lang.ref.WeakReference;34import java.net.URL;35import java.net.URLClassLoader;36import java.util.Arrays;37import java.util.Map;38import javax.management.Attribute;39import javax.management.JMX;40import javax.management.MBeanAttributeInfo;41import javax.management.MBeanInfo;42import javax.management.MBeanOperationInfo;43import javax.management.MBeanServer;44import javax.management.MBeanServerFactory;45import javax.management.MXBean;46import javax.management.ObjectName;47import javax.management.loading.PrivateMLet;48import javax.management.openmbean.CompositeData;49import javax.management.openmbean.CompositeDataSupport;50import javax.management.openmbean.CompositeType;51import javax.management.openmbean.OpenType;52import javax.management.openmbean.SimpleType;5354public class MXBeanLoadingTest1 {5556public static void main(String[] args) throws Exception {57MXBeanLoadingTest1 test = new MXBeanLoadingTest1();58test.run((Map<String, Object>)null);59}606162public void run(Map<String, Object> args) {6364System.out.println("MXBeanLoadingTest1::run: Start") ;6566try {67System.out.println("We ensure no reference is retained on MXBean class"68+ " after it is unregistered. We take time to perform"69+ " some little extra check of Descriptors, MBean*Info.");7071ClassLoader myClassLoader = MXBeanLoadingTest1.class.getClassLoader();7273if (!(myClassLoader instanceof URLClassLoader)) {74String message = "(ERROR) Test's class loader is not " +75"a URLClassLoader";76System.out.println(message);77throw new RuntimeException(message);78}7980URLClassLoader myURLClassLoader = (URLClassLoader) myClassLoader;81URL[] urls = myURLClassLoader.getURLs();82PrivateMLet mlet = new PrivateMLet(urls, null, false);83Class<?> shadowClass = mlet.loadClass(TestMXBean.class.getName());8485if (shadowClass == TestMXBean.class) {86String message = "(ERROR) MLet got original TestMXBean, not shadow";87System.out.println(message);88throw new RuntimeException(message);89}90shadowClass = null;9192MBeanServer mbs = MBeanServerFactory.createMBeanServer();93ObjectName mletName = new ObjectName("x:type=mlet");94mbs.registerMBean(mlet, mletName);9596ObjectName testName = new ObjectName("x:type=test");97mbs.createMBean(Test.class.getName(), testName, mletName);9899// That test fails because the MXBean instance is accessed via100// a delegate OpenMBean which has101ClassLoader testLoader = mbs.getClassLoaderFor(testName);102103if (testLoader != mlet) {104System.out.println("MLet " + mlet);105String message = "(ERROR) MXBean's class loader is not MLet: "106+ testLoader;107System.out.println(message);108throw new RuntimeException(message);109}110testLoader = null;111112113// Cycle get/set/get of the attribute of type Luis.114// We check the set is effective.115CompositeData cd_B = (CompositeData)mbs.getAttribute(testName, "B");116CompositeType compType_B = cd_B.getCompositeType();117118CompositeDataSupport cds_B =119new CompositeDataSupport(compType_B,120new String[]{"something"},121new Object[]{Integer.valueOf(13)});122Attribute myAtt = new Attribute("B", cds_B);123mbs.setAttribute(testName, myAtt);124125CompositeData cd_B2 = (CompositeData)mbs.getAttribute(testName, "B");126127if ( ((Integer)cd_B2.get("something")).intValue() != 13 ) {128String message = "(ERROR) The setAttribute of att B did not work;"129+ " expect Luis.something = 13 but got "130+ cd_B2.get("something");131System.out.println(message);132throw new RuntimeException(message);133}134135MBeanInfo info = mbs.getMBeanInfo(testName);136String mxbeanField =137(String)info.getDescriptor().getFieldValue(JMX.MXBEAN_FIELD);138139if ( mxbeanField == null || ! mxbeanField.equals("true")) {140String message = "(ERROR) Improper mxbean field value "141+ mxbeanField;142System.out.println(message);143throw new RuntimeException(message);144}145146// Check the 2 attributes.147MBeanAttributeInfo[] attrs = info.getAttributes();148149if ( attrs.length == 2 ) {150for (MBeanAttributeInfo mbai : attrs) {151String originalTypeFieldValue =152(String)mbai.getDescriptor().getFieldValue(JMX.ORIGINAL_TYPE_FIELD);153OpenType<?> openTypeFieldValue =154(OpenType<?>)mbai.getDescriptor().getFieldValue(JMX.OPEN_TYPE_FIELD);155156if ( mbai.getName().equals("A") ) {157if ( !mbai.isReadable() || !mbai.isWritable()158|| mbai.isIs()159|| !mbai.getType().equals("int") ) {160String message = "(ERROR) Unexpected MBeanAttributeInfo for A "161+ mbai;162System.out.println(message);163throw new RuntimeException(message);164}165166if ( ! originalTypeFieldValue.equals("int") ) {167String message = "(ERROR) Unexpected originalType in Descriptor for A "168+ originalTypeFieldValue;169System.out.println(message);170throw new RuntimeException(message);171}172173if ( ! openTypeFieldValue.equals(SimpleType.INTEGER) ) {174String message = "(ERROR) Unexpected openType in Descriptor for A "175+ originalTypeFieldValue;176System.out.println(message);177throw new RuntimeException(message);178}179} else if ( mbai.getName().equals("B") ) {180if ( !mbai.isReadable() || !mbai.isWritable()181|| mbai.isIs()182|| !mbai.getType().equals("javax.management.openmbean.CompositeData") ) {183String message = "(ERROR) Unexpected MBeanAttributeInfo for B "184+ mbai;185System.out.println(message);186throw new RuntimeException(message);187}188189if ( ! originalTypeFieldValue.equals(Luis.class.getName()) ) {190String message = "(ERROR) Unexpected originalType in Descriptor for B "191+ originalTypeFieldValue;192System.out.println(message);193throw new RuntimeException(message);194}195196if ( ! openTypeFieldValue.equals(compType_B) ) {197String message = "(ERROR) Unexpected openType in Descriptor for B "198+ compType_B;199System.out.println(message);200throw new RuntimeException(message);201}202} else {203String message = "(ERROR) Unknown attribute name";204System.out.println(message);205throw new RuntimeException(message);206}207}208} else {209String message = "(ERROR) Unexpected MBeanAttributeInfo array"210+ Arrays.deepToString(attrs);211System.out.println(message);212throw new RuntimeException(message);213}214215// Check the MXBean operation.216MBeanOperationInfo[] ops = info.getOperations();217// The impact is ACTION_INFO as for a standard MBean it is UNKNOWN,218// logged 6320104.219if (ops.length != 1 || !ops[0].getName().equals("bogus")220|| ops[0].getSignature().length > 0221|| !ops[0].getReturnType().equals("void")) {222String message = "(ERROR) Unexpected MBeanOperationInfo array "223+ Arrays.deepToString(ops);224System.out.println(message);225throw new RuntimeException(message);226}227228String originalTypeFieldValue =229(String)ops[0].getDescriptor().getFieldValue(JMX.ORIGINAL_TYPE_FIELD);230OpenType<?> openTypeFieldValue =231(OpenType<?>)ops[0].getDescriptor().getFieldValue(JMX.OPEN_TYPE_FIELD);232233if ( ! originalTypeFieldValue.equals("void") ) {234String message = "(ERROR) Unexpected originalType in Descriptor for bogus "235+ originalTypeFieldValue;236System.out.println(message);237throw new RuntimeException(message);238}239240if ( ! openTypeFieldValue.equals(SimpleType.VOID) ) {241String message = "(ERROR) Unexpected openType in Descriptor for bogus "242+ originalTypeFieldValue;243System.out.println(message);244throw new RuntimeException(message);245}246247// Check there is 2 constructors.248if (info.getConstructors().length != 2) {249String message = "(ERROR) Wrong number of constructors " +250"in introspected bean: " +251Arrays.asList(info.getConstructors());252System.out.println(message);253throw new RuntimeException(message);254}255256// Check MXBean class name.257if (!info.getClassName().endsWith("Test")) {258String message = "(ERROR) Wrong info class name: " +259info.getClassName();260System.out.println(message);261throw new RuntimeException(message);262}263264mbs.unregisterMBean(testName);265mbs.unregisterMBean(mletName);266267WeakReference<PrivateMLet> mletRef =268new WeakReference<PrivateMLet>(mlet);269mlet = null;270271System.out.println("MXBean registered and unregistered, waiting for " +272"garbage collector to collect class loader");273274for (int i = 0; i < 10000 && mletRef.get() != null; i++) {275System.gc();276Thread.sleep(1);277}278279if (mletRef.get() == null)280System.out.println("(OK) class loader was GC'd");281else {282String message = "(ERROR) Class loader was not GC'd";283System.out.println(message);284throw new RuntimeException(message);285}286} catch(Exception e) {287Utils.printThrowable(e, true) ;288throw new RuntimeException(e);289}290291System.out.println("MXBeanLoadingTest1::run: Done without any error") ;292}293294295// I agree the use of the MXBean annotation and the MXBean suffix for the296// interface name are redundant but however harmless.297//298@MXBean(true)299public static interface TestMXBean {300public void bogus();301public int getA();302public void setA(int a);303public Luis getB();304public void setB(Luis mi);305}306307308public static class Test implements TestMXBean {309private Luis luis = new Luis() ;310public Test() {}311public Test(int x) {}312313public void bogus() {}314public int getA() {return 0;}315public void setA(int a) {}316public Luis getB() {return this.luis;}317public void setB(Luis luis) {this.luis = luis;}318}319320321public static class Luis {322private int something = 0;323public Luis() {}324public int getSomething() {return something;}325public void setSomething(int v) {something = v;}326public void doNothing() {}327}328}329330331