jdk/test/javax/management/MBeanServer/MBeanServerNotificationTest.java
author ykantser
Thu, 07 May 2015 09:11:49 +0200
changeset 30376 2ccf2cf7ea48
parent 5506 202f599c92aa
permissions -rw-r--r--
8078896: Add @modules as needed to the jdk_svc tests Reviewed-by: alanb, mchung
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1003
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
     1
/*
30376
2ccf2cf7ea48 8078896: Add @modules as needed to the jdk_svc tests
ykantser
parents: 5506
diff changeset
     2
 * Copyright (c) 2008, 2015, 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
30376
2ccf2cf7ea48 8078896: Add @modules as needed to the jdk_svc tests
ykantser
parents: 5506
diff changeset
    30
 * @modules java.management/com.sun.jmx.mbeanserver
1003
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
    31
 * @compile MBeanServerNotificationTest.java
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
    32
 * @run main MBeanServerNotificationTest
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
    33
 */
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
    34
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
    35
import com.sun.jmx.mbeanserver.Util;
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
    36
import javax.management.*;
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
    37
import java.util.concurrent.*;
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
    38
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
    39
public class MBeanServerNotificationTest {
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
    40
    final static String[] names = {
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
    41
        ":type=Wombat", "wombat:type=Wombat",null,
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
    42
    };
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
    43
    public static void main(String[] args) throws Exception {
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
    44
        System.out.println("Test that MBeanServerNotification.toString " +
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
    45
                "contains the name of the MBean being registered " +
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
    46
                "or unregistered.");
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
    47
        int failures = 0;
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
    48
        final MBeanServer mbs = MBeanServerFactory.createMBeanServer();
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
    49
        for (String str:names) {
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
    50
            try {
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
    51
                final ObjectName name = (str==null)?null:new ObjectName(str);
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
    52
                failures+=test(mbs, name, name!=null);
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
    53
            } catch(Exception x) {
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
    54
                x.printStackTrace(System.out);
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
    55
                System.out.println("Test failed for: "+str);
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
    56
                failures++;
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
    57
            }
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
    58
        }
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
    59
        if (failures == 0)
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
    60
            System.out.println("Test passed");
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
    61
        else {
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
    62
            System.out.println("TEST FAILED: " + failures + " failure(s)");
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
    63
            System.exit(1);
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
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
    67
    private static enum Registration {
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
    68
        REGISTER(MBeanServerNotification.REGISTRATION_NOTIFICATION),
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
    69
        UNREGISTER(MBeanServerNotification.UNREGISTRATION_NOTIFICATION);
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
    70
        final String type;
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
    71
        private Registration(String type) {this.type = type;}
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
    72
        public int test(MBeanServerNotification n, ObjectName name) {
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
    73
            int failures = 0;
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
    74
            System.out.println("Testing: "+n);
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
    75
            if (!n.toString().endsWith("[type="+type+
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
    76
                "][message="+n.getMessage()+
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
    77
                "][mbeanName="+name+"]")) {
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
    78
                System.err.println("Test failed for "+ type+
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
    79
                   " ["+name+"]: "+n);
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
    80
                failures++;
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
    81
            }
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
    82
            return failures;
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
    83
        }
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
    84
        public MBeanServerNotification create(ObjectName name) {
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
    85
            return new MBeanServerNotification(type,
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
    86
                MBeanServerDelegate.DELEGATE_NAME, next(), name);
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
    87
        }
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
    88
        private static long next = 0;
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
    89
        private static synchronized long next() {return next++;}
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
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
    93
    private static int test(MBeanServer mbs, ObjectName name,
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
    94
                            boolean register)
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
    95
            throws Exception {
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
    96
        System.out.println("--------" + name + "--------");
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
    97
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
    98
        int failures = 0;
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
    99
        for (Registration reg : Registration.values()) {
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
   100
            failures = reg.test(reg.create(name), name);
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
   101
        }
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
   102
        if (!register) return failures;
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
   103
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
   104
        final ArrayBlockingQueue<Notification> queue =
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
   105
                new ArrayBlockingQueue<Notification>(10);
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
   106
        final NotificationListener listener = new NotificationListener() {
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
   107
            public void handleNotification(Notification notification,
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
   108
                    Object handback) {
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
   109
                try {
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
   110
                    queue.put(notification);
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
   111
                } catch(Exception x) {
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
   112
                    x.printStackTrace(System.out);
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
        };
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
   116
        mbs.addNotificationListener(MBeanServerDelegate.DELEGATE_NAME,
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
   117
                listener, null, name);
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
   118
        final ObjectInstance oi = mbs.registerMBean(new Wombat(), name);
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
   119
        try {
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
   120
            failures+=Registration.REGISTER.test((MBeanServerNotification)
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
   121
                queue.poll(2, TimeUnit.SECONDS), oi.getObjectName());
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
   122
        } finally {
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
   123
            mbs.unregisterMBean(oi.getObjectName());
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
   124
            failures+=Registration.UNREGISTER.test((MBeanServerNotification)
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
   125
                queue.poll(2, TimeUnit.SECONDS), oi.getObjectName());
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
   126
        }
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
   127
        return failures;
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
   128
    }
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
   129
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
   130
    public static interface WombatMBean {}
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
   131
    public static class Wombat implements WombatMBean {}
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
   132
b2f6b7e00c29 6689505: Improve MBeanServerNotification.toString
dfuchs
parents:
diff changeset
   133
}