Path: blob/master/test/jdk/javax/management/loading/SystemClassLoaderTest.java
41152 views
/*1* Copyright (c) 2003, 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 483938926* @summary Test that a class can load MBeans from its class loader27* (at least if it is the system class loader)28* @author Eamonn McManus29*30* @run clean SystemClassLoaderTest31* @run build SystemClassLoaderTest32* @run main SystemClassLoaderTest33*/3435import javax.management.MBeanServer;36import javax.management.MBeanServerFactory;37import javax.management.ObjectName;3839/* Test that we can load an MBean using createMBean(className, objectName)40even if the class of the MBean is not known to the JMX class loader. */41public class SystemClassLoaderTest {4243public static void main(String[] args) throws Exception {44// Instantiate the MBean server45//46System.out.println("Create the MBean server");47MBeanServer mbs = MBeanServerFactory.createMBeanServer();4849ClassLoader mbsClassLoader = mbs.getClass().getClassLoader();5051String testClassName = Test.class.getName();5253// Check that the MBeanServer class loader does not know our test class54try {55Class.forName(testClassName, true, mbsClassLoader);56System.out.println("TEST IS INVALID: MBEANSERVER'S CLASS LOADER " +57"KNOWS OUR TEST CLASS");58System.exit(1);59} catch (ClassNotFoundException e) {60// As required61}6263// Register the MBean64//65System.out.println("Create MBean from this class");66ObjectName objectName = new ObjectName("whatever:type=whatever");67mbs.createMBean(testClassName, objectName);68// Test OK!69//70System.out.println("Bye! Bye!");71}7273public static class Test implements TestMBean {74public Test() {}75}7677public static interface TestMBean {}78}798081