Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/management/mxbean/MXBeanAnnotationTest.java
38841 views
/*1* Copyright (c) 2005, 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* @run clean MXBeanAnnotationTest29* @run build MXBeanAnnotationTest30* @run main MXBeanAnnotationTest31*/3233import javax.management.*;3435public class MXBeanAnnotationTest {36@MXBean37public static interface Empty {}3839public static class EmptyImpl implements Empty {}4041@MXBean(false)42public static interface NotMXBean {}4344public static class NotImpl implements NotMXBean {}4546public static void main(String[] args) throws Exception {47MBeanServer mbs = MBeanServerFactory.newMBeanServer();48ObjectName on = new ObjectName("a:b=c");4950// Test empty interface5152try {53mbs.registerMBean(new EmptyImpl(), on);54boolean ok = checkMXBean(mbs.getMBeanInfo(on), true,55"empty MXBean interface");56mbs.unregisterMBean(on);57if (ok)58System.out.println("OK: empty MXBean interface");59} catch (Exception e) {60failure = "MXBean with empty interface got exception: " + e;61System.out.println("FAILED: " + failure);62e.printStackTrace(System.out);63}6465// Test @MXBean(false)6667try {68mbs.registerMBean(new NotImpl(), on);69failure = "Registered a non-Standard MBean with @MXBean(false)";70System.out.println("FAILED: " + failure);71} catch (NotCompliantMBeanException e) {72System.out.println("OK: non-Standard MBean with @MXBean(false) " +73"rejected");74}7576if (failure == null)77System.out.println("TEST PASSED");78else79throw new Exception("TEST FAILED: " + failure);80}8182private static boolean checkMXBean(MBeanInfo mbi, boolean expected,83String what) {84Descriptor d = mbi.getDescriptor();85String mxbean = (String) d.getFieldValue("mxbean");86boolean is = (mxbean != null && mxbean.equals("true"));87if (is == expected)88return true;89else {90failure = "MBean should " + (expected ? "" : "not ") +91"have mxbean=true: " + d;92return false;93}94}9596private static String failure;97}9899100