Path: blob/master/test/jdk/javax/management/mxbean/StandardMBeanOverrideTest.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 628304526* @summary Test the correctness of immutableInfo field in MBeanInfo descriptor27* when overriding the methods cacheMBeanInfo, getCachedMBeanInfo,28* getMBeanInfo and getNotificationInfo in StandardMBean and29* StandardEmitterMBean.30* @author Luis-Miguel Alventosa31*32* @run clean StandardMBeanOverrideTest33* @run build StandardMBeanOverrideTest34* @run main StandardMBeanOverrideTest35*/3637import java.io.*;38import java.lang.management.*;39import javax.management.*;40import javax.management.openmbean.*;4142public class StandardMBeanOverrideTest {4344private static Object testInstances[] = {45new TestClass0(false),46new TestClass1(false),47new TestClass2(false),48new TestClass3(false),49new TestClass4(false),50new TestClass5(false),51new TestClass6(false),52new TestClass7(false),53new TestClass8(false),54new TestClass9(false),55new TestClass0(true),56new TestClass1(true),57new TestClass2(true),58new TestClass3(true),59new TestClass4(true),60new TestClass5(true),61new TestClass6(true),62new TestClass7(true),63new TestClass8(true),64new TestClass9(true),65};6667public interface ImmutableInfo {68}6970public interface NonImmutableInfo {71}7273public interface TestInterface {74}7576public static class TestClass077extends StandardMBean78implements TestInterface, ImmutableInfo {79public TestClass0(boolean mxbean) {80super(TestInterface.class, mxbean);81}82}8384public static class TestClass185extends StandardMBean86implements TestInterface, NonImmutableInfo {87public TestClass1(boolean mxbean) {88super(TestInterface.class, mxbean);89}90protected void cacheMBeanInfo(MBeanInfo info) {91super.cacheMBeanInfo(info);92}93}9495public static class TestClass296extends StandardMBean97implements TestInterface, NonImmutableInfo {98public TestClass2(boolean mxbean) {99super(TestInterface.class, mxbean);100}101protected MBeanInfo getCachedMBeanInfo() {102return super.getCachedMBeanInfo();103}104}105106public static class TestClass3107extends StandardMBean108implements TestInterface, NonImmutableInfo {109public TestClass3(boolean mxbean) {110super(TestInterface.class, mxbean);111}112public MBeanInfo getMBeanInfo() {113return super.getMBeanInfo();114}115}116117public static class TestClass4118extends StandardMBean119implements TestInterface, ImmutableInfo {120public TestClass4(boolean mxbean) {121super(TestInterface.class, mxbean);122}123public MBeanNotificationInfo[] getNotificationInfo() {124return new MBeanNotificationInfo[0];125}126}127128public static class TestClass5129extends StandardEmitterMBean130implements TestInterface, ImmutableInfo {131public TestClass5(boolean mxbean) {132super(TestInterface.class, mxbean,133new NotificationBroadcasterSupport());134}135}136137public static class TestClass6138extends StandardEmitterMBean139implements TestInterface, NonImmutableInfo {140public TestClass6(boolean mxbean) {141super(TestInterface.class, mxbean,142new NotificationBroadcasterSupport());143}144protected void cacheMBeanInfo(MBeanInfo info) {145super.cacheMBeanInfo(info);146}147}148149public static class TestClass7150extends StandardEmitterMBean151implements TestInterface, NonImmutableInfo {152public TestClass7(boolean mxbean) {153super(TestInterface.class, mxbean,154new NotificationBroadcasterSupport());155}156protected MBeanInfo getCachedMBeanInfo() {157return super.getCachedMBeanInfo();158}159}160161public static class TestClass8162extends StandardEmitterMBean163implements TestInterface, NonImmutableInfo {164public TestClass8(boolean mxbean) {165super(TestInterface.class, mxbean,166new NotificationBroadcasterSupport());167}168public MBeanInfo getMBeanInfo() {169return super.getMBeanInfo();170}171}172173public static class TestClass9174extends StandardEmitterMBean175implements TestInterface, NonImmutableInfo {176public TestClass9(boolean mxbean) {177super(TestInterface.class, mxbean,178new NotificationBroadcasterSupport());179}180public MBeanNotificationInfo[] getNotificationInfo() {181return new MBeanNotificationInfo[0];182}183}184185public static void main(String[] args) throws Exception {186187int error = 0;188189// Instantiate the MBean server190//191echo("\n>>> Create the MBean server");192MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();193194// Get default domain195//196echo("\n>>> Get the MBean server's default domain");197String domain = mbs.getDefaultDomain();198echo("\tDefault Domain = " + domain);199200for (int i = 0; i < testInstances.length; i++) {201// Create and register the TestClass MBean202//203String cn = testInstances[i].getClass().getName();204String ons = domain + ":type=" + cn + ",name=" + i;205echo("\n>>> Create the " + cn +206" MBean within the MBeanServer");207echo("\tObjectName = " + ons);208ObjectName on = ObjectName.getInstance(ons);209mbs.registerMBean(testInstances[i], on);210211// Check immutableInfo field in descriptor212//213MBeanInfo mbi = mbs.getMBeanInfo(on);214Descriptor d = mbi.getDescriptor();215echo("MBeanInfo descriptor = " + d);216if (d == null || d.getFieldNames().length == 0) {217error++;218echo("Descriptor is null or empty");219continue;220}221if (testInstances[i] instanceof ImmutableInfo) {222if ("true".equals(d.getFieldValue("immutableInfo"))) {223echo("OK: immutableInfo field is true");224} else {225echo("KO: immutableInfo field should be true");226error++;227}228continue;229}230if (testInstances[i] instanceof NonImmutableInfo) {231if ("false".equals(d.getFieldValue("immutableInfo"))) {232echo("OK: immutableInfo field is false");233} else {234echo("KO: immutableInfo field should be false");235error++;236}237continue;238}239}240241if (error > 0) {242echo("\nTest failed! " + error + " errors.\n");243throw new IllegalArgumentException("Test failed");244} else {245echo("\nTest passed!\n");246}247}248249private static void echo(String msg) {250System.out.println(msg);251}252}253254255