Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/lang/management/ManagementFactory/MXBeanProxyTest.java
38828 views
/*1* Copyright (c) 2004, 2010, 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* @run main/othervm MXBeanProxyTest28* @author Mandy Chung29*/30import javax.management.*;31import java.lang.management.ClassLoadingMXBean;32import java.lang.management.RuntimeMXBean;33import static java.lang.management.ManagementFactory.*;34public class MXBeanProxyTest {35private static MBeanServer server = getPlatformMBeanServer();36public static void main(String[] argv) throws Exception {37boolean iae = false;38try {39// Get a MXBean proxy with invalid name40newPlatformMXBeanProxy(server,41"Invalid ObjectName",42RuntimeMXBean.class);43} catch (IllegalArgumentException e) {44// Expected exception45System.out.println("EXPECTED: " + e);46iae = true;47}48if (!iae) {49throw new RuntimeException("Invalid ObjectName " +50" was not detected");51}5253try {54// Get a MXBean proxy with non existent MXBean55newPlatformMXBeanProxy(server,56"java.lang:type=Foo",57RuntimeMXBean.class);58iae = false;59} catch (IllegalArgumentException e) {60// Expected exception61System.out.println("EXPECTED: " + e);62iae = true;63}64if (!iae) {65throw new RuntimeException("Non existent MXBean " +66" was not detected");67}6869try {70// Mismatch MXBean interface71newPlatformMXBeanProxy(server,72RUNTIME_MXBEAN_NAME,73ClassLoadingMXBean.class);74iae = false;75} catch (IllegalArgumentException e) {76// Expected exception77System.out.println("EXPECTED: " + e);78iae = true;79}80if (!iae) {81throw new RuntimeException("Mismatched MXBean interface " +82" was not detected");83}8485final FooMBean foo = new Foo();86final ObjectName objName = new ObjectName("java.lang:type=Foo");87server.registerMBean(foo, objName);88try {89// non-platform MXBean90newPlatformMXBeanProxy(server,91"java.lang:type=Foo",92FooMBean.class);93iae = false;94} catch (IllegalArgumentException e) {95// Expected exception96System.out.println("EXPECTED: " + e);97iae = true;98}99if (!iae) {100throw new RuntimeException("Non-platform MXBean " +101" was not detected");102}103104105// Successfully get MXBean106RuntimeMXBean rm = newPlatformMXBeanProxy(server,107RUNTIME_MXBEAN_NAME,108RuntimeMXBean.class);109System.out.println("VM uptime = " + rm.getUptime());110System.out.println("Test passed.");111}112113public interface FooMBean {114public boolean isFoo();115}116static class Foo implements FooMBean {117public boolean isFoo() {118return true;119}120}121}122123124