2
|
1 |
/*
|
1247
|
2 |
* Copyright 2003-2008 Sun Microsystems, Inc. All Rights Reserved.
|
2
|
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 4915825 4921009 4934965 4977469
|
|
27 |
* @summary Tests behavior when client or server gets object of unknown class
|
|
28 |
* @author Eamonn McManus
|
|
29 |
* @run clean MissingClassTest SingleClassLoader
|
|
30 |
* @run build MissingClassTest SingleClassLoader
|
|
31 |
* @run main MissingClassTest
|
|
32 |
*/
|
|
33 |
|
|
34 |
/*
|
|
35 |
Tests that clients and servers react correctly when they receive
|
|
36 |
objects of unknown classes. This can happen easily due to version
|
|
37 |
skew or missing jar files on one end or the other. The default
|
|
38 |
behaviour of causing a connection to die because of the resultant
|
|
39 |
IOException is not acceptable! We try sending attributes and invoke
|
|
40 |
parameters to the server of classes it doesn't know, and we try
|
|
41 |
sending attributes, exceptions and notifications to the client of
|
|
42 |
classes it doesn't know.
|
|
43 |
|
|
44 |
We also test objects that are of known class but not serializable.
|
|
45 |
The test cases are similar.
|
|
46 |
*/
|
1004
|
47 |
|
|
48 |
import java.io.ByteArrayOutputStream;
|
|
49 |
import java.io.IOException;
|
|
50 |
import java.io.NotSerializableException;
|
|
51 |
import java.io.ObjectOutputStream;
|
|
52 |
import java.net.MalformedURLException;
|
|
53 |
import java.util.HashMap;
|
|
54 |
import java.util.Map;
|
|
55 |
import java.util.Random;
|
|
56 |
import java.util.Set;
|
|
57 |
import javax.management.Attribute;
|
|
58 |
import javax.management.MBeanServer;
|
|
59 |
import javax.management.MBeanServerConnection;
|
|
60 |
import javax.management.MBeanServerFactory;
|
|
61 |
import javax.management.Notification;
|
|
62 |
import javax.management.NotificationBroadcasterSupport;
|
|
63 |
import javax.management.NotificationFilter;
|
|
64 |
import javax.management.NotificationListener;
|
|
65 |
import javax.management.ObjectName;
|
|
66 |
import javax.management.remote.JMXConnectionNotification;
|
|
67 |
import javax.management.remote.JMXConnector;
|
|
68 |
import javax.management.remote.JMXConnectorFactory;
|
|
69 |
import javax.management.remote.JMXConnectorServer;
|
|
70 |
import javax.management.remote.JMXConnectorServerFactory;
|
|
71 |
import javax.management.remote.JMXServiceURL;
|
2
|
72 |
import javax.management.remote.rmi.RMIConnectorServer;
|
1004
|
73 |
import org.omg.CORBA.MARSHAL;
|
2
|
74 |
|
|
75 |
public class MissingClassTest {
|
|
76 |
private static final int NNOTIFS = 50;
|
|
77 |
|
|
78 |
private static ClassLoader clientLoader, serverLoader;
|
|
79 |
private static Object serverUnknown;
|
|
80 |
private static Exception clientUnknown;
|
|
81 |
private static ObjectName on;
|
|
82 |
private static final Object[] NO_OBJECTS = new Object[0];
|
|
83 |
private static final String[] NO_STRINGS = new String[0];
|
|
84 |
|
|
85 |
private static final Object unserializableObject = Thread.currentThread();
|
|
86 |
|
|
87 |
public static void main(String[] args) throws Exception {
|
|
88 |
System.out.println("Test that the client or server end of a " +
|
|
89 |
"connection does not fail if sent an object " +
|
|
90 |
"it cannot deserialize");
|
|
91 |
|
|
92 |
on = new ObjectName("test:type=Test");
|
|
93 |
|
|
94 |
ClassLoader testLoader = MissingClassTest.class.getClassLoader();
|
|
95 |
clientLoader =
|
|
96 |
new SingleClassLoader("$ServerUnknown$", HashMap.class,
|
|
97 |
testLoader);
|
|
98 |
serverLoader =
|
|
99 |
new SingleClassLoader("$ClientUnknown$", Exception.class,
|
|
100 |
testLoader);
|
|
101 |
serverUnknown =
|
|
102 |
clientLoader.loadClass("$ServerUnknown$").newInstance();
|
|
103 |
clientUnknown = (Exception)
|
|
104 |
serverLoader.loadClass("$ClientUnknown$").newInstance();
|
|
105 |
|
|
106 |
final String[] protos = {"rmi", /*"iiop",*/ "jmxmp"};
|
|
107 |
boolean ok = true;
|
|
108 |
for (int i = 0; i < protos.length; i++) {
|
|
109 |
try {
|
|
110 |
ok &= test(protos[i]);
|
|
111 |
} catch (Exception e) {
|
|
112 |
System.out.println("TEST FAILED WITH EXCEPTION:");
|
|
113 |
e.printStackTrace(System.out);
|
|
114 |
ok = false;
|
|
115 |
}
|
|
116 |
}
|
|
117 |
|
|
118 |
if (ok)
|
|
119 |
System.out.println("Test passed");
|
|
120 |
else {
|
|
121 |
System.out.println("TEST FAILED");
|
|
122 |
System.exit(1);
|
|
123 |
}
|
|
124 |
}
|
|
125 |
|
|
126 |
private static boolean test(String proto) throws Exception {
|
1004
|
127 |
boolean ok = true;
|
|
128 |
for (boolean eventService : new boolean[] {false, true})
|
|
129 |
ok &= test(proto, eventService);
|
|
130 |
return ok;
|
|
131 |
}
|
|
132 |
|
|
133 |
private static boolean test(String proto, boolean eventService)
|
|
134 |
throws Exception {
|
|
135 |
System.out.println("Testing for proto " + proto + " with" +
|
|
136 |
(eventService ? "" : "out") + " Event Service");
|
2
|
137 |
|
|
138 |
boolean ok = true;
|
|
139 |
|
|
140 |
MBeanServer mbs = MBeanServerFactory.newMBeanServer();
|
|
141 |
mbs.createMBean(Test.class.getName(), on);
|
|
142 |
|
|
143 |
JMXConnectorServer cs;
|
|
144 |
JMXServiceURL url = new JMXServiceURL(proto, null, 0);
|
|
145 |
Map serverMap = new HashMap();
|
|
146 |
serverMap.put(JMXConnectorServerFactory.DEFAULT_CLASS_LOADER,
|
|
147 |
serverLoader);
|
1004
|
148 |
serverMap.put(RMIConnectorServer.DELEGATE_TO_EVENT_SERVICE,
|
|
149 |
Boolean.toString(eventService));
|
2
|
150 |
|
|
151 |
// make sure no auto-close at server side
|
|
152 |
serverMap.put("jmx.remote.x.server.connection.timeout", "888888888");
|
|
153 |
|
|
154 |
try {
|
|
155 |
cs = JMXConnectorServerFactory.newJMXConnectorServer(url,
|
|
156 |
serverMap,
|
|
157 |
mbs);
|
|
158 |
} catch (MalformedURLException e) {
|
|
159 |
System.out.println("System does not recognize URL: " + url +
|
|
160 |
"; ignoring");
|
|
161 |
return true;
|
|
162 |
}
|
|
163 |
cs.start();
|
|
164 |
JMXServiceURL addr = cs.getAddress();
|
|
165 |
Map clientMap = new HashMap();
|
|
166 |
clientMap.put(JMXConnectorFactory.DEFAULT_CLASS_LOADER,
|
|
167 |
clientLoader);
|
|
168 |
|
|
169 |
System.out.println("Connecting for client-unknown test");
|
|
170 |
|
|
171 |
JMXConnector client = JMXConnectorFactory.connect(addr, clientMap);
|
|
172 |
|
|
173 |
// add a listener to verify no failed notif
|
|
174 |
CNListener cnListener = new CNListener();
|
|
175 |
client.addConnectionNotificationListener(cnListener, null, null);
|
|
176 |
|
|
177 |
MBeanServerConnection mbsc = client.getMBeanServerConnection();
|
|
178 |
|
|
179 |
System.out.println("Getting attribute with class unknown to client");
|
|
180 |
try {
|
|
181 |
Object result = mbsc.getAttribute(on, "ClientUnknown");
|
|
182 |
System.out.println("TEST FAILS: getAttribute for class " +
|
|
183 |
"unknown to client should fail, returned: " +
|
|
184 |
result);
|
|
185 |
ok = false;
|
|
186 |
} catch (IOException e) {
|
|
187 |
Throwable cause = e.getCause();
|
1004
|
188 |
if (cause instanceof MARSHAL) // see CR 4935098
|
|
189 |
cause = cause.getCause();
|
2
|
190 |
if (cause instanceof ClassNotFoundException) {
|
|
191 |
System.out.println("Success: got an IOException wrapping " +
|
|
192 |
"a ClassNotFoundException");
|
|
193 |
} else {
|
|
194 |
System.out.println("TEST FAILS: Caught IOException (" + e +
|
|
195 |
") but cause should be " +
|
|
196 |
"ClassNotFoundException: " + cause);
|
|
197 |
ok = false;
|
|
198 |
}
|
|
199 |
}
|
|
200 |
|
|
201 |
System.out.println("Doing queryNames to ensure connection alive");
|
|
202 |
Set names = mbsc.queryNames(null, null);
|
|
203 |
System.out.println("queryNames returned " + names);
|
|
204 |
|
|
205 |
System.out.println("Provoke exception of unknown class");
|
|
206 |
try {
|
|
207 |
mbsc.invoke(on, "throwClientUnknown", NO_OBJECTS, NO_STRINGS);
|
|
208 |
System.out.println("TEST FAILS: did not get exception");
|
|
209 |
ok = false;
|
|
210 |
} catch (IOException e) {
|
|
211 |
Throwable wrapped = e.getCause();
|
1004
|
212 |
if (wrapped instanceof MARSHAL) // see CR 4935098
|
|
213 |
wrapped = wrapped.getCause();
|
2
|
214 |
if (wrapped instanceof ClassNotFoundException) {
|
|
215 |
System.out.println("Success: got an IOException wrapping " +
|
|
216 |
"a ClassNotFoundException: " +
|
|
217 |
wrapped);
|
|
218 |
} else {
|
|
219 |
System.out.println("TEST FAILS: Got IOException but cause " +
|
|
220 |
"should be ClassNotFoundException: ");
|
|
221 |
if (wrapped == null)
|
|
222 |
System.out.println("(null)");
|
|
223 |
else
|
|
224 |
wrapped.printStackTrace(System.out);
|
|
225 |
ok = false;
|
|
226 |
}
|
|
227 |
} catch (Exception e) {
|
|
228 |
System.out.println("TEST FAILS: Got wrong exception: " +
|
|
229 |
"should be IOException with cause " +
|
|
230 |
"ClassNotFoundException:");
|
|
231 |
e.printStackTrace(System.out);
|
|
232 |
ok = false;
|
|
233 |
}
|
|
234 |
|
|
235 |
System.out.println("Doing queryNames to ensure connection alive");
|
|
236 |
names = mbsc.queryNames(null, null);
|
|
237 |
System.out.println("queryNames returned " + names);
|
|
238 |
|
|
239 |
ok &= notifyTest(client, mbsc);
|
|
240 |
|
|
241 |
System.out.println("Doing queryNames to ensure connection alive");
|
|
242 |
names = mbsc.queryNames(null, null);
|
|
243 |
System.out.println("queryNames returned " + names);
|
|
244 |
|
|
245 |
for (int i = 0; i < 2; i++) {
|
|
246 |
boolean setAttribute = (i == 0); // else invoke
|
|
247 |
String what = setAttribute ? "setAttribute" : "invoke";
|
|
248 |
System.out.println("Trying " + what +
|
|
249 |
" with class unknown to server");
|
|
250 |
try {
|
|
251 |
if (setAttribute) {
|
|
252 |
mbsc.setAttribute(on, new Attribute("ServerUnknown",
|
|
253 |
serverUnknown));
|
|
254 |
} else {
|
|
255 |
mbsc.invoke(on, "useServerUnknown",
|
|
256 |
new Object[] {serverUnknown},
|
|
257 |
new String[] {"java.lang.Object"});
|
|
258 |
}
|
|
259 |
System.out.println("TEST FAILS: " + what + " with " +
|
|
260 |
"class unknown to server should fail " +
|
|
261 |
"but did not");
|
|
262 |
ok = false;
|
|
263 |
} catch (IOException e) {
|
|
264 |
Throwable cause = e.getCause();
|
1004
|
265 |
if (cause instanceof MARSHAL) // see CR 4935098
|
|
266 |
cause = cause.getCause();
|
2
|
267 |
if (cause instanceof ClassNotFoundException) {
|
|
268 |
System.out.println("Success: got an IOException " +
|
|
269 |
"wrapping a ClassNotFoundException");
|
|
270 |
} else {
|
|
271 |
System.out.println("TEST FAILS: Caught IOException (" + e +
|
|
272 |
") but cause should be " +
|
|
273 |
"ClassNotFoundException: " + cause);
|
|
274 |
e.printStackTrace(System.out); // XXX
|
|
275 |
ok = false;
|
|
276 |
}
|
|
277 |
}
|
|
278 |
}
|
|
279 |
|
|
280 |
System.out.println("Doing queryNames to ensure connection alive");
|
|
281 |
names = mbsc.queryNames(null, null);
|
|
282 |
System.out.println("queryNames returned " + names);
|
|
283 |
|
|
284 |
System.out.println("Trying to get unserializable attribute");
|
|
285 |
try {
|
|
286 |
mbsc.getAttribute(on, "Unserializable");
|
|
287 |
System.out.println("TEST FAILS: get unserializable worked " +
|
|
288 |
"but should not");
|
|
289 |
ok = false;
|
|
290 |
} catch (IOException e) {
|
|
291 |
System.out.println("Success: got an IOException: " + e +
|
|
292 |
" (cause: " + e.getCause() + ")");
|
|
293 |
}
|
|
294 |
|
|
295 |
System.out.println("Doing queryNames to ensure connection alive");
|
|
296 |
names = mbsc.queryNames(null, null);
|
|
297 |
System.out.println("queryNames returned " + names);
|
|
298 |
|
|
299 |
System.out.println("Trying to set unserializable attribute");
|
|
300 |
try {
|
|
301 |
Attribute attr =
|
|
302 |
new Attribute("Unserializable", unserializableObject);
|
|
303 |
mbsc.setAttribute(on, attr);
|
|
304 |
System.out.println("TEST FAILS: set unserializable worked " +
|
|
305 |
"but should not");
|
|
306 |
ok = false;
|
|
307 |
} catch (IOException e) {
|
|
308 |
System.out.println("Success: got an IOException: " + e +
|
|
309 |
" (cause: " + e.getCause() + ")");
|
|
310 |
}
|
|
311 |
|
|
312 |
System.out.println("Doing queryNames to ensure connection alive");
|
|
313 |
names = mbsc.queryNames(null, null);
|
|
314 |
System.out.println("queryNames returned " + names);
|
|
315 |
|
|
316 |
System.out.println("Trying to throw unserializable exception");
|
|
317 |
try {
|
|
318 |
mbsc.invoke(on, "throwUnserializable", NO_OBJECTS, NO_STRINGS);
|
|
319 |
System.out.println("TEST FAILS: throw unserializable worked " +
|
|
320 |
"but should not");
|
|
321 |
ok = false;
|
|
322 |
} catch (IOException e) {
|
|
323 |
System.out.println("Success: got an IOException: " + e +
|
|
324 |
" (cause: " + e.getCause() + ")");
|
|
325 |
}
|
|
326 |
|
|
327 |
client.removeConnectionNotificationListener(cnListener);
|
|
328 |
ok = ok && !cnListener.failed;
|
|
329 |
|
|
330 |
client.close();
|
|
331 |
cs.stop();
|
|
332 |
|
|
333 |
if (ok)
|
|
334 |
System.out.println("Test passed for protocol " + proto);
|
|
335 |
|
|
336 |
System.out.println();
|
|
337 |
return ok;
|
|
338 |
}
|
|
339 |
|
|
340 |
private static class TestListener implements NotificationListener {
|
|
341 |
TestListener(LostListener ll) {
|
|
342 |
this.lostListener = ll;
|
|
343 |
}
|
|
344 |
|
|
345 |
public void handleNotification(Notification n, Object h) {
|
|
346 |
/* Connectors can handle unserializable notifications in
|
|
347 |
one of two ways. Either they can arrange for the
|
|
348 |
client to get a NotSerializableException from its
|
|
349 |
fetchNotifications call (RMI connector), or they can
|
|
350 |
replace the unserializable notification by a
|
|
351 |
JMXConnectionNotification.NOTIFS_LOST (JMXMP
|
|
352 |
connector). The former case is handled by code within
|
|
353 |
the connector client which will end up sending a
|
|
354 |
NOTIFS_LOST to our LostListener. The logic here
|
|
355 |
handles the latter case by converting it into the
|
|
356 |
former.
|
|
357 |
*/
|
|
358 |
if (n instanceof JMXConnectionNotification
|
|
359 |
&& n.getType().equals(JMXConnectionNotification.NOTIFS_LOST)) {
|
|
360 |
lostListener.handleNotification(n, h);
|
|
361 |
return;
|
|
362 |
}
|
|
363 |
|
|
364 |
synchronized (result) {
|
|
365 |
if (!n.getType().equals("interesting")
|
|
366 |
|| !n.getUserData().equals("known")) {
|
|
367 |
System.out.println("TestListener received strange notif: "
|
|
368 |
+ notificationString(n));
|
|
369 |
result.failed = true;
|
|
370 |
result.notifyAll();
|
|
371 |
} else {
|
|
372 |
result.knownCount++;
|
|
373 |
if (result.knownCount == NNOTIFS)
|
|
374 |
result.notifyAll();
|
|
375 |
}
|
|
376 |
}
|
|
377 |
}
|
|
378 |
|
|
379 |
private LostListener lostListener;
|
|
380 |
}
|
|
381 |
|
|
382 |
private static class LostListener implements NotificationListener {
|
|
383 |
public void handleNotification(Notification n, Object h) {
|
|
384 |
synchronized (result) {
|
|
385 |
handle(n, h);
|
|
386 |
}
|
|
387 |
}
|
|
388 |
|
|
389 |
private void handle(Notification n, Object h) {
|
|
390 |
if (!(n instanceof JMXConnectionNotification)) {
|
|
391 |
System.out.println("LostListener received strange notif: " +
|
|
392 |
notificationString(n));
|
|
393 |
result.failed = true;
|
|
394 |
result.notifyAll();
|
|
395 |
return;
|
|
396 |
}
|
|
397 |
|
|
398 |
JMXConnectionNotification jn = (JMXConnectionNotification) n;
|
|
399 |
if (!jn.getType().equals(jn.NOTIFS_LOST)) {
|
|
400 |
System.out.println("Ignoring JMXConnectionNotification: " +
|
|
401 |
notificationString(jn));
|
|
402 |
return;
|
|
403 |
}
|
|
404 |
final String msg = jn.getMessage();
|
|
405 |
if ((!msg.startsWith("Dropped ")
|
|
406 |
|| !msg.endsWith("classes were missing locally"))
|
|
407 |
&& (!msg.startsWith("Not serializable: "))) {
|
|
408 |
System.out.println("Surprising NOTIFS_LOST getMessage: " +
|
|
409 |
msg);
|
|
410 |
}
|
|
411 |
if (!(jn.getUserData() instanceof Long)) {
|
|
412 |
System.out.println("JMXConnectionNotification userData " +
|
|
413 |
"not a Long: " + jn.getUserData());
|
|
414 |
result.failed = true;
|
|
415 |
} else {
|
|
416 |
int lost = ((Long) jn.getUserData()).intValue();
|
|
417 |
result.lostCount += lost;
|
|
418 |
if (result.lostCount == NNOTIFS*2)
|
|
419 |
result.notifyAll();
|
|
420 |
}
|
|
421 |
}
|
|
422 |
}
|
|
423 |
|
|
424 |
private static class TestFilter implements NotificationFilter {
|
|
425 |
public boolean isNotificationEnabled(Notification n) {
|
|
426 |
return (n.getType().equals("interesting"));
|
|
427 |
}
|
|
428 |
}
|
|
429 |
|
|
430 |
private static class Result {
|
|
431 |
int knownCount, lostCount;
|
|
432 |
boolean failed;
|
|
433 |
}
|
|
434 |
private static Result result;
|
|
435 |
|
|
436 |
/* Send a bunch of notifications to exercise the logic to recover
|
|
437 |
from unknown notification classes. We have four kinds of
|
|
438 |
notifications: "known" ones are of a class known to the client
|
|
439 |
and which match its filters; "unknown" ones are of a class that
|
|
440 |
match the client's filters but that the client can't load;
|
|
441 |
"tricky" ones are unserializable; and "boring" notifications
|
|
442 |
are of a class that the client knows but that doesn't match its
|
|
443 |
filters. We emit NNOTIFS notifications of each kind. We do a
|
|
444 |
random shuffle on these 4*NNOTIFS notifications so it is likely
|
|
445 |
that we will cover the various different cases in the logic.
|
|
446 |
|
|
447 |
Specifically, what we are testing here is the logic that
|
|
448 |
recovers from a fetchNotifications request that gets a
|
|
449 |
ClassNotFoundException. Because the request can contain
|
|
450 |
several notifications, the client doesn't know which of them
|
|
451 |
generated the exception. So it redoes a request that asks for
|
|
452 |
just one notification. We need to be sure that this works when
|
|
453 |
that one notification is of an unknown class and when it is of
|
|
454 |
a known class, and in both cases when there are filtered
|
|
455 |
notifications that are skipped.
|
|
456 |
|
|
457 |
We are also testing the behaviour in the server when it tries
|
|
458 |
to include an unserializable notification in the response to a
|
|
459 |
fetchNotifications, and in the client when that happens.
|
|
460 |
|
|
461 |
If the test succeeds, the listener should receive the NNOTIFS
|
|
462 |
"known" notifications, and the connection listener should
|
|
463 |
receive an indication of NNOTIFS lost notifications
|
|
464 |
representing the "unknown" notifications.
|
|
465 |
|
|
466 |
We depend on some implementation-specific features here:
|
|
467 |
|
|
468 |
1. The buffer size is sufficient to contain the 4*NNOTIFS
|
|
469 |
notifications which are all sent at once, before the client
|
|
470 |
gets a chance to start receiving them.
|
|
471 |
|
|
472 |
2. When one or more notifications are dropped because they are
|
|
473 |
of unknown classes, the NOTIFS_LOST notification contains a
|
|
474 |
userData that is a Long with a count of the number dropped.
|
|
475 |
|
|
476 |
3. If a notification is not serializable on the server, the
|
|
477 |
client gets told about it somehow, rather than having it just
|
|
478 |
dropped on the floor. The somehow might be through an RMI
|
|
479 |
exception, or it might be by the server replacing the
|
|
480 |
unserializable notif by a JMXConnectionNotification.NOTIFS_LOST.
|
|
481 |
*/
|
|
482 |
private static boolean notifyTest(JMXConnector client,
|
|
483 |
MBeanServerConnection mbsc)
|
|
484 |
throws Exception {
|
|
485 |
System.out.println("Send notifications including unknown ones");
|
|
486 |
result = new Result();
|
|
487 |
LostListener ll = new LostListener();
|
|
488 |
client.addConnectionNotificationListener(ll, null, null);
|
|
489 |
TestListener nl = new TestListener(ll);
|
|
490 |
mbsc.addNotificationListener(on, nl, new TestFilter(), null);
|
|
491 |
mbsc.invoke(on, "sendNotifs", NO_OBJECTS, NO_STRINGS);
|
|
492 |
|
|
493 |
// wait for the listeners to receive all their notifs
|
|
494 |
// or to fail
|
|
495 |
long deadline = System.currentTimeMillis() + 60000;
|
|
496 |
long remain;
|
|
497 |
while ((remain = deadline - System.currentTimeMillis()) >= 0) {
|
|
498 |
synchronized (result) {
|
|
499 |
if (result.failed
|
1004
|
500 |
|| (result.knownCount >= NNOTIFS
|
|
501 |
&& result.lostCount >= NNOTIFS*2))
|
2
|
502 |
break;
|
|
503 |
result.wait(remain);
|
|
504 |
}
|
|
505 |
}
|
1004
|
506 |
Thread.sleep(2); // allow any spurious extra notifs to arrive
|
2
|
507 |
if (result.failed) {
|
|
508 |
System.out.println("TEST FAILS: Notification strangeness");
|
|
509 |
return false;
|
|
510 |
} else if (result.knownCount == NNOTIFS
|
|
511 |
&& result.lostCount == NNOTIFS*2) {
|
|
512 |
System.out.println("Success: received known notifications and " +
|
|
513 |
"got NOTIFS_LOST for unknown and " +
|
|
514 |
"unserializable ones");
|
|
515 |
return true;
|
1004
|
516 |
} else if (result.knownCount >= NNOTIFS
|
|
517 |
|| result.lostCount >= NNOTIFS*2) {
|
|
518 |
System.out.println("TEST FAILS: Received too many notifs: " +
|
|
519 |
"known=" + result.knownCount + "; lost=" + result.lostCount);
|
|
520 |
return false;
|
2
|
521 |
} else {
|
|
522 |
System.out.println("TEST FAILS: Timed out without receiving " +
|
|
523 |
"all notifs: known=" + result.knownCount +
|
|
524 |
"; lost=" + result.lostCount);
|
|
525 |
return false;
|
|
526 |
}
|
|
527 |
}
|
|
528 |
|
|
529 |
public static interface TestMBean {
|
|
530 |
public Object getClientUnknown() throws Exception;
|
|
531 |
public void throwClientUnknown() throws Exception;
|
|
532 |
public void setServerUnknown(Object o) throws Exception;
|
|
533 |
public void useServerUnknown(Object o) throws Exception;
|
|
534 |
public Object getUnserializable() throws Exception;
|
|
535 |
public void setUnserializable(Object un) throws Exception;
|
|
536 |
public void throwUnserializable() throws Exception;
|
|
537 |
public void sendNotifs() throws Exception;
|
|
538 |
}
|
|
539 |
|
|
540 |
public static class Test extends NotificationBroadcasterSupport
|
|
541 |
implements TestMBean {
|
|
542 |
|
|
543 |
public Object getClientUnknown() {
|
|
544 |
return clientUnknown;
|
|
545 |
}
|
|
546 |
|
|
547 |
public void throwClientUnknown() throws Exception {
|
|
548 |
throw clientUnknown;
|
|
549 |
}
|
|
550 |
|
|
551 |
public void setServerUnknown(Object o) {
|
|
552 |
throw new IllegalArgumentException("setServerUnknown succeeded "+
|
|
553 |
"but should not have");
|
|
554 |
}
|
|
555 |
|
|
556 |
public void useServerUnknown(Object o) {
|
|
557 |
throw new IllegalArgumentException("useServerUnknown succeeded "+
|
|
558 |
"but should not have");
|
|
559 |
}
|
|
560 |
|
|
561 |
public Object getUnserializable() {
|
|
562 |
return unserializableObject;
|
|
563 |
}
|
|
564 |
|
|
565 |
public void setUnserializable(Object un) {
|
|
566 |
throw new IllegalArgumentException("setUnserializable succeeded " +
|
|
567 |
"but should not have");
|
|
568 |
}
|
|
569 |
|
|
570 |
public void throwUnserializable() throws Exception {
|
|
571 |
throw new Exception() {
|
|
572 |
private Object unserializable = unserializableObject;
|
|
573 |
};
|
|
574 |
}
|
|
575 |
|
|
576 |
public void sendNotifs() {
|
|
577 |
/* We actually send the same four notification objects
|
|
578 |
NNOTIFS times each. This doesn't particularly matter,
|
|
579 |
but note that the MBeanServer will replace "this" by
|
|
580 |
the sender's ObjectName the first time. Since that's
|
|
581 |
always the same, no problem. */
|
|
582 |
Notification known =
|
|
583 |
new Notification("interesting", this, 1L, 1L, "known");
|
|
584 |
known.setUserData("known");
|
|
585 |
Notification unknown =
|
|
586 |
new Notification("interesting", this, 1L, 1L, "unknown");
|
|
587 |
unknown.setUserData(clientUnknown);
|
|
588 |
Notification boring =
|
|
589 |
new Notification("boring", this, 1L, 1L, "boring");
|
|
590 |
Notification tricky =
|
|
591 |
new Notification("interesting", this, 1L, 1L, "tricky");
|
|
592 |
tricky.setUserData(unserializableObject);
|
|
593 |
|
|
594 |
// check that the tricky notif is indeed unserializable
|
|
595 |
try {
|
|
596 |
new ObjectOutputStream(new ByteArrayOutputStream())
|
|
597 |
.writeObject(tricky);
|
|
598 |
System.out.println("TEST INCORRECT: tricky notif is " +
|
|
599 |
"serializable");
|
|
600 |
System.exit(1);
|
|
601 |
} catch (NotSerializableException e) {
|
|
602 |
// OK: tricky notif is not serializable
|
|
603 |
} catch (IOException e) {
|
|
604 |
System.out.println("TEST INCORRECT: tricky notif " +
|
|
605 |
"serialization check failed");
|
|
606 |
System.exit(1);
|
|
607 |
}
|
|
608 |
|
|
609 |
/* Now shuffle an imaginary deck of cards where K, U, T, and
|
|
610 |
B (known, unknown, tricky, boring) each appear NNOTIFS times.
|
|
611 |
We explicitly seed the random number generator so we
|
|
612 |
can reproduce rare test failures if necessary. We only
|
|
613 |
use a StringBuffer so we can print the shuffled deck --
|
|
614 |
otherwise we could just emit the notifications as the
|
|
615 |
cards are placed. */
|
|
616 |
long seed = System.currentTimeMillis();
|
|
617 |
System.out.println("Random number seed is " + seed);
|
|
618 |
Random r = new Random(seed);
|
|
619 |
int knownCount = NNOTIFS; // remaining K cards
|
|
620 |
int unknownCount = NNOTIFS; // remaining U cards
|
|
621 |
int trickyCount = NNOTIFS; // remaining T cards
|
|
622 |
int boringCount = NNOTIFS; // remaining B cards
|
|
623 |
StringBuffer notifList = new StringBuffer();
|
|
624 |
for (int i = NNOTIFS * 4; i > 0; i--) {
|
|
625 |
int rand = r.nextInt(i);
|
|
626 |
// use rand to pick a card from the remaining ones
|
|
627 |
if ((rand -= knownCount) < 0) {
|
|
628 |
notifList.append('k');
|
|
629 |
knownCount--;
|
|
630 |
} else if ((rand -= unknownCount) < 0) {
|
|
631 |
notifList.append('u');
|
|
632 |
unknownCount--;
|
|
633 |
} else if ((rand -= trickyCount) < 0) {
|
|
634 |
notifList.append('t');
|
|
635 |
trickyCount--;
|
|
636 |
} else {
|
|
637 |
notifList.append('b');
|
|
638 |
boringCount--;
|
|
639 |
}
|
|
640 |
}
|
|
641 |
if (knownCount != 0 || unknownCount != 0
|
|
642 |
|| trickyCount != 0 || boringCount != 0) {
|
|
643 |
System.out.println("TEST INCORRECT: Shuffle failed: " +
|
|
644 |
"known=" + knownCount +" unknown=" +
|
|
645 |
unknownCount + " tricky=" + trickyCount +
|
|
646 |
" boring=" + boringCount +
|
|
647 |
" deal=" + notifList);
|
|
648 |
System.exit(1);
|
|
649 |
}
|
|
650 |
String notifs = notifList.toString();
|
|
651 |
System.out.println("Shuffle: " + notifs);
|
|
652 |
for (int i = 0; i < NNOTIFS * 4; i++) {
|
|
653 |
Notification n;
|
|
654 |
switch (notifs.charAt(i)) {
|
|
655 |
case 'k': n = known; break;
|
|
656 |
case 'u': n = unknown; break;
|
|
657 |
case 't': n = tricky; break;
|
|
658 |
case 'b': n = boring; break;
|
|
659 |
default:
|
|
660 |
System.out.println("TEST INCORRECT: Bad shuffle char: " +
|
|
661 |
notifs.charAt(i));
|
|
662 |
System.exit(1);
|
|
663 |
throw new Error();
|
|
664 |
}
|
|
665 |
sendNotification(n);
|
|
666 |
}
|
|
667 |
}
|
|
668 |
}
|
|
669 |
|
|
670 |
private static String notificationString(Notification n) {
|
|
671 |
return n.getClass().getName() + "/" + n.getType() + " \"" +
|
|
672 |
n.getMessage() + "\" <" + n.getUserData() + ">";
|
|
673 |
}
|
|
674 |
|
|
675 |
//
|
|
676 |
private static class CNListener implements NotificationListener {
|
|
677 |
public void handleNotification(Notification n, Object o) {
|
|
678 |
if (n instanceof JMXConnectionNotification) {
|
|
679 |
JMXConnectionNotification jn = (JMXConnectionNotification)n;
|
|
680 |
if (JMXConnectionNotification.FAILED.equals(jn.getType())) {
|
|
681 |
failed = true;
|
|
682 |
}
|
|
683 |
}
|
|
684 |
}
|
|
685 |
|
|
686 |
public boolean failed = false;
|
|
687 |
}
|
|
688 |
}
|