Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/management/proxy/JMXProxyTest.java
38838 views
/*1* Copyright (c) 2013, 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*/2223import javax.management.JMX;24import javax.management.MBeanServer;25import javax.management.MBeanServerFactory;26import javax.management.NotCompliantMBeanException;27import javax.management.ObjectName;2829/*30* @test31* @bug 801028532* @summary Tests that javax.management.JMX creates proxies only for the33* compliant MBeans/MXBeans34* @author Jaroslav Bachorik35* @run clean JMXProxyTest36* @run build JMXProxyTest37* @run main JMXProxyTest38*/39public class JMXProxyTest {40private static interface PrivateMBean {41public int[] getInts();42}4344private static interface PrivateMXBean {45public int[] getInts();46}4748public static class Private implements PrivateMXBean, PrivateMBean {49public int[] getInts() {50return new int[]{1,2,3};51}52}5354public static interface NonCompliantMBean {55public boolean getInt();56public boolean isInt();57public void setInt(int a);58public void setInt(long b);59}6061public static interface NonCompliantMXBean {62public boolean getInt();63public boolean isInt();64public void setInt(int a);65public void setInt(long b);66}6768public static class NonCompliant implements NonCompliantMXBean, NonCompliantMBean {69public boolean getInt() {70return false;71}7273public boolean isInt() {74return true;75}7677public void setInt(int a) {78}7980public void setInt(long b) {81}82}8384public static interface CompliantMBean {85public boolean isFlag();86public int getInt();87public void setInt(int value);88}8990public static interface CompliantMXBean {91public boolean isFlag();92public int getInt();93public void setInt(int value);94}9596public static class Compliant implements CompliantMXBean, CompliantMBean {97public boolean isFlag() {98return false;99}100101public int getInt() {102return 1;103}104105public void setInt(int value) {106}107}108109private static int failures = 0;110111public static void main(String[] args) throws Exception {112testCompliant(CompliantMBean.class, false);113testCompliant(CompliantMXBean.class, true);114testNonCompliant(PrivateMBean.class, false);115testNonCompliant(PrivateMXBean.class, true);116testNonCompliant(NonCompliantMBean.class, false);117testNonCompliant(NonCompliantMXBean.class, true);118119if (failures == 0)120System.out.println("Test passed");121else122throw new Exception("TEST FAILURES: " + failures);123}124125private static void fail(String msg) {126failures++;127System.out.println("FAIL: " + msg);128}129130private static void success(String msg) {131System.out.println("OK: " + msg);132}133134private static void testNonCompliant(Class<?> iface, boolean isMx) throws Exception {135try {136System.out.println("Creating a proxy for non-compliant " +137(isMx ? "MXBean" : "MBean") + " " +138iface.getName() + " ...");139140MBeanServer mbs = MBeanServerFactory.newMBeanServer();141ObjectName on = new ObjectName("test:type=Proxy");142143if (isMx) {144JMX.newMXBeanProxy(mbs, on, iface);145} else {146JMX.newMBeanProxy(mbs, on, iface);147}148fail("Created a proxy for non-compliant " +149(isMx ? "MXBean" : "MBean") + " - " + iface.getName());150} catch (Exception e) {151Throwable t = e;152while (t != null && !(t instanceof NotCompliantMBeanException)) {153t = t.getCause();154}155if (t != null) {156success("Proxy not created");157} else {158throw e;159}160}161}162private static void testCompliant(Class<?> iface, boolean isMx) throws Exception {163try {164System.out.println("Creating a proxy for compliant " +165(isMx ? "MXBean" : "MBean") + " " +166iface.getName() + " ...");167168MBeanServer mbs = MBeanServerFactory.newMBeanServer();169ObjectName on = new ObjectName("test:type=Proxy");170171if (isMx) {172JMX.newMXBeanProxy(mbs, on, iface);173} else {174JMX.newMBeanProxy(mbs, on, iface);175}176success("Created a proxy for compliant " +177(isMx ? "MXBean" : "MBean") + " - " + iface.getName());178} catch (Exception e) {179Throwable t = e;180while (t != null && !(t instanceof NotCompliantMBeanException)) {181t = t.getCause();182}183if (t != null) {184fail("Proxy not created");185} else {186throw e;187}188}189}190}191192193