KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > jmx > compliance > query > QueryMBeansObjectNameOnlyTestCase


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.query;
23
24 import junit.framework.TestCase;
25
26
27 import java.util.Iterator JavaDoc;
28 import java.util.Set JavaDoc;
29 import java.util.HashSet JavaDoc;
30
31 import javax.management.MBeanServer JavaDoc;
32 import javax.management.MBeanServerFactory JavaDoc;
33 import javax.management.ObjectInstance JavaDoc;
34 import javax.management.ObjectName JavaDoc;
35
36 import org.jboss.test.jmx.compliance.query.support.Trivial;
37
38 /**
39  * Object Name Query tests.<p>
40  *
41  * TODO: More tests, more systematic?
42  *
43  * @author <a HREF="mailto:Adrian.Brock@HappeningTimes.com">Adrian Brock</a>.
44  */

45 public class QueryMBeansObjectNameOnlyTestCase
46   extends TestCase
47 {
48    // Attributes ----------------------------------------------------------------
49

50    /**
51     * The number of objects registered in a server
52     */

53    int implSize;
54
55    // Constructor ---------------------------------------------------------------
56

57    /**
58     * Construct the test
59     */

60    public QueryMBeansObjectNameOnlyTestCase(String JavaDoc s)
61    {
62       super(s);
63
64       // Determine the number of objects in the implementation
65
MBeanServer JavaDoc server = MBeanServerFactory.createMBeanServer();
66       implSize = server.getMBeanCount().intValue();
67       MBeanServerFactory.releaseMBeanServer(server);
68    }
69
70    // Tests ---------------------------------------------------------------------
71

72    /**
73     * Test single bean found.
74     */

75    public void testExactFound()
76    {
77       MBeanServer JavaDoc server = null;
78       HashSet JavaDoc instances = new HashSet JavaDoc();
79       Set JavaDoc resultMBeans = null;
80       Set JavaDoc resultNames = null;
81       try
82       {
83          server = MBeanServerFactory.createMBeanServer("QueryMBeans");
84          instances.add(server.registerMBean(new Trivial(),
85                        new ObjectName JavaDoc("Domain1:type=instance1")));
86          server.registerMBean(new Trivial(),
87                        new ObjectName JavaDoc("Domain1:type=instance2"));
88          server.registerMBean(new Trivial(),
89                        new ObjectName JavaDoc("Domain2:type=instance1"));
90          server.registerMBean(new Trivial(),
91                        new ObjectName JavaDoc("Domain3:type=instance1"));
92          resultMBeans = server.queryMBeans(new ObjectName JavaDoc("Domain1:type=instance1"), null);
93          resultNames = server.queryNames(new ObjectName JavaDoc("Domain1:type=instance1"), null);
94       }
95       catch (Exception JavaDoc e)
96       {
97          fail(e.toString());
98       }
99
100       checkit(resultMBeans, resultNames, instances, 0);
101
102       if (server != null)
103          MBeanServerFactory.releaseMBeanServer(server);
104    }
105
106    /**
107     * Test single bean not found.
108     */

109    public void testExactNotFound()
110    {
111       MBeanServer JavaDoc server = null;
112       HashSet JavaDoc instances = new HashSet JavaDoc();
113       Set JavaDoc resultMBeans = null;
114       Set JavaDoc resultNames = null;
115       try
116       {
117          server = MBeanServerFactory.createMBeanServer("QueryMBeans");
118          server.registerMBean(new Trivial(),
119                        new ObjectName JavaDoc("Domain1:type=instance1"));
120          server.registerMBean(new Trivial(),
121                        new ObjectName JavaDoc("Domain1:type=instance2"));
122          server.registerMBean(new Trivial(),
123                        new ObjectName JavaDoc("Domain2:type=instance1"));
124          server.registerMBean(new Trivial(),
125                        new ObjectName JavaDoc("Domain3:type=instance1"));
126          resultMBeans = server.queryMBeans(new ObjectName JavaDoc("Domain2:type=instance2"), null);
127          resultNames = server.queryNames(new ObjectName JavaDoc("Domain2:type=instance2"), null);
128       }
129       catch (Exception JavaDoc e)
130       {
131          fail(e.toString());
132       }
133
134       checkit(resultMBeans, resultNames, instances, 0);
135
136       if (server != null)
137          MBeanServerFactory.releaseMBeanServer(server);
138    }
139
140    /**
141     * Test Get All.
142     */

143    public void testGetAllMBeans()
144    {
145       MBeanServer JavaDoc server = null;
146       HashSet JavaDoc instances = new HashSet JavaDoc();
147       Set JavaDoc resultMBeans = null;
148       Set JavaDoc resultNames = null;
149       try
150       {
151          server = MBeanServerFactory.createMBeanServer("QueryMBeans");
152          instances.add(server.registerMBean(new Trivial(),
153                        new ObjectName JavaDoc("Domain1:type=instance1")));
154          instances.add(server.registerMBean(new Trivial(),
155                        new ObjectName JavaDoc("Domain1:type=instance2")));
156          instances.add(server.registerMBean(new Trivial(),
157                        new ObjectName JavaDoc("Domain2:type=instance1")));
158          instances.add(server.registerMBean(new Trivial(),
159                        new ObjectName JavaDoc("Domain3:type=instance1")));
160          resultMBeans = server.queryMBeans(new ObjectName JavaDoc("*:*"), null);
161          resultNames = server.queryNames(new ObjectName JavaDoc("*:*"), null);
162       }
163       catch (Exception JavaDoc e)
164       {
165          fail(e.toString());
166       }
167
168       checkit(resultMBeans, resultNames, instances, implSize);
169
170       if (server != null)
171          MBeanServerFactory.releaseMBeanServer(server);
172    }
173
174    /**
175     * Test Get All.
176     */

177    public void testGetAllMBeans2()
178    {
179       MBeanServer JavaDoc server = null;
180       HashSet JavaDoc instances = new HashSet JavaDoc();
181       Set JavaDoc resultMBeans = null;
182       Set JavaDoc resultNames = null;
183       try
184       {
185          server = MBeanServerFactory.createMBeanServer("QueryMBeans");
186          instances.add(server.registerMBean(new Trivial(),
187                        new ObjectName JavaDoc("Domain1:type=instance1")));
188          instances.add(server.registerMBean(new Trivial(),
189                        new ObjectName JavaDoc("Domain1:type=instance2")));
190          instances.add(server.registerMBean(new Trivial(),
191                        new ObjectName JavaDoc("Domain2:type=instance1")));
192          instances.add(server.registerMBean(new Trivial(),
193                        new ObjectName JavaDoc("Domain3:type=instance1")));
194          resultMBeans = server.queryMBeans(new ObjectName JavaDoc(""), null);
195          resultNames = server.queryNames(new ObjectName JavaDoc(""), null);
196       }
197       catch (Exception JavaDoc e)
198       {
199          fail(e.toString());
200       }
201
202       checkit(resultMBeans, resultNames, instances, implSize);
203
204       if (server != null)
205          MBeanServerFactory.releaseMBeanServer(server);
206    }
207
208    /**
209     * Test Get All.
210     */

211    public void testGetAllMBeans3()
212    {
213       MBeanServer JavaDoc server = null;
214       HashSet JavaDoc instances = new HashSet JavaDoc();
215       Set JavaDoc resultMBeans = null;
216       Set JavaDoc resultNames = null;
217       try
218       {
219          server = MBeanServerFactory.createMBeanServer("QueryMBeans");
220          instances.add(server.registerMBean(new Trivial(),
221                        new ObjectName JavaDoc("Domain1:type=instance1")));
222          instances.add(server.registerMBean(new Trivial(),
223                        new ObjectName JavaDoc("Domain1:type=instance2")));
224          instances.add(server.registerMBean(new Trivial(),
225                        new ObjectName JavaDoc("Domain2:type=instance1")));
226          instances.add(server.registerMBean(new Trivial(),
227                        new ObjectName JavaDoc("Domain3:type=instance1")));
228          resultMBeans = server.queryMBeans(null, null);
229          resultNames = server.queryNames(null, null);
230       }
231       catch (Exception JavaDoc e)
232       {
233          fail(e.toString());
234       }
235
236       checkit(resultMBeans, resultNames, instances, implSize);
237
238       if (server != null)
239          MBeanServerFactory.releaseMBeanServer(server);
240    }
241
242    /**
243     * Test one domain.
244     */

245    public void testGetOneDomain()
246    {
247       MBeanServer JavaDoc server = null;
248       HashSet JavaDoc instances = new HashSet JavaDoc();
249       Set JavaDoc resultMBeans = null;
250       Set JavaDoc resultNames = null;
251       try
252       {
253          server = MBeanServerFactory.createMBeanServer("QueryMBeans");
254          instances.add(server.registerMBean(new Trivial(),
255                        new ObjectName JavaDoc("Domain1:type=instance1")));
256          instances.add(server.registerMBean(new Trivial(),
257                        new ObjectName JavaDoc("Domain1:type=instance2")));
258          server.registerMBean(new Trivial(),
259                        new ObjectName JavaDoc("Domain2:type=instance1"));
260          server.registerMBean(new Trivial(),
261                        new ObjectName JavaDoc("Domain3:type=instance1"));
262          resultMBeans = server.queryMBeans(new ObjectName JavaDoc("Domain1:*"), null);
263          resultNames = server.queryNames(new ObjectName JavaDoc("Domain1:*"), null);
264       }
265       catch (Exception JavaDoc e)
266       {
267          fail(e.toString());
268       }
269
270       checkit(resultMBeans, resultNames, instances, 0);
271
272       if (server != null)
273          MBeanServerFactory.releaseMBeanServer(server);
274    }
275
276    /**
277     * Test default domain.
278     */

279    public void testGetDefaultDomain()
280    {
281       MBeanServer JavaDoc server = null;
282       HashSet JavaDoc instances = new HashSet JavaDoc();
283       Set JavaDoc resultMBeans = null;
284       Set JavaDoc resultNames = null;
285       try
286       {
287          server = MBeanServerFactory.createMBeanServer("Domain1");
288          instances.add(server.registerMBean(new Trivial(),
289                        new ObjectName JavaDoc("Domain1:type=instance1")));
290          instances.add(server.registerMBean(new Trivial(),
291                        new ObjectName JavaDoc("Domain1:type=instance2")));
292          server.registerMBean(new Trivial(),
293                        new ObjectName JavaDoc("Domain2:type=instance1"));
294          server.registerMBean(new Trivial(),
295                        new ObjectName JavaDoc("Domain3:type=instance1"));
296          resultMBeans = server.queryMBeans(new ObjectName JavaDoc(":*"), null);
297          resultNames = server.queryNames(new ObjectName JavaDoc(":*"), null);
298       }
299       catch (Exception JavaDoc e)
300       {
301          fail(e.toString());
302       }
303
304       checkit(resultMBeans, resultNames, instances, 0);
305
306       if (server != null)
307          MBeanServerFactory.releaseMBeanServer(server);
308    }
309
310    /**
311     * Test one property.
312     */

313    public void testGetOneProperty()
314    {
315       MBeanServer JavaDoc server = null;
316       HashSet JavaDoc instances = new HashSet JavaDoc();
317       Set JavaDoc resultMBeans = null;
318       Set JavaDoc resultNames = null;
319       try
320       {
321          server = MBeanServerFactory.createMBeanServer("QueryMBeans");
322          instances.add(server.registerMBean(new Trivial(),
323                        new ObjectName JavaDoc("Domain1:type=instance1")));
324          server.registerMBean(new Trivial(),
325                        new ObjectName JavaDoc("Domain1:type=instance2"));
326          instances.add(server.registerMBean(new Trivial(),
327                        new ObjectName JavaDoc("Domain2:type=instance1")));
328          instances.add(server.registerMBean(new Trivial(),
329                        new ObjectName JavaDoc("Domain3:type=instance1")));
330          resultMBeans = server.queryMBeans(new ObjectName JavaDoc("*:type=instance1"), null);
331          resultNames = server.queryNames(new ObjectName JavaDoc("*:type=instance1"), null);
332       }
333       catch (Exception JavaDoc e)
334       {
335          fail(e.toString());
336       }
337
338       checkit(resultMBeans, resultNames, instances, 0);
339
340       if (server != null)
341          MBeanServerFactory.releaseMBeanServer(server);
342    }
343
344    /**
345     * This one is from the spec.
346     */

347    public void testSpecAll()
348    {
349       MBeanServer JavaDoc server = null;
350       HashSet JavaDoc instances = new HashSet JavaDoc();
351       Set JavaDoc resultMBeans = null;
352       Set JavaDoc resultNames = null;
353       try
354       {
355          server = MBeanServerFactory.createMBeanServer();
356          instances.add(server.registerMBean(new Trivial(),
357                        new ObjectName JavaDoc("MyDomain:description=Printer,type=laser")));
358          instances.add(server.registerMBean(new Trivial(),
359                        new ObjectName JavaDoc("DefaultDomain:description=Printer,type=laser,date=1993")));
360          instances.add(server.registerMBean(new Trivial(),
361                        new ObjectName JavaDoc("Socrates:description=Printer,type=laser,date=1993")));
362          instances.add(server.registerMBean(new Trivial(),
363                        new ObjectName JavaDoc("DefaultDomain:description=Printer,type=ink")));
364          instances.add(server.registerMBean(new Trivial(),
365                        new ObjectName JavaDoc("MyDomain:description=Disk,capacity=2")));
366          instances.add(server.registerMBean(new Trivial(),
367                        new ObjectName JavaDoc("DefaultDomain:description=Disk,capacity=1")));
368          resultMBeans = server.queryMBeans(new ObjectName JavaDoc("*:*"), null);
369          resultNames = server.queryNames(new ObjectName JavaDoc("*:*"), null);
370       }
371       catch (Exception JavaDoc e)
372       {
373          fail(e.toString());
374       }
375
376       checkit(resultMBeans, resultNames, instances, implSize);
377
378       if (server != null)
379          MBeanServerFactory.releaseMBeanServer(server);
380    }
381
382    /**
383     * This one is from the spec.
384     */

385    public void testSpecDefault()
386    {
387       MBeanServer JavaDoc server = null;
388       HashSet JavaDoc instances = new HashSet JavaDoc();
389       Set JavaDoc resultMBeans = null;
390       Set JavaDoc resultNames = null;
391       try
392       {
393          server = MBeanServerFactory.createMBeanServer();
394          server.registerMBean(new Trivial(),
395                        new ObjectName JavaDoc("MyDomain:description=Printer,type=laser"));
396          instances.add(server.registerMBean(new Trivial(),
397                        new ObjectName JavaDoc("DefaultDomain:description=Printer,type=laser,date=1993")));
398          server.registerMBean(new Trivial(),
399                        new ObjectName JavaDoc("Socrates:description=Printer,type=laser,date=1993"));
400          instances.add(server.registerMBean(new Trivial(),
401                        new ObjectName JavaDoc("DefaultDomain:description=Printer,type=ink")));
402          server.registerMBean(new Trivial(),
403                        new ObjectName JavaDoc("MyDomain:description=Disk,capacity=2"));
404          instances.add(server.registerMBean(new Trivial(),
405                        new ObjectName JavaDoc("DefaultDomain:description=Disk,capacity=1")));
406          resultMBeans = server.queryMBeans(new ObjectName JavaDoc(":*"), null);
407          resultNames = server.queryNames(new ObjectName JavaDoc(":*"), null);
408       }
409       catch (Exception JavaDoc e)
410       {
411          fail(e.toString());
412       }
413
414       checkit(resultMBeans, resultNames, instances, 0);
415
416       if (server != null)
417          MBeanServerFactory.releaseMBeanServer(server);
418    }
419
420    /**
421     * This one is from the spec.
422     */

423    public void testSpecMyDomain()
424    {
425       MBeanServer JavaDoc server = null;
426       HashSet JavaDoc instances = new HashSet JavaDoc();
427       Set JavaDoc resultMBeans = null;
428       Set JavaDoc resultNames = null;
429       try
430       {
431          server = MBeanServerFactory.createMBeanServer();
432          instances.add(server.registerMBean(new Trivial(),
433                        new ObjectName JavaDoc("MyDomain:description=Printer,type=laser")));
434          server.registerMBean(new Trivial(),
435                        new ObjectName JavaDoc("DefaultDomain:description=Printer,type=laser,date=1993"));
436          server.registerMBean(new Trivial(),
437                        new ObjectName JavaDoc("Socrates:description=Printer,type=laser,date=1993"));
438          server.registerMBean(new Trivial(),
439                        new ObjectName JavaDoc("DefaultDomain:description=Printer,type=ink"));
440          instances.add(server.registerMBean(new Trivial(),
441                        new ObjectName JavaDoc("MyDomain:description=Disk,capacity=2")));
442          server.registerMBean(new Trivial(),
443                        new ObjectName JavaDoc("DefaultDomain:description=Disk,capacity=1"));
444          resultMBeans = server.queryMBeans(new ObjectName JavaDoc("MyDomain:*"), null);
445          resultNames = server.queryNames(new ObjectName JavaDoc("MyDomain:*"), null);
446       }
447       catch (Exception JavaDoc e)
448       {
449          fail(e.toString());
450       }
451
452       checkit(resultMBeans, resultNames, instances, 0);
453
454       if (server != null)
455          MBeanServerFactory.releaseMBeanServer(server);
456    }
457
458    /**
459     * This one is from the spec.
460     */

461    public void testSpecAnyAnyDomain()
462    {
463       MBeanServer JavaDoc server = null;
464       HashSet JavaDoc instances = new HashSet JavaDoc();
465       Set JavaDoc resultMBeans = null;
466       Set JavaDoc resultNames = null;
467       try
468       {
469          server = MBeanServerFactory.createMBeanServer();
470          instances.add(server.registerMBean(new Trivial(),
471                        new ObjectName JavaDoc("MyDomain:description=Printer,type=laser")));
472          server.registerMBean(new Trivial(),
473                        new ObjectName JavaDoc("DefaultDomain:description=Printer,type=laser,date=1993"));
474          server.registerMBean(new Trivial(),
475                        new ObjectName JavaDoc("Socrates:description=Printer,type=laser,date=1993"));
476          server.registerMBean(new Trivial(),
477                        new ObjectName JavaDoc("DefaultDomain:description=Printer,type=ink"));
478          instances.add(server.registerMBean(new Trivial(),
479                        new ObjectName JavaDoc("MyDomain:description=Disk,capacity=2")));
480          server.registerMBean(new Trivial(),
481                        new ObjectName JavaDoc("DefaultDomain:description=Disk,capacity=1"));
482          resultMBeans = server.queryMBeans(new ObjectName JavaDoc("??Domain:*"), null);
483          resultNames = server.queryNames(new ObjectName JavaDoc("??Domain:*"), null);
484       }
485       catch (Exception JavaDoc e)
486       {
487          fail(e.toString());
488       }
489
490       checkit(resultMBeans, resultNames, instances, 0);
491
492       if (server != null)
493          MBeanServerFactory.releaseMBeanServer(server);
494    }
495
496    /**
497     * This one is from the spec.
498     */

499    public void testAsteriskDomAsterisk()
500    {
501       MBeanServer JavaDoc server = null;
502       HashSet JavaDoc instances = new HashSet JavaDoc();
503       Set JavaDoc resultMBeans = null;
504       Set JavaDoc resultNames = null;
505       try
506       {
507          server = MBeanServerFactory.createMBeanServer();
508          instances.add(server.registerMBean(new Trivial(),
509                        new ObjectName JavaDoc("MyDomain:description=Printer,type=laser")));
510          instances.add(server.registerMBean(new Trivial(),
511                        new ObjectName JavaDoc("DefaultDomain:description=Printer,type=laser,date=1993")));
512          server.registerMBean(new Trivial(),
513                        new ObjectName JavaDoc("Socrates:description=Printer,type=laser,date=1993"));
514          instances.add(server.registerMBean(new Trivial(),
515                        new ObjectName JavaDoc("DefaultDomain:description=Printer,type=ink")));
516          instances.add(server.registerMBean(new Trivial(),
517                        new ObjectName JavaDoc("MyDomain:description=Disk,capacity=2")));
518          instances.add(server.registerMBean(new Trivial(),
519                        new ObjectName JavaDoc("DefaultDomain:description=Disk,capacity=1")));
520          resultMBeans = server.queryMBeans(new ObjectName JavaDoc("*Dom*:*"), null);
521          resultNames = server.queryNames(new ObjectName JavaDoc("*Dom*:*"), null);
522       }
523       catch (Exception JavaDoc e)
524       {
525          fail(e.toString());
526       }
527
528       checkit(resultMBeans, resultNames, instances, 0);
529
530       if (server != null)
531          MBeanServerFactory.releaseMBeanServer(server);
532    }
533
534    /**
535     * This one is from the spec.
536     */

537    public void testSpecLaserPrinters()
538    {
539       MBeanServer JavaDoc server = null;
540       HashSet JavaDoc instances = new HashSet JavaDoc();
541       Set JavaDoc resultMBeans = null;
542       Set JavaDoc resultNames = null;
543       try
544       {
545          server = MBeanServerFactory.createMBeanServer();
546          instances.add(server.registerMBean(new Trivial(),
547                        new ObjectName JavaDoc("MyDomain:description=Printer,type=laser")));
548          instances.add(server.registerMBean(new Trivial(),
549                        new ObjectName JavaDoc("DefaultDomain:description=Printer,type=laser,date=1993")));
550          instances.add(server.registerMBean(new Trivial(),
551                        new ObjectName JavaDoc("Socrates:description=Printer,type=laser,date=1993")));
552          server.registerMBean(new Trivial(),
553                        new ObjectName JavaDoc("DefaultDomain:description=Printer,type=ink"));
554          server.registerMBean(new Trivial(),
555                        new ObjectName JavaDoc("MyDomain:description=Disk,capacity=2"));
556          server.registerMBean(new Trivial(),
557                        new ObjectName JavaDoc("DefaultDomain:description=Disk,capacity=1"));
558          resultMBeans = server.queryMBeans(new ObjectName JavaDoc("*:description=Printer,type=laser,*"), null);
559          resultNames = server.queryNames(new ObjectName JavaDoc("*:description=Printer,type=laser,*"), null);
560       }
561       catch (Exception JavaDoc e)
562       {
563          fail(e.toString());
564       }
565
566       checkit(resultMBeans, resultNames, instances, 0);
567
568       if (server != null)
569          MBeanServerFactory.releaseMBeanServer(server);
570    }
571
572    /**
573     * This one is from the spec.
574     */

575    public void testSpecPrinters()
576    {
577       MBeanServer JavaDoc server = null;
578       HashSet JavaDoc instances = new HashSet JavaDoc();
579       Set JavaDoc resultMBeans = null;
580       Set JavaDoc resultNames = null;
581       try
582       {
583          server = MBeanServerFactory.createMBeanServer();
584          instances.add(server.registerMBean(new Trivial(),
585                        new ObjectName JavaDoc("MyDomain:description=Printer,type=laser")));
586          instances.add(server.registerMBean(new Trivial(),
587                        new ObjectName JavaDoc("DefaultDomain:description=Printer,type=laser,date=1993")));
588          instances.add(server.registerMBean(new Trivial(),
589                        new ObjectName JavaDoc("Socrates:description=Printer,type=laser,date=1993")));
590          instances.add(server.registerMBean(new Trivial(),
591                        new ObjectName JavaDoc("DefaultDomain:description=Printer,type=ink")));
592          server.registerMBean(new Trivial(),
593                        new ObjectName JavaDoc("MyDomain:description=Disk,capacity=2"));
594          server.registerMBean(new Trivial(),
595                        new ObjectName JavaDoc("DefaultDomain:description=Disk,capacity=2"));
596          resultMBeans = server.queryMBeans(new ObjectName JavaDoc("*:description=Printer,*"), null);
597          resultNames = server.queryNames(new ObjectName JavaDoc("*:description=Printer,*"), null);
598       }
599       catch (Exception JavaDoc e)
600       {
601          fail(e.toString());
602       }
603
604       checkit(resultMBeans, resultNames, instances, 0);
605
606       if (server != null)
607          MBeanServerFactory.releaseMBeanServer(server);
608    }
609
610    /**
611     * Test suffix asterisk on domain.
612     */

613    public void testSuffixMatchManyDomain()
614    {
615       MBeanServer JavaDoc server = null;
616       HashSet JavaDoc instances = new HashSet JavaDoc();
617       Set JavaDoc resultMBeans = null;
618       Set JavaDoc resultNames = null;
619       try
620       {
621          server = MBeanServerFactory.createMBeanServer("QueryMBeans");
622          instances.add(server.registerMBean(new Trivial(),
623                        new ObjectName JavaDoc("DomainA123:type=instance1")));
624          instances.add(server.registerMBean(new Trivial(),
625                        new ObjectName JavaDoc("DomainA321:type=instance2")));
626          instances.add(server.registerMBean(new Trivial(),
627                        new ObjectName JavaDoc("DomainA2224:type=instance1")));
628          instances.add(server.registerMBean(new Trivial(),
629                        new ObjectName JavaDoc("DomainA3:type=instance1")));
630          instances.add(server.registerMBean(new Trivial(),
631                        new ObjectName JavaDoc("DomainA:type=instance1")));
632          server.registerMBean(new Trivial(),
633                        new ObjectName JavaDoc("Domain:type=instance1"));
634          server.registerMBean(new Trivial(),
635                        new ObjectName JavaDoc("Dom:type=instance1"));
636          resultMBeans = server.queryMBeans(new ObjectName JavaDoc("DomainA*:*"), null);
637          resultNames = server.queryNames(new ObjectName JavaDoc("DomainA*:*"), null);
638       }
639       catch (Exception JavaDoc e)
640       {
641          fail(e.toString());
642       }
643
644       checkit(resultMBeans, resultNames, instances, 0);
645
646       if (server != null)
647          MBeanServerFactory.releaseMBeanServer(server);
648    }
649
650    /**
651     * Test prefix asterisk on domain.
652     */

653    public void testPrefixMatchManyDomain()
654    {
655       MBeanServer JavaDoc server = null;
656       HashSet JavaDoc instances = new HashSet JavaDoc();
657       Set JavaDoc resultMBeans = null;
658       Set JavaDoc resultNames = null;
659       try
660       {
661          server = MBeanServerFactory.createMBeanServer("QueryMBeans");
662          instances.add(server.registerMBean(new Trivial(),
663                        new ObjectName JavaDoc("123ADomain:type=instance1")));
664          instances.add(server.registerMBean(new Trivial(),
665                        new ObjectName JavaDoc("321ADomain:type=instance2")));
666          instances.add(server.registerMBean(new Trivial(),
667                        new ObjectName JavaDoc("2224ADomain:type=instance1")));
668          instances.add(server.registerMBean(new Trivial(),
669                        new ObjectName JavaDoc("3ADomain:type=instance1")));
670          instances.add(server.registerMBean(new Trivial(),
671                        new ObjectName JavaDoc("3ADomai123ADomain:type=instance1")));
672          instances.add(server.registerMBean(new Trivial(),
673                        new ObjectName JavaDoc("ADomain:type=instance1")));
674          server.registerMBean(new Trivial(),
675                        new ObjectName JavaDoc("Domain:type=instance1"));
676          server.registerMBean(new Trivial(),
677                        new ObjectName JavaDoc("ADomai:type=instance1"));
678          server.registerMBean(new Trivial(),
679                        new ObjectName JavaDoc("main:type=instance1"));
680          resultMBeans = server.queryMBeans(new ObjectName JavaDoc("*ADomain:*"), null);
681          resultNames = server.queryNames(new ObjectName JavaDoc("*ADomain:*"), null);
682       }
683       catch (Exception JavaDoc e)
684       {
685          fail(e.toString());
686       }
687
688       checkit(resultMBeans, resultNames, instances, 0);
689
690       if (server != null)
691          MBeanServerFactory.releaseMBeanServer(server);
692    }
693
694    // Support ----------------------------------------------------------------
695

696    /**
697     * Check the resultMBeans
698     */

699    private void checkit(Set JavaDoc resultMBeans, Set JavaDoc resultNames, HashSet JavaDoc expected,
700                         int others)
701    {
702       // Quick tests
703
assertEquals(expected.size() + others, resultMBeans.size());
704       assertEquals(expected.size() + others, resultNames.size());
705
706       // Get the expected ObjectNames
707
HashSet JavaDoc expectedNames = new HashSet JavaDoc();
708       Iterator JavaDoc iterator = expected.iterator();
709       while (iterator.hasNext())
710       {
711          expectedNames.add(((ObjectInstance JavaDoc) iterator.next()).getObjectName());
712       }
713
714       // Check resultMBeans
715
iterator = resultMBeans.iterator();
716       while (iterator.hasNext())
717       {
718          ObjectInstance JavaDoc instance = (ObjectInstance JavaDoc) iterator.next();
719          Iterator JavaDoc iterator2 = expected.iterator();
720          boolean found = false;
721          while (iterator2.hasNext())
722          {
723             if (iterator2.next().equals(instance))
724             {
725                iterator2.remove();
726                found = true;
727                break;
728             }
729          }
730         if (found == false &&
731             instance.getObjectName().getDomain().equals("JMImplementation") == false)
732            fail("Unexpected instance " + instance.getObjectName());
733       }
734
735       // Check resultNames
736
iterator = resultNames.iterator();
737       while (iterator.hasNext())
738       {
739          ObjectName JavaDoc name = (ObjectName JavaDoc) iterator.next();
740          Iterator JavaDoc iterator2 = expectedNames.iterator();
741          boolean found = false;
742          while (iterator2.hasNext())
743          {
744             if (iterator2.next().equals(name))
745             {
746                iterator2.remove();
747                found = true;
748                break;
749             }
750          }
751         if (found == false &&
752             name.getDomain().equals("JMImplementation") == false)
753            fail("Unexpected name " + name);
754       }
755    }
756 }
757
Popular Tags