jdk/test/javax/management/remote/mandatory/notif/DiffHBTest.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 1004 5ba8217eb504
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2003 Sun Microsystems, Inc.  All Rights Reserved.
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 4911721
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary test on add/remove NotificationListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * @author Shanliang JIANG
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * @run clean DiffHBTest
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * @run build DiffHBTest
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * @run main DiffHBTest
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.net.MalformedURLException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import javax.management.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import javax.management.remote.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * This test registeres an unique listener with two different handbacks,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * it expects to receive a same notification two times.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
public class DiffHBTest {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    private static final String[] protocols = {"rmi", "iiop", "jmxmp"};
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    private static final MBeanServer mbs = MBeanServerFactory.createMBeanServer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    private static ObjectName delegateName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    private static ObjectName timerName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    public static int received = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    public static final int[] receivedLock = new int[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    public static Notification receivedNotif = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    public static Object receivedHB = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    public static final String[] hbs = new String[] {"0", "1"};
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    public static void main(String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        System.out.println(">>> test on one listener with two different handbacks.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        delegateName = new ObjectName("JMImplementation:type=MBeanServerDelegate");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        timerName = new ObjectName("MBean:name=Timer");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        boolean ok = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        for (int i = 0; i < protocols.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
                if (!test(protocols[i])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
                    System.out.println(">>> Test failed for " + protocols[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
                    ok = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
                    System.out.println(">>> Test successed for " + protocols[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
                System.out.println(">>> Test failed for " + protocols[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
                e.printStackTrace(System.out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
                ok = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        if (ok) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            System.out.println(">>> Test passed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            System.out.println(">>> TEST FAILED");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            System.exit(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    private static boolean test(String proto) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        System.out.println(">>> Test for protocol " + proto);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        JMXServiceURL u = new JMXServiceURL(proto, null, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        JMXConnectorServer server;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        JMXServiceURL addr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        JMXConnector client;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        MBeanServerConnection mserver;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        final NotificationListener dummyListener = new NotificationListener() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                public void handleNotification(Notification n, Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                    synchronized(receivedLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                        if (n == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                            System.out.println(">>> Got a null notification.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                            System.exit(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                        // check number
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                        if (received > 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                            System.out.println(">>> Expect to receive 2 notifs,  but get "+received);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                            System.exit(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                        if (received == 0) { // first time
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                            receivedNotif = n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                            receivedHB = o;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                            if (!hbs[0].equals(o) && !hbs[1].equals(o)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                                System.out.println(">>> Unkown handback: "+o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                                System.exit(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                        } else { // second time
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                            if (!receivedNotif.equals(n)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                                System.out.println(">>> Not get same notif twice.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                                System.exit(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                            } else if (!hbs[0].equals(o) && !hbs[1].equals(o)) { // validate handback
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                                System.out.println(">>> Unkown handback: "+o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                                System.exit(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                            } else if (receivedHB.equals(o)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                                System.out.println(">>> Got same handback twice: "+o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                                System.exit(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                        ++received;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                        if (received == 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                            receivedLock.notify();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            server = JMXConnectorServerFactory.newJMXConnectorServer(u, null, mbs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            server.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            addr = server.getAddress();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            client = JMXConnectorFactory.newJMXConnector(addr, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            client.connect(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            mserver = client.getMBeanServerConnection();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            mserver.addNotificationListener(delegateName, dummyListener, null, hbs[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            mserver.addNotificationListener(delegateName, dummyListener, null, hbs[1]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            for (int i=0; i<20; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                synchronized(receivedLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                    received = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                mserver.createMBean("javax.management.timer.Timer", timerName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                synchronized(receivedLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                    if (received != 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                        long remainingTime = waitingTime;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                        final long startTime = System.currentTimeMillis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                        while (received != 2 && remainingTime > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                            receivedLock.wait(remainingTime);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                            remainingTime = waitingTime -
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                                (System.currentTimeMillis() - startTime);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                    if (received != 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                        System.out.println(">>> Expected 2 notifis, but received "+received);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                synchronized(receivedLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                    received = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                mserver.unregisterMBean(timerName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                synchronized(receivedLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                    if (received != 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                        long remainingTime = waitingTime;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                        final long startTime = System.currentTimeMillis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                        while (received != 2 && remainingTime >0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                            receivedLock.wait(remainingTime);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                            remainingTime = waitingTime -
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                                (System.currentTimeMillis() - startTime);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                    if (received != 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                        System.out.println(">>> Expected 2 notifis, but received "+received);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            mserver.removeNotificationListener(delegateName, dummyListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            client.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            server.stop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        } catch (MalformedURLException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
            System.out.println(">>> Skipping unsupported URL " + u);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    private final static long waitingTime = 10000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
}