Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/management/mxbean/StandardMBeanOverrideTest.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 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* @run clean StandardMBeanOverrideTest32* @run build StandardMBeanOverrideTest33* @run main StandardMBeanOverrideTest34*/3536import java.io.*;37import java.lang.management.*;38import javax.management.*;39import javax.management.openmbean.*;4041public class StandardMBeanOverrideTest {4243private static Object testInstances[] = {44new TestClass0(false),45new TestClass1(false),46new TestClass2(false),47new TestClass3(false),48new TestClass4(false),49new TestClass5(false),50new TestClass6(false),51new TestClass7(false),52new TestClass8(false),53new TestClass9(false),54new TestClass0(true),55new TestClass1(true),56new TestClass2(true),57new TestClass3(true),58new TestClass4(true),59new TestClass5(true),60new TestClass6(true),61new TestClass7(true),62new TestClass8(true),63new TestClass9(true),64};6566public interface ImmutableInfo {67}6869public interface NonImmutableInfo {70}7172public interface TestInterface {73}7475public static class TestClass076extends StandardMBean77implements TestInterface, ImmutableInfo {78public TestClass0(boolean mxbean) {79super(TestInterface.class, mxbean);80}81}8283public static class TestClass184extends StandardMBean85implements TestInterface, NonImmutableInfo {86public TestClass1(boolean mxbean) {87super(TestInterface.class, mxbean);88}89protected void cacheMBeanInfo(MBeanInfo info) {90super.cacheMBeanInfo(info);91}92}9394public static class TestClass295extends StandardMBean96implements TestInterface, NonImmutableInfo {97public TestClass2(boolean mxbean) {98super(TestInterface.class, mxbean);99}100protected MBeanInfo getCachedMBeanInfo() {101return super.getCachedMBeanInfo();102}103}104105public static class TestClass3106extends StandardMBean107implements TestInterface, NonImmutableInfo {108public TestClass3(boolean mxbean) {109super(TestInterface.class, mxbean);110}111public MBeanInfo getMBeanInfo() {112return super.getMBeanInfo();113}114}115116public static class TestClass4117extends StandardMBean118implements TestInterface, ImmutableInfo {119public TestClass4(boolean mxbean) {120super(TestInterface.class, mxbean);121}122public MBeanNotificationInfo[] getNotificationInfo() {123return new MBeanNotificationInfo[0];124}125}126127public static class TestClass5128extends StandardEmitterMBean129implements TestInterface, ImmutableInfo {130public TestClass5(boolean mxbean) {131super(TestInterface.class, mxbean,132new NotificationBroadcasterSupport());133}134}135136public static class TestClass6137extends StandardEmitterMBean138implements TestInterface, NonImmutableInfo {139public TestClass6(boolean mxbean) {140super(TestInterface.class, mxbean,141new NotificationBroadcasterSupport());142}143protected void cacheMBeanInfo(MBeanInfo info) {144super.cacheMBeanInfo(info);145}146}147148public static class TestClass7149extends StandardEmitterMBean150implements TestInterface, NonImmutableInfo {151public TestClass7(boolean mxbean) {152super(TestInterface.class, mxbean,153new NotificationBroadcasterSupport());154}155protected MBeanInfo getCachedMBeanInfo() {156return super.getCachedMBeanInfo();157}158}159160public static class TestClass8161extends StandardEmitterMBean162implements TestInterface, NonImmutableInfo {163public TestClass8(boolean mxbean) {164super(TestInterface.class, mxbean,165new NotificationBroadcasterSupport());166}167public MBeanInfo getMBeanInfo() {168return super.getMBeanInfo();169}170}171172public static class TestClass9173extends StandardEmitterMBean174implements TestInterface, NonImmutableInfo {175public TestClass9(boolean mxbean) {176super(TestInterface.class, mxbean,177new NotificationBroadcasterSupport());178}179public MBeanNotificationInfo[] getNotificationInfo() {180return new MBeanNotificationInfo[0];181}182}183184public static void main(String[] args) throws Exception {185186int error = 0;187188// Instantiate the MBean server189//190echo("\n>>> Create the MBean server");191MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();192193// Get default domain194//195echo("\n>>> Get the MBean server's default domain");196String domain = mbs.getDefaultDomain();197echo("\tDefault Domain = " + domain);198199for (int i = 0; i < testInstances.length; i++) {200// Create and register the TestClass MBean201//202String cn = testInstances[i].getClass().getName();203String ons = domain + ":type=" + cn + ",name=" + i;204echo("\n>>> Create the " + cn +205" MBean within the MBeanServer");206echo("\tObjectName = " + ons);207ObjectName on = ObjectName.getInstance(ons);208mbs.registerMBean(testInstances[i], on);209210// Check immutableInfo field in descriptor211//212MBeanInfo mbi = mbs.getMBeanInfo(on);213Descriptor d = mbi.getDescriptor();214echo("MBeanInfo descriptor = " + d);215if (d == null || d.getFieldNames().length == 0) {216error++;217echo("Descriptor is null or empty");218continue;219}220if (testInstances[i] instanceof ImmutableInfo) {221if ("true".equals(d.getFieldValue("immutableInfo"))) {222echo("OK: immutableInfo field is true");223} else {224echo("KO: immutableInfo field should be true");225error++;226}227continue;228}229if (testInstances[i] instanceof NonImmutableInfo) {230if ("false".equals(d.getFieldValue("immutableInfo"))) {231echo("OK: immutableInfo field is false");232} else {233echo("KO: immutableInfo field should be false");234error++;235}236continue;237}238}239240if (error > 0) {241echo("\nTest failed! " + error + " errors.\n");242throw new IllegalArgumentException("Test failed");243} else {244echo("\nTest passed!\n");245}246}247248private static void echo(String msg) {249System.out.println(msg);250}251}252253254