KickJava   Java API By Example, From Geeks To Geeks.

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


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 javax.management.InstanceAlreadyExistsException JavaDoc;
25 import javax.management.InstanceNotFoundException JavaDoc;
26 import javax.management.MBeanServer JavaDoc;
27 import javax.management.MBeanServerFactory JavaDoc;
28 import javax.management.ObjectName JavaDoc;
29 import javax.management.relation.RelationService JavaDoc;
30
31 import junit.framework.TestCase;
32
33 import org.jboss.test.jmx.compliance.server.support.Broadcaster;
34 import org.jboss.test.jmx.compliance.server.support.Test;
35
36 /**
37  * Tests default domain handling in the Server.<p>
38  *
39  * TODO createMBean x4.
40  * TODO deserialize x2
41  * TODO getAttribute x2
42  * TODO getMBeanInfo
43  * TODO instantiate x2
44  * TODO isInstanceOf
45  * TODO isRegistered
46  * TODO removeNotificationListener x2
47  * TODO setAttribute x2
48  *
49  * @author <a HREF="mailto:Adrian.Brock@HappeningTimes.com">Adrian Brock</a>.
50  */

51 public class DefaultDomainTestCase
52   extends TestCase
53 {
54    // Attributes ----------------------------------------------------------------
55

56    // Constructor ---------------------------------------------------------------
57

58    /**
59     * Construct the test
60     */

61    public DefaultDomainTestCase(String JavaDoc s)
62    {
63       super(s);
64    }
65
66    // Tests ---------------------------------------------------------------------
67

68    /**
69     * Test Register in the "anonymous" default domain.
70     */

71    public void testDefaultDomainRegisterUnqualified()
72    {
73       MBeanServer JavaDoc server =null;
74       ObjectName JavaDoc unqualifiedName = null;
75       ObjectName JavaDoc qualifiedName = null;
76       try
77       {
78          server = MBeanServerFactory.createMBeanServer();
79          unqualifiedName = new ObjectName JavaDoc(":property=1");
80          qualifiedName = new ObjectName JavaDoc("DefaultDomain:property=1");
81          server.registerMBean(new Test(), unqualifiedName);
82       }
83       catch (Exception JavaDoc e)
84       {
85          fail(e.toString());
86       }
87
88       assertEquals("DefaultDomain", server.getDefaultDomain());
89
90       try
91       {
92          server.getObjectInstance(unqualifiedName);
93       }
94       catch (InstanceNotFoundException JavaDoc e)
95       {
96          fail("Unqualified lookup failed");
97       }
98
99       try
100       {
101          server.getObjectInstance(qualifiedName);
102       }
103       catch (InstanceNotFoundException JavaDoc e)
104       {
105          fail("Qualified lookup failed");
106       }
107
108       if (server != null)
109          MBeanServerFactory.releaseMBeanServer(server);
110    }
111
112    /**
113     * Test Register in the "anonymous" default domain.
114     */

115    public void testDefaultDomainRegisterQualified()
116    {
117       MBeanServer JavaDoc server =null;
118       ObjectName JavaDoc unqualifiedName = null;
119       ObjectName JavaDoc qualifiedName = null;
120       try
121       {
122          server = MBeanServerFactory.createMBeanServer();
123          unqualifiedName = new ObjectName JavaDoc(":property=1");
124          qualifiedName = new ObjectName JavaDoc("DefaultDomain:property=1");
125          server.registerMBean(new Test(), qualifiedName);
126       }
127       catch (Exception JavaDoc e)
128       {
129          fail(e.toString());
130       }
131
132       assertEquals("DefaultDomain", server.getDefaultDomain());
133
134       try
135       {
136          server.getObjectInstance(unqualifiedName);
137       }
138       catch (InstanceNotFoundException JavaDoc e)
139       {
140          fail("Unqualified lookup failed");
141       }
142
143       try
144       {
145          server.getObjectInstance(qualifiedName);
146       }
147       catch (InstanceNotFoundException JavaDoc e)
148       {
149          fail("Qualified lookup failed");
150       }
151
152       if (server != null)
153          MBeanServerFactory.releaseMBeanServer(server);
154    }
155
156    /**
157     * Test Register in a named default domain.
158     */

159    public void testMyDefaultDomainRegisterUnqualified()
160    {
161       MBeanServer JavaDoc server =null;
162       ObjectName JavaDoc unqualifiedName = null;
163       ObjectName JavaDoc qualifiedName = null;
164       try
165       {
166          server = MBeanServerFactory.createMBeanServer("MyDomain");
167          unqualifiedName = new ObjectName JavaDoc(":property=1");
168          qualifiedName = new ObjectName JavaDoc("MyDomain:property=1");
169          server.registerMBean(new Test(), unqualifiedName);
170       }
171       catch (Exception JavaDoc e)
172       {
173          fail(e.toString());
174       }
175
176       assertEquals("MyDomain", server.getDefaultDomain());
177
178       try
179       {
180          server.getObjectInstance(unqualifiedName);
181       }
182       catch (InstanceNotFoundException JavaDoc e)
183       {
184          fail("Unqualified lookup failed");
185       }
186
187       try
188       {
189          server.getObjectInstance(qualifiedName);
190       }
191       catch (InstanceNotFoundException JavaDoc e)
192       {
193          fail("Qualified lookup failed");
194       }
195
196       if (server != null)
197          MBeanServerFactory.releaseMBeanServer(server);
198    }
199
200    /**
201     * Test Register in a named default domain.
202     */

203    public void testMyDefaultDomainRegisterQualified()
204    {
205       MBeanServer JavaDoc server =null;
206       ObjectName JavaDoc unqualifiedName = null;
207       ObjectName JavaDoc qualifiedName = null;
208       try
209       {
210          server = MBeanServerFactory.createMBeanServer("MyDomain");
211          unqualifiedName = new ObjectName JavaDoc(":property=1");
212          qualifiedName = new ObjectName JavaDoc("MyDomain:property=1");
213          server.registerMBean(new Test(), qualifiedName);
214       }
215       catch (Exception JavaDoc e)
216       {
217          fail(e.toString());
218       }
219
220       assertEquals("MyDomain", server.getDefaultDomain());
221
222       try
223       {
224          server.getObjectInstance(unqualifiedName);
225       }
226       catch (InstanceNotFoundException JavaDoc e)
227       {
228          fail("Unqualified lookup failed");
229       }
230
231       try
232       {
233          server.getObjectInstance(qualifiedName);
234       }
235       catch (InstanceNotFoundException JavaDoc e)
236       {
237          fail("Qualified lookup failed");
238       }
239
240       if (server != null)
241          MBeanServerFactory.releaseMBeanServer(server);
242    }
243
244    /**
245     * Test register qualified/unqualifed.
246     */

247    public void testRegisterQualifiedUnqualified()
248    {
249       duplicateRegister(":property=1", "MyDomain:property=1");
250    }
251
252    /**
253     * Test register unqualified/qualifed.
254     */

255    public void testRegisterUnqualifiedQualified()
256    {
257       duplicateRegister("MyDomain:property=1", ":property=1");
258    }
259
260    /**
261     * Test register unqualified/unqualified.
262     */

263    public void testRegisterUnqualifiedUnqualified()
264    {
265       duplicateRegister(":property=1", ":property=1");
266    }
267
268    /**
269     * Test invoke qualified/unqualifed.
270     */

271    public void testInvokeQualifiedUnqualified()
272    {
273       invoke(":property=1", "MyDomain:property=1");
274    }
275
276    /**
277     * Test invoke unqualified/qualifed.
278     */

279    public void testInvokeUnqualifiedQualified()
280    {
281       invoke("MyDomain:property=1", ":property=1");
282    }
283
284    /**
285     * Test invoke unqualified/unqualified.
286     */

287    public void testInvokeUnqualifiedUnqualified()
288    {
289       invoke(":property=1", ":property=1");
290    }
291
292    /**
293     * Test register qualified unregister unqualified.
294     */

295    public void testRegisterQualifiedUnregisterUnqualified()
296    {
297       unregister("MyDomain:property=1", ":property=1");
298    }
299
300    /**
301     * Test register unqualified unregister qualifed.
302     */

303    public void testRegisterUnQualifiedUnregisterQualified()
304    {
305       unregister(":property=1", "MyDomain:property=1");
306    }
307
308    /**
309     * Test register unqualified unregister unqualified.
310     */

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

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

327    public void testAddNLQualifiedNameListenerRegisterUnqualified()
328    {
329       addNLNameListener(":property=1", "MyDomain:property=1");
330    }
331
332    /**
333     * Add notification listenter. ObjectName, Listener
334     */

335    public void testAddNLUnqualifiedNameListenerRegisterUnqualified()
336    {
337       addNLNameListener(":property=1", ":property=1");
338    }
339
340    /**
341     * Add notification listenter. ObjectName, ObjectName
342     */

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

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

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

367    public void testAddNLQualifiedUnqualifiedRegisterQualifiedUnqualified()
368    {
369       addNLNameName("MyDomain:property=1", ":property=2","MyDomain:property=1", ":property=2");
370    }
371
372    /**
373     * Add notification listenter. ObjectName, ObjectName
374     */

375    public void testAddNLQualifiedUnqualifiedRegisterQualifiedQualified()
376    {
377       addNLNameName("MyDomain:property=1", ":property=2","MyDomain:property=1", "MyDomain:property=2");
378    }
379
380    /**
381     * Add notification listenter. ObjectName, ObjectName
382     */

383    public void testAddNLQualifiedQualifiedRegisterQualifiedUnqualified()
384    {
385       addNLNameName("MyDomain:property=1", "MyDomain:property=2","MyDomain:property=1", ":property=2");
386    }
387
388    // Support -----------------------------------------------------------------
389

390    /**
391     * Test Duplicate Register.
392     */

393    private void duplicateRegister(String JavaDoc register, String JavaDoc test)
394    {
395       MBeanServer JavaDoc server =null;
396       ObjectName JavaDoc registerName = null;
397       ObjectName JavaDoc testName = null;
398       try
399       {
400          server = MBeanServerFactory.createMBeanServer("MyDomain");
401          registerName = new ObjectName JavaDoc(register);
402          testName = new ObjectName JavaDoc(test);
403          server.registerMBean(new Test(), registerName);
404       }
405       catch (Exception JavaDoc e)
406       {
407          fail(e.toString());
408       }
409
410       boolean caught = false;
411       try
412       {
413          server.registerMBean(new Test(), testName);
414       }
415       catch (InstanceAlreadyExistsException JavaDoc e)
416       {
417          caught = true;
418       }
419       catch (Exception JavaDoc e)
420       {
421          fail(e.toString());
422       }
423
424       if (caught == false)
425          fail("Allows duplicate registration");
426
427       if (server != null)
428          MBeanServerFactory.releaseMBeanServer(server);
429    }
430
431    /**
432     * Test unregister
433     */

434    private void unregister(String JavaDoc register, String JavaDoc test)
435    {
436       MBeanServer JavaDoc server =null;
437       ObjectName JavaDoc registerName = null;
438       ObjectName JavaDoc testName = null;
439       try
440       {
441          server = MBeanServerFactory.createMBeanServer("MyDomain");
442          registerName = new ObjectName JavaDoc(register);
443          testName = new ObjectName JavaDoc(test);
444          server.registerMBean(new Test(), registerName);
445       }
446       catch (Exception JavaDoc e)
447       {
448          fail(e.toString());
449       }
450
451       try
452       {
453          server.unregisterMBean(testName);
454       }
455       catch (InstanceNotFoundException JavaDoc e)
456       {
457          fail("FAILS IN RI: unregisterMBean doesn't add the default domain");
458       }
459       catch (Exception JavaDoc e)
460       {
461          fail(e.toString());
462       }
463
464       if (server != null)
465          MBeanServerFactory.releaseMBeanServer(server);
466    }
467
468    /**
469     * Add notification listenter. ObjectName, Listener
470     */

471    private void addNLNameListener(String JavaDoc register, String JavaDoc test)
472    {
473       MBeanServer JavaDoc server =null;
474       ObjectName JavaDoc registerName = null;
475       ObjectName JavaDoc testName = null;
476       try
477       {
478          server = MBeanServerFactory.createMBeanServer("MyDomain");
479          registerName = new ObjectName JavaDoc(register);
480          testName = new ObjectName JavaDoc(test);
481          server.registerMBean(new Broadcaster(), registerName);
482       }
483       catch (Exception JavaDoc e)
484       {
485          fail(e.toString());
486       }
487
488       try
489       {
490          server.addNotificationListener(testName, new RelationService JavaDoc(true), null, null);
491       }
492       catch (Exception JavaDoc e)
493       {
494          fail(e.toString());
495       }
496
497       if (server != null)
498          MBeanServerFactory.releaseMBeanServer(server);
499    }
500
501    /**
502     * Add notification listenter. ObjectName, ObjectName
503     */

504    private void addNLNameName(String JavaDoc register1, String JavaDoc register2, String JavaDoc test1, String JavaDoc test2)
505    {
506       MBeanServer JavaDoc server =null;
507       ObjectName JavaDoc register1Name = null;
508       ObjectName JavaDoc register2Name = null;
509       ObjectName JavaDoc test1Name = null;
510       ObjectName JavaDoc test2Name = null;
511       try
512       {
513          server = MBeanServerFactory.createMBeanServer("MyDomain");
514          register1Name = new ObjectName JavaDoc(register1);
515          register2Name = new ObjectName JavaDoc(register2);
516          test1Name = new ObjectName JavaDoc(test1);
517          test2Name = new ObjectName JavaDoc(test2);
518          server.registerMBean(new Broadcaster(), register1Name);
519          server.registerMBean(new RelationService JavaDoc(true), register2Name);
520       }
521       catch (Exception JavaDoc e)
522       {
523          fail(e.toString());
524       }
525
526       try
527       {
528          server.addNotificationListener(test1Name, test2Name, null, null);
529       }
530       catch (Exception JavaDoc e)
531       {
532          fail(e.toString());
533       }
534
535       if (server != null)
536          MBeanServerFactory.releaseMBeanServer(server);
537    }
538
539    /**
540     * Test Invoke.
541     */

542    private void invoke(String JavaDoc register, String JavaDoc test)
543    {
544       MBeanServer JavaDoc server =null;
545       ObjectName JavaDoc registerName = null;
546       ObjectName JavaDoc testName = null;
547       try
548       {
549          server = MBeanServerFactory.createMBeanServer("MyDomain");
550          registerName = new ObjectName JavaDoc(register);
551          testName = new ObjectName JavaDoc(test);
552          server.registerMBean(new Broadcaster(), registerName);
553       }
554       catch (Exception JavaDoc e)
555       {
556          fail(e.toString());
557       }
558
559       try
560       {
561          server.invoke(testName, "doSomething", new Object JavaDoc[0], new String JavaDoc[0]);
562       }
563       catch (Exception JavaDoc e)
564       {
565          fail(e.toString());
566       }
567
568       if (server != null)
569          MBeanServerFactory.releaseMBeanServer(server);
570    }
571 }
572
Popular Tags