Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/management/MBeanServer/MBeanTest.java
38839 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.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* @run clean MBeanTest34* @run build MBeanTest35* @run main MBeanTest36*/37public class MBeanTest {38private static interface PrivateMBean {39public int[] getInts();40}4142public static class Private implements PrivateMBean {43public int[] getInts() {44return new int[]{1,2,3};45}46}4748public static interface NonCompliantMBean {49public boolean getInt();50public boolean isInt();51public void setInt(int a);52public void setInt(long b);53}5455public static class NonCompliant implements NonCompliantMBean {56public boolean getInt() {57return false;58}5960public boolean isInt() {61return true;62}6364public void setInt(int a) {65}6667public void setInt(long b) {68}69}7071public static interface CompliantMBean {72public boolean isFlag();73public int getInt();74public void setInt(int value);75}7677public static class Compliant implements CompliantMBean {78public boolean isFlag() {79return false;80}8182public int getInt() {83return 1;84}8586public void setInt(int value) {87}88}8990private static int failures = 0;9192public static void main(String[] args) throws Exception {93testCompliant(CompliantMBean.class, new Compliant());94testNonCompliant(PrivateMBean.class, new Private());95testNonCompliant(NonCompliantMBean.class, new NonCompliant());9697if (failures == 0)98System.out.println("Test passed");99else100throw new Exception("TEST FAILURES: " + failures);101}102103private static void fail(String msg) {104failures++;105System.out.println("FAIL: " + msg);106}107108private static void success(String msg) {109System.out.println("OK: " + msg);110}111112private static void testNonCompliant(Class<?> iface, Object bean) throws Exception {113try {114System.out.println("Registering a non-compliant MBean " +115iface.getName() + " ...");116117MBeanServer mbs = MBeanServerFactory.newMBeanServer();118ObjectName on = new ObjectName("test:type=NonCompliant");119120mbs.registerMBean(bean, on);121122fail("Registered a non-compliant MBean - " + iface.getName());123} catch (Exception e) {124Throwable t = e;125while (t != null && !(t instanceof NotCompliantMBeanException)) {126t = t.getCause();127}128if (t != null) {129success("MBean not registered");130} else {131throw e;132}133}134}135private static void testCompliant(Class<?> iface, Object bean) throws Exception {136try {137System.out.println("Registering a compliant MBean " +138iface.getName() + " ...");139140MBeanServer mbs = MBeanServerFactory.newMBeanServer();141ObjectName on = new ObjectName("test:type=Compliant");142143mbs.registerMBean(bean, on);144success("Registered a compliant MBean - " + iface.getName());145} catch (Exception e) {146Throwable t = e;147while (t != null && !(t instanceof NotCompliantMBeanException)) {148t = t.getCause();149}150if (t != null) {151fail("MBean not registered");152} else {153throw e;154}155}156}157}158159160