jdk/test/javax/management/MBeanServer/MBeanServerNotificationTest.java
author lana
Thu, 04 Nov 2010 15:39:43 -0700
changeset 7205 e6560614a608
parent 5506 202f599c92aa
child 30376 2ccf2cf7ea48
permissions -rw-r--r--
Merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1003
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1003
diff changeset
     2
 * Copyright (c) 2008, Oracle and/or its affiliates. All rights reserved.
1003
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
     4
 *
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
     7
 * published by the Free Software Foundation.
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
     8
 *
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
    13
 * accompanied this code).
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
    14
 *
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1003
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1003
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1003
diff changeset
    21
 * questions.
1003
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
    22
 */
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
    23
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
    24
/*
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
    25
 * @test
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
    26
 * @bug 6689505
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
    27
 * @summary Checks that MBeanServerNotification.toString contains the
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
    28
 *          MBean name.
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
    29
 * @author Daniel Fuchs
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
    30
 * @compile MBeanServerNotificationTest.java
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
    31
 * @run main MBeanServerNotificationTest
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
    32
 */
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
    33
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
    34
import com.sun.jmx.mbeanserver.Util;
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
    35
import javax.management.*;
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
    36
import java.util.concurrent.*;
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
    37
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
    38
public class MBeanServerNotificationTest {
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
    39
    final static String[] names = {
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
    40
        ":type=Wombat", "wombat:type=Wombat",null,
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
    41
    };
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
    42
    public static void main(String[] args) throws Exception {
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
    43
        System.out.println("Test that MBeanServerNotification.toString " +
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
    44
                "contains the name of the MBean being registered " +
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
    45
                "or unregistered.");
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
    46
        int failures = 0;
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
    47
        final MBeanServer mbs = MBeanServerFactory.createMBeanServer();
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
    48
        for (String str:names) {
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
    49
            try {
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
    50
                final ObjectName name = (str==null)?null:new ObjectName(str);
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
    51
                failures+=test(mbs, name, name!=null);
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
    52
            } catch(Exception x) {
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
    53
                x.printStackTrace(System.out);
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
    54
                System.out.println("Test failed for: "+str);
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
    55
                failures++;
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
    56
            }
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
    57
        }
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
    58
        if (failures == 0)
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
    59
            System.out.println("Test passed");
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
    60
        else {
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
    61
            System.out.println("TEST FAILED: " + failures + " failure(s)");
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
    62
            System.exit(1);
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
    63
        }
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
    64
    }
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
    65
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
    66
    private static enum Registration {
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
    67
        REGISTER(MBeanServerNotification.REGISTRATION_NOTIFICATION),
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
    68
        UNREGISTER(MBeanServerNotification.UNREGISTRATION_NOTIFICATION);
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
    69
        final String type;
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
    70
        private Registration(String type) {this.type = type;}
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
    71
        public int test(MBeanServerNotification n, ObjectName name) {
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
    72
            int failures = 0;
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
    73
            System.out.println("Testing: "+n);
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
    74
            if (!n.toString().endsWith("[type="+type+
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
    75
                "][message="+n.getMessage()+
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
    76
                "][mbeanName="+name+"]")) {
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
    77
                System.err.println("Test failed for "+ type+
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
    78
                   " ["+name+"]: "+n);
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
    79
                failures++;
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
    80
            }
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
    81
            return failures;
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
    82
        }
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
    83
        public MBeanServerNotification create(ObjectName name) {
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
    84
            return new MBeanServerNotification(type,
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
    85
                MBeanServerDelegate.DELEGATE_NAME, next(), name);
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
    86
        }
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
    87
        private static long next = 0;
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
    88
        private static synchronized long next() {return next++;}
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
    89
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
    90
    }
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
    91
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
    92
    private static int test(MBeanServer mbs, ObjectName name,
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
    93
                            boolean register)
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
    94
            throws Exception {
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
    95
        System.out.println("--------" + name + "--------");
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
    96
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
    97
        int failures = 0;
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
    98
        for (Registration reg : Registration.values()) {
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
    99
            failures = reg.test(reg.create(name), name);
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
   100
        }
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
   101
        if (!register) return failures;
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
   102
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
   103
        final ArrayBlockingQueue<Notification> queue =
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
   104
                new ArrayBlockingQueue<Notification>(10);
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
   105
        final NotificationListener listener = new NotificationListener() {
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
   106
            public void handleNotification(Notification notification,
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
   107
                    Object handback) {
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
   108
                try {
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
   109
                    queue.put(notification);
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
   110
                } catch(Exception x) {
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
   111
                    x.printStackTrace(System.out);
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
   112
                }
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
   113
            }
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
   114
        };
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
   115
        mbs.addNotificationListener(MBeanServerDelegate.DELEGATE_NAME,
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
   116
                listener, null, name);
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
   117
        final ObjectInstance oi = mbs.registerMBean(new Wombat(), name);
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
   118
        try {
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
   119
            failures+=Registration.REGISTER.test((MBeanServerNotification)
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
   120
                queue.poll(2, TimeUnit.SECONDS), oi.getObjectName());
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
   121
        } finally {
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
   122
            mbs.unregisterMBean(oi.getObjectName());
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
   123
            failures+=Registration.UNREGISTER.test((MBeanServerNotification)
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
   124
                queue.poll(2, TimeUnit.SECONDS), oi.getObjectName());
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
   125
        }
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
   126
        return failures;
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
   127
    }
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
   128
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
   129
    public static interface WombatMBean {}
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
   130
    public static class Wombat implements WombatMBean {}
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
   131
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
   132
}