Path: blob/master/test/jdk/java/lang/management/ManagementFactory/MXBeanProxyTest.java
41152 views
/*1* Copyright (c) 2004, 2016, 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 502453126* @summary Basic Test for ManagementFactory.newPlatformMXBean().27* @author Mandy Chung28*29* @run main/othervm MXBeanProxyTest30*/31import javax.management.*;32import java.lang.management.ClassLoadingMXBean;33import java.lang.management.RuntimeMXBean;34import static java.lang.management.ManagementFactory.*;35public class MXBeanProxyTest {36private static MBeanServer server = getPlatformMBeanServer();37public static void main(String[] argv) throws Exception {38boolean iae = false;39try {40// Get a MXBean proxy with invalid name41newPlatformMXBeanProxy(server,42"Invalid ObjectName",43RuntimeMXBean.class);44} catch (IllegalArgumentException e) {45// Expected exception46System.out.println("EXPECTED: " + e);47iae = true;48}49if (!iae) {50throw new RuntimeException("Invalid ObjectName " +51" was not detected");52}5354try {55// Get a MXBean proxy with non existent MXBean56newPlatformMXBeanProxy(server,57"java.lang:type=Foo",58RuntimeMXBean.class);59iae = false;60} catch (IllegalArgumentException e) {61// Expected exception62System.out.println("EXPECTED: " + e);63iae = true;64}65if (!iae) {66throw new RuntimeException("Non existent MXBean " +67" was not detected");68}6970try {71// Mismatch MXBean interface72newPlatformMXBeanProxy(server,73RUNTIME_MXBEAN_NAME,74ClassLoadingMXBean.class);75iae = false;76} catch (IllegalArgumentException e) {77// Expected exception78System.out.println("EXPECTED: " + e);79iae = true;80}81if (!iae) {82throw new RuntimeException("Mismatched MXBean interface " +83" was not detected");84}8586final FooMBean foo = new Foo();87final ObjectName objName = new ObjectName("java.lang:type=Foo");88server.registerMBean(foo, objName);89try {90// non-platform MXBean91newPlatformMXBeanProxy(server,92"java.lang:type=Foo",93FooMBean.class);94iae = false;95} catch (IllegalArgumentException e) {96// Expected exception97System.out.println("EXPECTED: " + e);98iae = true;99}100if (!iae) {101throw new RuntimeException("Non-platform MXBean " +102" was not detected");103}104105106// Successfully get MXBean107RuntimeMXBean rm = newPlatformMXBeanProxy(server,108RUNTIME_MXBEAN_NAME,109RuntimeMXBean.class);110System.out.println("VM uptime = " + rm.getUptime());111System.out.println("Test passed.");112}113114public interface FooMBean {115public boolean isFoo();116}117static class Foo implements FooMBean {118public boolean isFoo() {119return true;120}121}122}123124125