jdk/test/javax/management/remote/mandatory/notif/EmptyDomainNotificationTest.java
author ohair
Tue, 25 May 2010 15:58:33 -0700
changeset 5506 202f599c92aa
parent 4156 acaa49a2768a
child 30376 2ccf2cf7ea48
permissions -rw-r--r--
6943119: Rebrand source copyright notices Reviewed-by: darcy, weijun
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4156
diff changeset
     2
 * Copyright (c) 2005, Oracle and/or its affiliates. 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
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4156
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4156
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4156
diff changeset
    21
 * questions.
2
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 6238731
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary Check that the expected notification is received by the JMX
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 *          client even when the domain in the ObjectName is not specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * @author Shanliang JIANG
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * @run clean EmptyDomainNotificationTest
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * @run build EmptyDomainNotificationTest
4156
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1247
diff changeset
    32
 * @run main EmptyDomainNotificationTest
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
4156
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1247
diff changeset
    35
import java.util.ArrayList;
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1247
diff changeset
    36
import java.util.List;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import javax.management.MBeanServer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import javax.management.MBeanServerConnection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import javax.management.MBeanServerFactory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import javax.management.Notification;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import javax.management.NotificationBroadcasterSupport;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import javax.management.NotificationListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import javax.management.ObjectName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import javax.management.remote.JMXConnector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import javax.management.remote.JMXConnectorFactory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import javax.management.remote.JMXConnectorServer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
import javax.management.remote.JMXConnectorServerFactory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
import javax.management.remote.JMXServiceURL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
public class EmptyDomainNotificationTest {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    public static interface SimpleMBean {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        public void emitNotification();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    public static class Simple
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        extends NotificationBroadcasterSupport
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        implements SimpleMBean {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        public void emitNotification() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
            sendNotification(new Notification("simple", this, 0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    public static class Listener implements NotificationListener {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        public void handleNotification(Notification n, Object h) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        System.out.println(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
          "EmptyDomainNotificationTest-Listener-handleNotification: receives:" + n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
            if (n.getType().equals("simple")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
                synchronized(this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
                    received++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
                    this.notifyAll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        public int received;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    public static void main(String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        final MBeanServer mbs = MBeanServerFactory.createMBeanServer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        final JMXServiceURL url = new JMXServiceURL("service:jmx:rmi://");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
4156
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1247
diff changeset
    87
        JMXConnectorServer server = JMXConnectorServerFactory.newJMXConnectorServer(url, null, mbs);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        server.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        JMXConnector client = JMXConnectorFactory.connect(server.getAddress(), null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        final MBeanServerConnection mbsc = client.getMBeanServerConnection();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        final ObjectName mbean = ObjectName.getInstance(":type=Simple");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        mbsc.createMBean(Simple.class.getName(), mbean);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        System.out.println("EmptyDomainNotificationTest-main: add a listener ...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        final Listener li = new Listener();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        mbsc.addNotificationListener(mbean, li, null, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        System.out.println("EmptyDomainNotificationTest-main: ask to send a notif ...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        mbsc.invoke(mbean, "emitNotification", null, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        System.out.println("EmptyDomainNotificationTest-main: waiting notif...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        final long stopTime = System.currentTimeMillis() + 2000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        synchronized(li) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            long toWait = stopTime - System.currentTimeMillis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            while (li.received < 1 && toWait > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                li.wait(toWait);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                toWait = stopTime - System.currentTimeMillis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        if (li.received < 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            throw new RuntimeException("No notif received!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        } else if (li.received > 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            throw new RuntimeException("Wait one notif but got: "+li.received);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        System.out.println("EmptyDomainNotificationTest-main: Got the expected notif!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        System.out.println("EmptyDomainNotificationTest-main: remove the listener.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        mbsc.removeNotificationListener(mbean, li);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        // clean
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        client.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        server.stop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        System.out.println("EmptyDomainNotificationTest-main: Bye.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
}