0N/A/*
2362N/A * Copyright (c) 2005, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/*
0N/A * @test
0N/A * @bug 6316491
0N/A * @summary Check that the MXBean annotation works as advertised
0N/A * @author Eamonn McManus
0N/A * @run clean MXBeanAnnotationTest
0N/A * @run build MXBeanAnnotationTest
0N/A * @run main MXBeanAnnotationTest
0N/A */
0N/A
0N/Aimport javax.management.*;
0N/A
0N/Apublic class MXBeanAnnotationTest {
0N/A @MXBean
0N/A public static interface Empty {}
0N/A
0N/A public static class EmptyImpl implements Empty {}
0N/A
0N/A @MXBean(false)
0N/A public static interface NotMXBean {}
0N/A
0N/A public static class NotImpl implements NotMXBean {}
0N/A
0N/A public static void main(String[] args) throws Exception {
0N/A MBeanServer mbs = MBeanServerFactory.newMBeanServer();
0N/A ObjectName on = new ObjectName("a:b=c");
0N/A
0N/A // Test empty interface
0N/A
0N/A try {
0N/A mbs.registerMBean(new EmptyImpl(), on);
0N/A boolean ok = checkMXBean(mbs.getMBeanInfo(on), true,
0N/A "empty MXBean interface");
0N/A mbs.unregisterMBean(on);
0N/A if (ok)
0N/A System.out.println("OK: empty MXBean interface");
0N/A } catch (Exception e) {
0N/A failure = "MXBean with empty interface got exception: " + e;
0N/A System.out.println("FAILED: " + failure);
0N/A e.printStackTrace(System.out);
0N/A }
0N/A
0N/A // Test @MXBean(false)
0N/A
0N/A try {
0N/A mbs.registerMBean(new NotImpl(), on);
0N/A failure = "Registered a non-Standard MBean with @MXBean(false)";
0N/A System.out.println("FAILED: " + failure);
0N/A } catch (NotCompliantMBeanException e) {
0N/A System.out.println("OK: non-Standard MBean with @MXBean(false) " +
0N/A "rejected");
0N/A }
0N/A
0N/A if (failure == null)
0N/A System.out.println("TEST PASSED");
0N/A else
0N/A throw new Exception("TEST FAILED: " + failure);
0N/A }
0N/A
0N/A private static boolean checkMXBean(MBeanInfo mbi, boolean expected,
0N/A String what) {
0N/A Descriptor d = mbi.getDescriptor();
0N/A String mxbean = (String) d.getFieldValue("mxbean");
0N/A boolean is = (mxbean != null && mxbean.equals("true"));
0N/A if (is == expected)
0N/A return true;
0N/A else {
0N/A failure = "MBean should " + (expected ? "" : "not ") +
0N/A "have mxbean=true: " + d;
0N/A return false;
0N/A }
0N/A }
0N/A
0N/A private static String failure;
0N/A}