KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > compliance > server > MBeanServerTEST


1 /*
2  * JBoss, the OpenSource J2EE webOS
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7
8 package test.compliance.server;
9
10 import java.util.Arrays;
11 import java.util.List;
12
13 import javax.management.Attribute;
14 import javax.management.AttributeNotFoundException;
15 import javax.management.InstanceNotFoundException;
16 import javax.management.MBeanException;
17 import javax.management.MBeanRegistrationException;
18 import javax.management.MBeanServer;
19 import javax.management.MBeanServerFactory;
20 import javax.management.MBeanServerNotification;
21 import javax.management.Notification;
22 import javax.management.NotificationFilterSupport;
23 import javax.management.NotificationListener;
24 import javax.management.ObjectInstance;
25 import javax.management.ObjectName;
26 import javax.management.ReflectionException;
27 import javax.management.RuntimeErrorException;
28 import javax.management.RuntimeMBeanException;
29 import javax.management.RuntimeOperationsException;
30 import javax.management.loading.MLet;
31
32 import junit.framework.AssertionFailedError;
33 import junit.framework.TestCase;
34 import test.compliance.server.support.BabarError;
35 import test.compliance.server.support.Base;
36 import test.compliance.server.support.BaseMBean;
37 import test.compliance.server.support.Broadcaster;
38 import test.compliance.server.support.Derived;
39 import test.compliance.server.support.Dynamic;
40 import test.compliance.server.support.ExceptionOnTheRun;
41 import test.compliance.server.support.LockedTest;
42 import test.compliance.server.support.LockedTest2;
43 import test.compliance.server.support.LockedTest3;
44 import test.compliance.server.support.MBeanListener;
45 import test.compliance.server.support.MyScreamingException;
46 import test.compliance.server.support.Test;
47 import test.compliance.server.support.Test2;
48 import test.compliance.server.support.Test3;
49 import test.compliance.server.support.Test4;
50 import test.compliance.server.support.Unrelated;
51 import test.compliance.server.support.UnrelatedMBean;
52
53 /**
54  * Tests the MBean server impl. through the <tt>MBeanServer</tt> interface.
55  *
56  * @author <a HREF="mailto:juha@jboss.org">Juha Lindfors</a>.
57  * @version $Revision: 1.27 $
58  *
59  */

60 public class MBeanServerTEST
61    extends TestCase
62 {
63    public MBeanServerTEST(String s)
64    {
65       super(s);
66    }
67
68    
69    // MBeanServer invoke --------------------------------------------
70

71    /**
72     * Tests invoke with primitive boolean return type. <p>
73     */

74    public void testInvokeWithPrimitiveBooleanReturn() throws Exception
75    {
76       MBeanServer server = MBeanServerFactory.newMBeanServer();
77       ObjectName name = new ObjectName(":test=test");
78       server.registerMBean(new Test(), name);
79       
80       Boolean bool = (Boolean)server.invoke(name, "opWithPrimBooleanReturn", null, null);
81       
82       assertTrue(bool.booleanValue() == true);
83    }
84
85    /**
86     * Tests invoke with primitive long array return type. <p>
87     */

88    public void testInvokeWithPrimitiveLongArrayReturn() throws Exception
89    {
90       MBeanServer server = MBeanServerFactory.newMBeanServer();
91       ObjectName name = new ObjectName(":test=test");
92       server.registerMBean(new Test(), name);
93       
94       long[] array = (long[])server.invoke(name, "opWithPrimLongArrayReturn", null, null);
95       
96       assertTrue(array [0] == 1);
97       assertTrue(array [1] == 2);
98       assertTrue(array [2] == 3);
99    }
100
101    /**
102     * Tests invoke with Long array return type. <p>
103     */

104    public void testInvokeWithLongArrayReturn() throws Exception
105    {
106       MBeanServer server = MBeanServerFactory.newMBeanServer();
107       ObjectName name = new ObjectName(":test=test");
108       server.registerMBean(new Test(), name);
109       
110       Long[] array = (Long[])server.invoke(name, "opWithLongArrayReturn", null, null);
111       
112       assertTrue(array [0].longValue() == 1);
113       assertTrue(array [1].longValue() == 2);
114       assertTrue(array [2].longValue() == 3);
115    }
116    
117    /**
118     * Tests invoke with primitive long return type. <p>
119     */

120    public void testInvokeWithPrimitiveLongReturn() throws Exception
121    {
122       MBeanServer server = MBeanServerFactory.newMBeanServer();
123       ObjectName name = new ObjectName(":test=test");
124       server.registerMBean(new Test(), name);
125       
126       Long l = (Long)server.invoke(name, "opWithPrimLongReturn", null, null);
127       
128       assertTrue(l.longValue() == 1234567890123l);
129    }
130
131    /**
132     * Tests invoke with primitive double return type. <p>
133     */

134    public void testInvokeWithPrimitiveDoubleReturn() throws Exception
135    {
136       MBeanServer server = MBeanServerFactory.newMBeanServer();
137       ObjectName name = new ObjectName(":test=test");
138       server.registerMBean(new Test(), name);
139       
140       Double d = (Double)server.invoke(name, "opWithPrimDoubleReturn", null, null);
141       
142       assertTrue(d.doubleValue() == 0.1234567890123d);
143    }
144    
145    /**
146     * Tests invoke with long signature. <p>
147     */

148    public void testInvokeWithLongSignature() throws Exception
149    {
150       MBeanServer server = MBeanServerFactory.newMBeanServer();
151       ObjectName name = new ObjectName(":test=test");
152       server.registerMBean(new Test(), name);
153       
154       server.invoke(name, "opWithLongSignature",
155       new Object[] { new Integer(1), new Integer(2), new Integer(3), new Integer(4), new Integer(5),
156                      new Integer(6), new Integer(7), new Integer(8), new Integer(9), new Integer(10),
157                      new Integer(11), new Integer(12), new Integer(13), new Integer(14), new Integer(15),
158                      new Integer(16), new Integer(17), new Integer(18), new Integer(19), new Integer(20) },
159       new String[] { "int", "int", "int", "int", "int", "int", "int", "int", "int", "int",
160                      "int", "int", "int", "int", "int", "int", "int", "int", "int", "int" }
161       );
162    }
163
164    /**
165     * Tests invoke with mixed types in signature, especially types with double
166     * byte code length and arrays. <p>
167     */

168    public void testInvokeWithMixedSignature() throws Exception
169    {
170       MBeanServer server = MBeanServerFactory.newMBeanServer();
171       ObjectName name = new ObjectName(":test=test");
172       server.registerMBean(new Test(), name);
173       
174       server.invoke(name, "opWithMixedSignature",
175       new Object[] { new Integer(1), new Double(2.2D), new Long(333L), new Boolean(true), new Byte((byte)0x02),
176                      new Short((short)6), new long[]{7L, 8L}, new Long[]{new Long(1L), new Long(2L)}, new Short((short)9), new Byte((byte)10),
177                      new Long(11L), new Double(1.2D), new Integer(13), new Integer(14), new Integer(15),
178                      new Integer(16), new Integer(17), new Integer(18), new Integer(19), new Integer(20) },
179       new String[] { "int", "double", "long", "boolean", "byte", "short", "[J", "[Ljava.lang.Long;", "java.lang.Short", "java.lang.Byte",
180                      "java.lang.Long", "java.lang.Double", "int", "int", "int", "int", "int", "int", "int", "int" }
181       );
182    }
183
184    
185    /**
186     * Attempts to invoke a method on an unregistered MBean; <tt>InstanceNotFoundException</tt> should occur.
187     */

188    public void testInvokeWithNonExistantMBean() throws Exception
189    {
190       try
191       {
192          MBeanServer server = MBeanServerFactory.newMBeanServer();
193          server.invoke(new ObjectName(":mbean=doesnotexist"), "noMethod", null, null);
194
195          // should not reach here
196
fail("InstanceNotFoundException was not thrown from an invoke operation on a non-existant MBean.");
197       }
198       catch (InstanceNotFoundException e)
199       {
200          // should get here
201
}
202       
203    }
204
205    /**
206     * Attempts to invoke a MBean operation that throws a business exception; <tt>MBeanException</tt> should be thrown.
207     */

208    public void testInvokeWithBusinessException() throws Exception
209    {
210       try
211       {
212          MBeanServer server = MBeanServerFactory.newMBeanServer();
213          ObjectName name = new ObjectName("test:test=test");
214          server.registerMBean(new Test(), name);
215
216          server.invoke(name, "operationWithException", null, null);
217
218          // should not get here
219
fail("MBeanException was not thrown.");
220       }
221       catch (MBeanException e)
222       {
223          // this is expected
224
assertTrue(e.getTargetException() instanceof MyScreamingException);
225       }
226    }
227
228
229    // MBeanServer getAttribute --------------------------------------
230

231    public void testGetAttributeWithNonExistingAttribute() throws Exception
232    {
233       try
234       {
235          MBeanServer server = MBeanServerFactory.newMBeanServer();
236          Object foo = server.getAttribute(new ObjectName("JMImplementation:type=MBeanServerDelegate"), "Foo");
237
238          // should not reach here
239
fail("AttributeNotFoundexception was not thrown when invoking getAttribute() call on a non-existant attribute.");
240       }
241       catch (AttributeNotFoundException e)
242       {
243          // Expecting this.
244
}
245    }
246
247    public void testGetAttributeWithBusinessException() throws Exception
248    {
249       try
250       {
251          MBeanServer server = MBeanServerFactory.newMBeanServer();
252          ObjectName name = new ObjectName("test:test=test");
253          server.registerMBean(new Test(), name);
254
255          Object foo = server.getAttribute(name, "ThisWillScream");
256
257          // should not reach here
258
fail("Did not throw the screaming exception");
259       }
260       catch (MBeanException e)
261       {
262          // this is expected
263
assertTrue(e.getTargetException() instanceof MyScreamingException);
264       }
265    }
266
267    public void testGetAttributeWithNonExistingMBean() throws Exception
268    {
269       try
270       {
271          MBeanServer server = MBeanServerFactory.newMBeanServer();
272          ObjectName name = new ObjectName("test:name=DoesNotExist");
273
274          server.getAttribute(name, "Whatever");
275
276          // should not reach here
277
fail("InstanceNotFoundException was not thrown on a nonexistant MBean.");
278       }
279       catch (InstanceNotFoundException e)
280       {
281          // this is expected
282
}
283    }
284
285    public void testGetAttributeWithUncheckedException() throws Exception
286    {
287       try
288       {
289          MBeanServer server = MBeanServerFactory.newMBeanServer();
290          ObjectName name = new ObjectName("test:test=test");
291          server.registerMBean(new Test(), name);
292
293          server.getAttribute(name, "ThrowUncheckedException");
294
295          // should not reach here
296
fail("RuntimeMBeanException was not thrown");
297       }
298       catch (RuntimeMBeanException e)
299       {
300          // this is expected
301
assertTrue(e.getTargetException() instanceof ExceptionOnTheRun);
302       }
303    }
304
305    public void testGetAttributeWithError() throws Exception
306    {
307       try
308       {
309          MBeanServer server = MBeanServerFactory.newMBeanServer();
310          ObjectName name = new ObjectName("test:test=test");
311          server.registerMBean(new Test(), name);
312
313          server.getAttribute(name, "Error");
314
315          // should not reach here
316
fail("Error was not thrown");
317       }
318       catch (RuntimeErrorException e)
319       {
320          // this is expected
321
assertTrue(e.getTargetError() instanceof BabarError);
322       }
323    }
324
325    
326    // MBeanServer setAttribute --------------------------------------
327

328    public void testSetAttributeWithNonExistingAttribute() throws Exception
329    {
330       try
331       {
332          MBeanServer server = MBeanServerFactory.newMBeanServer();
333          server.setAttribute(new ObjectName("JMImplementation:type=MBeanServerDelegate"), new Attribute("Foo", "value"));
334
335          // should not reach here
336
fail("AttributeNotFoundexception was not thrown when invoking getAttribute() call on a non-existant attribute.");
337       }
338       catch (AttributeNotFoundException e)
339       {
340          // Expecting this.
341
}
342    }
343
344    public void testSetAttributeWithBusinessException() throws Exception
345    {
346       try
347       {
348          MBeanServer server = MBeanServerFactory.newMBeanServer();
349          ObjectName name = new ObjectName("test:test=test");
350          server.registerMBean(new Test(), name);
351
352          server.setAttribute(name, new Attribute("ThisWillScream", "value"));
353
354          // should not reach here
355
fail("Did not throw the screaming exception");
356       }
357       catch (MBeanException e)
358       {
359          // this is expected
360
assertTrue(e.getTargetException() instanceof MyScreamingException);
361       }
362    }
363
364    public void testSetAttributeWithNonExistingMBean() throws Exception
365    {
366       try
367       {
368          MBeanServer server = MBeanServerFactory.newMBeanServer();
369          ObjectName name = new ObjectName("test:name=DoesNotExist");
370
371          server.setAttribute(name, new Attribute("Whatever", "nothing"));
372
373          // should not reach here
374
fail("InstanceNotFoundException was not thrown on a nonexistant MBean.");
375       }
376       catch (InstanceNotFoundException e)
377       {
378          // this is expected
379
}
380    }
381
382    public void testSetAttributeWithUncheckedException() throws Exception
383    {
384       try
385       {
386          MBeanServer server = MBeanServerFactory.newMBeanServer();
387          ObjectName name = new ObjectName("test:test=test");
388          server.registerMBean(new Test(), name);
389
390          server.setAttribute(name, new Attribute("ThrowUncheckedException", "value"));
391
392          // should not reach here
393
fail("RuntimeMBeanException was not thrown");
394       }
395       catch (RuntimeMBeanException e)
396       {
397          // this is expected
398
assertTrue(e.getTargetException() instanceof ExceptionOnTheRun);
399       }
400    }
401
402    public void testSetAttributeWithError() throws Exception
403    {
404       try
405       {
406          MBeanServer server = MBeanServerFactory.newMBeanServer();
407          ObjectName name = new ObjectName("test:test=test");
408          server.registerMBean(new Test(), name);
409
410          server.setAttribute(name, new Attribute("Error", "value"));
411
412          // should not reach here
413
fail("Error was not thrown");
414       }
415       catch (RuntimeErrorException e)
416       {
417          // this is expected
418
assertTrue(e.getTargetError() instanceof BabarError);
419       }
420    }
421
422    
423    // MBeanServer instantiate ---------------------------------------
424

425    /**
426     * Tests instantiate(String className). Class defined by system classloader.
427     */

428    public void testInstantiateWithDefaultConstructor() throws Exception
429    {
430       MBeanServer server = MBeanServerFactory.newMBeanServer();
431       Object o = server.instantiate("test.compliance.server.support.Test");
432       
433       assertTrue(o instanceof test.compliance.server.support.Test);
434    }
435    
436    /**
437     * Tests instantiate(String className) with constructor that throws a checked application exception.
438     * Class defined by system classloader.
439     */

440     public void testInstantiateWithDefaultConstructorAndApplicationException() throws Exception
441     {
442        try
443        {
444           MBeanServer server = MBeanServerFactory.newMBeanServer();
445           Object o = server.instantiate("test.compliance.server.support.ConstructorTest");
446           
447           // shouldn't get here
448
fail("Instantiate should have thrown an MBeanException.");
449        }
450        catch (MBeanException e)
451        {
452           // this is expected
453
}
454     }
455     
456     /**
457      * Tests instantiate(String className) with constructor that throws an unchecked application exception.
458      * Class defined by the system classloader.
459      */

460     public void testInstantiateWithDefaultConstructorAndRuntimeException() throws Exception
461     {
462        try
463        {
464           MBeanServer server = MBeanServerFactory.newMBeanServer();
465           Object o = server.instantiate("test.compliance.server.support.ConstructorTest2");
466           
467           // shouldn't get here
468
fail("Instantiate should have thrown a RuntimeMBeanException.");
469        }
470        catch (RuntimeMBeanException e)
471        {
472           // this is expected
473
}
474     }
475     
476     /**
477      * Tests instantiate(String className) with constructor that throws an error.
478      * Class defined by the system classloader.
479      */

480     public void testInstantiateWithDefaultConstructorAndError() throws Exception
481     {
482        try
483        {
484           MBeanServer server = MBeanServerFactory.newMBeanServer();
485           Object o = server.instantiate("test.compliance.server.support.ConstructorTest3");
486           
487           // shouldn't get here
488
fail("Instantiate should have thrown a RuntimeErrorException.");
489        }
490        catch (RuntimeErrorException e)
491        {
492           // this is expected
493
}
494     }
495     
496     /**
497      * Tests instantiate(String className) with constructor that fails with an unchecked exception in static init block.
498      * Class defined by the system classloader.
499      */

500     public void testInstantiateWithDefaultConstructorAndExceptionInInit() throws Exception
501     {
502        try
503        {
504           MBeanServer server = MBeanServerFactory.newMBeanServer();
505           
506           // FAILS IN RI
507
try
508           {
509              Object o = server.instantiate("test.compliance.server.support.ConstructorTest4");
510           }
511           catch (ExceptionInInitializerError e)
512           {
513              // RI lets this error through unwrapped. In general, the MBean server is responsible
514
// of wrapping all errors and exceptions from MBeans and resource classes with either
515
// RuntimeErrorException, RuntimeMBeanException or MBeanException. The javadoc is unclear in
516
// this case should a ReflectionException or MBeanException be thrown (neither one can wrap an
517
// Error though). JBossMX throws an RuntimeMBeanException in case of an unchecked exception in
518
// static initializer and a RuntimeErrorException in case of an error in static initializer.
519
fail("FAILS IN RI: MBeanServer fails to wrap an error or exception from a static initializer block correctly.");
520           }
521           
522           // shouldn't get here
523
fail("Instantiate should have thrown a RuntimeMBeanException.");
524        }
525        catch (RuntimeMBeanException e)
526        {
527           // this is expected
528

529           assertTrue(e.getTargetException() instanceof NullPointerException);
530        }
531     }
532     
533     /**
534      * Tests instatiante(String className) with constructor that fails with an error in static init block.
535      * Class defined by the system classloader.
536      */

537     public void testInstantiateWithDefaultConstructorAndErrorInInit() throws Exception
538     {
539        try
540        {
541           MBeanServer server = MBeanServerFactory.newMBeanServer();
542           
543           // FAILS IN RI
544
try
545           {
546             Object o = server.instantiate("test.compliance.server.support.ConstructorTest5");
547           }
548           catch (BabarError e)
549           {
550              // RI lets this error through unwrapped. In general, the MBean server is responsible
551
// of wrapping all errors and exceptions from MBeans and resource classes with either
552
// RuntimeErrorException, RuntimeMBeanException or MBeanException. The javadoc is unclear in
553
// this case should a ReflectionException or MBeanException be thrown (neither one can wrap an
554
// Error though). JBossMX throws an RuntimeMBeanException in case of an unchecked exception in
555
// static initializer and a RuntimeErrorException in case of an error in static initializer.
556
fail("FAILS IN RI: MBeanServer fails to wrap an error or exception from a static initializer block correctly.");
557           }
558           
559           // shouldn't get here
560
fail("Instantiate should have thrown a RuntimeErrorException.");
561        }
562        catch (RuntimeErrorException e)
563        {
564           // this is expected
565

566           assertTrue(e.getTargetError() instanceof test.compliance.server.support.BabarError);
567        }
568     }
569     
570     /**
571      * Tests instantiate(String className) with unfound class.
572      */

573     public void testInstantiateWithDefaultConstructorAndUnknownClass() throws Exception
574     {
575        try
576        {
577           MBeanServer server = MBeanServerFactory.newMBeanServer();
578           Object o = server.instantiate("foo.Bar");
579           
580           // should not get here
581
fail("Instantiate should have thrown a ReflectionException.");
582        }
583        catch (ReflectionException e)
584        {
585           // this is expected
586
assertTrue(e.getTargetException() instanceof ClassNotFoundException);
587        }
588     }
589     
590     /**
591      * Tests instantiate(String className) with class that doesn't have a default constructor.
592      */

593     public void testInstantiateWithMissingDefaultConstructor() throws Exception
594     {
595        try
596        {
597           MBeanServer server = MBeanServerFactory.newMBeanServer();
598           Object o = server.instantiate("test.compliance.server.support.ConstructorTest6");
599           
600           // should not get here
601
fail("Instantiate should have thrown a ReflectionException.");
602        }
603        catch (ReflectionException e)
604        {
605           // this is expected
606
}
607     }
608     
609     /**
610      * Tests instantiate(String className) with protected (no access) no args constructor.
611      */

612     public void testInstantiateWithInaccessibleNoArgsConstructor() throws Exception
613     {
614        try
615        {
616           MBeanServer server = MBeanServerFactory.newMBeanServer();
617           Object o = server.instantiate("test.compliance.server.support.ConstructorTest7");
618           
619           // should not get here
620
fail("Instantiate should have thrown a ReflectionException.");
621        }
622        catch (ReflectionException e)
623        {
624           // this is expected
625
}
626     }
627
628    /**
629     * Tests instantiate(String className) with null class name. According to
630     * javadoc, should throw RuntimeOperationsException wrapping IllegalArgException.
631     */

632    public void testInstantiateWithNullClassName() throws Exception
633    {
634       try
635       {
636          MBeanServer server = MBeanServerFactory.newMBeanServer();
637          Object o = server.instantiate(null);
638
639          // should not reach here
640
fail("incorrect exception behavior");
641       }
642       catch (RuntimeOperationsException e)
643       {
644          // expected
645

646          // check that it wraps IAE
647
assertTrue(e.getTargetException() instanceof IllegalArgumentException);
648       }
649    }
650
651    /**
652     * Tests instantiate(String className) with empty class name string. should
653     * throw ReflectionException wrapping CNFE.
654     */

655    public void testInstantiateWithEmptyClassName() throws Exception
656    {
657       try
658       {
659          MBeanServer server = MBeanServerFactory.newMBeanServer();
660          Object o = server.instantiate("");
661
662          // should not reach here
663
fail("incorrect exception/classloading behavior");
664       }
665       catch (ReflectionException e)
666       {
667          // expected
668

669          // check that it wraps CNFE
670
assertTrue(e.getTargetException() instanceof ClassNotFoundException);
671       }
672    }
673
674    /**
675     * Tests instantiate(String className, ObjectName loader) with null class name. According to
676     * javadoc, should throw RuntimeOperationsException wrapping IllegalArgException.
677     */

678    public void testInstantiateWithNullClassName2() throws Exception
679    {
680       try
681       {
682          MBeanServer server = MBeanServerFactory.newMBeanServer();
683          Object o = server.instantiate(null, null);
684
685          // should not reach here
686
fail("incorrect exception behavior");
687       }
688       catch (RuntimeOperationsException e)
689       {
690          // expected
691

692          // check that it wraps IAE
693
assertTrue(e.getTargetException() instanceof IllegalArgumentException);
694       }
695    }
696
697    /**
698     * Tests instantiate(String className, ObjectName loader) with empty class name string. should
699     * throw ReflectionException wrapping CNFE.
700     */

701    public void testInstantiateWithEmptyClassName2() throws Exception
702    {
703       try
704       {
705          MBeanServer server = MBeanServerFactory.newMBeanServer();
706          Object o = server.instantiate("", null);
707
708          // should not reach here
709
fail("incorrect exception/classloading behavior");
710       }
711       catch (ReflectionException e)
712       {
713          // expected
714

715          // check that it wraps CNFE
716
assertTrue(e.getTargetException() instanceof ClassNotFoundException);
717       }
718    }
719
720    /**
721     * Tests instantiate(String className, Object[] args, String[] sign) with null
722     * class name. According to javadoc, should throw RuntimeOperationsException
723     * wrapping IllegalArgException.
724     */

725    public void testInstantiateWithNullClassName3() throws Exception
726    {
727       try
728       {
729          MBeanServer server = MBeanServerFactory.newMBeanServer();
730          Object o = server.instantiate(null, null, null);
731
732          // should not reach here
733
fail("incorrect exception behavior");
734       }
735       catch (RuntimeOperationsException e)
736       {
737          // expected
738

739          // check that it wraps IAE
740
assertTrue(e.getTargetException() instanceof IllegalArgumentException);
741       }
742    }
743
744    /**
745     * Tests instantiate(String className, Object[] args, String[] sign) with
746     * empty class name string. should throw ReflectionException wrapping CNFE.
747     */

748    public void testInstantiateWithEmptyClassName3() throws Exception
749    {
750       try
751       {
752          MBeanServer server = MBeanServerFactory.newMBeanServer();
753          Object o = server.instantiate("", null, null);
754
755          // should not reach here
756
fail("incorrect exception/classloading behavior");
757       }
758       catch (ReflectionException e)
759       {
760          // expected
761

762          // check that it wraps CNFE
763
assertTrue(e.getTargetException() instanceof ClassNotFoundException);
764       }
765    }
766
767    /**
768     * Tests instantiate(String className, ObjectName loader, Object[] args, String[] sign)
769     * with null class name. According to javadoc, should throw RuntimeOperationsException
770     * wrapping IllegalArgException.
771     */

772    public void testInstantiateWithNullClassName4() throws Exception
773    {
774       try
775       {
776          MBeanServer server = MBeanServerFactory.newMBeanServer();
777          Object o = server.instantiate(null, null, null, null);
778
779          // should not reach here
780
fail("incorrect exception behavior");
781       }
782       catch (RuntimeOperationsException e)
783       {
784          // expected
785

786          // check that it wraps IAE
787
assertTrue(e.getTargetException() instanceof IllegalArgumentException);
788       }
789    }
790
791    /**
792     * Tests instantiate(String className, ObjectName loader, Object[] args, String[] sign)
793     * with empty class name string. should throw ReflectionException wrapping CNFE.
794     */

795    public void testInstantiateWithEmptyClassName4() throws Exception
796    {
797       try
798       {
799          MBeanServer server = MBeanServerFactory.newMBeanServer();
800          Object o = server.instantiate("", null, null, null);
801
802          // should not reach here
803
fail("incorrect exception/classloading behavior");
804       }
805       catch (ReflectionException e)
806       {
807          // expected
808

809          // check that it wraps CNFE
810
assertTrue(e.getTargetException() instanceof ClassNotFoundException);
811       }
812    }
813     
814    /**
815     * Tests instantiate(String className) classloading behaviour. According to
816     * javadoc, DLR should be used to instantiate the class
817     */

818    public void testInstantiateWithDefaultLoaderRepository() throws Exception
819    {
820       // NOTE:
821
// the urls used here are relative to the location of the build.xml
822

823       MBeanServer server = MBeanServerFactory.newMBeanServer();
824       MLet mlet = new MLet();
825       ObjectName name = new ObjectName(":test=test");
826       
827       // mlet cl to DLR
828
try
829       {
830          mlet.addURL("file:./output/etc/test/compliance/server/Test.jar");
831          server.registerMBean(mlet, name);
832       
833          Object o = server.instantiate("test.compliance.server.support.AClass");
834       }
835       finally
836       {
837          try
838          {
839             server.unregisterMBean(name);
840          }
841          catch (Exception ignored) {}
842       }
843       
844       //assertTrue(o.getClass().getClassLoader().equals(mlet));
845
}
846    
847    
848    /**
849     * Tests instantiate(String className, ObjectName loader) classloading behaviour. According to
850     * javadoc, DLR should be used to instantiate the class. This should fail as
851     * the MLet MBean is never added to the agent and therefore not in the DLR.
852     */

853    public void testInstantiateWithDefaultLoaderRepository2() throws Exception
854    {
855       try
856       {
857          // NOTE:
858
// the urls used here are relative to the location of the build.xml
859

860          MBeanServer server = MBeanServerFactory.newMBeanServer();
861          MLet mlet = new MLet();
862       
863          // mlet cl to DLR
864
mlet.addURL("file:./output/etc/test/compliance/server/Test.jar");
865          //server.registerMBean(mlet, new ObjectName(":test=test"));
866

867          Object o = server.instantiate("test.compliance.server.support.AClass");
868       
869 //
870
// FIXME: this test won't work until we have means to reset the JVM wide
871
// loader repository
872
//
873

874          // should not reach here
875
//fail("incorrect classloading behavior");
876
//assertTrue(o.getClass().getClassLoader().equals(mlet));
877
}
878       catch (ReflectionException e)
879       {
880          // expected
881

882          // check that it wraps CNFE
883
assertTrue(e.getTargetException() instanceof ClassNotFoundException);
884       }
885    }
886    
887     // MBeanServer registerMBean ------------------------------------
888

889     /**
890      * Tests registering with null object name.
891      */

892     public void testRegisterNullObjectName() throws Exception
893     {
894        boolean caught = false;
895        try
896        {
897           MBeanServer server = MBeanServerFactory.newMBeanServer();
898           server.registerMBean(new Test(), null);
899        }
900        catch (RuntimeOperationsException e)
901        {
902           if (e.getTargetException() instanceof IllegalArgumentException)
903              caught = true;
904           else
905              fail("Wrong wrapped exception " + e.getTargetException());
906        }
907        if (caught == false)
908           fail("Allowed to register with a null object name");
909     }
910 </