Path: blob/master/test/jdk/javax/management/Introspector/ImmutableNotificationInfoTest.java
41152 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 629585926* @summary Check that a StandardMBean has immutableInfo=true if it is27* a NotificationBroadcasterSupport that doesn't override getNotificationInfo()28* @author Eamonn McManus29*30* @run clean ImmutableNotificationInfoTest31* @run build ImmutableNotificationInfoTest32* @run main ImmutableNotificationInfoTest33*/34import javax.management.Descriptor;35import javax.management.MBeanInfo;36import javax.management.MBeanNotificationInfo;37import javax.management.MBeanServer;38import javax.management.MBeanServerFactory;39import javax.management.NotificationBroadcaster;40import javax.management.NotificationBroadcasterSupport;41import javax.management.NotificationFilter;42import javax.management.NotificationListener;43import javax.management.ObjectName;4445public class ImmutableNotificationInfoTest {46public interface UserBroadcasterMBean {}47public interface NoOverrideNBSMBean {}48public interface OverrideNBSMBean {}4950public static class UserBroadcaster51implements UserBroadcasterMBean, NotificationBroadcaster {52public void removeNotificationListener(NotificationListener listener) {53}5455public void addNotificationListener(NotificationListener listener,56NotificationFilter filter, Object handback) {57}5859public MBeanNotificationInfo[] getNotificationInfo() {60return new MBeanNotificationInfo[0];61}62}6364public static class NoOverrideNBS extends NotificationBroadcasterSupport65implements NoOverrideNBSMBean {66}6768public static class OverrideNBS extends NotificationBroadcasterSupport69implements OverrideNBSMBean {70public MBeanNotificationInfo[] getNotificationInfo() {71return new MBeanNotificationInfo[0];72}73}7475public static void main(String[] args) throws Exception {76boolean ok;77ok = test(new UserBroadcaster(), false);78ok &= test(new NoOverrideNBS(), true);79ok &= test(new OverrideNBS(), false);80if (!ok)81throw new Exception("TEST FAILED: immutability incorrect");82}8384private static boolean test(Object mbean, boolean expectImmutable)85throws Exception {86MBeanServer mbs = MBeanServerFactory.newMBeanServer();87ObjectName on = new ObjectName("a:b=c");88mbs.registerMBean(mbean, on);89MBeanInfo mbi = mbs.getMBeanInfo(on);90Descriptor d = mbi.getDescriptor();91String immutableValue = (String) d.getFieldValue("immutableInfo");92boolean immutable = ("true".equals(immutableValue));93if (immutable != expectImmutable) {94System.out.println("FAILED: " + mbean.getClass().getName() +95" -> " + immutableValue);96return false;97} else {98System.out.println("OK: " + mbean.getClass().getName());99return true;100}101}102}103104105