Path: blob/master/test/jdk/javax/management/MBeanServer/MBeanFallbackTest.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 Test fallback for private MBean interfaces.32* It needs to be a separate class because the "jdk.jmx.mbeans.allowNonPublic"33* system property must be set before c.s.j.m.MBeanAnalyzer has been loaded.34* @author Jaroslav Bachorik35*36* @run clean MBeanFallbackTest37* @run build MBeanFallbackTest38* @run main/othervm -Djdk.jmx.mbeans.allowNonPublic=true MBeanFallbackTest39*/40public class MBeanFallbackTest {41private static interface PrivateMBean {42public int[] getInts();43}4445public static class Private implements PrivateMBean {46public int[] getInts() {47return new int[]{1,2,3};48}49}5051private static int failures = 0;5253public static void main(String[] args) throws Exception {54testPrivate(PrivateMBean.class, new Private());5556if (failures == 0)57System.out.println("Test passed");58else59throw new Exception("TEST FAILURES: " + failures);60}6162private static void fail(String msg) {63failures++;64System.out.println("FAIL: " + msg);65}6667private static void success(String msg) {68System.out.println("OK: " + msg);69}7071private static void testPrivate(Class<?> iface, Object bean) throws Exception {72try {73System.out.println("Registering a private MBean " +74iface.getName() + " ...");7576MBeanServer mbs = MBeanServerFactory.newMBeanServer();77ObjectName on = new ObjectName("test:type=Compliant");7879mbs.registerMBean(bean, on);80success("Registered a private MBean - " + iface.getName());81} catch (Exception e) {82Throwable t = e;83while (t != null && !(t instanceof NotCompliantMBeanException)) {84t = t.getCause();85}86if (t != null) {87fail("MBean not registered");88} else {89throw e;90}91}92}93}949596