Path: blob/master/test/jdk/javax/management/Introspector/ChangingNotifsTest.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 617551726* @summary Check that Standard MBeans can change their MBeanNotificationInfo[]27* and MXBeans cannot28* @author Eamonn McManus29*30* @run clean ChangingNotifsTest31* @run build ChangingNotifsTest32* @run main ChangingNotifsTest33*/3435import javax.management.*;36import java.util.Arrays;3738public class ChangingNotifsTest {39public static interface EmptyMBean {}4041public static interface EmptyMXBean {}4243public static class Base extends NotificationBroadcasterSupport {44@Override45public MBeanNotificationInfo[] getNotificationInfo() {46MBeanNotificationInfo inf =47new MBeanNotificationInfo(new String[0],48Integer.toString(++called),49"description");50return new MBeanNotificationInfo[] {inf};51}5253private static int called;54}5556public static class Empty extends Base implements EmptyMBean {}5758public static class EmptyMX extends Base implements EmptyMXBean {}5960public static void main(String[] args) throws Exception {61MBeanServer mbs = MBeanServerFactory.newMBeanServer();62ObjectName name = new ObjectName("a:b=c");63for (boolean mx : new boolean[] {false, true})64test(mbs, name, mx);65if (failure != null)66throw new Exception(failure);67System.out.println("TEST PASSED");68}6970private static void test(MBeanServer mbs, ObjectName name, boolean mx)71throws Exception {72Object mbean = mx ? new EmptyMX() : new Empty();73String what = mx ? "MXBean" : "Standard MBean";74mbs.registerMBean(mbean, name);75try {76MBeanInfo mbi = mbs.getMBeanInfo(name);77Descriptor d = mbi.getDescriptor();78String immut = (String) d.getFieldValue("immutableInfo");79boolean immutable = (immut != null && immut.equals("true"));80if (immutable != mx) {81fail(what + " has immutableInfo=" + immut + ", should be " +82mx);83return;84}85MBeanNotificationInfo[] n1 = mbi.getNotifications().clone();86mbi = mbs.getMBeanInfo(name);87boolean unchanged = Arrays.deepEquals(mbi.getNotifications(), n1);88if (unchanged != mx) {89fail(what + " notif info " +90(unchanged ? "did not change" : "changed"));91return;92}93System.out.println("OK: " + what);94} finally {95mbs.unregisterMBean(name);96}97}9899private static void fail(String why) {100failure = "FAILED: " + why;101System.out.println(failure);102}103104private static String failure;105}106107108