StandardMBeanOverrideTest.java revision 2362
2N/A/*
2N/A * Copyright (c) 2005, Oracle and/or its affiliates. All rights reserved.
2N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
2N/A *
2N/A * This code is free software; you can redistribute it and/or modify it
2N/A * under the terms of the GNU General Public License version 2 only, as
2N/A * published by the Free Software Foundation.
2N/A *
2N/A * This code is distributed in the hope that it will be useful, but WITHOUT
2N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
2N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
2N/A * version 2 for more details (a copy is included in the LICENSE file that
2N/A * accompanied this code).
2N/A *
2N/A * You should have received a copy of the GNU General Public License version
2N/A * 2 along with this work; if not, write to the Free Software Foundation,
2N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
2N/A *
2N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2N/A * or visit www.oracle.com if you need additional information or have any
2N/A * questions.
2N/A */
2N/A
2N/A/*
2N/A * @test
2N/A * @bug 6283045
2N/A * @summary Test the correctness of immutableInfo field in MBeanInfo descriptor
2N/A * when overriding the methods cacheMBeanInfo, getCachedMBeanInfo,
2N/A * getMBeanInfo and getNotificationInfo in StandardMBean and
2N/A * StandardEmitterMBean.
2N/A * @author Luis-Miguel Alventosa
2N/A * @run clean StandardMBeanOverrideTest
2N/A * @run build StandardMBeanOverrideTest
2N/A * @run main StandardMBeanOverrideTest
2N/A */
2N/A
2N/Aimport java.io.*;
2N/Aimport java.lang.management.*;
2N/Aimport javax.management.*;
2N/Aimport javax.management.openmbean.*;
2N/A
2N/Apublic class StandardMBeanOverrideTest {
2N/A
2N/A private static Object testInstances[] = {
2N/A new TestClass0(false),
2N/A new TestClass1(false),
2N/A new TestClass2(false),
2N/A new TestClass3(false),
2N/A new TestClass4(false),
2N/A new TestClass5(false),
2N/A new TestClass6(false),
2N/A new TestClass7(false),
2N/A new TestClass8(false),
2N/A new TestClass9(false),
2N/A new TestClass0(true),
2N/A new TestClass1(true),
2N/A new TestClass2(true),
2N/A new TestClass3(true),
2N/A new TestClass4(true),
2N/A new TestClass5(true),
2N/A new TestClass6(true),
2N/A new TestClass7(true),
2N/A new TestClass8(true),
2N/A new TestClass9(true),
2N/A };
2N/A
2N/A public interface ImmutableInfo {
2N/A }
2N/A
2N/A public interface NonImmutableInfo {
2N/A }
2N/A
2N/A public interface TestInterface {
2N/A }
2N/A
2N/A public static class TestClass0
2N/A extends StandardMBean
2N/A implements TestInterface, ImmutableInfo {
2N/A public TestClass0(boolean mxbean) {
2N/A super(TestInterface.class, mxbean);
2N/A }
2N/A }
2N/A
2N/A public static class TestClass1
2N/A extends StandardMBean
2N/A implements TestInterface, NonImmutableInfo {
2N/A public TestClass1(boolean mxbean) {
2N/A super(TestInterface.class, mxbean);
2N/A }
2N/A protected void cacheMBeanInfo(MBeanInfo info) {
2N/A super.cacheMBeanInfo(info);
2N/A }
2N/A }
2N/A
2N/A public static class TestClass2
2N/A extends StandardMBean
2N/A implements TestInterface, NonImmutableInfo {
2N/A public TestClass2(boolean mxbean) {
2N/A super(TestInterface.class, mxbean);
2N/A }
2N/A protected MBeanInfo getCachedMBeanInfo() {
2N/A return super.getCachedMBeanInfo();
2N/A }
2N/A }
2N/A
2N/A public static class TestClass3
2N/A extends StandardMBean
2N/A implements TestInterface, NonImmutableInfo {
2N/A public TestClass3(boolean mxbean) {
2N/A super(TestInterface.class, mxbean);
2N/A }
2N/A public MBeanInfo getMBeanInfo() {
2N/A return super.getMBeanInfo();
2N/A }
2N/A }
2N/A
2N/A public static class TestClass4
2N/A extends StandardMBean
2N/A implements TestInterface, ImmutableInfo {
2N/A public TestClass4(boolean mxbean) {
2N/A super(TestInterface.class, mxbean);
2N/A }
2N/A public MBeanNotificationInfo[] getNotificationInfo() {
2N/A return new MBeanNotificationInfo[0];
2N/A }
2N/A }
2N/A
2N/A public static class TestClass5
2N/A extends StandardEmitterMBean
2N/A implements TestInterface, ImmutableInfo {
2N/A public TestClass5(boolean mxbean) {
2N/A super(TestInterface.class, mxbean,
2N/A new NotificationBroadcasterSupport());
2N/A }
2N/A }
2N/A
2N/A public static class TestClass6
2N/A extends StandardEmitterMBean
2N/A implements TestInterface, NonImmutableInfo {
2N/A public TestClass6(boolean mxbean) {
2N/A super(TestInterface.class, mxbean,
2N/A new NotificationBroadcasterSupport());
2N/A }
2N/A protected void cacheMBeanInfo(MBeanInfo info) {
2N/A super.cacheMBeanInfo(info);
2N/A }
2N/A }
2N/A
2N/A public static class TestClass7
2N/A extends StandardEmitterMBean
2N/A implements TestInterface, NonImmutableInfo {
2N/A public TestClass7(boolean mxbean) {
2N/A super(TestInterface.class, mxbean,
2N/A new NotificationBroadcasterSupport());
2N/A }
2N/A protected MBeanInfo getCachedMBeanInfo() {
2N/A return super.getCachedMBeanInfo();
2N/A }
2N/A }
2N/A
2N/A public static class TestClass8
2N/A extends StandardEmitterMBean
2N/A implements TestInterface, NonImmutableInfo {
2N/A public TestClass8(boolean mxbean) {
2N/A super(TestInterface.class, mxbean,
2N/A new NotificationBroadcasterSupport());
2N/A }
2N/A public MBeanInfo getMBeanInfo() {
2N/A return super.getMBeanInfo();
2N/A }
2N/A }
2N/A
2N/A public static class TestClass9
2N/A extends StandardEmitterMBean
2N/A implements TestInterface, NonImmutableInfo {
2N/A public TestClass9(boolean mxbean) {
2N/A super(TestInterface.class, mxbean,
2N/A new NotificationBroadcasterSupport());
2N/A }
2N/A public MBeanNotificationInfo[] getNotificationInfo() {
2N/A return new MBeanNotificationInfo[0];
2N/A }
2N/A }
2N/A
2N/A public static void main(String[] args) throws Exception {
2N/A
2N/A int error = 0;
2N/A
2N/A // Instantiate the MBean server
2N/A //
2N/A echo("\n>>> Create the MBean server");
2N/A MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
2N/A
2N/A // Get default domain
2N/A //
2N/A echo("\n>>> Get the MBean server's default domain");
2N/A String domain = mbs.getDefaultDomain();
2N/A echo("\tDefault Domain = " + domain);
2N/A
2N/A for (int i = 0; i < testInstances.length; i++) {
2N/A // Create and register the TestClass MBean
2N/A //
2N/A String cn = testInstances[i].getClass().getName();
2N/A String ons = domain + ":type=" + cn + ",name=" + i;
2N/A echo("\n>>> Create the " + cn +
2N/A " MBean within the MBeanServer");
2N/A echo("\tObjectName = " + ons);
2N/A ObjectName on = ObjectName.getInstance(ons);
2N/A mbs.registerMBean(testInstances[i], on);
2N/A
2N/A // Check immutableInfo field in descriptor
2N/A //
2N/A MBeanInfo mbi = mbs.getMBeanInfo(on);
2N/A Descriptor d = mbi.getDescriptor();
2N/A echo("MBeanInfo descriptor = " + d);
2N/A if (d == null || d.getFieldNames().length == 0) {
2N/A error++;
2N/A echo("Descriptor is null or empty");
2N/A continue;
2N/A }
2N/A if (testInstances[i] instanceof ImmutableInfo) {
2N/A if ("true".equals(d.getFieldValue("immutableInfo"))) {
2N/A echo("OK: immutableInfo field is true");
2N/A } else {
2N/A echo("KO: immutableInfo field should be true");
2N/A error++;
2N/A }
2N/A continue;
2N/A }
2N/A if (testInstances[i] instanceof NonImmutableInfo) {
2N/A if ("false".equals(d.getFieldValue("immutableInfo"))) {
2N/A echo("OK: immutableInfo field is false");
2N/A } else {
2N/A echo("KO: immutableInfo field should be false");
2N/A error++;
2N/A }
2N/A continue;
2N/A }
2N/A }
2N/A
2N/A if (error > 0) {
2N/A echo("\nTest failed! " + error + " errors.\n");
2N/A throw new IllegalArgumentException("Test failed");
2N/A } else {
2N/A echo("\nTest passed!\n");
2N/A }
2N/A }
2N/A
2N/A private static void echo(String msg) {
2N/A System.out.println(msg);
2N/A }
2N/A}
2N/A