Path: blob/master/test/jdk/javax/management/mxbean/MXBeanAnnotationTest.java
41149 views
/*1* Copyright (c) 2005, 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*/2223/*24* @test25* @bug 631649126* @summary Check that the MXBean annotation works as advertised27* @author Eamonn McManus28*29* @run clean MXBeanAnnotationTest30* @run build MXBeanAnnotationTest31* @run main MXBeanAnnotationTest32*/3334import javax.management.*;3536public class MXBeanAnnotationTest {37@MXBean38public static interface Empty {}3940public static class EmptyImpl implements Empty {}4142@MXBean(false)43public static interface NotMXBean {}4445public static class NotImpl implements NotMXBean {}4647public static void main(String[] args) throws Exception {48MBeanServer mbs = MBeanServerFactory.newMBeanServer();49ObjectName on = new ObjectName("a:b=c");5051// Test empty interface5253try {54mbs.registerMBean(new EmptyImpl(), on);55boolean ok = checkMXBean(mbs.getMBeanInfo(on), true,56"empty MXBean interface");57mbs.unregisterMBean(on);58if (ok)59System.out.println("OK: empty MXBean interface");60} catch (Exception e) {61failure = "MXBean with empty interface got exception: " + e;62System.out.println("FAILED: " + failure);63e.printStackTrace(System.out);64}6566// Test @MXBean(false)6768try {69mbs.registerMBean(new NotImpl(), on);70failure = "Registered a non-Standard MBean with @MXBean(false)";71System.out.println("FAILED: " + failure);72} catch (NotCompliantMBeanException e) {73System.out.println("OK: non-Standard MBean with @MXBean(false) " +74"rejected");75}7677if (failure == null)78System.out.println("TEST PASSED");79else80throw new Exception("TEST FAILED: " + failure);81}8283private static boolean checkMXBean(MBeanInfo mbi, boolean expected,84String what) {85Descriptor d = mbi.getDescriptor();86String mxbean = (String) d.getFieldValue("mxbean");87boolean is = (mxbean != null && mxbean.equals("true"));88if (is == expected)89return true;90else {91failure = "MBean should " + (expected ? "" : "not ") +92"have mxbean=true: " + d;93return false;94}95}9697private static String failure;98}99100101