SystemClassLoaderTest.java revision 0
2fcbc377041d659446ded306a92901b4b0753b68yt/*
2fcbc377041d659446ded306a92901b4b0753b68yt * Copyright 2003 Sun Microsystems, Inc. All Rights Reserved.
2fcbc377041d659446ded306a92901b4b0753b68yt * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
2fcbc377041d659446ded306a92901b4b0753b68yt *
2fcbc377041d659446ded306a92901b4b0753b68yt * This code is free software; you can redistribute it and/or modify it
2fcbc377041d659446ded306a92901b4b0753b68yt * under the terms of the GNU General Public License version 2 only, as
2fcbc377041d659446ded306a92901b4b0753b68yt * published by the Free Software Foundation.
2fcbc377041d659446ded306a92901b4b0753b68yt *
2fcbc377041d659446ded306a92901b4b0753b68yt * This code is distributed in the hope that it will be useful, but WITHOUT
2fcbc377041d659446ded306a92901b4b0753b68yt * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
2fcbc377041d659446ded306a92901b4b0753b68yt * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
2fcbc377041d659446ded306a92901b4b0753b68yt * version 2 for more details (a copy is included in the LICENSE file that
2fcbc377041d659446ded306a92901b4b0753b68yt * accompanied this code).
2fcbc377041d659446ded306a92901b4b0753b68yt *
2fcbc377041d659446ded306a92901b4b0753b68yt * You should have received a copy of the GNU General Public License version
2fcbc377041d659446ded306a92901b4b0753b68yt * 2 along with this work; if not, write to the Free Software Foundation,
2fcbc377041d659446ded306a92901b4b0753b68yt * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
2fcbc377041d659446ded306a92901b4b0753b68yt *
2fcbc377041d659446ded306a92901b4b0753b68yt * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
2fcbc377041d659446ded306a92901b4b0753b68yt * CA 95054 USA or visit www.sun.com if you need additional information or
2fcbc377041d659446ded306a92901b4b0753b68yt * have any questions.
4e942d8cd27c7f8bb80549d7c2564445f19ba4a3shidokht */
2fcbc377041d659446ded306a92901b4b0753b68yt
2fcbc377041d659446ded306a92901b4b0753b68yt/*
2fcbc377041d659446ded306a92901b4b0753b68yt * @test
2fcbc377041d659446ded306a92901b4b0753b68yt * @bug 4839389
2fcbc377041d659446ded306a92901b4b0753b68yt * @summary Test that a class can load MBeans from its class loader
2fcbc377041d659446ded306a92901b4b0753b68yt * (at least if it is the system class loader)
2fcbc377041d659446ded306a92901b4b0753b68yt * @author Eamonn McManus
2fcbc377041d659446ded306a92901b4b0753b68yt * @run clean SystemClassLoaderTest
2fcbc377041d659446ded306a92901b4b0753b68yt * @run build SystemClassLoaderTest
2fcbc377041d659446ded306a92901b4b0753b68yt * @run main SystemClassLoaderTest
2fcbc377041d659446ded306a92901b4b0753b68yt */
2fcbc377041d659446ded306a92901b4b0753b68yt
2fcbc377041d659446ded306a92901b4b0753b68ytimport javax.management.MBeanServer;
2fcbc377041d659446ded306a92901b4b0753b68ytimport javax.management.MBeanServerFactory;
2fcbc377041d659446ded306a92901b4b0753b68ytimport javax.management.ObjectName;
2fcbc377041d659446ded306a92901b4b0753b68yt
2fcbc377041d659446ded306a92901b4b0753b68yt/* Test that we can load an MBean using createMBean(className, objectName)
2fcbc377041d659446ded306a92901b4b0753b68yt even if the class of the MBean is not known to the JMX class loader. */
2fcbc377041d659446ded306a92901b4b0753b68ytpublic class SystemClassLoaderTest {
2fcbc377041d659446ded306a92901b4b0753b68yt
2fcbc377041d659446ded306a92901b4b0753b68yt public static void main(String[] args) throws Exception {
2fcbc377041d659446ded306a92901b4b0753b68yt // Instantiate the MBean server
2fcbc377041d659446ded306a92901b4b0753b68yt //
2fcbc377041d659446ded306a92901b4b0753b68yt System.out.println("Create the MBean server");
2fcbc377041d659446ded306a92901b4b0753b68yt MBeanServer mbs = MBeanServerFactory.createMBeanServer();
2fcbc377041d659446ded306a92901b4b0753b68yt
2fcbc377041d659446ded306a92901b4b0753b68yt ClassLoader mbsClassLoader = mbs.getClass().getClassLoader();
2fcbc377041d659446ded306a92901b4b0753b68yt
2fcbc377041d659446ded306a92901b4b0753b68yt String testClassName = Test.class.getName();
2fcbc377041d659446ded306a92901b4b0753b68yt
2fcbc377041d659446ded306a92901b4b0753b68yt // Check that the MBeanServer class loader does not know our test class
2fcbc377041d659446ded306a92901b4b0753b68yt try {
2fcbc377041d659446ded306a92901b4b0753b68yt Class.forName(testClassName, true, mbsClassLoader);
2fcbc377041d659446ded306a92901b4b0753b68yt System.out.println("TEST IS INVALID: MBEANSERVER'S CLASS LOADER " +
2fcbc377041d659446ded306a92901b4b0753b68yt "KNOWS OUR TEST CLASS");
2fcbc377041d659446ded306a92901b4b0753b68yt System.exit(1);
2fcbc377041d659446ded306a92901b4b0753b68yt } catch (ClassNotFoundException e) {
2fcbc377041d659446ded306a92901b4b0753b68yt // As required
2fcbc377041d659446ded306a92901b4b0753b68yt }
2fcbc377041d659446ded306a92901b4b0753b68yt
2fcbc377041d659446ded306a92901b4b0753b68yt // Register the MBean
2fcbc377041d659446ded306a92901b4b0753b68yt //
2fcbc377041d659446ded306a92901b4b0753b68yt System.out.println("Create MBean from this class");
2fcbc377041d659446ded306a92901b4b0753b68yt ObjectName objectName = new ObjectName("whatever:type=whatever");
2fcbc377041d659446ded306a92901b4b0753b68yt mbs.createMBean(testClassName, objectName);
2fcbc377041d659446ded306a92901b4b0753b68yt // Test OK!
2fcbc377041d659446ded306a92901b4b0753b68yt //
2fcbc377041d659446ded306a92901b4b0753b68yt System.out.println("Bye! Bye!");
2fcbc377041d659446ded306a92901b4b0753b68yt }
2fcbc377041d659446ded306a92901b4b0753b68yt
2fcbc377041d659446ded306a92901b4b0753b68yt public static class Test implements TestMBean {
2fcbc377041d659446ded306a92901b4b0753b68yt public Test() {}
7014882c6a3672fd0e5d60200af8643ae53c5928Richard Lowe }
7014882c6a3672fd0e5d60200af8643ae53c5928Richard Lowe
7014882c6a3672fd0e5d60200af8643ae53c5928Richard Lowe public static interface TestMBean {}
7014882c6a3672fd0e5d60200af8643ae53c5928Richard Lowe}
2fcbc377041d659446ded306a92901b4b0753b68yt