jdk/test/javax/management/remote/mandatory/notif/NotificationBufferCreationTest.java
author emcmanus
Wed, 21 Oct 2009 17:33:18 +0200
changeset 4156 acaa49a2768a
parent 1247 b4c26443dee5
child 5506 202f599c92aa
permissions -rw-r--r--
6851617: Remove JSR 255 (JMX API 2.0) from JDK 7 Summary: See http://weblogs.java.net/blog/2009/06/16/jsr-255-jmx-api-20-postponed Reviewed-by: dfuchs
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
4156
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1247
diff changeset
     2
 * Copyright 2003-2007 Sun Microsystems, Inc.  All Rights Reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @bug 4934236
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary Tests that NotificationBuffer is created when used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * @author jfd@...
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * @run clean NotificationBufferCreationTest NotificationSender
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * @run build NotificationBufferCreationTest
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * @run main NotificationBufferCreationTest
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.net.MalformedURLException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import javax.management.MBeanServerFactory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import javax.management.MBeanServer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import javax.management.MBeanServerConnection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import javax.management.NotificationListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import javax.management.ObjectName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import javax.management.Notification;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import javax.management.remote.JMXConnector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import javax.management.remote.JMXConnectorFactory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import javax.management.remote.JMXConnectorServer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import javax.management.remote.JMXConnectorServerFactory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import javax.management.remote.JMXServiceURL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
public class NotificationBufferCreationTest {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    private static final MBeanServer mbs =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        MBeanServerFactory.createMBeanServer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    private static final String[] protocols = {"rmi", "iiop", "jmxmp"};
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    public static void main(String[] args) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
            boolean error = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
            ObjectName notifierName =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
                new ObjectName("TestDomain:type=NotificationSender");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
            NotificationSender s = new NotificationSender();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
            mbs.registerMBean(s, notifierName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
            for(int i = 0; i < protocols.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
                    System.out.println("dotest for " + protocols[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
                    dotest(protocols[i], s, notifierName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
                }catch(Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
                    e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
                    error = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
            if(error)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
                System.exit(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            System.out.println("Test OK");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        }catch(Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            System.exit(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    private static void dotest(String protocol,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                               NotificationSender s,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                               ObjectName notifierName) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        JMXConnector client = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        JMXConnectorServer server = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        JMXServiceURL u = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            u = new JMXServiceURL(protocol, null, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            server =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                JMXConnectorServerFactory.newJMXConnectorServer(u,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                                                                null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                                                                mbs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            checkNotifier(s, 0, "new ConnectorServer");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            server.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            checkNotifier(s, 0, "ConnectorServer start");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            JMXServiceURL addr = server.getAddress();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            client = JMXConnectorFactory.newJMXConnector(addr, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            checkNotifier(s, 0, "new Connector");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            client.connect(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            checkNotifier(s, 0, "Connector connect");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            MBeanServerConnection mbsc = client.getMBeanServerConnection();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            final NotificationListener dummyListener =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                new NotificationListener() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                        public void handleNotification(Notification n,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                                                       Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                            // do nothing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            mbsc.addNotificationListener(notifierName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                                         dummyListener,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                                         null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                                         null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            // 1 Listener is expected to be added by the ServerNotifForwader
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            checkNotifier(s, 1, "addNotificationListener");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            mbsc.removeNotificationListener(notifierName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                                            dummyListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            System.out.println("Test OK for " + protocol);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        }catch(MalformedURLException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            System.out.println("Skipping URL " + u);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            if(client != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                client.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            if(server != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                server.stop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    private static void checkNotifier(NotificationSender s,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                                      int expectedListenerCount,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                                      String msg) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        int count = s.getListenerCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        if(count != expectedListenerCount) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            String errorMsg = "Invalid expected listener count [" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                expectedListenerCount + "], real [" +  count +"] for " + msg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            System.out.println(errorMsg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            throw new Exception(errorMsg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
}