AnnotatedNotificationInfoTest.java 15.8 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
import javax.annotation.Resource;
import javax.management.AttributeChangeNotification;
import javax.management.Description;
import javax.management.Descriptor;
import javax.management.ImmutableDescriptor;
41
import javax.management.ListenerNotFoundException;
42 43 44 45 46
import javax.management.MBean;
import javax.management.MBeanInfo;
import javax.management.MBeanNotificationInfo;
import javax.management.MBeanServer;
import javax.management.MXBean;
47 48
import javax.management.Notification;
import javax.management.NotificationBroadcaster;
49
import javax.management.NotificationBroadcasterSupport;
50
import javax.management.NotificationFilter;
51 52
import javax.management.NotificationInfo;
import javax.management.NotificationInfos;
53
import javax.management.NotificationListener;
54 55 56 57
import javax.management.ObjectName;
import javax.management.SendNotification;

public class AnnotatedNotificationInfoTest {
58 59 60 61 62 63 64 65 66 67 68

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

    // Data for the first kind of test.  This tests that MBeanNotificationInfo
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 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
    // 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();

153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
    @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();

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 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
    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();

238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
    @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();
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271

    @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();

272 273 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 304 305 306 307 308 309 310 311 312 313
    // Test that @NotificationInfo and @NotificationInfos are ignored if
    // the MBean returns a non-empty MBeanNotificationInfo[] from its
    // NotificationBroadcaster.getNotifications() implementation.

    @NotificationInfo(types={"blim", "blam"})
    public static interface Explicit1MBean {}

    public static class Explicit1
            extends NotificationBroadcasterSupport implements Explicit1MBean {
        public Explicit1() {
            super(expected);
        }
    }

    private static Object mbeanExplicit1 = new Explicit1();

    @NotificationInfos(
        {
            @NotificationInfo(types="blim"), @NotificationInfo(types="blam")
        }
    )
    public static interface Explicit2MXBean {}

    public static class Explicit2
            implements NotificationBroadcaster, Explicit2MXBean {
        public void addNotificationListener(NotificationListener listener,
                NotificationFilter filter, Object handback) {}

        public void removeNotificationListener(NotificationListener listener)
                throws ListenerNotFoundException {}

        public MBeanNotificationInfo[] getNotificationInfo() {
            return new MBeanNotificationInfo[] {expected};
        }
    }

    // Data for the second kind of test.  This tests that @NotificationInfo is
    // ignored if the MBean is not a notification source.  Every static
    // field called ignoredMBean* is expected to be an MBean on which
    // isInstanceOf(NotificationBroadcaster.class.getName() is false,
    // addNotificationListener produces an exception, and the
    // MBeanNotificationInfo array is empty.
314 315 316 317 318 319 320 321 322 323 324 325
    @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;
        }
    }

326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392
    private static Object ignoredMBean1 = new Cache();

    @NotificationInfos(
        @NotificationInfo(types={"foo", "bar"})
    )
    public static interface Cache2MBean {
        public int getCachedNum();
    }

    public static class Cache2 implements Cache2MBean {
        public int getCachedNum() {
            return 0;
        }
    }

    private static Object ignoredMBean2 = new Cache2();

    private static final NotificationListener nullListener =
            new NotificationListener() {
                public void handleNotification(
                        Notification notification, Object handback) {}
            };

    // Test that inheriting inconsistent @NotificationInfo annotations is
    // an error, but not if they are overridden by a non-empty getNotifications()

    @NotificationInfo(types={"blim"})
    public static interface Inconsistent1 {}

    @NotificationInfo(types={"blam"})
    public static interface Inconsistent2 {}

    public static interface InconsistentMBean extends Inconsistent1, Inconsistent2 {}

    public static class Inconsistent
            extends NotificationBroadcasterSupport implements InconsistentMBean {}

    public static class Consistent
            extends Inconsistent implements NotificationBroadcaster {
        public void addNotificationListener(NotificationListener listener,
                NotificationFilter filter, Object handback) {}

        public void removeNotificationListener(NotificationListener listener)
                throws ListenerNotFoundException {}

        public MBeanNotificationInfo[] getNotificationInfo() {
            return new MBeanNotificationInfo[] {expected};
        }
    }

    private static Object mbeanConsistent = new Consistent();

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

    public static class Consistent2
            extends NotificationBroadcasterSupport implements Consistent2MBean {}

    private static Object mbeanConsistent2 = new Consistent2();

393 394 395 396 397 398 399 400 401 402
    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");

        System.out.println("Testing MBeans...");
        for (Field mbeanField :
                AnnotatedNotificationInfoTest.class.getDeclaredFields()) {
403 404 405 406 407 408
            boolean notifier;
            if (mbeanField.getName().startsWith("mbean"))
                notifier = true;
            else if (mbeanField.getName().startsWith("ignoredMBean"))
                notifier = false;
            else
409 410 411 412 413 414
                continue;
            System.out.println("..." + mbeanField.getName());
            Object mbean = mbeanField.get(null);
            mbs.registerMBean(mbean, on);
            MBeanInfo mbi = mbs.getMBeanInfo(on);
            MBeanNotificationInfo[] mbnis = mbi.getNotifications();
415 416 417 418 419 420 421 422 423 424 425 426 427
            if (notifier) {
                assert mbnis.length == 1 : mbnis.length;
                assert mbnis[0].equals(expected) : mbnis[0];
            } else {
                assert mbnis.length == 0 : mbnis.length;
                assert !mbs.isInstanceOf(on, NotificationBroadcaster.class.getName());
                try {
                    mbs.addNotificationListener(on, nullListener, null, null);
                    assert false : "addNotificationListener works";
                } catch (Exception e) {
                    // OK: addNL correctly refused
                }
            }
428 429
            mbs.unregisterMBean(on);
        }
430

431 432 433 434 435 436 437 438 439 440
        // Test that inconsistent @NotificationInfo annotations produce an
        // error.
        try {
            mbs.registerMBean(new Inconsistent(), on);
            System.out.println(mbs.getMBeanInfo(on));
            assert false : "Inconsistent @NotificationInfo not detected";
        } catch (Exception e) {
            System.out.println(
                    "Inconsistent @NotificationInfo correctly produced " + e);
        }
441 442
    }
}