2
|
1 |
/*
|
|
2 |
* Copyright 2003 Sun Microsystems, Inc. All Rights Reserved.
|
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
4 |
*
|
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
|
7 |
* published by the Free Software Foundation.
|
|
8 |
*
|
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
13 |
* accompanied this code).
|
|
14 |
*
|
|
15 |
* You should have received a copy of the GNU General Public License version
|
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
18 |
*
|
|
19 |
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
|
|
20 |
* CA 95054 USA or visit www.sun.com if you need additional information or
|
|
21 |
* have any questions.
|
|
22 |
*/
|
|
23 |
|
|
24 |
/*
|
|
25 |
* @test
|
|
26 |
* @bug 4911721
|
|
27 |
* @summary test on add/remove NotificationListener
|
|
28 |
* @author Shanliang JIANG
|
|
29 |
* @run clean DiffHBTest
|
|
30 |
* @run build DiffHBTest
|
|
31 |
* @run main DiffHBTest
|
|
32 |
*/
|
|
33 |
|
|
34 |
import java.net.MalformedURLException;
|
|
35 |
import java.io.IOException;
|
|
36 |
|
|
37 |
import javax.management.*;
|
|
38 |
import javax.management.remote.*;
|
|
39 |
|
|
40 |
/**
|
|
41 |
* This test registeres an unique listener with two different handbacks,
|
|
42 |
* it expects to receive a same notification two times.
|
|
43 |
*/
|
|
44 |
public class DiffHBTest {
|
|
45 |
private static final String[] protocols = {"rmi", "iiop", "jmxmp"};
|
|
46 |
|
|
47 |
private static final MBeanServer mbs = MBeanServerFactory.createMBeanServer();
|
|
48 |
private static ObjectName delegateName;
|
|
49 |
private static ObjectName timerName;
|
|
50 |
|
|
51 |
public static int received = 0;
|
|
52 |
public static final int[] receivedLock = new int[0];
|
|
53 |
public static Notification receivedNotif = null;
|
|
54 |
|
|
55 |
public static Object receivedHB = null;
|
|
56 |
public static final String[] hbs = new String[] {"0", "1"};
|
|
57 |
|
|
58 |
public static void main(String[] args) throws Exception {
|
|
59 |
System.out.println(">>> test on one listener with two different handbacks.");
|
|
60 |
|
|
61 |
delegateName = new ObjectName("JMImplementation:type=MBeanServerDelegate");
|
|
62 |
timerName = new ObjectName("MBean:name=Timer");
|
|
63 |
|
|
64 |
boolean ok = true;
|
|
65 |
for (int i = 0; i < protocols.length; i++) {
|
|
66 |
try {
|
|
67 |
if (!test(protocols[i])) {
|
|
68 |
System.out.println(">>> Test failed for " + protocols[i]);
|
|
69 |
ok = false;
|
|
70 |
} else {
|
|
71 |
System.out.println(">>> Test successed for " + protocols[i]);
|
|
72 |
}
|
|
73 |
} catch (Exception e) {
|
|
74 |
System.out.println(">>> Test failed for " + protocols[i]);
|
|
75 |
e.printStackTrace(System.out);
|
|
76 |
ok = false;
|
|
77 |
}
|
|
78 |
}
|
|
79 |
|
|
80 |
if (ok) {
|
|
81 |
System.out.println(">>> Test passed");
|
|
82 |
} else {
|
|
83 |
System.out.println(">>> TEST FAILED");
|
|
84 |
System.exit(1);
|
|
85 |
}
|
|
86 |
}
|
|
87 |
|
|
88 |
private static boolean test(String proto) throws Exception {
|
|
89 |
System.out.println(">>> Test for protocol " + proto);
|
|
90 |
JMXServiceURL u = new JMXServiceURL(proto, null, 0);
|
|
91 |
JMXConnectorServer server;
|
|
92 |
JMXServiceURL addr;
|
|
93 |
JMXConnector client;
|
|
94 |
MBeanServerConnection mserver;
|
|
95 |
|
|
96 |
final NotificationListener dummyListener = new NotificationListener() {
|
|
97 |
public void handleNotification(Notification n, Object o) {
|
|
98 |
synchronized(receivedLock) {
|
|
99 |
if (n == null) {
|
|
100 |
System.out.println(">>> Got a null notification.");
|
|
101 |
System.exit(1);
|
|
102 |
}
|
|
103 |
|
|
104 |
// check number
|
|
105 |
if (received > 2) {
|
|
106 |
System.out.println(">>> Expect to receive 2 notifs, but get "+received);
|
|
107 |
System.exit(1);
|
|
108 |
}
|
|
109 |
|
|
110 |
if (received == 0) { // first time
|
|
111 |
receivedNotif = n;
|
|
112 |
receivedHB = o;
|
|
113 |
|
|
114 |
if (!hbs[0].equals(o) && !hbs[1].equals(o)) {
|
|
115 |
System.out.println(">>> Unkown handback: "+o);
|
|
116 |
System.exit(1);
|
|
117 |
}
|
|
118 |
} else { // second time
|
|
119 |
if (!receivedNotif.equals(n)) {
|
|
120 |
System.out.println(">>> Not get same notif twice.");
|
|
121 |
System.exit(1);
|
|
122 |
} else if (!hbs[0].equals(o) && !hbs[1].equals(o)) { // validate handback
|
|
123 |
System.out.println(">>> Unkown handback: "+o);
|
|
124 |
System.exit(1);
|
|
125 |
} else if (receivedHB.equals(o)) {
|
|
126 |
System.out.println(">>> Got same handback twice: "+o);
|
|
127 |
System.exit(1);
|
|
128 |
}
|
|
129 |
}
|
|
130 |
|
|
131 |
++received;
|
|
132 |
|
|
133 |
if (received == 2) {
|
|
134 |
receivedLock.notify();
|
|
135 |
}
|
|
136 |
}
|
|
137 |
}
|
|
138 |
};
|
|
139 |
|
|
140 |
try {
|
|
141 |
server = JMXConnectorServerFactory.newJMXConnectorServer(u, null, mbs);
|
|
142 |
server.start();
|
|
143 |
|
|
144 |
addr = server.getAddress();
|
|
145 |
client = JMXConnectorFactory.newJMXConnector(addr, null);
|
|
146 |
client.connect(null);
|
|
147 |
|
|
148 |
mserver = client.getMBeanServerConnection();
|
|
149 |
|
|
150 |
mserver.addNotificationListener(delegateName, dummyListener, null, hbs[0]);
|
|
151 |
mserver.addNotificationListener(delegateName, dummyListener, null, hbs[1]);
|
|
152 |
|
|
153 |
for (int i=0; i<20; i++) {
|
|
154 |
synchronized(receivedLock) {
|
|
155 |
received = 0;
|
|
156 |
}
|
|
157 |
|
|
158 |
mserver.createMBean("javax.management.timer.Timer", timerName);
|
|
159 |
|
|
160 |
synchronized(receivedLock) {
|
|
161 |
if (received != 2) {
|
|
162 |
long remainingTime = waitingTime;
|
|
163 |
final long startTime = System.currentTimeMillis();
|
|
164 |
|
|
165 |
while (received != 2 && remainingTime > 0) {
|
|
166 |
receivedLock.wait(remainingTime);
|
|
167 |
remainingTime = waitingTime -
|
|
168 |
(System.currentTimeMillis() - startTime);
|
|
169 |
}
|
|
170 |
}
|
|
171 |
|
|
172 |
if (received != 2) {
|
|
173 |
System.out.println(">>> Expected 2 notifis, but received "+received);
|
|
174 |
|
|
175 |
return false;
|
|
176 |
}
|
|
177 |
}
|
|
178 |
|
|
179 |
|
|
180 |
synchronized(receivedLock) {
|
|
181 |
received = 0;
|
|
182 |
}
|
|
183 |
|
|
184 |
mserver.unregisterMBean(timerName);
|
|
185 |
|
|
186 |
synchronized(receivedLock) {
|
|
187 |
if (received != 2) {
|
|
188 |
|
|
189 |
long remainingTime = waitingTime;
|
|
190 |
final long startTime = System.currentTimeMillis();
|
|
191 |
|
|
192 |
while (received != 2 && remainingTime >0) {
|
|
193 |
receivedLock.wait(remainingTime);
|
|
194 |
remainingTime = waitingTime -
|
|
195 |
(System.currentTimeMillis() - startTime);
|
|
196 |
}
|
|
197 |
}
|
|
198 |
|
|
199 |
if (received != 2) {
|
|
200 |
System.out.println(">>> Expected 2 notifis, but received "+received);
|
|
201 |
|
|
202 |
return false;
|
|
203 |
}
|
|
204 |
}
|
|
205 |
}
|
|
206 |
|
|
207 |
mserver.removeNotificationListener(delegateName, dummyListener);
|
|
208 |
|
|
209 |
client.close();
|
|
210 |
|
|
211 |
server.stop();
|
|
212 |
|
|
213 |
} catch (MalformedURLException e) {
|
|
214 |
System.out.println(">>> Skipping unsupported URL " + u);
|
|
215 |
return true;
|
|
216 |
}
|
|
217 |
|
|
218 |
return true;
|
|
219 |
}
|
|
220 |
|
|
221 |
private final static long waitingTime = 10000;
|
|
222 |
}
|