KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > jmx > compliance > server > MBeanServerInvocationHandlerTestCase


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.test.jmx.compliance.server;
23
24 import java.util.ArrayList JavaDoc;
25
26 import javax.management.MBeanNotificationInfo JavaDoc;
27 import javax.management.MBeanServer JavaDoc;
28 import javax.management.MBeanServerFactory JavaDoc;
29 import javax.management.MBeanServerInvocationHandler JavaDoc;
30 import javax.management.Notification JavaDoc;
31 import javax.management.NotificationEmitter JavaDoc;
32 import javax.management.NotificationFilterSupport JavaDoc;
33 import javax.management.NotificationListener JavaDoc;
34 import javax.management.ObjectName JavaDoc;
35
36 import junit.framework.TestCase;
37
38 import org.jboss.test.jmx.compliance.server.support.BroadcasterInvocationHandlerTest;
39 import org.jboss.test.jmx.compliance.server.support.EmitterInvocationHandlerTest;
40 import org.jboss.test.jmx.compliance.server.support.InvocationHandlerTest;
41 import org.jboss.test.jmx.compliance.server.support.InvocationHandlerTestMBean;
42 import org.jboss.test.jmx.compliance.server.support.ObjectInvocationHandlerTest;
43
44 /**
45  * Tests the MBeanServerInvocationHandler
46  *
47  * @author <a HREF="mailto:Adrian.Brock@HappeningTimes.com">Adrian Brock</a>.
48  */

49 public class MBeanServerInvocationHandlerTestCase
50    extends TestCase
51    implements NotificationListener JavaDoc
52 {
53    // Attributes ----------------------------------------------------------------
54

55    private ObjectName JavaDoc invocationHandlerTestName;
56
57    private ArrayList JavaDoc messages = new ArrayList JavaDoc();
58
59    // Constructor ---------------------------------------------------------------
60

61    /**
62     * Construct the test
63     */

64    public MBeanServerInvocationHandlerTestCase(String JavaDoc s)
65    {
66       super(s);
67
68       try
69       {
70          invocationHandlerTestName = new ObjectName JavaDoc("MBeanServerInvocationHandlerTestCase:type=InvocationHandlerTest");
71       }
72       catch (Exception JavaDoc e)
73       {
74          throw new RuntimeException JavaDoc(e.toString());
75       }
76    }
77
78    // Tests ---------------------------------------------------------------------
79

80    public void testConstructor()
81       throws Exception JavaDoc
82    {
83       MBeanServerFactory.newMBeanServer();
84    }
85
86    public void testGetter()
87       throws Exception JavaDoc
88    {
89       MBeanServer JavaDoc server = MBeanServerFactory.newMBeanServer();
90       InvocationHandlerTest test = new InvocationHandlerTest();
91       server.registerMBean(test, invocationHandlerTestName);
92
93       InvocationHandlerTestMBean proxy = (InvocationHandlerTestMBean) MBeanServerInvocationHandler.newProxyInstance(
94          server, invocationHandlerTestName, InvocationHandlerTestMBean.class, false);
95       assertEquals("Attribute", proxy.getAttribute());
96    }
97
98    public void testSetter()
99       throws Exception JavaDoc
100    {
101       MBeanServer JavaDoc server = MBeanServerFactory.newMBeanServer();
102       InvocationHandlerTest test = new InvocationHandlerTest();
103       server.registerMBean(test, invocationHandlerTestName);
104       InvocationHandlerTestMBean proxy = (InvocationHandlerTestMBean) MBeanServerInvocationHandler.newProxyInstance(
105          server, invocationHandlerTestName, InvocationHandlerTestMBean.class, false);
106
107       proxy.setAttribute("Changed");
108       assertEquals("Changed", test.getAttribute());
109    }
110
111    public void testGetterPrimitiveBoolean()
112       throws Exception JavaDoc
113    {
114       MBeanServer JavaDoc server = MBeanServerFactory.newMBeanServer();
115       InvocationHandlerTest test = new InvocationHandlerTest();
116       server.registerMBean(test, invocationHandlerTestName);
117
118       InvocationHandlerTestMBean proxy = (InvocationHandlerTestMBean) MBeanServerInvocationHandler.newProxyInstance(
119          server, invocationHandlerTestName, InvocationHandlerTestMBean.class, false);
120
121       assertEquals(false, test.isIsPrimitive());
122       test.setIsPrimitive(true);
123
124       assertEquals(true, proxy.isIsPrimitive());
125    }
126
127    public void testSetterPrimitiveBoolean()
128       throws Exception JavaDoc
129    {
130       MBeanServer JavaDoc server = MBeanServerFactory.newMBeanServer();
131       InvocationHandlerTest test = new InvocationHandlerTest();
132       server.registerMBean(test, invocationHandlerTestName);
133       InvocationHandlerTestMBean proxy = (InvocationHandlerTestMBean) MBeanServerInvocationHandler.newProxyInstance(
134          server, invocationHandlerTestName, InvocationHandlerTestMBean.class, false);
135
136       assertEquals(false, test.isIsPrimitive());
137       proxy.setIsPrimitive(true);
138
139       assertEquals(true, test.isIsPrimitive());
140    }
141
142    public void testGetterTypeBoolean()
143       throws Exception JavaDoc
144    {
145       MBeanServer JavaDoc server = MBeanServerFactory.newMBeanServer();
146       InvocationHandlerTest test = new InvocationHandlerTest();
147       server.registerMBean(test, invocationHandlerTestName);
148
149       InvocationHandlerTestMBean proxy = (InvocationHandlerTestMBean) MBeanServerInvocationHandler.newProxyInstance(
150          server, invocationHandlerTestName, InvocationHandlerTestMBean.class, false);
151
152       assertEquals(null, test.getType());
153       test.setType(new Boolean JavaDoc(true));
154
155       assertEquals(true, proxy.getType().booleanValue());
156    }
157
158    public void testSetterTypeBoolean()
159       throws Exception JavaDoc
160    {
161       MBeanServer JavaDoc server = MBeanServerFactory.newMBeanServer();
162       InvocationHandlerTest test = new InvocationHandlerTest();
163       server.registerMBean(test, invocationHandlerTestName);
164       InvocationHandlerTestMBean proxy = (InvocationHandlerTestMBean) MBeanServerInvocationHandler.newProxyInstance(
165          server, invocationHandlerTestName, InvocationHandlerTestMBean.class, false);
166
167       assertEquals(null, test.getType());
168       proxy.setType(new Boolean JavaDoc(true));
169
170       assertEquals(true, test.getType().booleanValue());
171    }
172
173    public void testInvokeNoArgsNoReturn()
174       throws Exception JavaDoc
175    {
176       MBeanServer JavaDoc server = MBeanServerFactory.newMBeanServer();
177       InvocationHandlerTest test = new InvocationHandlerTest();
178       server.registerMBean(test, invocationHandlerTestName);
179       InvocationHandlerTestMBean proxy = (InvocationHandlerTestMBean) MBeanServerInvocationHandler.newProxyInstance(
180          server, invocationHandlerTestName, InvocationHandlerTestMBean.class, false);
181
182       proxy.invokeNoArgsNoReturn();
183       assertTrue(test.invokeNoArgsNoReturnInvoked);
184    }
185
186    public void testInvokeNoArgs()
187       throws Exception JavaDoc
188    {
189       MBeanServer JavaDoc server = MBeanServerFactory.newMBeanServer();
190       InvocationHandlerTest test = new InvocationHandlerTest();
191       server.registerMBean(test, invocationHandlerTestName);
192       InvocationHandlerTestMBean proxy = (InvocationHandlerTestMBean) MBeanServerInvocationHandler.newProxyInstance(
193          server, invocationHandlerTestName, InvocationHandlerTestMBean.class, false);
194
195       assertEquals("invokeNoArgs", proxy.invokeNoArgs());
196    }
197
198    public void testInvoke()
199       throws Exception JavaDoc
200    {
201       MBeanServer JavaDoc server = MBeanServerFactory.newMBeanServer();
202       InvocationHandlerTest test = new InvocationHandlerTest();
203       server.registerMBean(test, invocationHandlerTestName);
204       InvocationHandlerTestMBean proxy = (InvocationHandlerTestMBean) MBeanServerInvocationHandler.newProxyInstance(
205          server, invocationHandlerTestName, InvocationHandlerTestMBean.class, false);
206
207       assertEquals("parameter", proxy.invoke("parameter"));
208    }
209
210    public void testInvokeMixedParameters()
211       throws Exception JavaDoc
212    {
213       MBeanServer JavaDoc server = MBeanServerFactory.newMBeanServer();
214       InvocationHandlerTest test = new InvocationHandlerTest();
215       server.registerMBean(test, invocationHandlerTestName);
216       InvocationHandlerTestMBean proxy = (InvocationHandlerTestMBean) MBeanServerInvocationHandler.newProxyInstance(
217          server, invocationHandlerTestName, InvocationHandlerTestMBean.class, false);
218
219       Integer JavaDoc parameter = new Integer JavaDoc(20);
220       assertEquals(parameter, proxy.invokeMixedParameters("parameter", 10, parameter));
221    }
222
223    public void testNotificationEmitterAdd()
224       throws Exception JavaDoc
225    {
226       MBeanServer JavaDoc server = MBeanServerFactory.newMBeanServer();
227       EmitterInvocationHandlerTest test = new EmitterInvocationHandlerTest();
228       server.registerMBean(test, invocationHandlerTestName);
229       NotificationEmitter JavaDoc proxy = (NotificationEmitter JavaDoc) MBeanServerInvocationHandler.newProxyInstance(
230          server, invocationHandlerTestName, InvocationHandlerTestMBean.class, true);
231
232       proxy.addNotificationListener(this, null, null);
233
234       messages.clear();
235       test.sendNotification();
236       assertTrue(messages.size() == 1);
237    }
238
239    public void testNotificationEmitterRemove()
240       throws Exception JavaDoc
241    {
242       MBeanServer JavaDoc server = MBeanServerFactory.newMBeanServer();
243       EmitterInvocationHandlerTest test = new EmitterInvocationHandlerTest();
244       server.registerMBean(test, invocationHandlerTestName);
245       NotificationEmitter JavaDoc proxy = (NotificationEmitter JavaDoc) MBeanServerInvocationHandler.newProxyInstance(
246          server, invocationHandlerTestName, InvocationHandlerTestMBean.class, true);
247
248       proxy.addNotificationListener(this, null, null);
249
250       messages.clear();
251       test.sendNotification();
252       assertTrue(messages.size() == 1);
253
254       proxy.removeNotificationListener(this);
255
256       messages.clear();
257       test.sendNotification();
258       assertTrue(messages.size() == 0);
259    }
260
261    public void testNotificationEmitterRemoveTriplet()
262       throws Exception JavaDoc
263    {
264       MBeanServer JavaDoc server = MBeanServerFactory.newMBeanServer();
265       EmitterInvocationHandlerTest test = new EmitterInvocationHandlerTest();
266       server.registerMBean(test, invocationHandlerTestName);
267       NotificationEmitter JavaDoc proxy = (NotificationEmitter JavaDoc) MBeanServerInvocationHandler.newProxyInstance(
268          server, invocationHandlerTestName, InvocationHandlerTestMBean.class, true);
269
270       NotificationFilterSupport JavaDoc filter = new NotificationFilterSupport JavaDoc();
271       filter.enableType("test");
272       Object JavaDoc handback = new Object JavaDoc();
273       proxy.addNotificationListener(this, filter, handback);
274
275       messages.clear();
276       test.sendNotification();
277       assertTrue(messages.size() == 1);
278
279       proxy.removeNotificationListener(this, filter, handback);
280
281       messages.clear();
282       test.sendNotification();
283       assertTrue(messages.size() == 0);
284    }
285
286    public void testNotificationEmitterRemoveTripletFailsOnBroadcaster()
287       throws Exception JavaDoc
288    {
289       MBeanServer JavaDoc server = MBeanServerFactory.newMBeanServer();
290       BroadcasterInvocationHandlerTest test = new BroadcasterInvocationHandlerTest();
291       server.registerMBean(test, invocationHandlerTestName);
292       NotificationEmitter JavaDoc proxy = (NotificationEmitter JavaDoc) MBeanServerInvocationHandler.newProxyInstance(
293          server, invocationHandlerTestName, InvocationHandlerTestMBean.class, true);
294
295       NotificationFilterSupport JavaDoc filter = new NotificationFilterSupport JavaDoc();
296       filter.enableType("test");
297       Object JavaDoc handback = new Object JavaDoc();
298       proxy.addNotificationListener(this, filter, handback);
299
300       messages.clear();
301       test.sendNotification();
302       assertTrue(messages.size() == 1);
303
304       try
305       {
306          proxy.removeNotificationListener(this, filter, handback);
307          fail("FAILS IN JBOSSMX: removeNotificationListener(NotificationListener, NotificationFilter, Object) " +
308               "should not work for a broadcaster");
309       }
310       catch (Exception JavaDoc ignored)
311       {
312       }
313    }
314
315    public void testGetNotificationInfo()
316       throws Exception JavaDoc
317    {
318       MBeanServer JavaDoc server = MBeanServerFactory.newMBeanServer();
319       EmitterInvocationHandlerTest test = new EmitterInvocationHandlerTest();
320       server.registerMBean(test, invocationHandlerTestName);
321       NotificationEmitter JavaDoc proxy = (NotificationEmitter JavaDoc) MBeanServerInvocationHandler.newProxyInstance(
322          server, invocationHandlerTestName, InvocationHandlerTestMBean.class, true);
323
324       MBeanNotificationInfo JavaDoc[] info = proxy.getNotificationInfo();
325       assertEquals("test", info[0].getNotifTypes()[0]);
326    }
327
328    public void testToString()
329       throws Exception JavaDoc
330    {
331       MBeanServer JavaDoc server = MBeanServerFactory.newMBeanServer();
332       ObjectInvocationHandlerTest test = new ObjectInvocationHandlerTest();
333       server.registerMBean(test, invocationHandlerTestName);
334       InvocationHandlerTestMBean proxy = (InvocationHandlerTestMBean) MBeanServerInvocationHandler.newProxyInstance(
335          server, invocationHandlerTestName, InvocationHandlerTestMBean.class, true);
336
337       assertEquals("TOSTRING", proxy.toString());
338    }
339
340    public void testToStringFailsWhenNotExposed()
341       throws Exception JavaDoc
342    {
343       MBeanServer JavaDoc server = MBeanServerFactory.newMBeanServer();
344       InvocationHandlerTest test = new InvocationHandlerTest();
345       server.registerMBean(test, invocationHandlerTestName);
346       InvocationHandlerTestMBean proxy = (InvocationHandlerTestMBean) MBeanServerInvocationHandler.newProxyInstance(
347          server, invocationHandlerTestName, InvocationHandlerTestMBean.class, true);
348
349       try
350       {
351          proxy.toString();
352          fail("toString() should not work when it is not exposed for management");
353       }
354       catch (Exception JavaDoc ignored)
355       {
356       }
357    }
358
359    public void testEquals()
360       throws Exception JavaDoc
361    {
362       MBeanServer JavaDoc server = MBeanServerFactory.newMBeanServer();
363       ObjectInvocationHandlerTest test = new ObjectInvocationHandlerTest();
364       server.registerMBean(test, invocationHandlerTestName);
365       InvocationHandlerTestMBean proxy = (InvocationHandlerTestMBean) MBeanServerInvocationHandler.newProxyInstance(
366          server, invocationHandlerTestName, InvocationHandlerTestMBean.class, true);
367
368       assertTrue(proxy.equals(new Object JavaDoc()));
369    }
370
371    public void testEqualsFailsWhenNotExposed()
372       throws Exception JavaDoc
373    {
374       MBeanServer JavaDoc server = MBeanServerFactory.newMBeanServer();
375       InvocationHandlerTest test = new InvocationHandlerTest();
376       server.registerMBean(test, invocationHandlerTestName);
377       InvocationHandlerTestMBean proxy = (InvocationHandlerTestMBean) MBeanServerInvocationHandler.newProxyInstance(
378          server, invocationHandlerTestName, InvocationHandlerTestMBean.class, true);
379
380       try
381       {
382          proxy.equals(new Object JavaDoc());
383          fail("equals(Object) should not work when it is not exposed for management");
384       }
385       catch (Exception JavaDoc ignored)
386       {
387       }
388    }
389
390    public void testHashCode()
391       throws Exception JavaDoc
392    {
393       MBeanServer JavaDoc server = MBeanServerFactory.newMBeanServer();
394       ObjectInvocationHandlerTest test = new ObjectInvocationHandlerTest();
395       server.registerMBean(test, invocationHandlerTestName);
396       InvocationHandlerTestMBean proxy = (InvocationHandlerTestMBean) MBeanServerInvocationHandler.newProxyInstance(
397          server, invocationHandlerTestName, InvocationHandlerTestMBean.class, true);
398
399       assertEquals(1234, proxy.hashCode());
400    }
401
402    public void testHashCodeFailsWhenNotExposed()
403       throws Exception JavaDoc
404    {
405       MBeanServer JavaDoc server = MBeanServerFactory.newMBeanServer();
406       InvocationHandlerTest test = new InvocationHandlerTest();
407       server.registerMBean(test, invocationHandlerTestName);
408       InvocationHandlerTestMBean proxy = (InvocationHandlerTestMBean) MBeanServerInvocationHandler.newProxyInstance(
409          server, invocationHandlerTestName, InvocationHandlerTestMBean.class, true);
410
411       try
412       {
413          proxy.hashCode();
414          fail("hashCode() should not work when it is not exposed for management");
415       }
416       catch (Exception JavaDoc ignored)
417       {
418       }
419    }
420
421    // Notification Listener -----------------------------------------------------
422

423    public void handleNotification(Notification JavaDoc notification, Object JavaDoc handback)
424    {
425       messages.add(notification);
426    }
427 }
428
Popular Tags