1/*
2 * Copyright (c) 2005, 2015, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24/*
25 * @test
26 * @bug 6175517
27 * @summary Check that Standard MBeans can change their MBeanNotificationInfo[]
28 * and MXBeans cannot
29 * @author Eamonn McManus
30 *
31 * @run clean ChangingNotifsTest
32 * @run build ChangingNotifsTest
33 * @run main ChangingNotifsTest
34 */
35
36import javax.management.*;
37import java.util.Arrays;
38
39public class ChangingNotifsTest {
40    public static interface EmptyMBean {}
41
42    public static interface EmptyMXBean {}
43
44    public static class Base extends NotificationBroadcasterSupport {
45        @Override
46        public MBeanNotificationInfo[] getNotificationInfo() {
47            MBeanNotificationInfo inf =
48                new MBeanNotificationInfo(new String[0],
49                                          Integer.toString(++called),
50                                          "description");
51            return new MBeanNotificationInfo[] {inf};
52        }
53
54        private static int called;
55    }
56
57    public static class Empty extends Base implements EmptyMBean {}
58
59    public static class EmptyMX extends Base implements EmptyMXBean {}
60
61    public static void main(String[] args) throws Exception {
62        MBeanServer mbs = MBeanServerFactory.newMBeanServer();
63        ObjectName name = new ObjectName("a:b=c");
64        for (boolean mx : new boolean[] {false, true})
65            test(mbs, name, mx);
66        if (failure != null)
67            throw new Exception(failure);
68        System.out.println("TEST PASSED");
69    }
70
71    private static void test(MBeanServer mbs, ObjectName name, boolean mx)
72            throws Exception {
73        Object mbean = mx ? new EmptyMX() : new Empty();
74        String what = mx ? "MXBean" : "Standard MBean";
75        mbs.registerMBean(mbean, name);
76        try {
77            MBeanInfo mbi = mbs.getMBeanInfo(name);
78            Descriptor d = mbi.getDescriptor();
79            String immut = (String) d.getFieldValue("immutableInfo");
80            boolean immutable = (immut != null && immut.equals("true"));
81            if (immutable != mx) {
82                fail(what + " has immutableInfo=" + immut + ", should be " +
83                     mx);
84                return;
85            }
86            MBeanNotificationInfo[] n1 = mbi.getNotifications().clone();
87            mbi = mbs.getMBeanInfo(name);
88            boolean unchanged = Arrays.deepEquals(mbi.getNotifications(), n1);
89            if (unchanged != mx) {
90                fail(what + " notif info " +
91                     (unchanged ? "did not change" : "changed"));
92                return;
93            }
94            System.out.println("OK: " + what);
95        } finally {
96            mbs.unregisterMBean(name);
97        }
98    }
99
100    private static void fail(String why) {
101        failure = "FAILED: " + why;
102        System.out.println(failure);
103    }
104
105    private static String failure;
106}
107