KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > implementation > util > MBeanProxyTEST


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 test.implementation.util;
23
24 import java.util.Set JavaDoc;
25
26 import java.lang.reflect.Method JavaDoc;
27
28 import javax.management.Attribute JavaDoc;
29 import javax.management.AttributeNotFoundException JavaDoc;
30 import javax.management.DynamicMBean JavaDoc;
31 import javax.management.InstanceNotFoundException JavaDoc;
32 import javax.management.MBeanServer JavaDoc;
33 import javax.management.MBeanServerFactory JavaDoc;
34 import javax.management.ObjectInstance JavaDoc;
35 import javax.management.ObjectName JavaDoc;
36
37 import javax.management.modelmbean.ModelMBean JavaDoc;
38 import javax.management.modelmbean.RequiredModelMBean JavaDoc;
39
40 import test.implementation.util.support.Trivial;
41 import test.implementation.util.support.TrivialMBean;
42 import test.implementation.util.support.Trivial2;
43 import test.implementation.util.support.Trivial2MBean;
44 import test.implementation.util.support.ExtendedResource;
45 import test.implementation.util.support.MyInterface;
46 import test.implementation.util.support.MyInterface2;
47 import test.implementation.util.support.Resource;
48 import test.implementation.util.support.ResourceOverride;
49 import test.implementation.util.support.ResourceIncorrectInfo;
50
51 import junit.framework.TestCase;
52
53 import org.jboss.mx.util.AgentID;
54 import org.jboss.mx.util.DefaultExceptionHandler;
55 import org.jboss.mx.util.MBeanProxy;
56 import org.jboss.mx.util.ProxyContext;
57
58 import org.jboss.mx.modelmbean.XMBean;
59
60
61 /**
62  * Tests for mbean proxy
63  *
64  * @author <a HREF="mailto:juha@jboss.org">Juha Lindfors</a>.
65  * @version $Revision: 37459 $
66  */

67 public class MBeanProxyTEST extends TestCase
68 {
69    public MBeanProxyTEST(String JavaDoc s)
70    {
71       super(s);
72    }
73
74    public void testGetWithServer() throws Exception JavaDoc
75    {
76       MBeanServer JavaDoc server = MBeanServerFactory.createMBeanServer();
77       ObjectName JavaDoc oname = new ObjectName JavaDoc("test:name=test");
78    
79       server.registerMBean(new Trivial(), oname);
80    
81       TrivialMBean mbean = (TrivialMBean)MBeanProxy.get(
82             TrivialMBean.class, oname, server);
83             
84       mbean.doOperation();
85    }
86    
87    public void testGetWithAgentID() throws Exception JavaDoc
88    {
89       MBeanServer JavaDoc server = MBeanServerFactory.createMBeanServer();
90       String JavaDoc agentID = AgentID.get(server);
91       ObjectName JavaDoc oname = new ObjectName JavaDoc("test:name=test");
92       
93       server.registerMBean(new Trivial(), oname);
94
95       TrivialMBean mbean = (TrivialMBean)MBeanProxy.get(
96             TrivialMBean.class, oname, agentID);
97             
98       mbean.doOperation();
99    }
100    
101    public void testCreateWithServer() throws Exception JavaDoc
102    {
103       MBeanServer JavaDoc server = MBeanServerFactory.createMBeanServer();
104       ObjectName JavaDoc oname = new ObjectName JavaDoc("test:test=test");
105       
106       TrivialMBean mbean = (TrivialMBean)MBeanProxy.create(
107             Trivial.class, TrivialMBean.class, oname, server);
108             
109       mbean.doOperation();
110    }
111    
112    public void testCreateWithAgentID() throws Exception JavaDoc
113    {
114       MBeanServer JavaDoc server = MBeanServerFactory.createMBeanServer();
115       ObjectName JavaDoc oname = new ObjectName JavaDoc("test:test=test");
116       String JavaDoc agentID = AgentID.get(server);
117       
118       TrivialMBean mbean = (TrivialMBean)MBeanProxy.create(
119             Trivial.class, TrivialMBean.class, oname, agentID);
120             
121       mbean.doOperation();
122    }
123    
124    public void testProxyInvocations() throws Exception JavaDoc
125    {
126       MBeanServer JavaDoc server = MBeanServerFactory.createMBeanServer();
127       ObjectName JavaDoc oname = new ObjectName JavaDoc("test:name=test");
128       
129       server.registerMBean(new Trivial(), oname);
130       
131       TrivialMBean mbean = (TrivialMBean)MBeanProxy.get(
132             TrivialMBean.class, oname, AgentID.get(server));
133       
134       mbean.doOperation();
135       mbean.setSomething("JBossMX");
136       
137       assertEquals("JBossMX", mbean.getSomething());
138    }
139
140    public void testProxyInvocationWithConflictingMBeanAndContextMethods() throws Exception JavaDoc
141    {
142       MBeanServer JavaDoc server = MBeanServerFactory.createMBeanServer();
143       ObjectName JavaDoc oname = new ObjectName JavaDoc("test:test=test");
144       
145       server.registerMBean(new Trivial(), oname);
146       
147       TrivialMBean mbean = (TrivialMBean)MBeanProxy.get(
148             TrivialMBean.class, oname, AgentID.get(server));
149             
150       mbean.getMBeanServer();
151       assertTrue(mbean.isGMSInvoked());
152    }
153    
154    public void testContextAccess() throws Exception JavaDoc
155    {
156       MBeanServer JavaDoc server = MBeanServerFactory.createMBeanServer();
157       ObjectName JavaDoc oname = new ObjectName JavaDoc("test:test=test");
158       
159       Trivial2MBean mbean = (Trivial2MBean)MBeanProxy.create(
160             Trivial2.class, Trivial2MBean.class, oname, server
161       );
162     
163       ProxyContext ctx = (ProxyContext)mbean;
164       
165       ctx.getMBeanServer();
166    }
167    
168    public void testProxyInvocationBetweenServers() throws Exception JavaDoc
169    {
170       MBeanServer JavaDoc server1 = MBeanServerFactory.createMBeanServer();
171       MBeanServer JavaDoc server2 = MBeanServerFactory.createMBeanServer();
172       ObjectName JavaDoc oname1 = new ObjectName JavaDoc("test:name=target");
173       ObjectName JavaDoc oname2 = new ObjectName JavaDoc("test:name=proxy");
174       
175       // createMBean on server1 and retrieve a proxy to it
176
Trivial2MBean mbean = (Trivial2MBean)MBeanProxy.create(
177             Trivial2.class, Trivial2MBean.class, oname1, server1
178       );
179       
180       //bind the proxy to server2
181
server2.registerMBean(mbean, oname2);
182       
183       // invoke on server2
184
server2.invoke(oname2, "doOperation", null, null);
185       
186       // check that server1 received the invocation
187
assertTrue(((Boolean JavaDoc)server1.getAttribute(oname1, "OperationInvoked")).booleanValue());
188    }
189    
190    public void testSimultaneousTypedAndDetypedInvocations() throws Exception JavaDoc
191    {
192       MBeanServer JavaDoc server = MBeanServerFactory.createMBeanServer();
193       ObjectName JavaDoc oname = new ObjectName JavaDoc("test:test=test");
194       
195       Trivial2MBean mbean = (Trivial2MBean)MBeanProxy.create(
196             Trivial2.class, Trivial2MBean.class, oname,server
197       );
198       
199       // typed proxy interface
200
mbean.setSomething("Kissa");
201       assertTrue(mbean.getSomething().equals("Kissa"));
202       
203       // detyped proxy interface
204
DynamicMBean JavaDoc mbean2 = (DynamicMBean JavaDoc)mbean;
205       mbean2.setAttribute(new Attribute JavaDoc("Something", "Koira"));
206       assertTrue(mbean2.getAttribute("Something").equals("Koira"));
207       
208       // direct local server invocation
209
server.setAttribute(oname, new Attribute JavaDoc("Something", "Kissa"));
210       assertTrue(server.getAttribute(oname, "Something").equals("Kissa"));
211             
212       // typed proxy interface invocation
213
mbean.doOperation();
214       assertTrue(mbean.isOperationInvoked());
215       
216       mbean.reset();
217       
218       // detyped proxy invocation
219
mbean2.invoke("doOperation", null, null);
220       assertTrue(((Boolean JavaDoc)mbean2.getAttribute("OperationInvoked")).booleanValue());
221       
222       mbean2.invoke("reset", null, null);
223       
224       // direct local server invocation
225
server.invoke(oname, "doOperation", null, null);
226       assertTrue(((Boolean JavaDoc)server.getAttribute(oname, "OperationInvoked")).booleanValue());
227    }
228    
229    public void testContextAccessToMBeanServer() throws Exception JavaDoc
230    {
231       MBeanServer JavaDoc server = MBeanServerFactory.createMBeanServer();
232       ObjectName JavaDoc oname = new ObjectName JavaDoc("test:test=test");
233       
234       Trivial2MBean mbean = (Trivial2MBean)MBeanProxy.create(
235             Trivial2.class, Trivial2MBean.class, oname, server
236       );
237       
238       // query the server this mbean is registered to
239
ProxyContext ctx = (ProxyContext)mbean;
240       MBeanServer JavaDoc srvr = ctx.getMBeanServer();
241       
242       Set JavaDoc mbeans = srvr.queryMBeans(new ObjectName JavaDoc("test:*"), null);
243       ObjectInstance JavaDoc oi = (ObjectInstance JavaDoc)mbeans.iterator().next();
244       
245       assertTrue(oi.getObjectName().equals(oname));
246       
247       assertTrue(srvr.getAttribute(
248             new ObjectName JavaDoc("JMImplementation:type=MBeanServerDelegate"),
249             "ImplementationName"
250       ).equals("JBossMX"));
251       
252    }
253    
254    public void testArbitraryInterfaceWithProxy() throws Exception JavaDoc
255    {
256       MBeanServer JavaDoc server = MBeanServerFactory.createMBeanServer();
257       ObjectName JavaDoc oname = new ObjectName JavaDoc("test:test=test");
258     
259       RequiredModelMBean JavaDoc rmm = new RequiredModelMBean JavaDoc();
260       Resource resource = new Resource();
261       
262       rmm.setManagedResource(resource, "ObjectReference");
263       rmm.setModelMBeanInfo(resource.getMBeanInfo());
264     
265       server.registerMBean(rmm, oname);
266       
267       MyInterface mbean = (MyInterface)MBeanProxy.get(
268             MyInterface.class, oname, server
269       );
270       
271       mbean.setAttributeName("foo");
272       mbean.setAttributeName2("bar");
273       
274       assertTrue(mbean.getAttributeName2().equals("bar"));
275       assertTrue(mbean.doOperation().equals("tamppi"));
276    }
277    
278    /**
279     * This test shows how to override the default exception handling for
280     * proxy invocations. The default handling for exceptions that are not
281     * declared as part of the proxy typed interface (such as InstanceNotFound
282     * or AttributeNotFound exceptions) are rethrown as unchecked
283     * RuntimeProxyExceptions. See org.jboss.mx.proxy.DefaultExceptionHandler
284     * for details. This behavior can be changed at runtime through the proxy
285     * context interface setExceptionHandler() method.
286     */

287    public void testCustomExceptionHandler() throws Exception JavaDoc
288    {
289       MBeanServer JavaDoc server = MBeanServerFactory.createMBeanServer();
290       ObjectName JavaDoc oname = new ObjectName JavaDoc("test:test=test");
291       ObjectName JavaDoc oname2 = new ObjectName JavaDoc("test:test=test2");
292       
293       RequiredModelMBean JavaDoc rmm = new RequiredModelMBean JavaDoc();
294       Resource resource = new Resource();
295       
296       rmm.setManagedResource(resource, "ObjectReference");
297       rmm.setModelMBeanInfo(resource.getMBeanInfo());
298       
299       // create two identical mbeans
300
server.registerMBean(rmm, oname);
301       server.registerMBean(rmm, oname2);
302       
303       ProxyContext ctx = (ProxyContext)MBeanProxy.get(
304             MyInterface.class, oname, server
305       );
306
307       // override InstanceNotFound exception to redirect from test=test instance
308
// to test=test2 instance
309
ctx.setExceptionHandler(new DefaultExceptionHandler()
310       {
311          public Object JavaDoc handleInstanceNotFound(ProxyContext proxyCtx, InstanceNotFoundException JavaDoc e, Method JavaDoc m, Object JavaDoc[] args) throws Exception JavaDoc
312          {
313             return proxyCtx.getMBeanServer().invoke(new ObjectName JavaDoc("test:test=test2"), m.getName(), args, null);
314          }
315       });
316          
317       // unregister mbean 1
318
server.unregisterMBean(oname);
319       
320       // invocation attempt to mbean1 should now redirect to mbean2
321
MyInterface mbean = (MyInterface)ctx;
322       assertTrue(mbean.doOperation().equals("tamppi"));
323    }
324    
325    public void testObjectToStringOnProxy() throws Exception JavaDoc
326    {
327       MBeanServer JavaDoc server = MBeanServerFactory.createMBeanServer();
328       ObjectName JavaDoc oname = new ObjectName JavaDoc("test:test=test");
329       
330       RequiredModelMBean JavaDoc rmm = new RequiredModelMBean JavaDoc();
331       Resource resource = new Resource();
332       
333       rmm.setManagedResource(resource, "ObjectReference");
334       rmm.setModelMBeanInfo(resource.getMBeanInfo());
335       
336       server.registerMBean(rmm, oname);
337       
338       MyInterface mbean = (MyInterface)MBeanProxy.get(
339             MyInterface.class, oname, server
340       );
341       
342       mbean.toString();
343       
344       Object JavaDoc o = (Object JavaDoc)mbean;
345       o.toString();
346    }
347    
348    public void testObjectToStringOverride() throws Exception JavaDoc
349    {
350       MBeanServer JavaDoc server = MBeanServerFactory.createMBeanServer();
351       ObjectName JavaDoc oname = new ObjectName JavaDoc("test:test=test");
352       
353       RequiredModelMBean JavaDoc rmm = new RequiredModelMBean JavaDoc();
354       ResourceOverride resource = new ResourceOverride();
355       
356       rmm.setManagedResource(resource, "ObjectReference");
357       rmm.setModelMBeanInfo(resource.getMBeanInfo());
358       
359       server.registerMBean(rmm, oname);
360       
361       MyInterface mbean = (MyInterface)MBeanProxy.get(
362             MyInterface.class, oname, server
363       );
364       
365       assertTrue(mbean.toString().equals("Resource"));
366       Object JavaDoc o = (Object JavaDoc)mbean;
367       assertTrue(o.toString().equals("Resource"));
368    }
369    
370    public void testObjectHashCodeOnProxy() throws Exception JavaDoc
371    {
372       MBeanServer JavaDoc server = MBeanServerFactory.createMBeanServer();
373       ObjectName JavaDoc oname = new ObjectName JavaDoc("test:test=test");
374       
375       RequiredModelMBean JavaDoc rmm = new RequiredModelMBean JavaDoc();
376       Resource resource = new Resource();
377       
378       rmm.setManagedResource(resource, "ObjectReference");
379       rmm.setModelMBeanInfo(resource.getMBeanInfo());
380       
381       server.registerMBean(rmm, oname);
382       
383       MyInterface mbean = (MyInterface)MBeanProxy.get(
384             MyInterface.class, oname, server
385       );
386       
387       mbean.hashCode();
388       
389       Object JavaDoc o = (Object JavaDoc)mbean;
390       o.toString();
391    }
392    
393    public void testObjectHashCodeOverride() throws Exception JavaDoc
394    {
395       MBeanServer JavaDoc server = MBeanServerFactory.createMBeanServer();
396       ObjectName JavaDoc oname = new ObjectName JavaDoc("test:test=test");
397       
398       RequiredModelMBean JavaDoc rmm = new RequiredModelMBean JavaDoc();
399       ResourceOverride resource = new ResourceOverride();
400       
401       rmm.setManagedResource(resource, "ObjectReference");
402       rmm.setModelMBeanInfo(resource.getMBeanInfo());
403       
404       server.registerMBean(rmm, oname);
405       
406       MyInterface mbean = (MyInterface)MBeanProxy.get(
407             MyInterface.class, oname, server
408       );
409
410       assertTrue(mbean.hashCode() == 10);
411       Object JavaDoc o = (Object JavaDoc)mbean;
412       assertTrue(o.hashCode() == 10);
413    }
414
415    public void testObjectEqualsOnProxy() throws Exception JavaDoc
416    {
417       MBeanServer JavaDoc server = MBeanServerFactory.createMBeanServer();
418       ObjectName JavaDoc oname = new ObjectName JavaDoc("test:test=test");
419
420       ModelMBean JavaDoc mmbean = new XMBean();
421       Resource resource = new Resource();
422
423       mmbean.setManagedResource(resource, "ObjectReference");
424       mmbean.setModelMBeanInfo(resource.getMBeanInfo());
425
426       server.registerMBean(mmbean, oname);
427
428       MyInterface mbean = (MyInterface)MBeanProxy.get(
429             MyInterface.class, oname, server
430       );
431       
432       MyInterface mbean2 = (MyInterface)MBeanProxy.get(
433             MyInterface.class, oname, server
434       );
435       
436       assertTrue(mbean.equals(mbean));
437       assertTrue(!mbean.equals(mbean2));
438       assertTrue(!mbean2.equals(mbean));
439    }
440    
441    public void testObjectEqualsOverride() throws Exception JavaDoc
442    {
443       MBeanServer JavaDoc server = MBeanServerFactory.createMBeanServer();
444       ObjectName JavaDoc oname = new ObjectName JavaDoc("test:test=test");
445       
446       ModelMBean JavaDoc rmm = new RequiredModelMBean JavaDoc();
447       ResourceOverride resource = new ResourceOverride("state");
448       
449       rmm.setManagedResource(resource, "ObjectReference");
450       rmm.setModelMBeanInfo(resource.getMBeanInfo());
451       
452       server.registerMBean(rmm, oname);
453             
454       MyInterface mbean = (MyInterface)MBeanProxy.get(
455             MyInterface.class, oname, server
456       );
457       
458       assertTrue(mbean.equals(mbean));
459    }
460    
461    public void testAttributeNotFoundOnTypedProxy() throws Exception JavaDoc
462    {
463       MBeanServer JavaDoc server = MBeanServerFactory.createMBeanServer();
464       ObjectName JavaDoc oname = new ObjectName JavaDoc("test:test=test");
465       
466       ModelMBean JavaDoc mmbean = new XMBean();
467       ResourceIncorrectInfo resource = new ResourceIncorrectInfo();
468       
469       mmbean.setManagedResource(resource, "ObjectReference");
470       mmbean.setModelMBeanInfo(resource.getMBeanInfo());
471       
472       server.registerMBean(mmbean, oname);
473       
474       MyInterface mbean = (MyInterface)MBeanProxy.get(
475             MyInterface.class, oname, server
476       );
477       
478       ProxyContext ctx = (ProxyContext)mbean;
479       ctx.setExceptionHandler(new DefaultExceptionHandler());
480       
481       try
482       {
483          mbean.setAttributeName2("some name");
484       }
485       catch (IllegalArgumentException JavaDoc e)
486       {
487          // expected
488

489          // by default, if no such attribute 'AttributeName2' exists in the
490
// MBean metadata (as is the case with ResourceIncorrectInfo), the
491
// MBeanProxy invocation handler falls back attempting to execute
492
// MBeanServer.invoke() which fails with IAE.
493
}
494    }
495
496    public void testAttributeNotFoundOnDeTypedProxy() throws Exception JavaDoc
497    {
498       MBeanServer JavaDoc server = MBeanServerFactory.createMBeanServer();
499       ObjectName JavaDoc oname = new ObjectName JavaDoc("test:test=test");
500       
501       ModelMBean JavaDoc mmbean = new XMBean();
502       ResourceIncorrectInfo resource = new ResourceIncorrectInfo();
503       
504       mmbean.setManagedResource(resource, "ObjectReference");
505       mmbean.setModelMBeanInfo(resource.getMBeanInfo());
506       
507       server.registerMBean(mmbean, oname);
508       
509       DynamicMBean JavaDoc mbean = (DynamicMBean JavaDoc)MBeanProxy.get(oname, server);
510       
511       ProxyContext ctx = (ProxyContext)mbean;
512       ctx.setExceptionHandler(new DefaultExceptionHandler());
513       
514       try
515       {
516          mbean.setAttribute(new Attribute JavaDoc("AttributeName2", "some name"));
517       }
518       catch (AttributeNotFoundException JavaDoc e)
519       {
520          // expected
521
}
522    }
523    
524    public void testInheritanceInTypedProxyArgs() throws Exception JavaDoc
525    {
526       MBeanServer JavaDoc server = MBeanServerFactory.createMBeanServer();
527       ObjectName JavaDoc oname = new ObjectName JavaDoc("test:test=test");
528       
529       XMBean mmbean = new XMBean();
530       ExtendedResource resource = new ExtendedResource();
531       
532       mmbean.setManagedResource(resource, "ObjectReference");
533       mmbean.setModelMBeanInfo(resource.getMBeanInfo());
534       
535       server.registerMBean(mmbean, oname);
536       
537       MyInterface2 mbean = (MyInterface2)MBeanProxy.get(
538             MyInterface2.class, oname, server);
539             
540       assertTrue(mbean.doOperation().equals("doOperation"));
541       
542       try
543       {
544          assertTrue(mbean.executeThis("executeThis").equals("executeThis"));
545       }
546       catch (ClassCastException JavaDoc e) {
547          fail("KNOWN ISSUE: proxy generates incorrect JMX invocation " +
548               "signature in case argument subclasses are used.");
549       }
550    }
551    
552    public void testInheritanceInProxyAttribute() throws Exception JavaDoc
553    {
554       MBeanServer JavaDoc server = MBeanServerFactory.createMBeanServer();
555       ObjectName JavaDoc oname = new ObjectName JavaDoc("test:test=test");
556       
557       XMBean mmbean = new XMBean();
558       ExtendedResource resource = new ExtendedResource();
559       
560       mmbean.setManagedResource(resource, "ObjectReference");
561       mmbean.setModelMBeanInfo(resource.getMBeanInfo());
562       
563       server.registerMBean(mmbean, oname);
564       
565       MyInterface2 mbean = (MyInterface2)MBeanProxy.get(
566             MyInterface2.class, oname, server);
567             
568       mbean.setAttribute3("Attribute3");
569       
570       assertTrue(mbean.getAttribute3().equals("Attribute3"));
571    }
572    
573    public void testInheritanceInProxyReturnType() throws Exception JavaDoc
574    {
575       MBeanServer JavaDoc server = MBeanServerFactory.createMBeanServer();
576       ObjectName JavaDoc oname = new ObjectName JavaDoc("test:test=test");
577       
578       XMBean mmbean = new XMBean();
579       ExtendedResource resource = new ExtendedResource();
580       
581       mmbean.setManagedResource(resource, "ObjectReference");
582       mmbean.setModelMBeanInfo(resource.getMBeanInfo());
583       
584       server.registerMBean(mmbean, oname);
585       
586       MyInterface2 mbean = (MyInterface2)MBeanProxy.get(
587             MyInterface2.class, oname, server);
588             
589       assertTrue(mbean.runMe("runMe").equals("runMe"));
590    }
591    
592 }
593
Popular Tags