KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.File JavaDoc;
25 import java.net.URL JavaDoc;
26 import java.util.Arrays JavaDoc;
27 import java.util.List JavaDoc;
28
29 import javax.management.Attribute JavaDoc;
30 import javax.management.AttributeNotFoundException JavaDoc;
31 import javax.management.InstanceNotFoundException JavaDoc;
32 import javax.management.MBeanException JavaDoc;
33 import javax.management.MBeanRegistrationException JavaDoc;
34 import javax.management.MBeanServer JavaDoc;
35 import javax.management.MBeanServerFactory JavaDoc;
36 import javax.management.MBeanServerNotification JavaDoc;
37 import javax.management.Notification JavaDoc;
38 import javax.management.NotificationFilterSupport JavaDoc;
39 import javax.management.NotificationListener JavaDoc;
40 import javax.management.ObjectInstance JavaDoc;
41 import javax.management.ObjectName JavaDoc;
42 import javax.management.ReflectionException JavaDoc;
43 import javax.management.RuntimeErrorException JavaDoc;
44 import javax.management.RuntimeMBeanException JavaDoc;
45 import javax.management.RuntimeOperationsException JavaDoc;
46 import javax.management.loading.MLet JavaDoc;
47
48 import junit.framework.AssertionFailedError;
49 import junit.framework.TestCase;
50
51 import org.jboss.test.jmx.compliance.server.support.BabarError;
52 import org.jboss.test.jmx.compliance.server.support.Base;
53 import org.jboss.test.jmx.compliance.server.support.BaseMBean;
54 import org.jboss.test.jmx.compliance.server.support.Broadcaster;
55 import org.jboss.test.jmx.compliance.server.support.Derived;
56 import org.jboss.test.jmx.compliance.server.support.Dynamic;
57 import org.jboss.test.jmx.compliance.server.support.ExceptionOnTheRun;
58 import org.jboss.test.jmx.compliance.server.support.LockedTest;
59 import org.jboss.test.jmx.compliance.server.support.LockedTest2;
60 import org.jboss.test.jmx.compliance.server.support.LockedTest3;
61 import org.jboss.test.jmx.compliance.server.support.MBeanListener;
62 import org.jboss.test.jmx.compliance.server.support.MyScreamingException;
63 import org.jboss.test.jmx.compliance.server.support.Test;
64 import org.jboss.test.jmx.compliance.server.support.Test2;
65 import org.jboss.test.jmx.compliance.server.support.Test3;
66 import org.jboss.test.jmx.compliance.server.support.Test4;
67 import org.jboss.test.jmx.compliance.server.support.Unrelated;
68 import org.jboss.test.jmx.compliance.server.support.UnrelatedMBean;
69
70 /**
71  * Tests the MBean server impl. through the <tt>MBeanServer</tt> interface.
72  *
73  * @author <a HREF="mailto:juha@jboss.org">Juha Lindfors</a>.
74  * @version $Revision: 37459 $
75  *
76  */

77 public class MBeanServerTEST
78    extends TestCase
79 {
80    URL JavaDoc location;
81    
82    public MBeanServerTEST(String JavaDoc s) throws Exception JavaDoc
83    {
84       super(s);
85       
86       // Workout the output location for the dynamically loaded files
87
location = getClass().getResource("/org/jboss/test/jmx/compliance/server/MBeanServerTEST.class");
88       String JavaDoc jarPath = location.getPath();
89       int i = jarPath.indexOf('!');
90       if (i != -1)
91       {
92          jarPath = jarPath.substring(0, i);
93          location = new URL JavaDoc(jarPath);
94          location = new URL JavaDoc(location, "file:../");
95       }
96    }
97    
98    // MBeanServer invoke --------------------------------------------
99

100    /**
101     * Tests invoke with primitive boolean return type. <p>
102     */

103    public void testInvokeWithPrimitiveBooleanReturn() throws Exception JavaDoc
104    {
105       MBeanServer JavaDoc server = MBeanServerFactory.newMBeanServer();
106       ObjectName JavaDoc name = new ObjectName JavaDoc(":test=test");
107       server.registerMBean(new Test(), name);
108       
109       Boolean JavaDoc bool = (Boolean JavaDoc)server.invoke(name, "opWithPrimBooleanReturn", null, null);
110       
111       assertTrue(bool.booleanValue() == true);
112    }
113
114    /**
115     * Tests invoke with primitive long array return type. <p>
116     */

117    public void testInvokeWithPrimitiveLongArrayReturn() throws Exception JavaDoc
118    {
119       MBeanServer JavaDoc server = MBeanServerFactory.newMBeanServer();
120       ObjectName JavaDoc name = new ObjectName JavaDoc(":test=test");
121       server.registerMBean(new Test(), name);
122       
123       long[] array = (long[])server.invoke(name, "opWithPrimLongArrayReturn", null, null);
124       
125       assertTrue(array [0] == 1);
126       assertTrue(array [1] == 2);
127       assertTrue(array [2] == 3);
128    }
129
130    /**
131     * Tests invoke with Long array return type. <p>
132     */

133    public void testInvokeWithLongArrayReturn() throws Exception JavaDoc
134    {
135       MBeanServer JavaDoc server = MBeanServerFactory.newMBeanServer();
136       ObjectName JavaDoc name = new ObjectName JavaDoc(":test=test");
137       server.registerMBean(new Test(), name);
138       
139       Long JavaDoc[] array = (Long JavaDoc[])server.invoke(name, "opWithLongArrayReturn", null, null);
140       
141       assertTrue(array [0].longValue() == 1);
142       assertTrue(array [1].longValue() == 2);
143       assertTrue(array [2].longValue() == 3);
144    }
145    
146    /**
147     * Tests invoke with primitive long return type. <p>
148     */

149    public void testInvokeWithPrimitiveLongReturn() throws Exception JavaDoc
150    {
151       MBeanServer JavaDoc server = MBeanServerFactory.newMBeanServer();
152       ObjectName JavaDoc name = new ObjectName JavaDoc(":test=test");
153       server.registerMBean(new Test(), name);
154       
155       Long JavaDoc l = (Long JavaDoc)server.invoke(name, "opWithPrimLongReturn", null, null);
156       
157       assertTrue(l.longValue() == 1234567890123l);
158    }
159
160    /**
161     * Tests invoke with primitive double return type. <p>
162     */

163    public void testInvokeWithPrimitiveDoubleReturn() throws Exception JavaDoc
164    {
165       MBeanServer JavaDoc server = MBeanServerFactory.newMBeanServer();
166       ObjectName JavaDoc name = new ObjectName JavaDoc(":test=test");
167       server.registerMBean(new Test(), name);
168       
169       Double JavaDoc d = (Double JavaDoc)server.invoke(name, "opWithPrimDoubleReturn", null, null);
170       
171       assertTrue(d.doubleValue() == 0.1234567890123d);
172    }
173    
174    /**
175     * Tests invoke with long signature. <p>
176     */

177    public void testInvokeWithLongSignature() throws Exception JavaDoc
178    {
179       MBeanServer JavaDoc server = MBeanServerFactory.newMBeanServer();
180       ObjectName JavaDoc name = new ObjectName JavaDoc(":test=test");
181       server.registerMBean(new Test(), name);
182       
183       server.invoke(name, "opWithLongSignature",
184       new Object JavaDoc[] { new Integer JavaDoc(1), new Integer JavaDoc(2), new Integer JavaDoc(3), new Integer JavaDoc(4), new Integer JavaDoc(5),
185                      new Integer JavaDoc(6), new Integer JavaDoc(7), new Integer JavaDoc(8), new Integer JavaDoc(9), new Integer JavaDoc(10),
186                      new Integer JavaDoc(11), new Integer JavaDoc(12), new Integer JavaDoc(13), new Integer JavaDoc(14), new Integer JavaDoc(15),
187                      new Integer JavaDoc(16), new Integer JavaDoc(17), new Integer JavaDoc(18), new Integer JavaDoc(19), new Integer JavaDoc(20) },
188       new String JavaDoc[] { "int", "int", "int", "int", "int", "int", "int", "int", "int", "int",
189                      "int", "int", "int", "int", "int", "int", "int", "int", "int", "int" }
190       );
191    }
192
193    /**
194     * Tests invoke with mixed types in signature, especially types with double
195     * byte code length and arrays. <p>
196     */

197    public void testInvokeWithMixedSignature() throws Exception JavaDoc
198    {
199       MBeanServer JavaDoc server = MBeanServerFactory.newMBeanServer();
200       ObjectName JavaDoc name = new ObjectName JavaDoc(":test=test");
201       server.registerMBean(new Test(), name);
202       
203       server.invoke(name, "opWithMixedSignature",
204       new Object JavaDoc[] { new Integer JavaDoc(1), new Double JavaDoc(2.2D), new Long JavaDoc(333L), new Boolean JavaDoc(true), new Byte JavaDoc((byte)0x02),
205                      new Short JavaDoc((short)6), new long[]{7L, 8L}, new Long JavaDoc[]{new Long JavaDoc(1L), new Long JavaDoc(2L)}, new Short JavaDoc((short)9), new Byte JavaDoc((byte)10),
206                      new Long JavaDoc(11L), new Double JavaDoc(1.2D), new Integer JavaDoc(13), new Integer JavaDoc(14), new Integer JavaDoc(15),
207                      new Integer JavaDoc(16), new Integer JavaDoc(17), new Integer JavaDoc(18), new Integer JavaDoc(19), new Integer JavaDoc(20) },
208       new String JavaDoc[] { "int", "double", "long", "boolean", "byte", "short", "[J", "[Ljava.lang.Long;", "java.lang.Short", "java.lang.Byte",
209                      "java.lang.Long", "java.lang.Double", "int", "int", "int", "int", "int", "int", "int", "int" }
210       );
211    }
212
213    
214    /**
215     * Attempts to invoke a method on an unregistered MBean; <tt>InstanceNotFoundException</tt> should occur.
216     */

217    public void testInvokeWithNonExistantMBean() throws Exception JavaDoc
218    {
219       try
220       {
221          MBeanServer JavaDoc server = MBeanServerFactory.newMBeanServer();
222          server.invoke(new ObjectName JavaDoc(":mbean=doesnotexist"), "noMethod", null, null);
223
224          // should not reach here
225
fail("InstanceNotFoundException was not thrown from an invoke operation on a non-existant MBean.");
226       }
227       catch (InstanceNotFoundException JavaDoc e)
228       {
229          // should get here
230
}
231       
232    }
233
234    /**
235     * Attempts to invoke a MBean operation that throws a business exception; <tt>MBeanException</tt> should be thrown.
236     */

237    public void testInvokeWithBusinessException() throws Exception JavaDoc
238    {
239       try
240       {
241          MBeanServer JavaDoc server = MBeanServerFactory.newMBeanServer();
242          ObjectName JavaDoc name = new ObjectName JavaDoc("test:test=test");
243          server.registerMBean(new Test(), name);
244
245          server.invoke(name, "operationWithException", null, null);
246
247          // should not get here
248
fail("MBeanException was not thrown.");
249       }
250       catch (MBeanException JavaDoc e)
251       {
252          // this is expected
253
assertTrue(e.getTargetException() instanceof MyScreamingException);
254       }
255    }
256
257
258    // MBeanServer getAttribute --------------------------------------
259

260    public void testGetAttributeWithNonExistingAttribute() throws Exception JavaDoc
261    {
262       try
263       {
264          MBeanServer JavaDoc server = MBeanServerFactory.newMBeanServer();
265          server.getAttribute(new ObjectName JavaDoc("JMImplementation:type=MBeanServerDelegate"), "Foo");
266
267          // should not reach here
268
fail("AttributeNotFoundexception was not thrown when invoking getAttribute() call on a non-existant attribute.");
269       }
270       catch (AttributeNotFoundException JavaDoc e)
271       {
272          // Expecting this.
273
}
274    }
275
276    public void testGetAttributeWithBusinessException() throws Exception JavaDoc
277    {
278       try
279       {
280          MBeanServer JavaDoc server = MBeanServerFactory.newMBeanServer();
281          ObjectName JavaDoc name = new ObjectName JavaDoc("test:test=test");
282          server.registerMBean(new Test(), name);
283
284          server.getAttribute(name, "ThisWillScream");
285
286          // should not reach here
287
fail("Did not throw the screaming exception");
288       }
289       catch (MBeanException JavaDoc e)
290       {
291          // this is expected
292
assertTrue(e.getTargetException() instanceof MyScreamingException);
293       }
294    }
295
296    public void testGetAttributeWithNonExistingMBean() throws Exception JavaDoc
297    {
298       try
299       {
300          MBeanServer JavaDoc server = MBeanServerFactory.newMBeanServer();
301          ObjectName JavaDoc name = new ObjectName JavaDoc("test:name=DoesNotExist");
302
303          server.getAttribute(name, "Whatever");
304
305          // should not reach here
306
fail("InstanceNotFoundException was not thrown on a nonexistant MBean.");
307       }
308       catch (InstanceNotFoundException JavaDoc e)
309       {
310          // this is expected
311
}
312    }
313
314    public void testGetAttributeWithUncheckedException() throws Exception JavaDoc
315    {
316       try
317       {
318          MBeanServer JavaDoc server = MBeanServerFactory.newMBeanServer();
319          ObjectName JavaDoc name = new ObjectName JavaDoc("test:test=test");
320          server.registerMBean(new Test(), name);
321
322          server.getAttribute(name, "ThrowUncheckedException");
323
324          // should not reach here
325
fail("RuntimeMBeanException was not thrown");
326       }
327       catch (RuntimeMBeanException JavaDoc e)
328       {
329          // this is expected
330
assertTrue(e.getTargetException() instanceof ExceptionOnTheRun);
331       }
332    }
333
334    public void testGetAttributeWithError() throws Exception JavaDoc
335    {
336       try
337       {
338          MBeanServer JavaDoc server = MBeanServerFactory.newMBeanServer();
339          ObjectName JavaDoc name = new ObjectName JavaDoc("test:test=test");
340          server.registerMBean(new Test(), name);
341
342          server.getAttribute(name, "Error");
343
344          // should not reach here
345
fail("Error was not thrown");
346       }
347       catch (RuntimeErrorException JavaDoc e)
348       {
349          // this is expected
350
assertTrue(e.getTargetError() instanceof BabarError);
351       }
352    }
353
354    
355    // MBeanServer setAttribute --------------------------------------
356

357    public void testSetAttributeWithNonExistingAttribute() throws Exception JavaDoc
358    {
359       try
360       {
361          MBeanServer JavaDoc server = MBeanServerFactory.newMBeanServer();
362          server.setAttribute(new ObjectName JavaDoc("JMImplementation:type=MBeanServerDelegate"), new Attribute JavaDoc("Foo", "value"));
363
364          // should not reach here
365
fail("AttributeNotFoundexception was not thrown when invoking getAttribute() call on a non-existant attribute.");
366       }
367       catch (AttributeNotFoundException JavaDoc e)
368       {
369          // Expecting this.
370
}
371    }
372
373    public void testSetAttributeWithBusinessException() throws Exception JavaDoc
374    {
375       try
376       {
377          MBeanServer JavaDoc server = MBeanServerFactory.newMBeanServer();
378          ObjectName JavaDoc name = new ObjectName JavaDoc("test:test=test");
379          server.registerMBean(new Test(), name);
380
381          server.setAttribute(name, new Attribute JavaDoc("ThisWillScream", "value"));
382
383          // should not reach here
384
fail("Did not throw the screaming exception");
385       }
386       catch (MBeanException JavaDoc e)
387       {
388          // this is expected
389
assertTrue(e.getTargetException() instanceof MyScreamingException);
390       }
391    }
392
393    public void testSetAttributeWithNonExistingMBean() throws Exception JavaDoc
394    {
395       try
396       {
397          MBeanServer JavaDoc server = MBeanServerFactory.newMBeanServer();
398          ObjectName JavaDoc name = new ObjectName JavaDoc("test:name=DoesNotExist");
399
400          server.setAttribute(name, new Attribute JavaDoc("Whatever", "nothing"));
401
402          // should not reach here
403
fail("InstanceNotFoundException was not thrown on a nonexistant MBean.");
404       }
405       catch (InstanceNotFoundException JavaDoc e)
406       {
407          // this is expected
408
}
409    }
410
411    public void testSetAttributeWithUncheckedException() throws Exception JavaDoc
412    {
413       try
414       {
415          MBeanServer JavaDoc server = MBeanServerFactory.newMBeanServer();
416          ObjectName JavaDoc name = new ObjectName JavaDoc("test:test=test");
417          server.registerMBean(new Test(), name);
418
419          server.setAttribute(name, new Attribute JavaDoc("ThrowUncheckedException", "value"));
420
421          // should not reach here
422
fail("RuntimeMBeanException was not thrown");
423       }
424       catch (RuntimeMBeanException JavaDoc e)
425       {
426          // this is expected
427
assertTrue(e.getTargetException() instanceof ExceptionOnTheRun);
428       }
429    }
430
431    public void testSetAttributeWithError() throws Exception JavaDoc
432    {
433       try
434       {
435          MBeanServer JavaDoc server = MBeanServerFactory.newMBeanServer();
436          ObjectName JavaDoc name = new ObjectName JavaDoc("test:test=test");
437          server.registerMBean(new Test(), name);
438
439          server.setAttribute(name, new Attribute JavaDoc("Error", "value"));
440
441          // should not reach here
442
fail("Error was not thrown");
443       }
444       catch (RuntimeErrorException JavaDoc e)
445       {
446          // this is expected
447
assertTrue(e.getTargetError() instanceof BabarError);
448       }
449    }
450
451    
452    // MBeanServer instantiate ---------------------------------------
453

454    /**
455     * Tests instantiate(String className). Class defined by system classloader.
456     */

457    public void testInstantiateWithDefaultConstructor() throws Exception JavaDoc
458    {
459       MBeanServer JavaDoc server = MBeanServerFactory.newMBeanServer();
460       Object JavaDoc o = server.instantiate("org.jboss.test.jmx.compliance.server.support.Test");
461       
462       assertTrue(o instanceof org.jboss.test.jmx.compliance.server.support.Test);
463    }
464    
465    /**
466     * Tests instantiate(String className) with constructor that throws a checked application exception.
467     * Class defined by system classloader.
468     */

469     public void testInstantiateWithDefaultConstructorAndApplicationException() throws Exception JavaDoc
470     {
471        try
472        {
473           MBeanServer JavaDoc server = MBeanServerFactory.newMBeanServer();
474           server.instantiate("org.jboss.test.jmx.compliance.server.support.ConstructorTest");
475           
476           // shouldn't get here
477
fail("Instantiate should have thrown an MBeanException.");
478        }
479        catch (MBeanException JavaDoc e)
480        {
481           // this is expected
482
}
483     }
484     
485     /**
486      * Tests instantiate(String className) with constructor that throws an unchecked application exception.
487      * Class defined by the system classloader.
488      */

489     public void testInstantiateWithDefaultConstructorAndRuntimeException() throws Exception JavaDoc
490     {
491        try
492        {
493           MBeanServer JavaDoc server = MBeanServerFactory.newMBeanServer();
494           server.instantiate("org.jboss.test.jmx.compliance.server.support.ConstructorTest2");
495           
496           // shouldn't get here
497
fail("Instantiate should have thrown a RuntimeMBeanException.");
498        }
499        catch (RuntimeMBeanException JavaDoc e)
500        {
501           // this is expected
502
}
503     }
504     
505     /**
506      * Tests instantiate(String className) with constructor that throws an error.
507      * Class defined by the system classloader.
508      */

509     public void testInstantiateWithDefaultConstructorAndError() throws Exception JavaDoc
510     {
511        try
512        {
513           MBeanServer JavaDoc server = MBeanServerFactory.newMBeanServer();
514           server.instantiate("org.jboss.test.jmx.compliance.server.support.ConstructorTest3");
515           
516           // shouldn't get here
517
fail("Instantiate should have thrown a RuntimeErrorException.");
518        }
519        catch (RuntimeErrorException JavaDoc e)
520        {
521           // this is expected
522
}
523     }
524     
525     /**
526      * Tests instantiate(String className) with constructor that fails with an unchecked exception in static init block.
527      * Class defined by the system classloader.
528      */

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

558           assertTrue(e.getTargetException() instanceof NullPointerException JavaDoc);
559        }
560     }
561     
562     /**
563      * Tests instatiante(String className) with constructor that fails with an error in static init block.
564      * Class defined by the system classloader.
565      */

566     public void testInstantiateWithDefaultConstructorAndErrorInInit() throws Exception JavaDoc
567     {
568        try
569        {
570           MBeanServer JavaDoc server = MBeanServerFactory.newMBeanServer();
571           
572           // FAILS IN RI
573
try
574           {
575              server.instantiate("org.jboss.test.jmx.compliance.server.support.ConstructorTest5");
576           }
577           catch (BabarError e)
578           {
579              // RI lets this error through unwrapped. In general, the MBean server is responsible
580
// of wrapping all errors and exceptions from MBeans and resource classes with either
581
// RuntimeErrorException, RuntimeMBeanException or MBeanException. The javadoc is unclear in
582
// this case should a ReflectionException or MBeanException be thrown (neither one can wrap an
583
// Error though). JBossMX throws an RuntimeMBeanException in case of an unchecked exception in
584
// static initializer and a RuntimeErrorException in case of an error in static initializer.
585
fail("FAILS IN RI: MBeanServer fails to wrap an error or exception from a static initializer block correctly.");
586           }
587           
588           // shouldn't get here
589
fail("Instantiate should have thrown a RuntimeErrorException.");
590        }
591        catch (RuntimeErrorException JavaDoc e)
592        {
593           // this is expected
594

595           assertTrue(e.getTargetError() instanceof org.jboss.test.jmx.compliance.server.support.BabarError);
596        }
597     }
598     
599     /**
600      * Tests instantiate(String className) with unfound class.
601      */

602     public void testInstantiateWithDefaultConstructorAndUnknownClass() throws Exception JavaDoc
603     {
604        try
605        {
606           MBeanServer JavaDoc server = MBeanServerFactory.newMBeanServer();
607           server.instantiate("foo.Bar");
608           
609           // should not get here
610
fail("Instantiate should have thrown a ReflectionException.");
611        }
612        catch (ReflectionException JavaDoc e)
613        {
614           // this is expected
615
assertTrue(e.getTargetException() instanceof ClassNotFoundException JavaDoc);
616        }
617     }
618     
619     /**
620      * Tests instantiate(String className) with class that doesn't have a default constructor.
621      */

622     public void testInstantiateWithMissingDefaultConstructor() throws Exception JavaDoc
623     {
624        try
625        {
626           MBeanServer JavaDoc server = MBeanServerFactory.newMBeanServer();
627           server.instantiate("org.jboss.test.jmx.compliance.server.support.ConstructorTest6");
628           
629           // should not get here
630
fail("Instantiate should have thrown a ReflectionException.");
631        }
632        catch (ReflectionException JavaDoc e)
633        {
634           // this is expected
635
}
636     }
637     
638     /**
639      * Tests instantiate(String className) with protected (no access) no args constructor.
640      */

641     public void testInstantiateWithInaccessibleNoArgsConstructor() throws Exception JavaDoc
642     {
643        try
644        {
645           MBeanServer JavaDoc server = MBeanServerFactory.newMBeanServer();
646           server.instantiate("org.jboss.test.jmx.compliance.server.support.ConstructorTest7");
647           
648           // should not get here
649
fail("Instantiate should have thrown a ReflectionException.");
650        }
651        catch (ReflectionException JavaDoc e)
652        {
653           // this is expected
654
}
655     }
656
657    /**
658     * Tests instantiate(String className) with null class name. According to
659     * javadoc, should throw RuntimeOperationsException wrapping IllegalArgException.
660     */

661    public void testInstantiateWithNullClassName() throws Exception JavaDoc
662    {
663       try
664       {
665          MBeanServer JavaDoc server = MBeanServerFactory.newMBeanServer();
666          server.instantiate(null);
667
668          // should not reach here
669
fail("incorrect exception behavior");
670       }
671       catch (RuntimeOperationsException JavaDoc e)
672       {
673          // expected
674

675          // check that it wraps IAE
676
assertTrue(e.getTargetException() instanceof IllegalArgumentException JavaDoc);
677       }
678    }
679
680    /**
681     * Tests instantiate(String className) with empty class name string. should
682     * throw ReflectionException wrapping CNFE.
683     */

684    public void testInstantiateWithEmptyClassName() throws Exception JavaDoc
685    {
686       try
687       {
688          MBeanServer JavaDoc server = MBeanServerFactory.newMBeanServer();
689          server.instantiate("");
690
691          // should not reach here
692
fail("incorrect exception/classloading behavior");
693       }
694       catch (ReflectionException JavaDoc e)
695       {
696          // expected
697

698          // check that it wraps CNFE
699
assertTrue(e.getTargetException() instanceof ClassNotFoundException JavaDoc);
700       }
701    }
702
703    /**
704     * Tests instantiate(String className, ObjectName loader) with null class name. According to
705     * javadoc, should throw RuntimeOperationsException wrapping IllegalArgException.
706     */

707    public void testInstantiateWithNullClassName2() throws Exception JavaDoc
708    {
709       try
710       {
711          MBeanServer JavaDoc server = MBeanServerFactory.newMBeanServer();
712          server.instantiate(null, null);
713
714          // should not reach here
715
fail("incorrect exception behavior");
716       }
717       catch (RuntimeOperationsException JavaDoc e)
718       {
719          // expected
720

721          // check that it wraps IAE
722
assertTrue(e.getTargetException() instanceof IllegalArgumentException JavaDoc);
723       }
724    }
725
726    /**
727     * Tests instantiate(String className, ObjectName loader) with empty class name string. should
728     * throw ReflectionException wrapping CNFE.
729     */

730    public void testInstantiateWithEmptyClassName2() throws Exception JavaDoc
731    {
732       try
733       {
734          MBeanServer JavaDoc server = MBeanServerFactory.newMBeanServer();
735          server.instantiate("", null);
736
737          // should not reach here
738
fail("incorrect exception/classloading behavior");
739       }
740       catch (ReflectionException JavaDoc e)
741       {
742          // expected
743

744          // check that it wraps CNFE
745
assertTrue(e.getTargetException() instanceof ClassNotFoundException JavaDoc);
746       }
747    }
748
749    /**
750     * Tests instantiate(String className, Object[] args, String[] sign) with null
751     * class name. According to javadoc, should throw RuntimeOperationsException
752     * wrapping IllegalArgException.
753     */

754    public void testInstantiateWithNullClassName3() throws Exception JavaDoc
755    {
756       try
757       {
758          MBeanServer JavaDoc server = MBeanServerFactory.newMBeanServer();
759          server.instantiate(null, null, null);
760
761          // should not reach here
762
fail("incorrect exception behavior");
763       }
764       catch (RuntimeOperationsException JavaDoc e)
765       {
766          // expected
767

768          // check that it wraps IAE
769
assertTrue(e.getTargetException() instanceof IllegalArgumentException JavaDoc);
770       }
771    }
772
773    /**
774     * Tests instantiate(String className, Object[] args, String[] sign) with
775     * empty class name string. should throw ReflectionException wrapping CNFE.
776     */

777    public void testInstantiateWithEmptyClassName3() throws Exception JavaDoc
778    {
779       try
780       {
781          MBeanServer JavaDoc server = MBeanServerFactory.newMBeanServer();
782          server.instantiate("", null, null);
783
784          // should not reach here
785
fail("incorrect exception/classloading behavior");
786       }
787       catch (ReflectionException JavaDoc e)
788       {
789          // expected
790

791          // check that it wraps CNFE
792
assertTrue(e.getTargetException() instanceof ClassNotFoundException JavaDoc);
793       }
794    }
795
796    /**
797     * Tests instantiate(String className, ObjectName loader, Object[] args, String[] sign)
798     * with null class name. According to javadoc, should throw RuntimeOperationsException
799     * wrapping IllegalArgException.
800     */

801    public void testInstantiateWithNullClassName4() throws Exception JavaDoc
802    {
803       try
804       {
805          MBeanServer JavaDoc server = MBeanServerFactory.newMBeanServer();
806          server.instantiate(null, null, null, null);
807
808          // should not reach here
809
fail("incorrect exception behavior");
810       }
811       catch (RuntimeOperationsException JavaDoc e)
812       {
813          // expected
814

815          // check that it wraps IAE
816
assertTrue(e.getTargetException() instanceof IllegalArgumentException JavaDoc);
817       }
818    }
819
820    /**
821     * Tests instantiate(String className, ObjectName loader, Object[] args, String[] sign)
822     * with empty class name string. should throw ReflectionException wrapping CNFE.
823     */

824    public void testInstantiateWithEmptyClassName4() throws Exception JavaDoc
825    {
826       try
827       {
828          MBeanServer JavaDoc server = MBeanServerFactory.newMBeanServer();
829          server.instantiate("", null, null, null);
830
831          // should not reach here
832
fail("incorrect exception/classloading behavior");
833       }
834       catch (ReflectionException JavaDoc e)
835       {
836          // expected
837

838          // check that it wraps CNFE
839
assertTrue(e.getTargetException() instanceof ClassNotFoundException JavaDoc);
840       }
841    }
842     
843    /**
844     * Tests instantiate(String className) classloading behaviour. According to
845     * javadoc, DLR should be used to instantiate the class
846     */

847    public void testInstantiateWithDefaultLoaderRepository() throws Exception JavaDoc
848    {
849       // NOTE:
850
// the urls used here are relative to the location of the build.xml
851

852       MBeanServer JavaDoc server = MBeanServerFactory.newMBeanServer();
853       MLet JavaDoc mlet = new MLet JavaDoc();
854       ObjectName JavaDoc name = new ObjectName JavaDoc(":test=test");
855       
856       // mlet cl to DLR
857
try
858       {
859          mlet.addURL(new URL JavaDoc(location, "lib/jmxcompliance-Test.jar"));
860          server.registerMBean(mlet, name);
861       
862          server.instantiate("org.jboss.test.jmx.compliance.server.support.AClass");
863       }
864       finally
865       {
866          try
867          {
868             server.unregisterMBean(name);
869          }
870          catch (Exception JavaDoc ignored) {}
871       }
872       
873       //assertTrue(o.getClass().getClassLoader().equals(mlet));
874
}
875    
876    
877    /**
878     * Tests instantiate(String className, ObjectName loader) classloading behaviour. According to
879     * javadoc, DLR should be used to instantiate the class. This should fail as
880     * the MLet MBean is never added to the agent and therefore not in the DLR.
881     */

882    public void testInstantiateWithDefaultLoaderRepository2() throws Exception JavaDoc
883    {
884       try
885       {
886          // NOTE:
887
// the urls used here are relative to the location of the build.xml
888

889          MBeanServer JavaDoc server = MBeanServerFactory.newMBeanServer();
890          MLet JavaDoc mlet = new MLet JavaDoc();
891       
892          // mlet cl to DLR
893
mlet.addURL("file:./output/etc/test/compliance/server/Test.jar");
894          //server.registerMBean(mlet, new ObjectName(":test=test"));
895

896          server.instantiate("org.jboss.test.jmx.compliance.server.support.AClass");
897       
898 //
899
// FIXME: this test won't work until we have means to reset the JVM wide
900
// loader repository
901
//
902

903          // should not reach here
904
//fail("incorrect classloading behavior");
905
//assertTrue(o.getClass().getClassLoader().equals(mlet));
906
}
907       catch (ReflectionException JavaDoc e)
908       {
909          // expected
910

911          // check that it wraps CNFE
912
assertTrue(e.getTargetException() instanceof ClassNotFoundException JavaDoc);
913       }
914    }
915    
916     // MBeanServer registerMBean ------------------------------------
917

918     /**
919      * Tests registering with null object name.
920      */

921     public void testRegisterNullObjectName() throws Exception JavaDoc
922     {
923        boolean caught = false;
924        try
925        {
926           MBeanServer JavaDoc server = MBeanServerFactory.newMBeanServer();
927           server.registerMBean(new Test(), null);
928        }
929        catch (RuntimeOperationsException JavaDoc e)
930        {
931           if (e.getTargetException() instanceof IllegalArgumentException JavaDoc)
932              caught = true;
933           else
934              fail("Wrong wrapped exception " + e.getTargetException());
935        }
936        if (caught == false)
937           fail("Allowed to register with a null object name");
938     }
939     
940     /**
941      * Tests registering with a pattern object name.
942      */

943     public void testRegisterPatternObjectName() throws Exception JavaDoc
944     {
945        boolean caught = false;
946        try
947        {
948           MBeanServer JavaDoc server = MBeanServerFactory.newMBeanServer();
949           server.registerMBean(new Test(), new ObjectName JavaDoc("Domai?:type=test"));
950        }
951        catch (RuntimeOperationsException JavaDoc e)
952        {
953           if (e.getTargetException() instanceof IllegalArgumentException JavaDoc)
954              caught = true;
955           else
956              fail("Wrong wrapped exception " + e.getTargetException());
957        }
958        if (caught == false)
959           fail("Allowed to register with a pattern object name");
960     }
961     
962     /**
963      * Tests registering into JMImplementation
964      */

965     public void testRegisterJMImplementationObjectName() throws Exception JavaDoc
966     {
967        boolean caught = false;
968        try
969        {
970           MBeanServer JavaDoc server = MBeanServerFactory.newMBeanServer();
971           server.registerMBean(new Test(), new ObjectName JavaDoc("JMImplementation:type=test"));
972        }
973        catch (RuntimeOperationsException JavaDoc e)
974        {
975           if (e.getTargetException() instanceof IllegalArgumentException JavaDoc)
976              caught = true;
977           else
978              fail("Wrong wrapped exception " + e.getTargetException());
979        }
980        if (caught == false)
981           fail("Allowed to register into JMImplementation");
982     }
983     
984     /**
985      * Tests registering into JMImplementation using default domain
986      */

987     public void testRegisterJMImplementationDefaultDomainObjectName() throws Exception JavaDoc
988     {
989        boolean caught = false;
990        try
991        {
992           MBeanServer JavaDoc server = MBeanServerFactory.newMBeanServer("JMImplementation");
993           server.registerMBean(new Test(), new ObjectName JavaDoc(":type=test"));
994        }
995        catch (RuntimeOperationsException JavaDoc e)
996        {
997           if (e.getTargetException() instanceof IllegalArgumentException JavaDoc)
998              caught = true;
999           else
1000             fail("Wrong wrapped exception " + e.getTargetException());
1001       }
1002       if (caught == false)
1003          fail("Allowed to register into JMImplementation");
1004    }
1005    
1006    
1007   /**
1008    * Tests register for an MBean that throws unchecked exception from preRegister()
1009    */

1010   public void testRegisterMBeanOnExceptionFromPreRegister() throws Exception JavaDoc
1011   {
1012      MBeanServer JavaDoc server = MBeanServerFactory.newMBeanServer();
1013      ObjectName JavaDoc name = new ObjectName JavaDoc("test:foo=bar");
1014    
1015      try
1016      {
1017         server.registerMBean(new Test2(), name);
1018         
1019         // should not reach here
1020
fail("Test2 registered despite of throwing an exception from the preRegister() method.");
1021      }
1022      catch (MBeanRegistrationException JavaDoc e)
1023      {
1024         // expected
1025
assertTrue(!server.isRegistered(name));
1026         assertTrue(e.getTargetException() instanceof java.lang.RuntimeException JavaDoc);
1027      }
1028      catch (RuntimeMBeanException JavaDoc e)
1029      {
1030         fail("FAILS IN RI: RuntimeMBeanException instead of MBeanRegistrationException?");
1031      }
1032    }
1033    
1034    /**
1035     * Tests register for an MBean that throws checked exception from preRegister()
1036     */

1037    public void testRegisterMBeanOnExceptionFromPreRegister2() throws Exception JavaDoc
1038    {
1039      MBeanServer JavaDoc server = MBeanServerFactory.newMBeanServer();
1040      ObjectName JavaDoc name = new ObjectName JavaDoc("test:foo=bar");
1041      
1042      try
1043      {
1044         server.registerMBean(new Test3(), name);
1045         
1046         // should not reach here
1047
fail("Test3 registered despite of throwin an exception from the preRegister() method");
1048      }
1049      catch (MBeanRegistrationException JavaDoc e)
1050      {
1051         // expected
1052
assertTrue(!server.isRegistered(name));
1053         assertTrue(e.getTargetException() instanceof MyScreamingException);
1054      }
1055    }
1056    
1057    /**
1058     * Tests register for an MBean that throws an MBeanRegistrationException from
1059     * preRegister() method.
1060     */

1061    public void testRegisterMBeanOnExceptionFromPreRegister3() throws Exception JavaDoc
1062    {
1063       MBeanServer JavaDoc server = MBeanServerFactory.newMBeanServer();
1064       ObjectName JavaDoc name = new ObjectName JavaDoc("test:foo=bar");
1065       
1066       try
1067       {
1068          server.registerMBean(new Test4(), name);
1069          
1070          // should not reach here
1071
fail("Test4 registered despite of throwing an exception from the preRegister() method.");
1072       }
1073       catch (MBeanRegistrationException JavaDoc e)
1074       {
1075          // expected
1076
assertTrue(!server.isRegistered(name));
1077          assertTrue(e.getTargetException() instanceof MyScreamingException);
1078       }
1079    }
1080    
1081    
1082    // MBeanServer unregisterMBean ----------------------------------
1083

1084    /**
1085     * Tests unregister the delegate.
1086     */

1087    public void testUnregisterDelegate() throws Exception JavaDoc
1088    {
1089       boolean caught = false;
1090       try
1091       {
1092          MBeanServer JavaDoc server = MBeanServerFactory.newMBeanServer();
1093          server.unregisterMBean(new ObjectName JavaDoc("JMImplementation:type=MBeanServerDelegate"));
1094       }
1095       // REVIEW: This exception type isn't specified, but it is logical
1096
// and agrees with the RI.
1097
// JPL: agreed
1098
catch (RuntimeOperationsException JavaDoc e)
1099       {
1100          caught = true;
1101       }
1102       if (caught == false)
1103          fail("Allowed to unregister the delegate");
1104    }
1105    
1106    /**
1107     * Tests basic register/unregister
1108     */

1109    public void testBasicUnregister() throws Exception JavaDoc
1110    {
1111       MBeanServer JavaDoc server = MBeanServerFactory.newMBeanServer();
1112       ObjectName JavaDoc name = new ObjectName JavaDoc("test:foo=bar");
1113       
1114       server.registerMBean(new Test(), name);
1115       server.unregisterMBean(name);
1116    }
1117    
1118    /**
1119     * Tests unregister with default domain name
1120     */

1121    public void testUnregisterWithDefaultDomainName() throws Exception JavaDoc
1122    {
1123       try
1124       {
1125          MBeanServer JavaDoc server = MBeanServerFactory.newMBeanServer();
1126          ObjectName JavaDoc name = new ObjectName JavaDoc(":foo=bar");
1127          
1128          server.registerMBean(new Test(), name);
1129          server.unregisterMBean(name);
1130       
1131       }
1132       catch (InstanceNotFoundException JavaDoc e)
1133       {
1134          // FAILS IN RI: RI throws InstanceNotFoundException if you try to
1135
// unregister with implicit default domain name
1136
fail("FAILS IN RI: RI throws InstanceNotFoundException when an existing MBean is unregistered with an implicit default domain name.");
1137       }
1138    }
1139    
1140    /**
1141     * Tests unregister with default domain name gotten from ObjectInstance at registration time.
1142     */

1143     public void testUnregisterWithObjectNameFromRegistration() throws Exception JavaDoc
1144     {
1145        try
1146        {
1147           MBeanServer JavaDoc server = MBeanServerFactory.newMBeanServer();
1148           ObjectName JavaDoc name = new ObjectName JavaDoc(":foo=bar");
1149           
1150           ObjectInstance JavaDoc oi = server.registerMBean(new Test(), name);
1151           name = oi.getObjectName();
1152           
1153           server.unregisterMBean(name);
1154        
1155        }
1156        catch (InstanceNotFoundException JavaDoc e)
1157        {
1158           // FAILS IN RI: RI throws InstanceNotFoundExceptin if you try yo
1159
// unregister with implicit default domain name
1160
fail("FAILS IN RI: RI throws InstanceNotFoundException when an existing MBean is unregistered with an implicit default domain name retrieved from the ObjectInstance returned at registration time.");
1161        }
1162     }
1163    
1164   /**
1165    * Tests unregister for an MBean that prevents unregistration by throwing an
1166    * unchecked exception from its preDeregister() method.
1167    */

1168   public void testUnregisterMBeanOnExceptionFromPreDeregister() throws Exception JavaDoc
1169   {
1170      MBeanServer JavaDoc server = MBeanServerFactory.newMBeanServer();
1171      ObjectName JavaDoc name = new ObjectName JavaDoc("test:foo=bar");
1172    
1173      server.registerMBean(new LockedTest(), name);
1174
1175      try
1176      {
1177         server.unregisterMBean(name);
1178         
1179         // should not reach here
1180
fail("LockedTest unregistered despite of throwing an exception from the preDeregister() method.");
1181      }
1182      catch (MBeanRegistrationException JavaDoc e)
1183      {
1184         // expected, LockedTest should prevent unregistration
1185
assertTrue(server.isRegistered(name));
1186         assertTrue(e.getTargetException() instanceof java.lang.RuntimeException JavaDoc);
1187      }
1188      catch (RuntimeMBeanException JavaDoc e)
1189      {
1190         // FAILS IN RI: according to spec (v1.0, p. 117) any exception thrown from the
1191
// preDeregister() method is wrapped in MBeanRegistrationException by the agent.
1192
fail("FAILS IN RI: spec v1.0: any exception thrown from MBean's preDeregister() method should be wrapped in an MBeanRegistrationException by the agent.");
1193      }
1194    }
1195    
1196    /**
1197     * Tests unregister for an MBean that prevents unregistration by throwing a
1198     * checked exception from its preDeregister() method.
1199     */

1200    public void testUnregisterMBeanOnExceptionFromPreDeregister2() throws Exception JavaDoc
1201    {
1202      MBeanServer JavaDoc server = MBeanServerFactory.newMBeanServer();
1203      ObjectName JavaDoc name = new ObjectName JavaDoc("test:foo=bar");
1204      
1205      server.registerMBean(new LockedTest2(), name);
1206      
1207      try
1208      {
1209         
1210         server.unregisterMBean(name);
1211       
1212         // should not reach here
1213
fail("LockedTest2 unregistered despite of throwin an exception from the preDeregister() method");
1214      }
1215      catch (MBeanRegistrationException JavaDoc e)
1216      {
1217         // expected
1218
assertTrue(server.isRegistered(name));
1219         assertTrue(e.getTargetException() instanceof MyScreamingException);
1220      }
1221    }
1222    
1223    /**
1224     * Tests unregister for an MBean that prevents unregistration by throwing a
1225     * MBeanRegistrationException from its preDeregister() method. This should
1226     * be rethrown by the agent as-is, and not wrapped into another MBeanRegistrationException.
1227     */

1228    public void testUnregisterMBeanOnExceptionFromPreDeregister3() throws Exception JavaDoc
1229    {
1230       MBeanServer JavaDoc server = MBeanServerFactory.newMBeanServer();
1231       ObjectName JavaDoc name = new ObjectName JavaDoc("test:foo=bar");
1232       
1233       server.registerMBean(new LockedTest3(), name);
1234       
1235       try
1236       {
1237          server.unregisterMBean(name);
1238          
1239          // should not reach here
1240
fail("LockedTest3 unregistered despite of throwing an exception from the preDeregister() method.");
1241       }
1242       catch (MBeanRegistrationException JavaDoc e)
1243       {
1244          // expected
1245
assertTrue(server.isRegistered(name));
1246          assertTrue(e.getTargetException() instanceof MyScreamingException);
1247       }
1248    }
1249    
1250   // MBeanServer NotificationListener Plain -----------------------
1251

1252   /**
1253    * Tests basic listener registration to server delegate
1254    */

1255   public synchronized void testAddNotificationListenerToDelegate() throws Exception JavaDoc
1256   {
1257      MBeanServer JavaDoc server = MBeanServerFactory.newMBeanServer();
1258      
1259      class MyNotificationListener implements NotificationListener JavaDoc {
1260
1261         int notificationCount = 0;
1262         
1263         public void handleNotification(Notification JavaDoc notification, Object JavaDoc handback)
1264         {
1265            try
1266            {
1267               notificationCount++;
1268
1269               assertTrue(handback instanceof String JavaDoc);
1270               assertTrue(handback.equals("MyHandback"));
1271               assertTrue(notification.getSource().equals(new ObjectName JavaDoc("JMImplementation:type=MBeanServerDelegate")));
1272            }
1273            catch (Exception JavaDoc e)
1274            {
1275               fail("Unexpected error: " + e.toString());
1276            }
1277         }
1278      }
1279      
1280      MyNotificationListener listener = new MyNotificationListener();
1281      
1282      NotificationFilterSupport JavaDoc filter = new NotificationFilterSupport JavaDoc();
1283      filter.enableType(MBeanServerNotification.REGISTRATION_NOTIFICATION);
1284      
1285      server.addNotificationListener(
1286            new ObjectName JavaDoc("JMImplementation:type=MBeanServerDelegate"),
1287            listener, filter, "MyHandback"
1288      );
1289    
1290      // force notification
1291
server.registerMBean(new Test(), new ObjectName JavaDoc(":foo=bar"));
1292    
1293      assertTrue(listener.notificationCount == 1);
1294   }
1295    
1296   /**
1297    * Tests multiple listeners with different handbacks
1298    */

1299   public synchronized void testAddMultipleListeners()
1300      throws Exception JavaDoc
1301   {
1302      MBeanServer JavaDoc server = MBeanServerFactory.newMBeanServer();
1303      
1304      class MyNotificationListener implements NotificationListener JavaDoc
1305      {
1306         Object JavaDoc handback;
1307         int result = 0;
1308         public MyNotificationListener(Object JavaDoc handback)
1309         {
1310            this.handback = handback;
1311         }
1312         public void handleNotification(Notification JavaDoc notification, Object JavaDoc handback)
1313         {
1314            result++;
1315            assertEquals(this.handback, handback);
1316            result++;
1317         }
1318      }
1319      
1320      MyNotificationListener listener1 = new MyNotificationListener("handback1");
1321      MyNotificationListener listener2 = new MyNotificationListener("handback2");
1322      
1323      server.addNotificationListener(
1324            new ObjectName JavaDoc("JMImplementation:type=MBeanServerDelegate"),
1325            listener1, null, "handback1"
1326      );
1327      server.addNotificationListener(
1328            new ObjectName JavaDoc("JMImplementation:type=MBeanServerDelegate"),
1329            listener2, null, "handback2"
1330      );
1331    
1332      // force notification
1333
server.registerMBean(new Test(), new ObjectName JavaDoc(":foo=bar"));
1334
1335      assertTrue(listener1.result == 2);
1336      assertTrue(listener2.result == 2);
1337   }
1338    
1339   /**
1340    * Tests one listener multiple handbacks
1341    */

1342   public synchronized void testAddListenerMultipleHandbacks()
1343      throws Exception JavaDoc
1344   {
1345      MBeanServer JavaDoc server = MBeanServerFactory.newMBeanServer();
1346      
1347      class MyNotificationListener implements NotificationListener JavaDoc
1348      {
1349         boolean result1 = false;
1350         boolean result2 = false;
1351         public void handleNotification(Notification JavaDoc notification, Object JavaDoc handback)
1352         {
1353            if (handback.equals("handback1"))
1354               result1 = true;
1355            else if (handback.equals("handback2"))
1356               result2 = true;
1357            else
1358               fail("Unexpected handback: " + handback);
1359         }
1360      }
1361      
1362      MyNotificationListener listener = new MyNotificationListener();
1363      
1364      server.addNotificationListener(
1365            new ObjectName JavaDoc("JMImplementation:type=MBeanServerDelegate"),
1366            listener, null, "handback1"
1367      );
1368      server.addNotificationListener(
1369            new ObjectName JavaDoc("JMImplementation:type=MBeanServerDelegate"),
1370            listener, null, "handback2"
1371      );
1372    
1373      // force notification
1374
server.registerMBean(new Test(), new ObjectName JavaDoc(":foo=bar"));
1375
1376      assertTrue(listener.result1);
1377      assertTrue(listener.result2);
1378   }
1379    
1380   /**
1381    * Tests removing a notification listener including multiple handbacks
1382    */

1383   public synchronized void testRemoveListener()
1384      throws Exception JavaDoc
1385   {
1386      MBeanServer JavaDoc server = MBeanServerFactory.newMBeanServer();
1387      
1388      class MyNotificationListener implements NotificationListener JavaDoc
1389      {
1390         Object JavaDoc handback;
1391         int result = 0;
1392         public MyNotificationListener(Object JavaDoc handback)
1393         {
1394            this.handback = handback;
1395         }
1396         public void handleNotification(Notification JavaDoc notification, Object JavaDoc handback)
1397         {
1398            result++;
1399            assertEquals(this.handback, handback);
1400            result++;
1401         }
1402      }
1403      
1404      class MyOtherNotificationListener implements NotificationListener JavaDoc
1405      {
1406         boolean result1 = false;
1407         boolean result2 = false;
1408         public void handleNotification(Notification JavaDoc notification, Object JavaDoc handback)
1409         {
1410            if (handback.equals("handback1"))
1411               result1 = true;
1412            else if (handback.equals("handback2"))
1413               result2 = true;
1414            else
1415               fail("Unexpected handback: " + handback);
1416         }
1417      }
1418      
1419      MyNotificationListener listener1 = new MyNotificationListener("handback1");
1420      MyOtherNotificationListener listener2 = new MyOtherNotificationListener();
1421      
1422      server.addNotificationListener(
1423            new ObjectName JavaDoc("JMImplementation:type=MBeanServerDelegate"),
1424            listener1, null, "handback1"
1425      );
1426      server.addNotificationListener(
1427            new ObjectName JavaDoc("JMImplementation:type=MBeanServerDelegate"),
1428            listener2, null, "handback2"
1429      );
1430      server.addNotificationListener(
1431            new ObjectName JavaDoc("JMImplementation:type=MBeanServerDelegate"),
1432            listener2, null, "handback3"
1433      );
1434      server.removeNotificationListener(
1435            new ObjectName JavaDoc("JMImplementation:type=MBeanServerDelegate"),
1436            listener2
1437      );
1438    
1439      // force notification
1440
server.registerMBean(new Test(), new ObjectName JavaDoc(":foo=bar"));
1441
1442      assertTrue(listener1.result == 2);
1443      assertTrue(listener2.result1 == false);
1444      assertTrue(listener2.result2 == false);
1445   }
1446        
1447   /**
1448    * Tests removing a notification listener triplet
1449    */

1450   public synchronized void testRemoveTriplet()
1451      throws Exception JavaDoc
1452   {
1453      MBeanServer JavaDoc server = MBeanServerFactory.newMBeanServer();
1454      
1455      class MyNotificationListener implements NotificationListener JavaDoc
1456      {
1457         Object JavaDoc handback;
1458         int result = 0;
1459         public MyNotificationListener(Object JavaDoc handback)
1460         {
1461            this.handback = handback;
1462         }
1463         public void handleNotification(Notification JavaDoc notification, Object JavaDoc handback)
1464         {
1465            result++;
1466            assertEquals(this.handback, handback);
1467            result++;
1468         }
1469      }
1470      
1471      MyNotificationListener listener1 = new MyNotificationListener("handback1");
1472      MyNotificationListener listener2 = new MyNotificationListener("handback2");
1473      
1474      server.addNotificationListener(
1475            new ObjectName JavaDoc("JMImplementation:type=MBeanServerDelegate"),
1476            listener1, null, "handback1"
1477      );
1478      server.addNotificationListener(
1479            new ObjectName JavaDoc("JMImplementation:type=MBeanServerDelegate"),
1480            listener2, null, "handback2"
1481      );
1482      server.addNotificationListener(
1483            new ObjectName JavaDoc("JMImplementation:type=MBeanServerDelegate"),
1484            listener2, null, "handback3"
1485      );
1486      server.removeNotificationListener(
1487            new ObjectName JavaDoc("JMImplementation:type=MBeanServerDelegate"),
1488            listener2, null, "handback3"
1489      );
1490    
1491      // force notification
1492
server.registerMBean(new Test(), new ObjectName JavaDoc(":foo=bar"));
1493
1494      assertTrue(listener1.result == 2);
1495      assertTrue(listener1.result == 2);
1496   }
1497
1498   /**
1499    * Tests removing a broadcaster
1500    */

1501   public synchronized void testRemoveBroadcaster()
1502      throws Exception JavaDoc
1503   {
1504      MBeanServer JavaDoc server = MBeanServerFactory.newMBeanServer();
1505      
1506      class MyNotificationListener implements NotificationListener JavaDoc
1507      {
1508         long result = 0;
1509         public MyNotificationListener()
1510         {
1511         }
1512         public void handleNotification(Notification JavaDoc notification, Object JavaDoc handback)
1513         {
1514            result = notification.getSequenceNumber();
1515         }
1516      }
1517
1518      // Register the broadcaster
1519
ObjectName JavaDoc broadcasterName = new ObjectName JavaDoc("test:type=broadcaster");
1520      Broadcaster broadcaster = new Broadcaster();
1521      server.registerMBean(broadcaster, broadcasterName);
1522      
1523      // Add the listener
1524
MyNotificationListener listener = new MyNotificationListener();
1525      server.addNotificationListener(broadcasterName, listener, null, null);
1526
1527      // Test we get a notification
1528
broadcaster.doSomething();
1529      assertEquals(1, listener.result);
1530
1531      // Remove the broadcaster
1532
server.unregisterMBean(broadcasterName);
1533      
1534      // This notification shouldn't work
1535
broadcaster.doSomething();
1536      try
1537      {
1538         assertEquals(1, listener.result);
1539      }
1540      catch (AssertionFailedError e)
1541      {
1542         fail("FAILS IN RI: Removing a notification broadcaster does not " +
1543              "remove the listeners registered against the object name.");
1544      }
1545   }
1546    
1547   /**
1548    * Tests adding the listener to different broadcasters
1549    */

1550   public synchronized void testAddListenerToTwoBroadcasters()
1551      throws Exception JavaDoc
1552   {
1553      MBeanServer JavaDoc server = MBeanServerFactory.newMBeanServer();
1554      
1555      class MyNotificationListener implements NotificationListener JavaDoc
1556      {
1557         long result = 0;
1558         public MyNotificationListener()
1559         {
1560         }
1561         public void handleNotification(Notification JavaDoc notification, Object JavaDoc handback)
1562         {
1563            result++;
1564         }
1565      }
1566
1567      // Register the broadcaster
1568
ObjectName JavaDoc broadcasterName = new ObjectName JavaDoc("test:type=broadcaster");
1569      Broadcaster broadcaster = new Broadcaster();
1570      server.registerMBean(broadcaster, broadcasterName);
1571      
1572      // Add the listener to the broadcaster
1573
MyNotificationListener listener = new MyNotificationListener();
1574      server.addNotificationListener(broadcasterName, listener, null, null);
1575      
1576      // Add the listener to the delegate
1577
server.addNotificationListener(
1578            new ObjectName JavaDoc("JMImplementation:type=MBeanServerDelegate"),
1579            listener, null, null
1580      );
1581
1582      // Test we get a notification from the broadcaster
1583
broadcaster.doSomething();
1584      assertEquals(1, listener.result);
1585
1586      // Test we get a notification from the delegate
1587
server.registerMBean(new Test(), new ObjectName JavaDoc("Test:foo=bar"));
1588      assertEquals(2, listener.result);
1589
1590      // Remove the broadcaster
1591
server.unregisterMBean(broadcasterName);
1592      assertEquals(3, listener.result);
1593
1594      // Make sure we are still listening to the delegate
1595
server.unregisterMBean(new ObjectName JavaDoc("Test:foo=bar"));
1596      assertEquals(4, listener.result);
1597   }
1598    
1599   /**
1600    * Tests adding the listener to different broadcasters but remove one
1601    */

1602   public synchronized void testAddListenerToTwoBroadcastersRemoveOne()
1603      throws Exception JavaDoc
1604   {
1605      MBeanServer JavaDoc server = MBeanServerFactory.newMBeanServer();
1606      
1607      class MyNotificationListener implements NotificationListener JavaDoc
1608      {
1609         long result = 0;
1610         public MyNotificationListener()
1611         {
1612         }
1613         public void handleNotification(Notification JavaDoc notification, Object JavaDoc handback)
1614         {
1615            result++;
1616         }
1617      }
1618
1619      // Register the broadcaster
1620
ObjectName JavaDoc broadcasterName = new ObjectName JavaDoc("test:type=broadcaster");
1621      Broadcaster broadcaster = new Broadcaster();
1622      server.registerMBean(broadcaster, broadcasterName);
1623      
1624      // Add the listener to the broadcaster
1625
MyNotificationListener listener = new MyNotificationListener();
1626      server.addNotificationListener(broadcasterName, listener, null, null);
1627      
1628      // Add the listener to the delegate
1629
server.addNotificationListener(
1630            new ObjectName JavaDoc("JMImplementation:type=MBeanServerDelegate"),
1631            listener, null, null
1632      );
1633
1634      // Remove ourselves from the broadcaster
1635
server.removeNotificationListener(broadcasterName, listener);
1636
1637      // Test we get a notification from the broadcaster
1638
broadcaster.doSomething();
1639      assertEquals(0, listener.result);
1640
1641      // Test we get a notification from the delegate
1642
server.registerMBean(new Test(), new ObjectName JavaDoc("Test:foo=bar"));
1643      assertEquals(1, listener.result);
1644   }
1645    
1646   // MBeanServer NotificationListener Object Name -----------------
1647

1648   /**
1649    * Tests basic listener registration to server delegate
1650    */

1651   public synchronized void testaddMBeanToDelegate() throws Exception JavaDoc
1652   {
1653      MBeanServer JavaDoc server = MBeanServerFactory.newMBeanServer();
1654      
1655      MBeanListener listener = new MBeanListener();
1656      ObjectName JavaDoc listenerName = new ObjectName JavaDoc("test:type=listener");
1657      server.registerMBean(listener, listenerName);
1658      
1659      NotificationFilterSupport JavaDoc filter = new NotificationFilterSupport JavaDoc();
1660      filter.enableType(MBeanServerNotification.REGISTRATION_NOTIFICATION);
1661      
1662      ObjectName JavaDoc delegateName =
1663         new ObjectName JavaDoc("JMImplementation:type=MBeanServerDelegate");
1664      server.addNotificationListener(delegateName, listenerName, filter, "MyHandback");
1665    
1666      // force notification
1667
server.registerMBean(new Test(), new ObjectName JavaDoc(":foo=bar"));
1668    
1669      assertTrue(listener.count == 1);
1670      assertTrue(listener.source.equals(delegateName));
1671      assertTrue(listener.handback.equals("MyHandback"));
1672   }
1673    
1674   /**
1675    * Tests multiple listeners with different handbacks
1676    */

1677   public synchronized void testAddMBeanMultipleListeners()
1678      throws Exception JavaDoc
1679   {
1680      MBeanServer JavaDoc server = MBeanServerFactory.newMBeanServer();
1681      
1682      MBeanListener listener1 = new MBeanListener();
1683      ObjectName JavaDoc listenerName1 = new ObjectName JavaDoc("test:type=listener1");
1684      server.registerMBean(listener1, listenerName1);
1685      MBeanListener listener2 = new MBeanListener();
1686      ObjectName JavaDoc listenerName2 = new ObjectName JavaDoc("test:type=listener2");
1687      server.registerMBean(listener2, listenerName2);
1688      
1689      ObjectName JavaDoc delegateName =
1690         new ObjectName JavaDoc("JMImplementation:type=MBeanServerDelegate");
1691      server.addNotificationListener(delegateName, listenerName1, null, "handback1");
1692      server.addNotificationListener(delegateName, listenerName2, null, "handback2");
1693    
1694      // force notification
1695
server.registerMBean(new Test(), new ObjectName JavaDoc(":foo=bar"));
1696
1697      assertEquals(1, listener1.count);
1698      assertEquals(listener1.source,delegateName);
1699      assertEquals(listener1.handback,"handback1");
1700      assertEquals(1, listener2.count);
1701      assertEquals(listener2.source,delegateName);
1702      assertEquals(listener2.handback,"handback2");
1703   }
1704    
1705   /**
1706    * Tests one listener multiple handbacks
1707    */

1708   public synchronized void testAddMBeanListenerMultipleHandbacks()
1709      throws Exception JavaDoc
1710   {
1711      MBeanServer JavaDoc server = MBeanServerFactory.newMBeanServer();
1712      
1713      MBeanListener listener = new MBeanListener("handback1", "handback2");
1714      ObjectName JavaDoc listenerName = new ObjectName JavaDoc("test:type=listener");
1715      server.registerMBean(listener, listenerName);
1716      
1717      ObjectName JavaDoc delegateName =
1718         new ObjectName JavaDoc("JMImplementation:type=MBeanServerDelegate");
1719      server.addNotificationListener(delegateName, listenerName, null, "handback1");
1720      server.addNotificationListener(delegateName, listenerName, null, "handback2");
1721    
1722      // force notification
1723
server.registerMBean(new Test(), new ObjectName JavaDoc(":foo=bar"));
1724
1725      assertTrue(listener.count1 == 1);
1726      assertEquals(listener.source1,delegateName);
1727      assertEquals(listener.handback1,"handback1");
1728      assertTrue(listener.count2 == 1);
1729      assertEquals(listener.source2,delegateName);
1730      assertEquals(listener.handback2,"handback2");
1731   }
1732    
1733   /**
1734    * Tests removing a notification listener including multiple handbacks
1735    */

1736   public synchronized void testMBeanRemoveListener()
1737      throws Exception JavaDoc
1738   {
1739      MBeanServer JavaDoc server = MBeanServerFactory.newMBeanServer();
1740
1741      MBeanListener listener1 = new MBeanListener();
1742      ObjectName JavaDoc listenerName1 = new ObjectName JavaDoc("test:type=listener1");
1743      server.registerMBean(listener1, listenerName1);
1744      MBeanListener listener2 = new MBeanListener("handback2", "handback3");
1745      ObjectName JavaDoc listenerName2 = new ObjectName JavaDoc("test:type=listener2");
1746      server.registerMBean(listener2, listenerName2);
1747      
1748      ObjectName JavaDoc delegateName =
1749         new ObjectName JavaDoc("JMImplementation:type=MBeanServerDelegate");
1750      server.addNotificationListener(delegateName, listenerName1, null, "handback1");
1751      server.addNotificationListener(delegateName, listenerName2, null, "handback2");
1752      server.addNotificationListener(delegateName, listenerName2, null, "handback3");
1753      server.removeNotificationListener(delegateName, listenerName2);
1754    
1755      // force notification
1756
server.registerMBean(new Test(), new ObjectName JavaDoc(":foo=bar"));
1757      assertTrue("Listener1 should get a notification", listener1.count == 1);
1758      assertTrue("Source should be the delegate", listener1.source.equals(delegateName));
1759      assertTrue("Listener1 should get handback1", listener1.handback.equals("handback1"));
1760      assertTrue("Listener2 should have no notiifcation", listener2.count == 0);
1761      assertTrue("Listener2 should have no notiifcation for handback2", listener2.count1 == 0);
1762      assertTrue("Listener2 should have no notiifcation for handback3", listener2.count2 == 0);
1763   }
1764    
1765   /**
1766    * Tests removing a notification listener triple
1767    */

1768   public synchronized void testMBeanRemoveTriplet()
1769      throws Exception JavaDoc
1770   {
1771      MBeanServer JavaDoc server = MBeanServerFactory.newMBeanServer();
1772
1773      MBeanListener listener1 = new MBeanListener();
1774      ObjectName JavaDoc listenerName1 = new ObjectName JavaDoc("test:type=listener1");
1775      server.registerMBean(listener1, listenerName1);
1776      MBeanListener listener2 = new MBeanListener("handback2", "handback3");
1777      ObjectName JavaDoc listenerName2 = new ObjectName JavaDoc("test:type=listener2");
1778      server.registerMBean(listener2, listenerName2);
1779      
1780      ObjectName JavaDoc delegateName =
1781         new ObjectName JavaDoc("JMImplementation:type=MBeanServerDelegate");
1782      server.addNotificationListener(delegateName, listenerName1, null, "handback1");
1783      server.addNotificationListener(delegateName, listenerName2, null, "handback2");
1784      server.addNotificationListener(delegateName, listenerName2, null, "handback3");
1785      server.removeNotificationListener(delegateName, listenerName2, null, "handback3");
1786    
1787      // force notification
1788
server.registerMBean(new Test(), new ObjectName JavaDoc(":foo=bar"));
1789      assertTrue("Listener1 should get a notification", listener1.count == 1);
1790      assertTrue("Source should be the delegate", listener1.source.equals(delegateName));
1791      assertTrue("Listener1 should get handback1", listener1.handback.equals("handback1"));
1792      assertTrue("Listener2 should get a notification", listener2.count1 == 1);
1793      assertTrue("Source should be the delegate", listener2.source1.equals(delegateName));
1794      assertTrue("Listener2 should get handback2", listener2.handback1.equals("handback2"));
1795      assertTrue("Listener2 should have no notiifcation for handback3", listener2.count2 == 0);
1796   }
1797    
1798   /**
1799    * Tests MBeanRedeploy notification
1800    */

1801   public synchronized void testMBeanRedeployNotification()
1802      throws Exception JavaDoc
1803   {
1804      MBeanServer JavaDoc server = MBeanServerFactory.newMBeanServer();
1805
1806      MBeanListener listener = new MBeanListener();
1807      ObjectName JavaDoc listenerName = new ObjectName JavaDoc("test:type=listener");
1808      server.registerMBean(listener, listenerName);
1809
1810      ObjectName JavaDoc broadcasterName = new ObjectName JavaDoc("test:type=Broadcaster");
1811      server.registerMBean(new Broadcaster(), broadcasterName);
1812      
1813      server.addNotificationListener(broadcasterName, listenerName, null, "handback1");
1814      server.removeNotificationListener(broadcasterName, listenerName, null, "handback1");
1815      server.unregisterMBean(broadcasterName);
1816
1817      Broadcaster broadcaster = new Broadcaster();
1818      server.registerMBean(broadcaster, broadcasterName);
1819      server.addNotificationListener(broadcasterName, listenerName, null, "handback2");
1820    
1821      // force notification
1822
broadcaster.doSomething();
1823      assertTrue("Listener should get a notification", listener.count == 1);
1824      assertTrue("Source should be the broadcaster", listener.source.equals(broadcasterName));
1825      assertTrue("Listener should get handback2", listener.handback.equals("handback2"));
1826   }
1827    
1828   /**
1829    * Tests removing a broadcaster
1830    */

1831   public synchronized void testMBeanRemoveBroadcaster()
1832      throws Exception JavaDoc
1833   {
1834      MBeanServer JavaDoc server = MBeanServerFactory.newMBeanServer();
1835      
1836      MBeanListener listener1 = new MBeanListener();
1837      ObjectName JavaDoc listenerName1 = new ObjectName JavaDoc("test:type=listener1");
1838      server.registerMBean(listener1, listenerName1);
1839
1840      // Register the broadcaster
1841
ObjectName JavaDoc broadcasterName = new ObjectName JavaDoc("test:type=broadcaster");
1842      Broadcaster broadcaster = new Broadcaster();
1843      server.registerMBean(broadcaster, broadcasterName);
1844      
1845      // Add the listener
1846
server.addNotificationListener(broadcasterName, listenerName1, null, null);
1847
1848      // Test we get a notification
1849
broadcaster.doSomething();
1850      assertEquals(1, listener1.count);
1851      assertEquals(broadcasterName, listener1.source);
1852
1853      // Remove the broadcaster
1854
server.unregisterMBean(broadcasterName);
1855      
1856      // This notification shouldn't work
1857
broadcaster.doSomething();
1858      try
1859      {
1860         assertEquals(1, listener1.count);
1861      }
1862      catch (AssertionFailedError e)
1863      {
1864         fail("FAILS IN RI: Removing a notification broadcaster does not " +
1865              "remove the listeners registered against the object name.");
1866      }
1867   }
1868    
1869   /**
1870    * Tests adding the listener to different broadcasters
1871    */

1872   public synchronized void testAddMBeanListenerToTwoBroadcasters()
1873      throws Exception JavaDoc
1874   {
1875      MBeanServer JavaDoc server = MBeanServerFactory.newMBeanServer();
1876      
1877      MBeanListener listener1 = new MBeanListener();
1878      ObjectName JavaDoc listenerName1 = new ObjectName JavaDoc("test:type=listener1");
1879      server.registerMBean(listener1, listenerName1);
1880
1881      // Register the broadcaster
1882
ObjectName JavaDoc broadcasterName = new ObjectName JavaDoc("test:type=broadcaster");
1883      Broadcaster broadcaster = new Broadcaster();
1884      server.registerMBean(broadcaster, broadcasterName);
1885      
1886      // Add the listener to the broadcaster
1887
server.addNotificationListener(broadcasterName, listenerName1, null, null);
1888      
1889      // Add the listener to the delegate
1890
ObjectName JavaDoc delegateName =
1891         new ObjectName JavaDoc("JMImplementation:type=MBeanServerDelegate");
1892      server.addNotificationListener(delegateName,listenerName1, null, null);
1893
1894      // Test we get a notification from the broadcaster
1895
broadcaster.doSomething();
1896      assertEquals(1, listener1.count);
1897      assertEquals(broadcasterName, listener1.source);
1898
1899      try
1900      {
1901         // Test we get a notification from the delegate
1902
server.registerMBean(new Test(), new ObjectName JavaDoc("Test:foo=bar"));
1903         assertEquals(2, listener1.count);
1904         assertEquals(delegateName, listener1.source);
1905
1906         // Remove the broadcaster
1907
server.unregisterMBean(broadcasterName);
1908         assertEquals(3, listener1.count);
1909         assertEquals(delegateName, listener1.source);
1910
1911         // Make sure we are still listening to the delegate
1912
server.unregisterMBean(new ObjectName JavaDoc("Test:foo=bar"));
1913         assertEquals(4, listener1.count);
1914         assertEquals(delegateName, listener1.source);
1915      }
1916      catch (AssertionFailedError e)
1917      {
1918         fail("FAILS IN RI: Listener registered with ObjectName in MBeanServer " +
1919              "reports the wrong source for multiple broadcaster.");
1920      }
1921   }
1922    
1923   /**
1924    * Tests adding the listener to different broadcasters but remove one
1925    */

1926   public synchronized void testAddMBeanListenerToTwoBroadcastersRemoveOne()
1927      throws Exception JavaDoc
1928   {
1929      MBeanServer JavaDoc server = MBeanServerFactory.newMBeanServer();
1930      
1931      MBeanListener listener1 = new MBeanListener();
1932      ObjectName JavaDoc listenerName1 = new ObjectName JavaDoc("test:type=listener1");
1933      server.registerMBean(listener1, listenerName1);
1934
1935      // Register the broadcaster
1936
ObjectName JavaDoc broadcasterName = new ObjectName JavaDoc("test:type=broadcaster");
1937      Broadcaster broadcaster = new Broadcaster();
1938      server.registerMBean(broadcaster, broadcasterName);
1939      
1940      // Add the listener to the broadcaster
1941
server.addNotificationListener(broadcasterName, listenerName1, null, null);
1942      
1943      // Add the listener to the delegate
1944
ObjectName JavaDoc delegateName =
1945         new ObjectName JavaDoc("JMImplementation:type=MBeanServerDelegate");
1946      server.addNotificationListener(delegateName,listenerName1, null, null);
1947
1948      // Remove ourselves from the broadcaster
1949
server.removeNotificationListener(broadcasterName, listener1);
1950
1951      // Test we get a notification from the broadcaster
1952
broadcaster.doSomething();
1953      assertEquals(0, listener1.count);
1954
1955      // Test we get a notification from the delegate
1956
server.registerMBean(new Test(), new ObjectName JavaDoc("Test:foo=bar"));
1957      assertEquals(1, listener1.count);
1958      try
1959      {
1960         assertEquals(delegateName, listener1.source);
1961      }
1962      catch (AssertionFailedError e)
1963      {
1964         fail("FAILS IN RI: Listener registered with ObjectName in MBeanServer " +
1965              "reports the wrong source for multiple broadcaster, " +
1966              "even when the broadcaster it reports has been removed.");
1967      }
1968   }
1969    
1970   public void testGetDomains()
1971      throws Exception JavaDoc
1972   {
1973      MBeanServer JavaDoc server = MBeanServerFactory.newMBeanServer();
1974
1975      assertTrue("Only one domain at the start", server.getDomains().length == 1);
1976      assertEquals(server.getDomains()[0], "JMImplementation");
1977
1978      server.registerMBean(new Test(), new ObjectName JavaDoc("Domain1:test=test1"));
1979      server.registerMBean(new Test(), new ObjectName JavaDoc("Domain1:test=test2"));
1980      server.registerMBean(new Test(), new ObjectName JavaDoc("Domain2:test=test1"));
1981      server.registerMBean(new Test(), new ObjectName JavaDoc("Domain3:test=test1"));
1982
1983      assertTrue("Now four domains", server.getDomains().length == 4);
1984      List JavaDoc domains = Arrays.asList(server.getDomains());
1985      assertTrue("server has JMImplementation", domains.contains("JMImplementation"));
1986      assertTrue("server has Domain1", domains.contains("Domain1"));
1987      assertTrue("server has Domain2", domains.contains("Domain2"));
1988      assertTrue("server has Domain3", domains.contains("Domain3"));
1989
1990      server.unregisterMBean(new ObjectName JavaDoc("Domain3:test=test1"));
1991
1992      assertTrue("Now three domains", server.getDomains().length == 3);
1993      domains = Arrays.asList(server.getDomains());
1994      assertTrue("server has JMImplementation", domains.contains("JMImplementation"));
1995      assertTrue("server has Domain1", domains.contains("Domain1"));
1996      assertTrue("server has Domain2", domains.contains("Domain2"));
1997      assertTrue("server no longer has Domain3", domains.contains("Domain3") == false);
1998
1999      server.unregisterMBean(new ObjectName JavaDoc("Domain1:test=test1"));
2000
2001      assertTrue("Still three domains", server.getDomains().length == 3);
2002      domains = Arrays.asList(server.getDomains());
2003      assertTrue("server has JMImplementation", domains.contains("JMImplementation"));
2004      assertTrue("server still has Domain1", domains.contains("Domain1"));
2005      assertTrue("server has Domain2", domains.contains("Domain2"));
2006      assertTrue("server no longer has Domain3", domains.contains("Domain3") == false);
2007   }
2008
2009   public void testIsInstanceOf()
2010      throws Exception JavaDoc
2011   {
2012      MBeanServer JavaDoc server = MBeanServerFactory.newMBeanServer();
2013      ObjectName JavaDoc baseName = new ObjectName JavaDoc("MBeanServerTEST:type=testIsInstanceOf,name=Base");
2014      ObjectName JavaDoc derivedName = new ObjectName JavaDoc("MBeanServerTEST:type=testIsInstanceOf,name=Derived");
2015      ObjectName JavaDoc unrelatedName = new ObjectName JavaDoc("MBeanServerTEST:type=testIsInstanceOf,name=Unrelated");
2016
2017      server.registerMBean(new Base(), baseName);
2018      server.registerMBean(new Derived(), derivedName);
2019      server.registerMBean(new Unrelated(), unrelatedName);
2020
2021      assertTrue("Base is an instance Object",
2022         server.isInstanceOf(baseName, Object JavaDoc.class.getName()));
2023      assertTrue("Base is an instance BaseMBean",
2024         server.isInstanceOf(baseName, BaseMBean.class.getName()));
2025      assertTrue("Base is an instance Base",
2026         server.isInstanceOf(baseName, Base.class.getName()));
2027      assertTrue("Derived is an instance Object",
2028         server.isInstanceOf(derivedName, Object JavaDoc.class.getName()));
2029      assertTrue("Derived is an instance BaseMBean",
2030         server.isInstanceOf(derivedName, BaseMBean.class.getName()));
2031      assertTrue("Derived is an instance Base",
2032         server.isInstanceOf(derivedName, Base.class.getName()));
2033      assertTrue("Derived is an instance Derived",
2034         server.isInstanceOf(derivedName, Derived.class.getName()));
2035      assertTrue("Unrelated is an instance Object",
2036         server.isInstanceOf(unrelatedName, Object JavaDoc.class.getName()));
2037      assertTrue("Unrelated is an instance UnrelatedMBean",
2038         server.isInstanceOf(unrelatedName, UnrelatedMBean.class.getName()));
2039      assertTrue("Unrelated is an instance Unrelated",
2040         server.isInstanceOf(unrelatedName, Unrelated.class.getName()));
2041
2042      assertTrue("Base is an not instance Derived",
2043         server.isInstanceOf(baseName, Derived.class.getName()) == false);
2044      assertTrue("Base is an not instance UnrelatedMBean",
2045         server.isInstanceOf(baseName, UnrelatedMBean.class.getName()) == false);
2046      assertTrue("Base is an not instance Unrelated",
2047         server.isInstanceOf(baseName, Unrelated.class.getName()) == false);
2048      assertTrue("Derived is an not instance UnrelatedMBean",
2049         server.isInstanceOf(derivedName, UnrelatedMBean.class.getName()) == false);
2050      assertTrue("Dervied is an not instance Unrelated",
2051         server.isInstanceOf(derivedName, Unrelated.class.getName()) == false);
2052      assertTrue("Unrelated is an not instance BaseMBean",
2053         server.isInstanceOf(unrelatedName, BaseMBean.class.getName()) == false);
2054      assertTrue("Unrelated is an not instance Base",
2055         server.isInstanceOf(unrelatedName, Base.class.getName()) == false);
2056      assertTrue("Unrelated is an not instance Derived",
2057         server.isInstanceOf(unrelatedName, Derived.class.getName()) == false);
2058   }
2059
2060   public void testIsInstanceOfErrors()
2061      throws Exception JavaDoc
2062   {
2063      MBeanServer JavaDoc server = MBeanServerFactory.newMBeanServer();
2064      ObjectName JavaDoc baseName = new ObjectName JavaDoc("MBeanServerTEST:type=testIsInstanceOf,name=Base");
2065      ObjectName JavaDoc dynamicName = new ObjectName JavaDoc("MBeanServerTEST:type=testIsInstanceOf,name=Dynamic");
2066      ObjectName JavaDoc doesNotExistName = new ObjectName JavaDoc("MBeanServerTEST:type=testIsInstanceOf,name=DoesNotExist");
2067
2068      server.registerMBean(new Base(), baseName);
2069      server.registerMBean(new Dynamic(), dynamicName);
2070
2071      assertTrue("Base is not an instance of a class that does not exist",
2072         server.isInstanceOf(baseName, "does.not.exist") == false);
2073
2074      assertTrue("FAILS IN RI: Not an instance if the getMBeanInfo reports a class name that does not exist",
2075         server.isInstanceOf(dynamicName, Object JavaDoc.class.getName()) == false);
2076
2077      boolean caught = false;
2078      try
2079      {
2080         server.isInstanceOf(doesNotExistName, Object JavaDoc.class.getName());
2081      }
2082      catch (InstanceNotFoundException JavaDoc e)
2083      {
2084         caught = true;
2085      }
2086      assertTrue("Should get an instance not found for a non-existent mbean", caught);
2087   }
2088}
2089
Popular Tags