blob: 2b82bb78af81f8fc2bdda69b8035c3bbd46fe366 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 2004 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 * @summary Tests to send a not serializable notification.
27 * @bug 5022196
28 * @author Shanliang JIANG
29 * @run clean NotSerializableNotifTest
30 * @run build NotSerializableNotifTest
31 * @run main NotSerializableNotifTest
32 */
33
34// java imports
35//
36import java.io.IOException;
37import java.net.MalformedURLException;
38
39// JMX imports
40//
41import javax.management.* ;
42
43import javax.management.remote.*;
44import javax.management.remote.JMXServiceURL;
45
46public class NotSerializableNotifTest {
47 private static MBeanServer mbeanServer = MBeanServerFactory.createMBeanServer();
48 private static ObjectName emitter;
49 private static int port = 2468;
50
51 private static String[] protocols;
52
53 private static final int sentNotifs = 10;
54
55 public static void main(String[] args) throws Exception {
56 System.out.println(">>> Test to send a not serializable notification");
57
58 // IIOP fails on JDK1.4, see 5034318
59 final String v = System.getProperty("java.version");
60 float f = Float.parseFloat(v.substring(0, 3));
61 if (f<1.5) {
62 protocols = new String[] {"rmi", "jmxmp"};
63 } else {
64 protocols = new String[] {"rmi", "iiop", "jmxmp"};
65 }
66
67 emitter = new ObjectName("Default:name=NotificationEmitter");
68 mbeanServer.registerMBean(new NotificationEmitter(), emitter);
69
70 boolean ok = true;
71 for (int i = 0; i < protocols.length; i++) {
72 try {
73 if (!test(protocols[i])) {
74 System.out.println(">>> Test failed for " + protocols[i]);
75 ok = false;
76 } else {
77 System.out.println(">>> Test successed for " + protocols[i]);
78 }
79 } catch (Exception e) {
80 System.out.println(">>> Test failed for " + protocols[i]);
81 e.printStackTrace(System.out);
82 ok = false;
83 }
84 }
85
86 if (ok) {
87 System.out.println(">>> Test passed");
88 } else {
89 System.out.println(">>> TEST FAILED");
90 System.exit(1);
91 }
92 }
93
94
95 private static boolean test(String proto) throws Exception {
96 System.out.println("\n>>> Test for protocol " + proto);
97
98 JMXServiceURL url = new JMXServiceURL(proto, null, port++);
99
100 System.out.println(">>> Create a server: "+url);
101
102 JMXConnectorServer server = null;
103 try {
104 server = JMXConnectorServerFactory.newJMXConnectorServer(url, null, mbeanServer);
105 } catch (MalformedURLException e) {
106 System.out.println("System does not recognize URL: " + url +
107 "; ignoring");
108 return true;
109 }
110
111 server.start();
112
113 url = server.getAddress();
114
115 System.out.println(">>> Creating a client connectint to: "+url);
116 JMXConnector conn = JMXConnectorFactory.connect(url, null);
117 MBeanServerConnection client = conn.getMBeanServerConnection();
118
119 // add listener from the client side
120 Listener listener = new Listener();
121 client.addNotificationListener(emitter, listener, null, null);
122
123 // ask to send one not serializable notif
124 Object[] params = new Object[] {new Integer(1)};
125 String[] signatures = new String[] {"java.lang.Integer"};
126 client.invoke(emitter, "sendNotserializableNotifs", params, signatures);
127
128 // listener clean
129 client.removeNotificationListener(emitter, listener);
130 listener = new Listener();
131 client.addNotificationListener(emitter, listener, null, null);
132
133 //ask to send serializable notifs
134 params = new Object[] {new Integer(sentNotifs)};
135 client.invoke(emitter, "sendNotifications", params, signatures);
136
137 // waiting ...
138 synchronized (listener) {
139 for (int i=0; i<10; i++) {
140 if (listener.received() < sentNotifs) {
141 listener.wait(1000);
142 } else {
143 break;
144 }
145 }
146 }
147
148 // check
149 boolean ok = true;
150
151 if (listener.received() != sentNotifs) {
152 System.out.println("Failed: received "+listener.received()+
153 " but should be "+sentNotifs);
154 ok = false;
155 } else {
156 System.out.println("The client received all notifications.");
157 }
158
159 // clean
160 client.removeNotificationListener(emitter, listener);
161
162 conn.close();
163 server.stop();
164
165 return ok;
166 }
167
168//--------------------------
169// private classes
170//--------------------------
171
172 private static class Listener implements NotificationListener {
173 public void handleNotification(Notification notif, Object handback) {
174 synchronized (this) {
175 if(++receivedNotifs == sentNotifs) {
176 this.notifyAll();
177 }
178 }
179 }
180
181 public int received() {
182 return receivedNotifs;
183 }
184
185 private int receivedNotifs = 0;
186 }
187
188 public static class NotificationEmitter extends NotificationBroadcasterSupport
189 implements NotificationEmitterMBean {
190
191 public MBeanNotificationInfo[] getNotificationInfo() {
192 final String[] ntfTypes = {myType};
193
194 final MBeanNotificationInfo[] ntfInfoArray = {
195 new MBeanNotificationInfo(ntfTypes,
196 "javax.management.Notification",
197 "Notifications sent by the NotificationEmitter")};
198
199 return ntfInfoArray;
200 }
201
202 /**
203 * Send not serializable Notifications.
204 *
205 * @param nb The number of notifications to send
206 */
207 public void sendNotserializableNotifs(Integer nb) {
208
209 Notification notif;
210 for (int i=1; i<=nb.intValue(); i++) {
211 notif = new Notification(myType, this, i);
212
213 notif.setUserData(new Object());
214 sendNotification(notif);
215 }
216 }
217
218 /**
219 * Send Notification objects.
220 *
221 * @param nb The number of notifications to send
222 */
223 public void sendNotifications(Integer nb) {
224 Notification notif;
225 for (int i=1; i<=nb.intValue(); i++) {
226 notif = new Notification(myType, this, i);
227
228 sendNotification(notif);
229 }
230 }
231
232 private final String myType = "notification.my_notification";
233 }
234
235 public interface NotificationEmitterMBean {
236 public void sendNotifications(Integer nb);
237
238 public void sendNotserializableNotifs(Integer nb);
239 }
240}