Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/management/Introspector/ImmutableNotificationInfoTest.java
38839 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 629585926* @summary Check that a StandardMBean has immutableInfo=true if it is27* a NotificationBroadcasterSupport that doesn't override getNotificationInfo()28* @author Eamonn McManus29* @run clean ImmutableNotificationInfoTest30* @run build ImmutableNotificationInfoTest31* @run main ImmutableNotificationInfoTest32*/33import javax.management.Descriptor;34import javax.management.MBeanInfo;35import javax.management.MBeanNotificationInfo;36import javax.management.MBeanServer;37import javax.management.MBeanServerFactory;38import javax.management.NotificationBroadcaster;39import javax.management.NotificationBroadcasterSupport;40import javax.management.NotificationFilter;41import javax.management.NotificationListener;42import javax.management.ObjectName;4344public class ImmutableNotificationInfoTest {45public interface UserBroadcasterMBean {}46public interface NoOverrideNBSMBean {}47public interface OverrideNBSMBean {}4849public static class UserBroadcaster50implements UserBroadcasterMBean, NotificationBroadcaster {51public void removeNotificationListener(NotificationListener listener) {52}5354public void addNotificationListener(NotificationListener listener,55NotificationFilter filter, Object handback) {56}5758public MBeanNotificationInfo[] getNotificationInfo() {59return new MBeanNotificationInfo[0];60}61}6263public static class NoOverrideNBS extends NotificationBroadcasterSupport64implements NoOverrideNBSMBean {65}6667public static class OverrideNBS extends NotificationBroadcasterSupport68implements OverrideNBSMBean {69public MBeanNotificationInfo[] getNotificationInfo() {70return new MBeanNotificationInfo[0];71}72}7374public static void main(String[] args) throws Exception {75boolean ok;76ok = test(new UserBroadcaster(), false);77ok &= test(new NoOverrideNBS(), true);78ok &= test(new OverrideNBS(), false);79if (!ok)80throw new Exception("TEST FAILED: immutability incorrect");81}8283private static boolean test(Object mbean, boolean expectImmutable)84throws Exception {85MBeanServer mbs = MBeanServerFactory.newMBeanServer();86ObjectName on = new ObjectName("a:b=c");87mbs.registerMBean(mbean, on);88MBeanInfo mbi = mbs.getMBeanInfo(on);89Descriptor d = mbi.getDescriptor();90String immutableValue = (String) d.getFieldValue("immutableInfo");91boolean immutable = ("true".equals(immutableValue));92if (immutable != expectImmutable) {93System.out.println("FAILED: " + mbean.getClass().getName() +94" -> " + immutableValue);95return false;96} else {97System.out.println("OK: " + mbean.getClass().getName());98return true;99}100}101}102103104