Path: blob/master/test/jdk/javax/management/MBeanServer/MBeanTest.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.MBeanServer;24import javax.management.MBeanServerFactory;25import javax.management.NotCompliantMBeanException;26import javax.management.ObjectName;2728/*29* @test30* @bug 801028531* @summary General MBean test.32* @author Jaroslav Bachorik33*34* @run clean MBeanTest35* @run build MBeanTest36* @run main MBeanTest37*/38public class MBeanTest {39private static interface PrivateMBean {40public int[] getInts();41}4243public static class Private implements PrivateMBean {44public int[] getInts() {45return new int[]{1,2,3};46}47}4849public static interface NonCompliantMBean {50public boolean getInt();51public boolean isInt();52public void setInt(int a);53public void setInt(long b);54}5556public static class NonCompliant implements NonCompliantMBean {57public boolean getInt() {58return false;59}6061public boolean isInt() {62return true;63}6465public void setInt(int a) {66}6768public void setInt(long b) {69}70}7172public static interface CompliantMBean {73public boolean isFlag();74public int getInt();75public void setInt(int value);76}7778public static class Compliant implements CompliantMBean {79public boolean isFlag() {80return false;81}8283public int getInt() {84return 1;85}8687public void setInt(int value) {88}89}9091private static int failures = 0;9293public static void main(String[] args) throws Exception {94testCompliant(CompliantMBean.class, new Compliant());95testNonCompliant(PrivateMBean.class, new Private());96testNonCompliant(NonCompliantMBean.class, new NonCompliant());9798if (failures == 0)99System.out.println("Test passed");100else101throw new Exception("TEST FAILURES: " + failures);102}103104private static void fail(String msg) {105failures++;106System.out.println("FAIL: " + msg);107}108109private static void success(String msg) {110System.out.println("OK: " + msg);111}112113private static void testNonCompliant(Class<?> iface, Object bean) throws Exception {114try {115System.out.println("Registering a non-compliant MBean " +116iface.getName() + " ...");117118MBeanServer mbs = MBeanServerFactory.newMBeanServer();119ObjectName on = new ObjectName("test:type=NonCompliant");120121mbs.registerMBean(bean, on);122123fail("Registered a non-compliant MBean - " + iface.getName());124} catch (Exception e) {125Throwable t = e;126while (t != null && !(t instanceof NotCompliantMBeanException)) {127t = t.getCause();128}129if (t != null) {130success("MBean not registered");131} else {132throw e;133}134}135}136private static void testCompliant(Class<?> iface, Object bean) throws Exception {137try {138System.out.println("Registering a compliant MBean " +139iface.getName() + " ...");140141MBeanServer mbs = MBeanServerFactory.newMBeanServer();142ObjectName on = new ObjectName("test:type=Compliant");143144mbs.registerMBean(bean, on);145success("Registered a compliant MBean - " + iface.getName());146} catch (Exception e) {147Throwable t = e;148while (t != null && !(t instanceof NotCompliantMBeanException)) {149t = t.getCause();150}151if (t != null) {152fail("MBean not registered");153} else {154throw e;155}156}157}158}159160161