Path: blob/master/test/jdk/javax/management/proxy/JMXProxyTest.java
41149 views
/*1* Copyright (c) 2013, 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*/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*36* @run clean JMXProxyTest37* @run build JMXProxyTest38* @run main JMXProxyTest39*/40public class JMXProxyTest {41private static interface PrivateMBean {42public int[] getInts();43}4445private static interface PrivateMXBean {46public int[] getInts();47}4849public static class Private implements PrivateMXBean, PrivateMBean {50public int[] getInts() {51return new int[]{1,2,3};52}53}5455public static interface NonCompliantMBean {56public boolean getInt();57public boolean isInt();58public void setInt(int a);59public void setInt(long b);60}6162public static interface NonCompliantMXBean {63public boolean getInt();64public boolean isInt();65public void setInt(int a);66public void setInt(long b);67}6869public static class NonCompliant implements NonCompliantMXBean, NonCompliantMBean {70public boolean getInt() {71return false;72}7374public boolean isInt() {75return true;76}7778public void setInt(int a) {79}8081public void setInt(long b) {82}83}8485public static interface CompliantMBean {86public boolean isFlag();87public int getInt();88public void setInt(int value);89}9091public static interface CompliantMXBean {92public boolean isFlag();93public int getInt();94public void setInt(int value);95}9697public static class Compliant implements CompliantMXBean, CompliantMBean {98public boolean isFlag() {99return false;100}101102public int getInt() {103return 1;104}105106public void setInt(int value) {107}108}109110private static int failures = 0;111112public static void main(String[] args) throws Exception {113testCompliant(CompliantMBean.class, false);114testCompliant(CompliantMXBean.class, true);115testNonCompliant(PrivateMBean.class, false);116testNonCompliant(PrivateMXBean.class, true);117testNonCompliant(NonCompliantMBean.class, false);118testNonCompliant(NonCompliantMXBean.class, true);119120if (failures == 0)121System.out.println("Test passed");122else123throw new Exception("TEST FAILURES: " + failures);124}125126private static void fail(String msg) {127failures++;128System.out.println("FAIL: " + msg);129}130131private static void success(String msg) {132System.out.println("OK: " + msg);133}134135private static void testNonCompliant(Class<?> iface, boolean isMx) throws Exception {136try {137System.out.println("Creating a proxy for non-compliant " +138(isMx ? "MXBean" : "MBean") + " " +139iface.getName() + " ...");140141MBeanServer mbs = MBeanServerFactory.newMBeanServer();142ObjectName on = new ObjectName("test:type=Proxy");143144if (isMx) {145JMX.newMXBeanProxy(mbs, on, iface);146} else {147JMX.newMBeanProxy(mbs, on, iface);148}149fail("Created a proxy for non-compliant " +150(isMx ? "MXBean" : "MBean") + " - " + iface.getName());151} catch (Exception e) {152Throwable t = e;153while (t != null && !(t instanceof NotCompliantMBeanException)) {154t = t.getCause();155}156if (t != null) {157success("Proxy not created");158} else {159throw e;160}161}162}163private static void testCompliant(Class<?> iface, boolean isMx) throws Exception {164try {165System.out.println("Creating a proxy for compliant " +166(isMx ? "MXBean" : "MBean") + " " +167iface.getName() + " ...");168169MBeanServer mbs = MBeanServerFactory.newMBeanServer();170ObjectName on = new ObjectName("test:type=Proxy");171172if (isMx) {173JMX.newMXBeanProxy(mbs, on, iface);174} else {175JMX.newMBeanProxy(mbs, on, iface);176}177success("Created a proxy for compliant " +178(isMx ? "MXBean" : "MBean") + " - " + iface.getName());179} catch (Exception e) {180Throwable t = e;181while (t != null && !(t instanceof NotCompliantMBeanException)) {182t = t.getCause();183}184if (t != null) {185fail("Proxy not created");186} else {187throw e;188}189}190}191}192193194