AnnotatedNotificationInfoTest.java 11.3 KB
Newer Older
1
/*
X
xdono 已提交
2
 * Copyright 2007-2008 Sun Microsystems, Inc.  All Rights Reserved.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
 * CA 95054 USA or visit www.sun.com if you need additional information or
 * have any questions.
 */

/*
25 26
 * @test
 * @bug 6323980 6772779
27 28 29 30 31 32 33 34
 * @summary Test @NotificationInfo annotation
 * @author Eamonn McManus
 * @run main/othervm -ea AnnotatedNotificationInfoTest
 */

import java.io.Serializable;
import java.lang.management.ManagementFactory;
import java.lang.reflect.Field;
35
import java.util.Arrays;
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
import javax.annotation.Resource;
import javax.management.AttributeChangeNotification;
import javax.management.Description;
import javax.management.Descriptor;
import javax.management.ImmutableDescriptor;
import javax.management.MBean;
import javax.management.MBeanInfo;
import javax.management.MBeanNotificationInfo;
import javax.management.MBeanServer;
import javax.management.MXBean;
import javax.management.NotificationBroadcasterSupport;
import javax.management.NotificationInfo;
import javax.management.NotificationInfos;
import javax.management.ObjectName;
import javax.management.SendNotification;

public class AnnotatedNotificationInfoTest {
    // Data for the first test.  This tests that MBeanNotificationInfo
    // is correctly derived from @NotificationInfo.
    // Every static field called mbean* is expected to be an MBean
    // with a single MBeanNotificationInfo that has the same value
    // in each case.

    @NotificationInfo(
            types = {"foo", "bar"},
            notificationClass = AttributeChangeNotification.class,
            description = @Description(
                value = "description",
                bundleBaseName = "bundle",
                key = "key"),
            descriptorFields = {"foo=bar"})
    public static interface Intf1MBean {}

    public static class Intf1
            extends NotificationBroadcasterSupport implements Intf1MBean {}

    private static Object mbeanIntf1 = new Intf1();

    @NotificationInfos(
        @NotificationInfo(
                types = {"foo", "bar"},
                notificationClass = AttributeChangeNotification.class,
                description = @Description(
                    value = "description",
                    bundleBaseName = "bundle",
                    key = "key"),
                descriptorFields = {"foo=bar"}))
    public static interface Intf2MBean {}

    public static class Intf2
            extends NotificationBroadcasterSupport implements Intf2MBean {}

    private static Object mbeanIntf2 = new Intf2();

    @NotificationInfos({})
    @NotificationInfo(
            types = {"foo", "bar"},
            notificationClass = AttributeChangeNotification.class,
            description = @Description(
                value = "description",
                bundleBaseName = "bundle",
                key = "key"),
            descriptorFields = {"foo=bar"})
    public static interface Intf3MBean {}

    public static class Intf3
            extends NotificationBroadcasterSupport implements Intf3MBean {}

    private static Object mbeanIntf3 = new Intf3();

    @NotificationInfo(
            types = {"foo", "bar"},
            notificationClass = AttributeChangeNotification.class,
            description = @Description(
                value = "description",
                bundleBaseName = "bundle",
                key = "key"),
            descriptorFields = {"foo=bar"})
    public static interface ParentIntf {}

    public static interface Intf4MBean extends Serializable, ParentIntf, Cloneable {}

    public static class Intf4
            extends NotificationBroadcasterSupport implements Intf4MBean {}

    private static Object mbeanIntf4 = new Intf4();

    @NotificationInfo(
            types = {"foo", "bar"},
            notificationClass = AttributeChangeNotification.class,
            description = @Description(
                value = "description",
                bundleBaseName = "bundle",
                key = "key"),
            descriptorFields = {"foo=bar"})
    public static interface Intf5MXBean {}

    public static class Intf5Impl
            extends NotificationBroadcasterSupport implements Intf5MXBean {}

    private static Object mbeanIntf5 = new Intf5Impl();

138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
    @NotificationInfo(
            types = {"foo", "bar"},
            notificationClass = AttributeChangeNotification.class,
            description = @Description(
                value = "description",
                bundleBaseName = "bundle",
                key = "key"),
            descriptorFields = {"foo=bar"})
    public static interface Intf6MBean {}

    public static class Intf6 implements Intf6MBean {
        @Resource
        private volatile SendNotification send;
    }

    private static Object mbeanIntf6 = new Intf6();

155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
    public static interface Impl1MBean {}

    @NotificationInfo(
            types = {"foo", "bar"},
            notificationClass = AttributeChangeNotification.class,
            description = @Description(
                value = "description",
                bundleBaseName = "bundle",
                key = "key"),
            descriptorFields = {"foo=bar"})
    public static class Impl1
            extends NotificationBroadcasterSupport implements Impl1MBean {}

    private static Object mbeanImpl1 = new Impl1();

    @NotificationInfo(
            types = {"foo", "bar"},
            notificationClass = AttributeChangeNotification.class,
            description = @Description(
                value = "description",
                bundleBaseName = "bundle",
                key = "key"),
            descriptorFields = {"foo=bar"})
    public static class ParentImpl extends NotificationBroadcasterSupport {}

    public static interface Impl2MBean {}

    public static class Impl2 extends ParentImpl implements Impl2MBean {}

    private static Object mbeanImpl2 = new Impl2();

    public static interface Impl3MXBean {}

    @NotificationInfo(
            types = {"foo", "bar"},
            notificationClass = AttributeChangeNotification.class,
            description = @Description(
                value = "description",
                bundleBaseName = "bundle",
                key = "key"),
            descriptorFields = {"foo=bar"})
    public static class Impl3
            extends NotificationBroadcasterSupport implements Impl3MXBean {}

    private static Object mbeanImpl3 = new Impl3();

    public static class Impl4 extends ParentImpl implements Impl3MXBean {}

    private static Object mbeanImpl4 = new Impl4();

    @MBean
    @NotificationInfo(
            types = {"foo", "bar"},
            notificationClass = AttributeChangeNotification.class,
            description = @Description(
                value = "description",
                bundleBaseName = "bundle",
                key = "key"),
            descriptorFields = {"foo=bar"})
    public static class MBean1 extends NotificationBroadcasterSupport {}

    private static Object mbeanMBean1 = new MBean1();

    @MBean
    public static class MBean2 extends ParentImpl {}

    private static Object mbeanMBean2 = new MBean2();

223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
    @MBean
    @NotificationInfo(
            types = {"foo", "bar"},
            notificationClass = AttributeChangeNotification.class,
            description = @Description(
                value = "description",
                bundleBaseName = "bundle",
                key = "key"),
            descriptorFields = {"foo=bar"})
    public static class MBean3 {
        @Resource
        private volatile SendNotification send;
    }

    private static Object mbeanMBean3 = new MBean3();
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256

    @MXBean
    @NotificationInfo(
            types = {"foo", "bar"},
            notificationClass = AttributeChangeNotification.class,
            description = @Description(
                value = "description",
                bundleBaseName = "bundle",
                key = "key"),
            descriptorFields = {"foo=bar"})
    public static class MXBean1 extends NotificationBroadcasterSupport {}

    private static Object mbeanMXBean1 = new MXBean1();

    @MXBean
    public static class MXBean2 extends ParentImpl {}

    private static Object mbeanMXBean2 = new MXBean2();

257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
    // Classes for the second test.  This tests the simplest case, which is
    // the first example in the javadoc for @NotificationInfo.  Notice that
    // this MBean is not a NotificationBroadcaster and does not inject a
    // SendNotification!  That should possibly be an error, but it's currently
    // allowed by the spec.
    @NotificationInfo(types={"com.example.notifs.create",
                             "com.example.notifs.destroy"})
    public static interface CacheMBean {
        public int getCachedNum();
    }

    public static class Cache implements CacheMBean {
        public int getCachedNum() {
            return 0;
        }
    }

274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
    public static void main(String[] args) throws Exception {
        if (!AnnotatedNotificationInfoTest.class.desiredAssertionStatus())
            throw new Exception("Test must be run with -ea");

        MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
        ObjectName on = new ObjectName("a:b=c");

        Descriptor expectedDescriptor = new ImmutableDescriptor(
                "foo=bar", "descriptionResourceBundleBaseName=bundle",
                "descriptionResourceKey=key");
        MBeanNotificationInfo expected = new MBeanNotificationInfo(
                new String[] {"foo", "bar"},
                AttributeChangeNotification.class.getName(),
                "description",
                expectedDescriptor);

        System.out.println("Testing MBeans...");
        for (Field mbeanField :
                AnnotatedNotificationInfoTest.class.getDeclaredFields()) {
            if (!mbeanField.getName().startsWith("mbean"))
                continue;
            System.out.println("..." + mbeanField.getName());
            Object mbean = mbeanField.get(null);
            mbs.registerMBean(mbean, on);
            MBeanInfo mbi = mbs.getMBeanInfo(on);
            MBeanNotificationInfo[] mbnis = mbi.getNotifications();
            assert mbnis.length == 1 : mbnis.length;
            assert mbnis[0].equals(expected) : mbnis[0];
            mbs.unregisterMBean(on);
        }
304 305 306 307 308 309 310 311 312

        mbs.registerMBean(new Cache(), on);
        MBeanInfo mbi = mbs.getMBeanInfo(on);
        MBeanNotificationInfo[] mbnis = mbi.getNotifications();
        assert mbnis.length == 1 : mbnis.length;
        String[] types = mbnis[0].getNotifTypes();
        String[] expectedTypes =
                CacheMBean.class.getAnnotation(NotificationInfo.class).types();
        assert Arrays.equals(types, expectedTypes) : Arrays.toString(types);
313 314
    }
}