blob: 7ddbbed7c60f3377e1e900bfbe48cf8f63f9c844 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 2005 Sun Microsystems, Inc. All Rights Reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any questions.
22 */
23
24/*
25 * @test
26 * @bug 6295859
27 * @summary Check that a StandardMBean has immutableInfo=true if it is
28 * a NotificationBroadcasterSupport that doesn't override getNotificationInfo()
29 * @author Eamonn McManus
30 * @run clean ImmutableNotificationInfoTest
31 * @run build ImmutableNotificationInfoTest
32 * @run main ImmutableNotificationInfoTest
33 */
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;
44
45public class ImmutableNotificationInfoTest {
46 public interface UserBroadcasterMBean {}
47 public interface NoOverrideNBSMBean {}
48 public interface OverrideNBSMBean {}
49
50 public static class UserBroadcaster
51 implements UserBroadcasterMBean, NotificationBroadcaster {
52 public void removeNotificationListener(NotificationListener listener) {
53 }
54
55 public void addNotificationListener(NotificationListener listener,
56 NotificationFilter filter, Object handback) {
57 }
58
59 public MBeanNotificationInfo[] getNotificationInfo() {
60 return new MBeanNotificationInfo[0];
61 }
62 }
63
64 public static class NoOverrideNBS extends NotificationBroadcasterSupport
65 implements NoOverrideNBSMBean {
66 }
67
68 public static class OverrideNBS extends NotificationBroadcasterSupport
69 implements OverrideNBSMBean {
70 public MBeanNotificationInfo[] getNotificationInfo() {
71 return new MBeanNotificationInfo[0];
72 }
73 }
74
75 public static void main(String[] args) throws Exception {
76 boolean ok;
77 ok = test(new UserBroadcaster(), false);
78 ok &= test(new NoOverrideNBS(), true);
79 ok &= test(new OverrideNBS(), false);
80 if (!ok)
81 throw new Exception("TEST FAILED: immutability incorrect");
82 }
83
84 private static boolean test(Object mbean, boolean expectImmutable)
85 throws Exception {
86 MBeanServer mbs = MBeanServerFactory.newMBeanServer();
87 ObjectName on = new ObjectName("a:b=c");
88 mbs.registerMBean(mbean, on);
89 MBeanInfo mbi = mbs.getMBeanInfo(on);
90 Descriptor d = mbi.getDescriptor();
91 String immutableValue = (String) d.getFieldValue("immutableInfo");
92 boolean immutable = ("true".equals(immutableValue));
93 if (immutable != expectImmutable) {
94 System.out.println("FAILED: " + mbean.getClass().getName() +
95 " -> " + immutableValue);
96 return false;
97 } else {
98 System.out.println("OK: " + mbean.getClass().getName());
99 return true;
100 }
101 }
102}