Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/management/Introspector/ChangingNotifsTest.java
38838 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 617551726* @summary Check that Standard MBeans can change their MBeanNotificationInfo[]27* and MXBeans cannot28* @author Eamonn McManus29* @run clean ChangingNotifsTest30* @run build ChangingNotifsTest31* @run main ChangingNotifsTest32*/3334import javax.management.*;35import java.util.Arrays;3637public class ChangingNotifsTest {38public static interface EmptyMBean {}3940public static interface EmptyMXBean {}4142public static class Base extends NotificationBroadcasterSupport {43@Override44public MBeanNotificationInfo[] getNotificationInfo() {45MBeanNotificationInfo inf =46new MBeanNotificationInfo(new String[0],47Integer.toString(++called),48"description");49return new MBeanNotificationInfo[] {inf};50}5152private static int called;53}5455public static class Empty extends Base implements EmptyMBean {}5657public static class EmptyMX extends Base implements EmptyMXBean {}5859public static void main(String[] args) throws Exception {60MBeanServer mbs = MBeanServerFactory.newMBeanServer();61ObjectName name = new ObjectName("a:b=c");62for (boolean mx : new boolean[] {false, true})63test(mbs, name, mx);64if (failure != null)65throw new Exception(failure);66System.out.println("TEST PASSED");67}6869private static void test(MBeanServer mbs, ObjectName name, boolean mx)70throws Exception {71Object mbean = mx ? new EmptyMX() : new Empty();72String what = mx ? "MXBean" : "Standard MBean";73mbs.registerMBean(mbean, name);74try {75MBeanInfo mbi = mbs.getMBeanInfo(name);76Descriptor d = mbi.getDescriptor();77String immut = (String) d.getFieldValue("immutableInfo");78boolean immutable = (immut != null && immut.equals("true"));79if (immutable != mx) {80fail(what + " has immutableInfo=" + immut + ", should be " +81mx);82return;83}84MBeanNotificationInfo[] n1 = mbi.getNotifications().clone();85mbi = mbs.getMBeanInfo(name);86boolean unchanged = Arrays.deepEquals(mbi.getNotifications(), n1);87if (unchanged != mx) {88fail(what + " notif info " +89(unchanged ? "did not change" : "changed"));90return;91}92System.out.println("OK: " + what);93} finally {94mbs.unregisterMBean(name);95}96}9798private static void fail(String why) {99failure = "FAILED: " + why;100System.out.println(failure);101}102103private static String failure;104}105106107