KickJava   Java API By Example, From Geeks To Geeks.

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


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 javax.management.InstanceAlreadyExistsException;
11 import javax.management.InstanceNotFoundException;
12 import javax.management.MBeanServer;
13 import javax.management.MBeanServerFactory;
14 import javax.management.ObjectName;
15 import javax.management.relation.RelationService;
16
17 import junit.framework.TestCase;
18 import test.compliance.server.support.Broadcaster;
19 import test.compliance.server.support.Test;
20
21 /**
22  * Tests default domain handling in the Server.<p>
23  *
24  * TODO createMBean x4.
25  * TODO deserialize x2
26  * TODO getAttribute x2
27  * TODO getMBeanInfo
28  * TODO instantiate x2
29  * TODO isInstanceOf
30  * TODO isRegistered
31  * TODO removeNotificationListener x2
32  * TODO setAttribute x2
33  *
34  * @author <a HREF="mailto:Adrian.Brock@HappeningTimes.com">Adrian Brock</a>.
35  */

36 public class DefaultDomainTestCase
37   extends TestCase
38 {
39    // Attributes ----------------------------------------------------------------
40

41    // Constructor ---------------------------------------------------------------
42

43    /**
44     * Construct the test
45     */

46    public DefaultDomainTestCase(String s)
47    {
48       super(s);
49    }
50
51    // Tests ---------------------------------------------------------------------
52

53    /**
54     * Test Register in the "anonymous" default domain.
55     */

56    public void testDefaultDomainRegisterUnqualified()
57    {
58       MBeanServer server =null;
59       ObjectName unqualifiedName = null;
60       ObjectName qualifiedName = null;
61       try
62       {
63          server = MBeanServerFactory.createMBeanServer();
64          unqualifiedName = new ObjectName(":property=1");
65          qualifiedName = new ObjectName("DefaultDomain:property=1");
66          server.registerMBean(new Test(), unqualifiedName);
67       }
68       catch (Exception e)
69       {
70          fail(e.toString());
71       }
72
73       assertEquals("DefaultDomain", server.getDefaultDomain());
74
75       try
76       {
77          server.getObjectInstance(unqualifiedName);
78       }
79       catch (InstanceNotFoundException e)
80       {
81          fail("Unqualified lookup failed");
82       }
83
84       try
85       {
86          server.getObjectInstance(qualifiedName);
87       }
88       catch (InstanceNotFoundException e)
89       {
90          fail("Qualified lookup failed");
91       }
92
93       if (server != null)
94          MBeanServerFactory.releaseMBeanServer(server);
95    }
96
97    /**
98     * Test Register in the "anonymous" default domain.
99     */

100    public void testDefaultDomainRegisterQualified()
101    {
102       MBeanServer server =null;
103       ObjectName unqualifiedName = null;
104       ObjectName qualifiedName = null;
105       try
106       {
107          server = MBeanServerFactory.createMBeanServer();
108          unqualifiedName = new ObjectName(":property=1");
109          qualifiedName = new ObjectName("DefaultDomain:property=1");
110          server.registerMBean(new Test(), qualifiedName);
111       }
112       catch (Exception e)
113       {
114          fail(e.toString());
115       }
116
117       assertEquals("DefaultDomain", server.getDefaultDomain());
118
119       try
120       {
121          server.getObjectInstance(unqualifiedName);
122       }
123       catch (InstanceNotFoundException e)
124       {
125          fail("Unqualified lookup failed");
126       }
127
128       try
129       {
130          server.getObjectInstance(qualifiedName);
131       }
132       catch (InstanceNotFoundException e)
133       {
134          fail("Qualified lookup failed");
135       }
136
137       if (server != null)
138          MBeanServerFactory.releaseMBeanServer(server);
139    }
140
141    /**
142     * Test Register in a named default domain.
143     */

144    public void testMyDefaultDomainRegisterUnqualified()
145    {
146       MBeanServer server =null;
147       ObjectName unqualifiedName = null;
148       ObjectName qualifiedName = null;
149       try
150       {
151          server = MBeanServerFactory.createMBeanServer("MyDomain");
152          unqualifiedName = new ObjectName(":property=1");
153          qualifiedName = new ObjectName("MyDomain:property=1");
154          server.registerMBean(new Test(), unqualifiedName);
155       }
156       catch (Exception e)
157       {
158          fail(e.toString());
159       }
160
161       assertEquals("MyDomain", server.getDefaultDomain());
162
163       try
164       {
165          server.getObjectInstance(unqualifiedName);
166       }
167       catch (InstanceNotFoundException e)
168       {
169          fail("Unqualified lookup failed");
170       }
171
172       try
173       {
174          server.getObjectInstance(qualifiedName);
175       }
176       catch (InstanceNotFoundException e)
177       {
178          fail("Qualified lookup failed");
179       }
180
181       if (server != null)
182          MBeanServerFactory.releaseMBeanServer(server);
183    }
184
185    /**
186     * Test Register in a named default domain.
187     */

188    public void testMyDefaultDomainRegisterQualified()
189    {
190       MBeanServer server =null;
191       ObjectName unqualifiedName = null;
192       ObjectName qualifiedName = null;
193       try
194       {
195          server = MBeanServerFactory.createMBeanServer("MyDomain");
196          unqualifiedName = new ObjectName(":property=1");
197          qualifiedName = new ObjectName("MyDomain:property=1");
198          server.registerMBean(new Test(), qualifiedName);
199       }
200       catch (Exception e)
201       {
202          fail(e.toString());
203       }
204
205       assertEquals("MyDomain", server.getDefaultDomain());
206
207       try
208       {
209          server.getObjectInstance(unqualifiedName);
210       }
211       catch (InstanceNotFoundException e)
212       {
213          fail("Unqualified lookup failed");
214       }
215
216       try
217       {
218          server.getObjectInstance(qualifiedName);
219       }
220       catch (InstanceNotFoundException e)
221       {
222          fail("Qualified lookup failed");
223       }
224
225       if (server != null)
226          MBeanServerFactory.releaseMBeanServer(server);
227    }
228
229    /**
230     * Test register qualified/unqualifed.
231     */

232    public void testRegisterQualifiedUnqualified()
233    {
234       duplicateRegister(":property=1", "MyDomain:property=1");
235    }
236
237    /**
238     * Test register unqualified/qualifed.
239     */

240    public void testRegisterUnqualifiedQualified()
241    {
242       duplicateRegister("MyDomain:property=1", ":property=1");
243    }
244
245    /**
246     * Test register unqualified/unqualified.
247     */

248    public void testRegisterUnqualifiedUnqualified()
249    {
250       duplicateRegister(":property=1", ":property=1");
251    }
252
253    /**
254     * Test invoke qualified/unqualifed.
255     */

256    public void testInvokeQualifiedUnqualified()
257    {
258       invoke(":property=1", "MyDomain:property=1");
259    }
260
261    /**
262     * Test invoke unqualified/qualifed.
263     */

264    public void testInvokeUnqualifiedQualified()
265    {
266       invoke("MyDomain:property=1", ":property=1");
267    }
268
269    /**
270     * Test invoke unqualified/unqualified.
271     */

272    public void testInvokeUnqualifiedUnqualified()
273    {
274       invoke(":property=1", ":property=1");
275    }
276
277    /**
278     * Test register qualified unregister unqualified.
279     */

280    public void testRegisterQualifiedUnregisterUnqualified()
281    {
282       unregister("MyDomain:property=1", ":property=1");
283    }
284
285    /**
286     * Test register unqualified unregister qualifed.
287     */

288    public void testRegisterUnQualifiedUnregisterQualified()
289    {
290       unregister(":property=1", "MyDomain:property=1");
291    }
292
293    /**
294     * Test register unqualified unregister unqualified.
295     */

296    public void testRegisterUnqualifiedUnregisterUnqualified()
297    {
298       unregister(":property=1", ":property=1");
299    }
300
301    /**
302     * Add notification listenter. ObjectName, Listener
303     */

304    public void testAddNLUnqualifiedNameListenerRegisterQualified()
305    {
306       addNLNameListener("MyDomain:property=1", ":property=1");
307    }
308
309    /**
310     * Add notification listenter. ObjectName, Listener
311     */

312    public void testAddNLQualifiedNameListenerRegisterUnqualified()
313    {
314       addNLNameListener(":property=1", "MyDomain:property=1");
315    }
316
317    /**
318     * Add notification listenter. ObjectName, Listener
319     */

320    public void testAddNLUnqualifiedNameListenerRegisterUnqualified()
321    {
322       addNLNameListener(":property=1", ":property=1");
323    }
324
325    /**
326     * Add notification listenter. ObjectName, ObjectName
327     */

328    public void testAddNLUnqualifiedQualifiedRegisterUnqualifiedQualified()
329    {
330       addNLNameName(":property=1", "MyDomain:property=2",":property=1", "MyDomain:property=2");
331    }
332
333    /**
334     * Add notification listenter. ObjectName, ObjectName
335     */

336    public void testAddNLUnqualifiedQualifiedRegisterQualifiedQualified()
337    {
338       addNLNameName(":property=1", "MyDomain:property=2","MyDomain:property=1", "MyDomain:property=2");
339    }
340
341    /**
342     * Add notification listenter. ObjectName, ObjectName
343     */

344    public void testAddNLQualifiedQualifiedRegisterUnqualifiedQualified()
345    {
346       addNLNameName("MyDomain:property=1", "MyDomain:property=2",":property=1", "MyDomain:property=2");
347    }
348
349    /**
350     * Add notification listenter. ObjectName, ObjectName
351     */

352    public void testAddNLQualifiedUnqualifiedRegisterQualifiedUnqualified()
353    {
354       addNLNameName("MyDomain:property=1", ":property=2","MyDomain:property=1", ":property=2");
355    }
356
357    /**
358     * Add notification listenter. ObjectName, ObjectName
359     */

360    public void testAddNLQualifiedUnqualifiedRegisterQualifiedQualified()
361    {
362       addNLNameName("MyDomain:property=1", ":property=2","MyDomain:property=1", "MyDomain:property=2");
363    }
364
365    /**
366     * Add notification listenter. ObjectName, ObjectName
367     */

368    public void testAddNLQualifiedQualifiedRegisterQualifiedUnqualified()
369    {
370       addNLNameName("MyDomain:property=1", "MyDomain:property=2","MyDomain:property=1", ":property=2");
371    }
372
373    // Support -----------------------------------------------------------------
374

375    /**
376     * Test Duplicate Register.
377     */

378    private void duplicateRegister(String register, String test)
379    {
380       MBeanServer server =null;
381       ObjectName registerName = null;
382       ObjectName testName = null;
383       try
384       {
385          server = MBeanServerFactory.createMBeanServer("MyDomain");
386          registerName = new ObjectName(register);
387          testName = new ObjectName(test);
388          server.registerMBean(new Test(), registerName);
389       }
390       catch (Exception e)
391       {
392          fail(e.toString());
393       }
394
395       boolean caught = false;
396       try
397       {
398          server.registerMBean(new Test(), testName);
399       }
400       catch (InstanceAlreadyExistsException e)
401       {
402          caught = true;
403       }
404       catch (Exception e)
405       {
406          fail(e.toString());
407       }
408
409       if (caught == false)
410          fail("Allows duplicate registration");
411
412       if (server != null)
413          MBeanServerFactory.releaseMBeanServer(server);
414    }
415
416    /**
417     * Test unregister
418     */

419    private void unregister(String register, String test)
420    {
421       MBeanServer server =null;
422       ObjectName registerName = null;
423       ObjectName testName = null;
424       try
425       {
426          server = MBeanServerFactory.createMBeanServer("MyDomain");
427          registerName = new ObjectName(register);
428          testName = new ObjectName(test);
429          server.registerMBean(new Test(), registerName);
430       }
431       catch (Exception e)
432       {
433          fail(e.toString());
434       }
435
436       try
437       {
438          server.unregisterMBean(testName);
439       }
440       catch (InstanceNotFoundException e)
441       {
442          fail("FAILS IN RI: unregisterMBean doesn't add the default domain");
443       }
444       catch (Exception e)
445       {
446          fail(e.toString());
447       }
448
449       if (server != null)
450          MBeanServerFactory.releaseMBeanServer(server);
451    }
452
453    /**
454     * Add notification listenter. ObjectName, Listener
455     */

456    private void addNLNameListener(String register, String test)
457    {
458       MBeanServer server =null;
459       ObjectName registerName = null;
460       ObjectName testName = null;
461       try
462       {
463          server = MBeanServerFactory.createMBeanServer("MyDomain");
464          registerName = new ObjectName(register);
465          testName = new ObjectName(test);
466          server.registerMBean(new Broadcaster(), registerName);
467       }
468       catch (Exception e)
469       {
470          fail(e.toString());
471       }
472
473       try
474       {
475          server.addNotificationListener(testName, new RelationService(true), null, null);
476       }
477       catch (Exception e)
478       {
479          fail(e.toString());
480       }
481
482       if (server != null)
483          MBeanServerFactory.releaseMBeanServer(server);
484    }
485
486    /**
487     * Add notification listenter. ObjectName, ObjectName
488     */

489    private void addNLNameName(String register1, String register2, String test1, String test2)
490    {
491       MBeanServer server =null;
492       ObjectName register1Name = null;
493       ObjectName register2Name = null;
494       ObjectName test1Name = null;
495       ObjectName test2Name = null;
496       try
497       {
498          server = MBeanServerFactory.createMBeanServer("MyDomain");
499          register1Name = new ObjectName(register1);
500          register2Name = new ObjectName(register2);
501          test1Name = new ObjectName(test1);
502          test2Name = new ObjectName(test2);
503          server.registerMBean(new Broadcaster(), register1Name);
504          server.registerMBean(new RelationService(true), register2Name);
505       }
506       catch (Exception e)
507       {
508          fail(e.toString());
509       }
510
511       try
512       {
513          server.addNotificationListener(test1Name, test2Name, null, null);
514       }
515       catch (Exception e)
516       {
517          fail(e.toString());
518       }
519
520       if (server != null)
521          MBeanServerFactory.releaseMBeanServer(server);
522    }
523
524    /**
525     * Test Invoke.
526     */

527    private void invoke(String register, String test)
528    {
529       MBeanServer server =null;
530       ObjectName registerName = null;
531       ObjectName testName = null;
532       try
533       {
534          server = MBeanServerFactory.createMBeanServer("MyDomain");
535          registerName = new ObjectName(register);
536          testName = new ObjectName(test);
537          server.registerMBean(new Broadcaster(), registerName);
538       }
539       catch (Exception e)
540       {
541          fail(e.toString());
542       }
543
544       try
545       {
546          server.invoke(testName, "doSomething", new Object[0], new String[0]);
547       }
548       catch (Exception e)
549       {
550          fail(e.toString());
551       }
552
553       if (server != null)
554          MBeanServerFactory.releaseMBeanServer(server);
555    }
556 }
557
Popular Tags