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 6283045
0N/A * @summary Test the correctness of immutableInfo field in MBeanInfo descriptor
0N/A * when overriding the methods cacheMBeanInfo, getCachedMBeanInfo,
0N/A * getMBeanInfo and getNotificationInfo in StandardMBean and
0N/A * StandardEmitterMBean.
0N/A * @author Luis-Miguel Alventosa
0N/A * @run clean StandardMBeanOverrideTest
0N/A * @run build StandardMBeanOverrideTest
0N/A * @run main StandardMBeanOverrideTest
0N/A */
0N/A
0N/Aimport java.io.*;
0N/Aimport java.lang.management.*;
0N/Aimport javax.management.*;
0N/Aimport javax.management.openmbean.*;
0N/A
0N/Apublic class StandardMBeanOverrideTest {
0N/A
0N/A private static Object testInstances[] = {
0N/A new TestClass0(false),
0N/A new TestClass1(false),
0N/A new TestClass2(false),
0N/A new TestClass3(false),
0N/A new TestClass4(false),
0N/A new TestClass5(false),
0N/A new TestClass6(false),
0N/A new TestClass7(false),
0N/A new TestClass8(false),
0N/A new TestClass9(false),
0N/A new TestClass0(true),
0N/A new TestClass1(true),
0N/A new TestClass2(true),
0N/A new TestClass3(true),
0N/A new TestClass4(true),
0N/A new TestClass5(true),
0N/A new TestClass6(true),
0N/A new TestClass7(true),
0N/A new TestClass8(true),
0N/A new TestClass9(true),
0N/A };
0N/A
0N/A public interface ImmutableInfo {
0N/A }
0N/A
0N/A public interface NonImmutableInfo {
0N/A }
0N/A
0N/A public interface TestInterface {
0N/A }
0N/A
0N/A public static class TestClass0
0N/A extends StandardMBean
0N/A implements TestInterface, ImmutableInfo {
0N/A public TestClass0(boolean mxbean) {
0N/A super(TestInterface.class, mxbean);
0N/A }
0N/A }
0N/A
0N/A public static class TestClass1
0N/A extends StandardMBean
0N/A implements TestInterface, NonImmutableInfo {
0N/A public TestClass1(boolean mxbean) {
0N/A super(TestInterface.class, mxbean);
0N/A }
0N/A protected void cacheMBeanInfo(MBeanInfo info) {
0N/A super.cacheMBeanInfo(info);
0N/A }
0N/A }
0N/A
0N/A public static class TestClass2
0N/A extends StandardMBean
0N/A implements TestInterface, NonImmutableInfo {
0N/A public TestClass2(boolean mxbean) {
0N/A super(TestInterface.class, mxbean);
0N/A }
0N/A protected MBeanInfo getCachedMBeanInfo() {
0N/A return super.getCachedMBeanInfo();
0N/A }
0N/A }
0N/A
0N/A public static class TestClass3
0N/A extends StandardMBean
0N/A implements TestInterface, NonImmutableInfo {
0N/A public TestClass3(boolean mxbean) {
0N/A super(TestInterface.class, mxbean);
0N/A }
0N/A public MBeanInfo getMBeanInfo() {
0N/A return super.getMBeanInfo();
0N/A }
0N/A }
0N/A
0N/A public static class TestClass4
0N/A extends StandardMBean
0N/A implements TestInterface, ImmutableInfo {
0N/A public TestClass4(boolean mxbean) {
0N/A super(TestInterface.class, mxbean);
0N/A }
0N/A public MBeanNotificationInfo[] getNotificationInfo() {
0N/A return new MBeanNotificationInfo[0];
0N/A }
0N/A }
0N/A
0N/A public static class TestClass5
0N/A extends StandardEmitterMBean
0N/A implements TestInterface, ImmutableInfo {
0N/A public TestClass5(boolean mxbean) {
0N/A super(TestInterface.class, mxbean,
0N/A new NotificationBroadcasterSupport());
0N/A }
0N/A }
0N/A
0N/A public static class TestClass6
0N/A extends StandardEmitterMBean
0N/A implements TestInterface, NonImmutableInfo {
0N/A public TestClass6(boolean mxbean) {
0N/A super(TestInterface.class, mxbean,
0N/A new NotificationBroadcasterSupport());
0N/A }
0N/A protected void cacheMBeanInfo(MBeanInfo info) {
0N/A super.cacheMBeanInfo(info);
0N/A }
0N/A }
0N/A
0N/A public static class TestClass7
0N/A extends StandardEmitterMBean
0N/A implements TestInterface, NonImmutableInfo {
0N/A public TestClass7(boolean mxbean) {
0N/A super(TestInterface.class, mxbean,
0N/A new NotificationBroadcasterSupport());
0N/A }
0N/A protected MBeanInfo getCachedMBeanInfo() {
0N/A return super.getCachedMBeanInfo();
0N/A }
0N/A }
0N/A
0N/A public static class TestClass8
0N/A extends StandardEmitterMBean
0N/A implements TestInterface, NonImmutableInfo {
0N/A public TestClass8(boolean mxbean) {
0N/A super(TestInterface.class, mxbean,
0N/A new NotificationBroadcasterSupport());
0N/A }
0N/A public MBeanInfo getMBeanInfo() {
0N/A return super.getMBeanInfo();
0N/A }
0N/A }
0N/A
0N/A public static class TestClass9
0N/A extends StandardEmitterMBean
0N/A implements TestInterface, NonImmutableInfo {
0N/A public TestClass9(boolean mxbean) {
0N/A super(TestInterface.class, mxbean,
0N/A new NotificationBroadcasterSupport());
0N/A }
0N/A public MBeanNotificationInfo[] getNotificationInfo() {
0N/A return new MBeanNotificationInfo[0];
0N/A }
0N/A }
0N/A
0N/A public static void main(String[] args) throws Exception {
0N/A
0N/A int error = 0;
0N/A
0N/A // Instantiate the MBean server
0N/A //
0N/A echo("\n>>> Create the MBean server");
0N/A MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
0N/A
0N/A // Get default domain
0N/A //
0N/A echo("\n>>> Get the MBean server's default domain");
0N/A String domain = mbs.getDefaultDomain();
0N/A echo("\tDefault Domain = " + domain);
0N/A
0N/A for (int i = 0; i < testInstances.length; i++) {
0N/A // Create and register the TestClass MBean
0N/A //
0N/A String cn = testInstances[i].getClass().getName();
0N/A String ons = domain + ":type=" + cn + ",name=" + i;
0N/A echo("\n>>> Create the " + cn +
0N/A " MBean within the MBeanServer");
0N/A echo("\tObjectName = " + ons);
0N/A ObjectName on = ObjectName.getInstance(ons);
0N/A mbs.registerMBean(testInstances[i], on);
0N/A
0N/A // Check immutableInfo field in descriptor
0N/A //
0N/A MBeanInfo mbi = mbs.getMBeanInfo(on);
0N/A Descriptor d = mbi.getDescriptor();
0N/A echo("MBeanInfo descriptor = " + d);
0N/A if (d == null || d.getFieldNames().length == 0) {
0N/A error++;
0N/A echo("Descriptor is null or empty");
0N/A continue;
0N/A }
0N/A if (testInstances[i] instanceof ImmutableInfo) {
0N/A if ("true".equals(d.getFieldValue("immutableInfo"))) {
0N/A echo("OK: immutableInfo field is true");
0N/A } else {
0N/A echo("KO: immutableInfo field should be true");
0N/A error++;
0N/A }
0N/A continue;
0N/A }
0N/A if (testInstances[i] instanceof NonImmutableInfo) {
0N/A if ("false".equals(d.getFieldValue("immutableInfo"))) {
0N/A echo("OK: immutableInfo field is false");
0N/A } else {
0N/A echo("KO: immutableInfo field should be false");
0N/A error++;
0N/A }
0N/A continue;
0N/A }
0N/A }
0N/A
0N/A if (error > 0) {
0N/A echo("\nTest failed! " + error + " errors.\n");
0N/A throw new IllegalArgumentException("Test failed");
0N/A } else {
0N/A echo("\nTest passed!\n");
0N/A }
0N/A }
0N/A
0N/A private static void echo(String msg) {
0N/A System.out.println(msg);
0N/A }
0N/A}