KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > compliance > query > QueryTestCase


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.query;
9
10 import java.util.HashSet;
11 import java.util.Iterator;
12 import java.util.Set;
13
14 import javax.management.AttributeValueExp;
15 import javax.management.MBeanServer;
16 import javax.management.MBeanServerFactory;
17 import javax.management.ObjectInstance;
18 import javax.management.ObjectName;
19 import javax.management.Query;
20 import javax.management.QueryExp;
21 import javax.management.ValueExp;
22
23 import junit.framework.AssertionFailedError;
24 import junit.framework.TestCase;
25 import test.compliance.query.support.BooleanTest;
26 import test.compliance.query.support.NumberTest;
27 import test.compliance.query.support.StringTest;
28 import test.compliance.query.support.Trivial;
29
30 /**
31  * Query unit tests
32  *
33  * @author <a HREF="mailto:Adrian.Brock@HappeningTimes.com">Adrian Brock</a>.
34  */

35 public class QueryTestCase
36   extends TestCase
37 {
38    // Attributes ----------------------------------------------------------------
39

40    // Constructor ---------------------------------------------------------------
41

42    /**
43     * Construct the test
44     */

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

52    /**
53     * Test a boolean
54     */

55    public void testBoolean() throws Exception
56    {
57       ValueExp one = Query.value(true);
58       ValueExp two = Query.value(false);
59       equalsTEST(one, two);
60       attrTEST(new BooleanTest(true), Query.attr("Boolean"), one, two);
61       attrTEST(new BooleanTest(true), Query.attr(BooleanTest.class.getName(), "Boolean"), one, two);
62       try
63       {
64          // Test in first
65
new QueryTEST(
66             new MBean[]
67             {
68                new MBean(new Trivial(), "Domain1:type=instance1")
69             },
70             new MBean[0],
71             Query.in
72             (
73                one,
74                new ValueExp[]
75                {
76                   one, two, two
77                }
78             )
79          ).test();
80          // Test in last
81
new QueryTEST(
82             new MBean[]
83             {
84                new MBean(new Trivial(), "Domain1:type=instance1")
85             },
86             new MBean[0],
87             Query.in
88             (
89                one,
90                new ValueExp[]
91                {
92                   two, two, one
93                }
94             )
95          ).test();
96          // Test in not the first or last
97
new QueryTEST(
98             new MBean[]
99             {
100                new MBean(new Trivial(), "Domain1:type=instance1")
101             },
102             new MBean[0],
103             Query.in
104             (
105                one,
106                new ValueExp[]
107                {
108                   two, one, two
109                }
110             )
111          ).test();
112          // Test not in
113
new QueryTEST(
114             new MBean[0],
115             new MBean[]
116             {
117                new MBean(new Trivial(), "Domain1:type=instance1")
118             },
119             Query.in
120             (
121                one,
122                new ValueExp[]
123                {
124                   two, two, two
125                }
126             )
127          ).test();
128       }
129       catch (AssertionFailedError e)
130       {
131          fail("FAILS IN RI: Query.in boolean");
132       }
133    }
134
135    /**
136     * Test a double
137     */

138    public void testDouble() throws Exception
139    {
140       ValueExp one = Query.value(10d);
141       ValueExp two = Query.value(20d);
142       ValueExp div = Query.value(2d);
143       ValueExp minus = Query.value(-10d);
144       ValueExp mult = Query.value(200d);
145       ValueExp plus = Query.value(30d);
146       equalsTEST(one, two);
147       operationTEST(one, two, div, minus, mult, plus);
148       comparisonTEST(one, two);
149       betweenTEST(one, two, plus);
150       attrTEST(new NumberTest(10d), Query.attr("Number"), one, two);
151       attrTEST(new NumberTest(10d), Query.attr(NumberTest.class.getName(), "Number"), one, two);
152       inTEST(one, two, div, minus, mult, plus);
153    }
154
155    /**
156     * Test a Double
157     */

158    public void testDoubleObject() throws Exception
159    {
160       ValueExp one = Query.value(new Double(10d));
161       ValueExp two = Query.value(new Double(20d));
162       ValueExp div = Query.value(new Double(2d));
163       ValueExp minus = Query.value(new Double(-10d));
164       ValueExp mult = Query.value(new Double(200d));
165       ValueExp plus = Query.value(new Double(30d));
166       equalsTEST(one, two);
167       operationTEST(one, two, div, minus, mult, plus);
168       comparisonTEST(one, two);
169       betweenTEST(one, two, plus);
170       attrTEST(new NumberTest(new Double(10d)), Query.attr("Number"), one, two);
171       attrTEST(new NumberTest(new Double(10d)), Query.attr(NumberTest.class.getName(), "Number"), one, two);
172       inTEST(one, two, div, minus, mult, plus);
173    }
174
175    /**
176     * Test a float
177     */

178    public void testFloat() throws Exception
179    {
180       ValueExp one = Query.value(10f);
181       ValueExp two = Query.value(20f);
182       ValueExp div = Query.value(2f);
183       ValueExp minus = Query.value(-10f);
184       ValueExp mult = Query.value(200f);
185       ValueExp plus = Query.value(30f);
186       equalsTEST(one, two);
187       operationTEST(one, two, div, minus, mult, plus);
188       comparisonTEST(one, two);
189       betweenTEST(one, two, plus);
190       attrTEST(new NumberTest(10f), Query.attr("Number"), one, two);
191       attrTEST(new NumberTest(10f), Query.attr(NumberTest.class.getName(), "Number"), one, two);
192       inTEST(one, two, div, minus, mult, plus);
193    }
194
195    /**
196     * Test a Float
197     */

198    public void testFloatObject() throws Exception
199    {
200       ValueExp one = Query.value(new Float(10f));
201       ValueExp two = Query.value(new Float(20f));
202       ValueExp div = Query.value(new Double(2f));
203       ValueExp minus = Query.value(new Double(-10f));
204       ValueExp mult = Query.value(new Double(200f));
205       ValueExp plus = Query.value(new Double(30f));
206       equalsTEST(one, two);
207       operationTEST(one, two, div, minus, mult, plus);
208       comparisonTEST(one, two);
209       betweenTEST(one, two, plus);
210       attrTEST(new NumberTest(new Float(10f)), Query.attr("Number"), one, two);
211       attrTEST(new NumberTest(new Float(10f)), Query.attr(NumberTest.class.getName(), "Number"), one, two);
212       inTEST(one, two, div, minus, mult, plus);
213    }
214
215    /**
216     * Test a int
217     */

218    public void testInteger() throws Exception
219    {
220       ValueExp one = Query.value(10);
221       ValueExp two = Query.value(20);
222       ValueExp div = Query.value(2);
223       ValueExp minus = Query.value(-10);
224       ValueExp mult = Query.value(200);
225       ValueExp plus = Query.value(30);
226       equalsTEST(one, two);
227       operationTEST(one, two, div, minus, mult, plus);
228       comparisonTEST(one, two);
229       betweenTEST(one, two, plus);
230       attrTEST(new NumberTest(10), Query.attr("Number"), one, two);
231       attrTEST(new NumberTest(10), Query.attr(NumberTest.class.getName(), "Number"), one, two);
232       inTEST(one, two, div, minus, mult, plus);
233    }
234
235    /**
236     * Test a Integer
237     */

238    public void testIntegerObject() throws Exception
239    {
240       ValueExp one = Query.value(new Integer(10));
241       ValueExp two = Query.value(new Integer(20));
242       ValueExp div = Query.value(new Double(2));
243       ValueExp minus = Query.value(new Double(-10));
244       ValueExp mult = Query.value(new Double(200));
245       ValueExp plus = Query.value(new Double(30));
246       equalsTEST(one, two);
247       operationTEST(one, two, div, minus, mult, plus);
248       comparisonTEST(one, two);
249       betweenTEST(one, two, plus);
250       attrTEST(new NumberTest(new Integer(10)), Query.attr("Number"), one, two);
251       attrTEST(new NumberTest(new Integer(10)), Query.attr(NumberTest.class.getName(), "Number"), one, two);
252       inTEST(one, two, div, minus, mult, plus);
253    }
254
255    /**
256     * Test a long
257     */

258    public void testLong() throws Exception
259    {
260       ValueExp one = Query.value(10l);
261       ValueExp two = Query.value(20l);
262       ValueExp div = Query.value(2l);
263       ValueExp minus = Query.value(-10l);
264       ValueExp mult = Query.value(200l);
265       ValueExp plus = Query.value(30l);
266       equalsTEST(one, two);
267       operationTEST(one, two, div, minus, mult, plus);
268       comparisonTEST(one, two);
269       betweenTEST(one, two, plus);
270       attrTEST(new NumberTest(10l), Query.attr("Number"), one, two);
271       attrTEST(new NumberTest(10l), Query.attr(NumberTest.class.getName(), "Number"), one, two);
272       inTEST(one, two, div, minus, mult, plus);
273    }
274
275    /**
276     * Test a Long
277     */

278    public void testLongObject() throws Exception
279    {
280       ValueExp one = Query.value(new Long(10l));
281       ValueExp two = Query.value(new Long(20l));
282       ValueExp div = Query.value(new Double(2l));
283       ValueExp minus = Query.value(new Double(-10l));
284       ValueExp mult = Query.value(new Double(200l));
285       ValueExp plus = Query.value(new Double(30l));
286       equalsTEST(one, two);
287       operationTEST(one, two, div, minus, mult, plus);
288       comparisonTEST(one, two);
289       betweenTEST(one, two, plus);
290       attrTEST(new NumberTest(new Long(10l)), Query.attr("Number"), one, two);
291       attrTEST(new NumberTest(new Long(10l)), Query.attr(NumberTest.class.getName(), "Number"), one, two);
292       inTEST(one, two, div, minus, mult, plus);
293    }
294
295    /**
296     * Test a String
297     */

298    public void testString() throws Exception
299    {
300       ValueExp one = Query.value("Hello");
301       ValueExp two = Query.value("Goodbye");
302       ValueExp cat = Query.value("HelloGoodbye");
303       ValueExp three = Query.value("ZZZZZZ");
304       ValueExp four = Query.value("Hi");
305       ValueExp five = Query.value("See ya");
306       ValueExp six = Query.value("Laytaz");
307       equalsTEST(one, two);
308       catTEST(one, two, cat);
309       comparisonTEST(two, one);
310       betweenTEST(two, one, three);
311       attrTEST(new StringTest("Hello"), Query.attr("String"), one, two);
312       attrTEST(new StringTest("Hello"), Query.attr(StringTest.class.getName(), "String"), one, two);
313       inTEST(one, two, three, four, five, six);
314    }
315
316    /**
317     * Test and is true both
318     */

319    public void testAndTrueBoth() throws Exception
320    {
321       new QueryTEST(
322          new MBean[]
323          {
324             new MBean(new NumberTest(0), "Domain1:type=instance1")
325          },
326          new MBean[0],
327          Query.and
328          (
329             Query.eq
330             (
331                Query.value(10), Query.value(10)
332             ),
333             Query.eq
334             (
335                Query.value("Hello"), Query.value("Hello")
336             )
337          )
338       ).test();
339    }
340
341    /**
342     * Test and is false first parameter
343     */

344    public void testAndFalseFirst() throws Exception
345    {
346       new QueryTEST(
347          new MBean[0],
348          new MBean[]
349          {
350             new MBean(new NumberTest(0), "Domain1:type=instance1")
351          },
352          Query.and
353          (
354             Query.eq
355             (
356                Query.value(10), Query.value(20)
357             ),
358             Query.eq
359             (
360                Query.value("Hello"), Query.value("Hello")
361             )
362          )
363       ).test();
364    }
365
366    /**
367     * Test and is false second parameter
368     */

369    public void testAndFalseSecond() throws Exception
370    {
371       new QueryTEST(
372          new MBean[0],
373          new MBean[]
374          {
375             new MBean(new NumberTest(0), "Domain1:type=instance1")
376          },
377          Query.and
378          (
379             Query.eq
380             (
381                Query.value(10), Query.value(10)
382             ),
383             Query.eq
384             (
385                Query.value("Hello"), Query.value("Goodbye")
386             )
387          )
388       ).test();
389    }
390
391    /**
392     * Test and is false both parameters
393     */

394    public void testAndFalseBoth() throws Exception
395    {
396       new QueryTEST(
397          new MBean[0],
398          new MBean[]
399          {
400             new MBean(new NumberTest(0), "Domain1:type=instance1")
401          },
402          Query.and
403          (
404             Query.eq
405             (
406                Query.value(10), Query.value(20)
407             ),
408             Query.eq
409             (
410                Query.value("Hello"), Query.value("Goodbye")
411             )
412          )
413       ).test();
414    }
415
416    /**
417     * Test or is true both
418     */

419    public void testOrTrueBoth() throws Exception
420    {
421       new QueryTEST(
422          new MBean[]
423          {
424             new MBean(new NumberTest(0), "Domain1:type=instance1")
425          },
426          new MBean[0],
427          Query.or
428          (
429             Query.eq
430             (
431                Query.value(10), Query.value(10)
432             ),
433             Query.eq
434             (
435                Query.value("Hello"), Query.value("Hello")
436             )
437          )
438       ).test();
439    }
440
441    /**
442     * Test or is false first parameter
443     */

444    public void testOrFalseFirst() throws Exception
445    {
446       new QueryTEST(
447          new MBean[]
448          {
449             new MBean(new NumberTest(0), "Domain1:type=instance1")
450          },
451          new MBean[0],
452          Query.or
453          (
454             Query.eq
455             (
456                Query.value(10), Query.value(20)
457             ),
458             Query.eq
459             (
460                Query.value("Hello"), Query.value("Hello")
461             )
462          )
463       ).test();
464    }
465
466    /**
467     * Test or is false second parameter
468     */

469    public void testOrFalseSecond() throws Exception
470    {
471       new QueryTEST(
472          new MBean[0],
473          new MBean[]
474          {
475             new MBean(new NumberTest(0), "Domain1:type=instance1")
476          },
477          Query.and
478          (
479             Query.eq
480             (
481                Query.value(10), Query.value(10)
482             ),
483             Query.eq
484             (
485                Query.value("Hello"), Query.value("Goodbye")
486             )
487          )
488       ).test();
489    }
490
491    /**
492     * Test or is false both parameters
493     */

494    public void testOrFalseBoth() throws Exception
495    {
496       new QueryTEST(
497          new MBean[0],
498          new MBean[]
499          {
500             new MBean(new NumberTest(0), "Domain1:type=instance1")
501          },
502          Query.or
503          (
504             Query.eq
505             (
506                Query.value(10), Query.value(20)
507             ),
508             Query.eq
509             (
510                Query.value("Hello"), Query.value("Goodbye")
511             )
512          )
513       ).test();
514    }
515
516    /**
517     * Test not
518     */

519    public void testNot() throws Exception
520    {
521       new QueryTEST(
522          new MBean[0],
523          new MBean[]
524          {
525             new MBean(new NumberTest(0), "Domain1:type=instance1")
526          },
527          Query.not
528          (
529             Query.eq
530             (
531                Query.value("Hello"), Query.value("Hello")
532             )
533          )
534       ).test();
535    }
536
537    /**
538     * Test not not
539     */

540    public void testNotNot() throws Exception
541    {
542       new QueryTEST(
543          new MBean[]
544          {
545             new MBean(new NumberTest(0), "Domain1:type=instance1")
546          },
547          new MBean[0],
548          Query.not
549          (
550             Query.eq
551             (
552                Query.value("Hello"), Query.value("Goodbye")
553             )
554          )
555       ).test();
556    }
557
558    /**
559     * Test class attribute
560     */

561    public void testClassAttribute() throws Exception
562    {
563       new QueryTEST(
564          new MBean[]
565          {
566             new MBean(new NumberTest(0), "Domain1:type=instance1")
567          },
568          new MBean[]
569          {
570             new MBean(new Trivial(), "Domain1:type=instance2")
571          },
572          Query.eq
573          (
574             Query.classattr(), Query.value(NumberTest.class.getName())
575          )
576       ).test();
577    }
578
579    /**
580     * Test simple object name
581     */

582    public void testSimpleObjectName() throws Exception
583    {
584       new QueryTEST(
585          new MBean[]
586          {
587             new MBean(new Trivial(), "Domain1:type=instance1")
588          },
589          new MBean[]
590          {
591             new MBean(new Trivial(), "Domain1:type=instance2")
592          },
593          new ObjectName("Domain1:type=instance1")
594       ).test();
595    }
596
597    /**
598     * Test domain pattern object name
599     */

600    public void testDomainPatternObjectName() throws Exception
601    {
602       new QueryTEST(
603          new MBean[]
604          {
605             new MBean(new Trivial(), "Domain1:type=instance1")
606          },
607          new MBean[]
608          {
609             new MBean(new Trivial(), "Domain1:type=instance2")
610          },
611          new ObjectName("*:type=instance1")
612       ).test();
613    }
614
615    /**
616     * Test property pattern object name
617     */

618    public void testPropertyPatternObjectName() throws Exception
619    {
620       new QueryTEST(
621          new MBean[]
622          {
623             new MBean(new Trivial(), "Domain1:type=instance1"),
624             new MBean(new Trivial(), "Domain1:type=instance2")
625          },
626          new MBean[]
627          {
628             new MBean(new Trivial(), "Domain2:type=instance1")
629          },
630          new ObjectName("Domain1:*")
631       ).test();
632    }
633
634    /**
635     * Test multiple property pattern object name
636     */

637    public void testMultiplePropertyPatternObjectName() throws Exception
638    {
639       new QueryTEST(
640          new MBean[]
641          {
642             new MBean(new Trivial(), "Domain1:type=instance1,extra=true")
643          },
644          new MBean[]
645          {
646             new MBean(new Trivial(), "Domain1:type=instance2")
647          },
648          new ObjectName("Domain1:extra=true,*")
649       ).test();
650    }
651
652    /**
653     * Test invalid name passed to ObjectName
654     */

655    public void testInvalidNamePassedToObjectName() throws Exception
656    {
657       if (new ObjectName("*:*").apply(new ObjectName("*:type=patternNotAllowedHere")))
658          fail("Patterns should not be matched");
659    }
660
661    /**
662     * Test any substring
663     */

664    public void testAnySubstring() throws Exception
665    {
666       new QueryTEST(
667          new MBean[]
668          {
669             new MBean(new StringTest("Hello"), "Domain1:type=instance1"),
670             new MBean(new StringTest("ellbeginning"), "Domain1:type=instance2"),
671             new MBean(new StringTest("endell"), "Domain1:type=instance3"),
672             new MBean(new StringTest("ell"), "Domain1:type=instance4")
673          },
674          new MBean[]
675          {
676             new MBean(new StringTest("Goodbye"), "Domain2:type=instance1"),
677             new MBean(new StringTest("el"), "Domain2:type=instance2"),
678             new MBean(new StringTest("ll"), "Domain2:type=instance3"),
679             new MBean(new StringTest("e ll"), "Domain2:type=instance4"),
680             new MBean(new StringTest("ELL"), "Domain2:type=instance5"),
681             new MBean(new StringTest("Ell"), "Domain2:type=instance6")
682          },
683          Query.anySubString(Query.attr("String"), Query.value("ell"))
684       ).test();
685    }
686
687    /**
688     * Test final substring
689     */

690    public void testFinalSubstring() throws Exception
691    {
692       new QueryTEST(
693          new MBean[]
694          {
695             new MBean(new StringTest("endell"), "Domain1:type=instance1"),
696             new MBean(new StringTest("ell"), "Domain1:type=instance2")
697          },
698          new MBean[]
699          {
700             new MBean(new StringTest("Hello"), "Domain2:type=instance1"),
701             new MBean(new StringTest("ellbeginning"), "Domain2:type=instance2"),
702             new MBean(new StringTest("Goodbye"), "Domain2:type=instance3"),
703             new MBean(new StringTest("el"), "Domain2:type=instance4"),
704             new MBean(new StringTest("ll"), "Domain2:type=instance5"),
705             new MBean(new StringTest("e ll"), "Domain2:type=instance6"),
706             new MBean(new StringTest("ELL"), "Domain2:type=instance7"),
707             new MBean(new StringTest("Ell"), "Domain2:type=instance8")
708          },
709          Query.finalSubString(Query.attr("String"), Query.value("ell"))
710       ).test();
711    }
712
713    /**
714     * Test initial substring
715     */

716    public void testInitialSubstring() throws Exception
717    {
718       new QueryTEST(
719          new MBean[]
720          {
721             new MBean(new StringTest("ellbeginning"), "Domain1:type=instance1"),
722             new MBean(new StringTest("ell"), "Domain1:type=instance2")
723          },
724          new MBean[]
725          {
726             new MBean(new StringTest("Hello"), "Domain2:type=instance1"),
727             new MBean(new StringTest("endell"), "Domain2:type=instance2"),
728             new MBean(new StringTest("Goodbye"), "Domain2:type=instance3"),
729             new MBean(new StringTest("el"), "Domain2:type=instance4"),
730             new MBean(new StringTest("ll"), "Domain2:type=instance5"),
731             new MBean(new StringTest("e ll"), "Domain2:type=instance6"),
732             new MBean(new StringTest("ELL"), "Domain2:type=instance7"),
733             new MBean(new StringTest("Ell"), "Domain2:type=instance8")
734          },
735          Query.initialSubString(Query.attr("String"), Query.value("ell"))
736       ).test();
737    }
738
739    /**
740     * Test match asterisk beginning
741     */

742    public void testMatchAsteriskBeginning() throws Exception
743    {
744       new QueryTEST(
745          new MBean[]
746          {
747             new MBean(new StringTest("endell"), "Domain1:type=instance1"),
748             new MBean(new StringTest("ell"), "Domain1:type=instance2")
749          },
750          new MBean[]
751          {
752             new MBean(new StringTest("Hello"), "Domain2:type=instance1"),
753             new MBean(new StringTest("ellbeginning"), "Domain2:type=instance2"),
754             new MBean(new StringTest("Goodbye"), "Domain2:type=instance3"),
755             new MBean(new StringTest("el"), "Domain2:type=instance4"),
756             new MBean(new StringTest("ll"), "Domain2:type=instance5"),
757             new MBean(new StringTest("e ll"), "Domain2:type=instance6"),
758             new MBean(new StringTest("ELL"), "Domain2:type=instance7"),
759             new MBean(new StringTest("Ell"), "Domain2:type=instance8")
760          },
761          Query.match(Query.attr("String"), Query.value("*ell"))
762       ).test();
763    }
764
765    /**
766     * Test match asterisk end
767     */

768    public void testMatchAsteriskEnd() throws Exception
769    {
770       new QueryTEST(
771          new MBean[]
772          {
773             new MBean(new StringTest("ellbeginning"), "Domain1:type=instance1"),
774             new MBean(new StringTest("ell"), "Domain1:type=instance2")
775          },
776          new MBean[]
777          {
778             new MBean(new StringTest("Hello"), "Domain2:type=instance1"),
779             new MBean(new StringTest("beginningell"), "Domain2:type=instance2"),
780             new MBean(new StringTest("Goodbye"), "Domain2:type=instance3"),
781             new MBean(new StringTest("el"), "Domain2:type=instance4"),
782             new MBean(new StringTest("ll"), "Domain2:type=instance5"),
783             new MBean(new StringTest("e ll"), "Domain2:type=instance6"),
784             new MBean(new StringTest("ELL"), "Domain2:type=instance7"),
785             new MBean(new StringTest("Ell"), "Domain2:type=instance8")
786          },
787          Query.match(Query.attr("String"), Query.value("ell*"))
788       ).test();
789    }
790
791    /**
792     * Test any match asterisk beginning and end
793     */

794    public void testMatchAsteriskBeginningAndEnd() throws Exception
795    {
796       new QueryTEST(
797          new MBean[]
798          {
799             new MBean(new StringTest("Hello"), "Domain1:type=instance1"),
800             new MBean(new StringTest("ell"), "Domain1:type=instance2"),
801             new MBean(new StringTest("endell"), "Domain1:type=instance3"),
802             new MBean(new StringTest("beginningell"), "Domain1:type=instance4")
803          },
804          new MBean[]
805          {
806             new MBean(new StringTest("Goodbye"), "Domain2:type=instance1"),
807             new MBean(new StringTest("el"), "Domain2:type=instance2"),
808             new MBean(new StringTest("ll"), "Domain2:type=instance3"),
809             new MBean(new StringTest("e ll"), "Domain2:type=instance4"),
810             new MBean(new StringTest("ELL"), "Domain2:type=instance5"),
811             new MBean(new StringTest("Ell"), "Domain2:type=instance6")
812          },
813          Query.match(Query.attr("String"), Query.value("*ell*"))
814       ).test();
815    }
816
817    /**
818     * Test match asterisk embedded
819     */

820    public void testMatchAsteriskEmbedded() throws Exception
821    {
822       new QueryTEST(
823          new MBean[]
824          {
825             new MBean(new StringTest("Hello"), "Domain1:type=instance1"),
826          },
827          new MBean[]
828          {
829             new MBean(new StringTest("ell"), "Domain2:type=instance1"),
830             new MBean(new StringTest("endell"), "Domain2:type=instance2"),
831             new MBean(new StringTest("beginningell"), "Domain2:type=instance3"),
832             new MBean(new StringTest("Goodbye"), "Domain2:type=instance4"),
833             new MBean(new StringTest("el"), "Domain2:type=instance5"),
834             new MBean(new StringTest("ll"), "Domain2:type=instance6"),
835             new MBean(new StringTest("e ll"), "Domain4:type=instance7"),
836             new MBean(new StringTest("ELL"), "Domain2:type=instance8"),
837             new MBean(new StringTest("Ell"), "Domain2:type=instance9")
838          },
839          Query.match(Query.attr("String"), Query.value("H*o"))
840       ).test();
841    }
842
843    /**
844     * Test match question beginning
845     */

846    public void testMatchQuestionBeginning() throws Exception
847    {
848       new QueryTEST(
849          new MBean[]
850          {
851             new MBean(new StringTest("Hello"), "Domain1:type=instance1"),
852             new MBean(new StringTest("hello"), "Domain1:type=instance2"),
853             new MBean(new StringTest(" ello"), "Domain1:type=instance3")
854          },
855          new MBean[]
856          {
857             new MBean(new StringTest("ello"), "Domain2:type=instance1"),
858             new MBean(new StringTest("Ello"), "Domain2:type=instance2"),
859             new MBean(new StringTest("hhello"), "Domain2:type=instance3"),
860          },
861          Query.match(Query.attr("String"), Query.value("?ello"))
862       ).test();
863    }
864
865    /**
866     * Test match question end
867     */

868    public void testMatchQuestionEnd() throws Exception
869    {
870       new QueryTEST(
871          new MBean[]
872          {
873             new MBean(new StringTest("Hello"), "Domain1:type=instance1"),
874             new MBean(new StringTest("HellO"), "Domain1:type=instance2"),
875             new MBean(new StringTest("Hell "), "Domain1:type=instance3")
876          },
877          new MBean[]
878          {
879             new MBean(new StringTest("hell"), "Domain2:type=instance1"),
880             new MBean(new StringTest("helL"), "Domain2:type=instance2"),
881             new MBean(new StringTest("Helloo"), "Domain2:type=instance3"),
882          },
883          Query.match(Query.attr("String"), Query.value("Hell?"))
884       ).test();
885    }
886
887    /**
888     * Test match question beginning and end
889     */

890    public void testMatchQuestionBeginningEnd() throws Exception
891    {
892       new QueryTEST(
893          new MBean[]
894          {
895             new MBean(new StringTest("Hello"), "Domain1:type=instance1"),
896             new MBean(new StringTest("HellO"), "Domain1:type=instance2"),
897             new MBean(new StringTest("hello"), "Domain1:type=instance3"),
898             new MBean(new StringTest("hellO"), "Domain1:type=instance4"),
899             new MBean(new StringTest(" ell "), "Domain1:type=instance5")
900          },
901          new MBean[]
902          {
903             new MBean(new StringTest("hell"), "Domain2:type=instance1"),
904             new MBean(new StringTest("helL"), "Domain2:type=instance2"),
905             new MBean(new StringTest("ello"), "Domain2:type=instance3"),
906             new MBean(new StringTest("Ello"), "Domain2:type=instance4"),
907             new MBean(new StringTest("ell"), "Domain2:type=instance5"),
908             new MBean(new StringTest("Helloo"), "Domain2:type=instance6"),
909             new MBean(new StringTest("HHello"), "Domain2:type=instance7"),
910             new MBean(new StringTest("HHelloo"), "Domain2:type=instance8"),
911          },
912          Query.match(Query.attr("String"), Query.value("?ell?"))
913       ).test();
914    }
915
916    /**
917     * Test match question embedded
918     */

919    public void testMatchQuestionEmbedded() throws Exception
920    {
921       new QueryTEST(
922          new MBean[]
923          {
924             new MBean(new StringTest("Hello"), "Domain1:type=instance1"),
925             new MBean(new StringTest("HeLlo"), "Domain1:type=instance2"),
926             new MBean(new StringTest("He lo"), "Domain1:type=instance3")
927          },
928          new MBean[]
929          {
930             new MBean(new StringTest("hell"), "Domain2:type=instance1"),
931             new MBean(new StringTest("ello"), "Domain2:type=instance2"),
932             new MBean(new StringTest("ell"), "Domain2:type=instance3"),
933             new MBean(new StringTest("Helloo"), "Domain2:type=instance4"),
934             new MBean(new StringTest("HHello"), "Domain2:type=instance5"),
935             new MBean(new StringTest("HHelloo"), "Domain2:type=instance6"),
936          },
937          Query.match(Query.attr("String"), Query.value("He?lo"))
938       ).test();
939    }
940
941    /**
942     * Test match character set
943     */

944    public void testMatchCharacterSet() throws Exception
945    {
946       try
947       {
948          new QueryTEST(
949             new MBean[]
950             {
951                new MBean(new StringTest("Hello"), "Domain1:type=instance1"),
952                new MBean(new StringTest("HeLlo"), "Domain1:type=instance2"),
953             },
954             new MBean[]
955             {
956                new MBean(new StringTest("hell"), "Domain2:type=instance1"),
957                new MBean(new StringTest("ello"), "Domain2:type=instance2"),
958                new MBean(new StringTest("ell"), "Domain2:type=instance3"),
959                new MBean(new StringTest("Helloo"), "Domain2:type=instance4"),
960                new MBean(new StringTest("HHello"), "Domain2:type=instance5"),
961                new MBean(new StringTest("HHelloo"), "Domain2:type=instance6"),
962             },
963             Query.match(Query.attr("String"), Query.value("He[lL]lo"))
964          ).test();
965       }
966       catch (AssertionFailedError e)
967       {
968           fail("FAILS IN RI: expected Hello to match He[lL]lo");
969       }
970    }
971
972    /**
973     * Test match character range
974     */

975    public void testMatchCharacterRange() throws Exception
976    {
977       try
978       {
979         new QueryTEST(
980             new MBean[]
981             {
982                new MBean(new StringTest("Hello"), "Domain1:type=instance1"),
983                new MBean(new StringTest("Hemlo"), "Domain1:type=instance2"),
984             },
985             new MBean[]
986             {
987                new MBean(new StringTest("hell"), "Domain2:type=instance1"),
988                new MBean(new StringTest("He lo"), "Domain2:type=instance2"),
989                new MBean(new StringTest("Heklo"), "Domain2:type=instance3"),
990                new MBean(new StringTest("Henlo"), "Domain2:type=instance4"),
991                new MBean(new StringTest("HeLlo"), "Domain2:type=instance5"),
992                new MBean(new StringTest("HeMlo"), "Domain2:type=instance6"),
993             },
994             Query.match(Query.attr("String"), Query.value("He[l-m]lo"))
995          ).test();
996       }
997       catch (AssertionFailedError e)
998       {
999           fail("FAILS IN RI: didn't expected HeMlo to match He[l-m]lo");
1000      }
1001   }
1002
1003   /**
1004    * Test match escaping question mark
1005    */

1006   public void testEscapingQuestion() throws Exception
1007   {
1008      new QueryTEST(
1009         new MBean[]
1010         {
1011            new MBean(new StringTest("Hello?"), "Domain1:type=instance1"),
1012         },
1013         new MBean[]
1014         {
1015            new MBean(new StringTest("Helloz"), "Domain2:type=instance1"),
1016         },
1017         Query.match(Query.attr("String"), Query.value("Hello\\?"))
1018      ).test();
1019   }
1020
1021   /**
1022    * Test match escaping asterisk
1023    */

1024   public void testEscapingAsterisk() throws Exception
1025   {
1026      new QueryTEST(
1027         new MBean[]
1028         {
1029            new MBean(new StringTest("Hello*"), "Domain1:type=instance1"),
1030         },
1031         new MBean[]
1032         {
1033            new MBean(new StringTest("Helloz"), "Domain2:type=instance1"),
1034         },
1035         Query.match(Query.attr("String"), Query.value("Hello\\*"))
1036      ).test();
1037   }
1038
1039   /**
1040    * Test match escaping open bracket
1041    */

1042   public void testEscapingOpenBracket() throws Exception
1043   {
1044      new QueryTEST(
1045         new MBean[]
1046         {
1047            new MBean(new StringTest("Hello[ab]"), "Domain1:type=instance1"),
1048         },
1049         new MBean[]
1050         {
1051            new MBean(new StringTest("Helloa"), "Domain2:type=instance1"),
1052            new MBean(new StringTest("Hello\\a"), "Domain2:type=instance2"),
1053         },
1054         Query.match(Query.attr("String"), Query.value("Hello\\[ab]"))
1055      ).test();
1056   }
1057
1058   /**
1059    * Test match minus in character set
1060    */

1061   public void testMinusInCharacterSet() throws Exception
1062   {
1063      try
1064      {
1065         new QueryTEST(
1066            new MBean[]
1067            {
1068               new MBean(new StringTest("Hello-"), "Domain1:type=instance1"),
1069            },
1070            new MBean[]
1071            {
1072               new MBean(new StringTest("Hello[ab-]"), "Domain2:type=instance1"),
1073            },
1074            Query.match(Query.attr("String"), Query.value("Hello[ab-]"))
1075         ).test();
1076      }
1077      catch (AssertionFailedError e)
1078      {
1079          fail("FAILS IN RI: expected Hello- to match Hello[ab-]");
1080      }
1081   }
1082
1083   /**
1084    * Test threading, tests that running the same query in multiple threads
1085    * works. This test might not catch a threading problem on every run.
1086    */

1087   public void testThreading() throws Exception
1088   {
1089      MBeanServer server1 = MBeanServerFactory.createMBeanServer("server1");
1090      MBeanServer server2 = MBeanServerFactory.createMBeanServer("server2");
1091      try
1092      {
1093         ObjectName name = new ObjectName("Domain1:type=instance1");
1094         NumberTest bean1 = new NumberTest(1);
1095         NumberTest bean2 = new NumberTest(2);
1096         server1.registerMBean(bean1, name);
1097         server2.registerMBean(bean2, name);
1098         QueryExp query = Query.eq(Query.attr("Number"), Query.value(2));
1099         QueryThread thread1 = new QueryThread(server1, query, 0);
1100         QueryThread thread2 = new QueryThread(server2, query, 1);
1101         thread1.start();
1102         thread2.start();
1103         thread1.join(10000);
1104         thread1.check();
1105         thread2.join(10000);
1106         thread2.check();
1107      }
1108      finally
1109      {
1110         MBeanServerFactory.releaseMBeanServer(server1);
1111         MBeanServerFactory.releaseMBeanServer(server2);
1112      }
1113   }
1114
1115   /**
1116    * Test pathological
1117    */

1118   public void testPathological() throws Exception
1119   {
1120      try
1121      {
1122         new QueryTEST(
1123            new MBean[]
1124            {
1125               new MBean(new StringTest("Hello(?:.)"), "Domain1:type=instance1"),
1126            },
1127            new MBean[]
1128            {
1129               new MBean(new StringTest("Hellox"), "Domain2:type=instance1"),
1130            },
1131            Query.match(Query.attr("String"), Query.value("Hello(?:.)"))
1132         ).test();
1133      }
1134      catch (AssertionFailedError e)
1135      {
1136          fail("FAILS IN JBossMX: expected Hello(?:.) to match Hello(?:.)");
1137      }
1138   }
1139
1140   // Support ----------------------------------------------------------------
1141

1142   private void equalsTEST(ValueExp value1, ValueExp value2)
1143      throws Exception
1144   {
1145      // Test equals
1146
new QueryTEST(
1147         new MBean[]
1148         {
1149            new MBean(new NumberTest(0), "Domain1:type=instance1")
1150         },
1151         new MBean[0],
1152         Query.eq
1153         (
1154            value1, value1
1155         )
1156      ).test();
1157      // Test not equals
1158
new QueryTEST(
1159         new MBean[0],
1160         new MBean[]
1161         {
1162            new MBean(new NumberTest(0), "Domain1:type=instance1")
1163         },
1164         Query.eq
1165         (
1166            value1, value2
1167         )
1168      ).test();
1169   }
1170
1171   private void operationTEST(ValueExp value1, ValueExp value2, ValueExp div,
1172                              ValueExp minus, ValueExp mult, ValueExp plus)
1173      throws Exception
1174   {
1175      // Test div
1176
new QueryTEST(
1177         new MBean[]
1178         {
1179            new MBean(new NumberTest(0), "Domain1:type=instance1")
1180         },
1181         new MBean[0],
1182         Query.eq
1183         (
1184            Query.div(value2, value1),
1185            div
1186         )
1187      ).test();
1188      // Test minus
1189
new QueryTEST(
1190         new MBean[]
1191         {
1192            new MBean(new NumberTest(0), "Domain1:type=instance1")
1193         },
1194         new MBean[0],
1195         Query.eq
1196         (
1197            Query.minus(value1, value2),
1198            minus
1199         )
1200      ).test();
1201      // Test mult
1202
new QueryTEST(
1203         new MBean[]
1204         {
1205            new MBean(new NumberTest(0), "Domain1:type=instance1")
1206         },
1207         new MBean[0],
1208         Query.eq
1209         (
1210            Query.times(value1, value2),
1211            mult
1212         )
1213      ).test();
1214      // Test plus
1215
new QueryTEST(
1216         new MBean[]
1217         {
1218            new MBean(new NumberTest(0), "Domain1:type=instance1")
1219         },
1220         new MBean[0],
1221         Query.eq
1222         (
1223            Query.plus(value1, value2),
1224            plus
1225         )
1226      ).test();
1227   }
1228
1229   private void catTEST(ValueExp value1, ValueExp value2, ValueExp cat)
1230      throws Exception
1231   {
1232      // Test cat
1233
new QueryTEST(
1234         new MBean[]
1235         {
1236            new MBean(new NumberTest(0), "Domain1:type=instance1")
1237         },
1238         new MBean[0],
1239         Query.eq
1240         (
1241            Query.plus(value1, value2),
1242            cat
1243         )
1244      ).test();
1245   }
1246
1247   private void comparisonTEST(ValueExp value1, ValueExp value2)
1248      throws Exception
1249   {
1250      // Test greater than or equals (really greater than)
1251
new QueryTEST(
1252         new MBean[]
1253         {
1254            new MBean(new NumberTest(0), "Domain1:type=instance1")
1255         },
1256         new MBean[0],
1257         Query.geq
1258         (
1259            value2, value1
1260         )
1261      ).test();
1262      // Test greater than or equals (really greater equals)
1263
new QueryTEST(
1264         new MBean[]
1265         {
1266            new MBean(new NumberTest(0), "Domain1:type=instance1")
1267         },
1268         new MBean[0],
1269         Query.geq
1270         (
1271            value1, value1
1272         )
1273      ).test();
1274      // Test not greater than or equals
1275
new QueryTEST(
1276         new MBean[0],
1277         new MBean[]
1278         {
1279            new MBean(new NumberTest(0), "Domain1:type=instance1")
1280         },
1281         Query.geq
1282         (
1283            value1, value2
1284         )
1285      ).test();
1286      // Test greater than
1287
new QueryTEST(
1288         new MBean[]
1289         {
1290            new MBean(new NumberTest(0), "Domain1:type=instance1")
1291         },
1292         new MBean[0],
1293         Query.gt
1294         (
1295            value2, value1
1296         )
1297      ).test();
1298      // Test not greater than
1299
new QueryTEST(
1300         new MBean[0],
1301         new MBean[]
1302         {
1303            new MBean(new NumberTest(0), "Domain1:type=instance1")
1304         },
1305         Query.gt
1306         (
1307            value1, value2
1308         )
1309      ).test();
1310      // Test greater than or equals (really greater than)
1311
new QueryTEST(
1312         new MBean[]
1313         {
1314            new MBean(new NumberTest(0), "Domain1:type=instance1")
1315         },
1316         new MBean[0],
1317         Query.leq
1318         (
1319            value1, value2
1320         )
1321      ).test();
1322      // Test greater than or equals (really greater equals)
1323
new QueryTEST(
1324         new MBean[]
1325         {
1326            new MBean(new NumberTest(0), "Domain1:type=instance1")
1327         },
1328         new MBean[0],
1329         Query.leq
1330         (
1331            value1, value1
1332         )
1333      ).test();
1334      // Test not greater than or equals
1335
new QueryTEST(
1336         new MBean[0],
1337         new MBean[]
1338         {
1339            new MBean(new NumberTest(0), "Domain1:type=instance1")
1340         },
1341         Query.leq
1342         (
1343            value2, value1
1344         )
1345      ).test();
1346      // Test greater than
1347
new QueryTEST(
1348         new MBean[]
1349         {
1350            new MBean(new NumberTest(0), "Domain1:type=instance1")
1351         },
1352         new MBean[0],
1353         Query.lt
1354         (
1355            value1, value2
1356         )
1357      ).test();
1358      // Test not greater than
1359
new QueryTEST(
1360         new MBean[0],
1361         new MBean[]
1362         {
1363            new MBean(new NumberTest(0), "Domain1:type=instance1")
1364         },
1365         Query.lt
1366         (
1367            value2, value1
1368         )
1369      ).test();
1370   }
1371
1372   private void betweenTEST(ValueExp value1, ValueExp value2, ValueExp value3)
1373      throws Exception
1374   {
1375      // Test between (really between)
1376
new QueryTEST(
1377         new MBean[]
1378         {
1379            new MBean(new NumberTest(0), "Domain1:type=instance1")
1380         },
1381         new MBean[0],
1382         Query.between
1383         (
1384            value2, value1, value3
1385         )
1386      ).test();
1387      // Test between (equals first)
1388
new QueryTEST(
1389         new MBean[]
1390         {
1391            new MBean(new NumberTest(0), "Domain1:type=instance1")
1392         },
1393         new MBean[0],
1394         Query.between
1395         (
1396            value2, value2, value3
1397         )
1398      ).test();
1399      // Test between (equals second)
1400
new QueryTEST(
1401         new MBean[]
1402         {
1403            new MBean(new NumberTest(0), "Domain1:type=instance1")
1404         },
1405         new MBean[0],
1406         Query.between
1407         (
1408            value2, value1, value2
1409         )
1410      ).test();
1411      // Test between (equals both)
1412
new QueryTEST(
1413         new MBean[]
1414         {
1415            new MBean(new NumberTest(0), "Domain1:type=instance1")
1416         },
1417         new MBean[0],
1418         Query.between
1419         (
1420            value2, value2, value2
1421         )
1422      ).test();
1423      // Test not between (first)
1424
new QueryTEST(
1425         new MBean[0],
1426         new MBean[]
1427         {
1428            new MBean(new NumberTest(0), "Domain1:type=instance1")
1429         },
1430         Query.between
1431         (
1432            value1, value2, value3
1433         )
1434      ).test();
1435      // Test not between (second)
1436
new QueryTEST(
1437         new MBean[0],
1438         new MBean[]
1439         {
1440            new MBean(new NumberTest(0), "Domain1:type=instance1")
1441         },
1442         Query.between
1443         (
1444            value3, value1, value2
1445         )
1446      ).test();
1447   }
1448
1449   private void attrTEST(Object mbean, AttributeValueExp attr, ValueExp value1, ValueExp value2)
1450      throws Exception
1451   {
1452      // Test true
1453
new QueryTEST(
1454         new MBean[]
1455         {
1456            new MBean(mbean, "Domain1:type=instance1")
1457         },
1458         new MBean[]
1459         {
1460            new MBean(new Trivial(), "Domain1:type=instance2")
1461         },
1462         Query.eq
1463         (
1464            attr, value1
1465         )
1466      ).test();
1467      // Test false
1468
new QueryTEST(
1469         new MBean[0],
1470         new MBean[]
1471         {
1472            new MBean(mbean, "Domain1:type=instance1")
1473         },
1474         Query.eq
1475         (
1476            attr, value2
1477         )
1478      ).test();
1479   }
1480
1481   private void inTEST(ValueExp value1, ValueExp value2, ValueExp value3,
1482                       ValueExp value4, ValueExp value5, ValueExp value6)
1483      throws Exception
1484   {
1485      // Test in first
1486
new QueryTEST(
1487         new MBean[]
1488         {
1489            new MBean(new Trivial(), "Domain1:type=instance1")
1490         },
1491         new MBean[0],
1492         Query.in
1493         (
1494            value1,
1495            new ValueExp[]
1496            {
1497               value1, value2, value3, value4, value5, value6
1498            }
1499         )
1500      ).test();
1501      // Test in last
1502
new QueryTEST(
1503         new MBean[]
1504         {
1505            new MBean(new Trivial(), "Domain1:type=instance1")
1506         },
1507         new MBean[0],
1508         Query.in
1509         (
1510            value6,
1511            new ValueExp[]
1512            {
1513               value1, value2, value3, value4, value5, value6
1514            }
1515         )
1516      ).test();
1517      // Test in not the first or last
1518
new QueryTEST(
1519         new MBean[]
1520         {
1521            new MBean(new Trivial(), "Domain1:type=instance1")
1522         },
1523         new MBean[0],
1524         Query.in
1525         (
1526            value3,
1527            new ValueExp[]
1528            {
1529               value1, value2, value3, value4, value5, value6
1530            }
1531         )
1532      ).test();
1533      // Test not in
1534
new QueryTEST(
1535         new MBean[0],
1536         new MBean[]
1537         {
1538            new MBean(new Trivial(), "Domain1:type=instance1")
1539         },
1540         Query.in
1541         (
1542            value1,
1543            new ValueExp[]
1544            {
1545               value2, value3, value4, value5, value6
1546            }
1547         )
1548      ).test();
1549   }
1550
1551   private class MBean
1552   {
1553      public Object mbean;
1554      public ObjectName objectName;
1555      public MBean(Object object, String name)
1556         throws Exception
1557      {
1558         this.mbean = object;
1559         this.objectName = new ObjectName(name);
1560      }
1561   }
1562
1563   private class QueryTEST
1564   {
1565      HashSet expectedInstances = new HashSet();
1566      HashSet expectedNames = new HashSet();
1567      QueryExp queryExp;
1568      MBeanServer server;
1569
1570      public QueryTEST(MBean[] expected, MBean[] others, QueryExp queryExp)
1571         throws Exception
1572      {
1573         this.queryExp = queryExp;
1574
1575         server = MBeanServerFactory.createMBeanServer();
1576
1577         for (int i = 0; i < expected.length; i++)
1578         {
1579            ObjectInstance instance = server.registerMBean(expected[i].mbean,
1580                                                           expected[i].objectName);
1581            expectedInstances.add(instance);
1582            expectedNames.add(instance.getObjectName());
1583         }
1584         for (int i = 0; i < others.length; i++)
1585            server.registerMBean(others[i].mbean, others[i].objectName);
1586      }
1587
1588      public void test()
1589         throws Exception
1590      {
1591         try
1592         {
1593            testQueryMBeans();
1594            testQueryNames();
1595         }
1596         finally
1597         {
1598            MBeanServerFactory.releaseMBeanServer(server);
1599         }
1600      }
1601
1602      public void testQueryMBeans()
1603         throws Exception
1604      {
1605         Set result = server.queryMBeans(null, queryExp);
1606
1607         Iterator iterator = result.iterator();
1608         while (iterator.hasNext())
1609         {
1610            ObjectInstance instance = (ObjectInstance) iterator.next();
1611            Iterator iterator2 = expectedInstances.iterator();
1612            boolean found = false;
1613            while (iterator2.hasNext())
1614            {
1615               if (iterator2.next().equals(instance))
1616               {
1617                  iterator2.remove();
1618                  found = true;
1619                  break;
1620               }
1621            }
1622            if (found == false &&
1623               instance.getObjectName().getDomain().equals("JMImplementation") == false)
1624               fail("Unexpected instance " + instance.getObjectName()
1625                   + "\nfor query " + queryExp);
1626         }
1627
1628         iterator = expectedInstances.iterator();
1629         if (iterator.hasNext())
1630         {
1631            ObjectInstance instance = (ObjectInstance) iterator.next();
1632            fail("Expected instance " + instance.getObjectName()
1633                   + "\nfor query " + queryExp);
1634         }
1635      }
1636
1637      public void testQueryNames()
1638         throws Exception
1639      {
1640         Set result = server.queryNames(null, queryExp);
1641
1642         Iterator iterator = result.iterator();
1643         while (iterator.hasNext())
1644         {
1645            ObjectName name = (ObjectName) iterator.next();
1646            Iterator iterator2 = expectedNames.iterator();
1647            boolean found = false;
1648            while (iterator2.hasNext())
1649            {
1650               if (iterator2.next().equals(name))
1651               {
1652                  iterator2.remove();
1653                  found = true;
1654                  break;
1655               }
1656            }
1657            if (found == false &&
1658               name.getDomain().equals("JMImplementation") == false)
1659               fail("Unexpected name " + name
1660                    + "\nfor query " + queryExp);
1661         }
1662
1663         iterator = expectedNames.iterator();
1664         if (iterator.hasNext())
1665         {
1666            fail("Expected instance " + iterator.next()
1667                   + "\nfor query " + queryExp);
1668         }
1669      }
1670   }
1671
1672   public class QueryThread
1673      extends Thread
1674   {
1675      MBeanServer server;
1676      QueryExp query;
1677      int expected;
1678      int result;
1679      public QueryThread(MBeanServer server, QueryExp query, int expected)
1680      {
1681         this.server = server;
1682         this.query = query;
1683         this.expected = expected;
1684      }
1685      public int getExpected()
1686      {
1687         return expected;
1688      }
1689      public void check()
1690      {
1691         assertEquals(expected, result);
1692      }
1693      public void run()
1694      {
1695         for (int i = 0; i < 1000; i++)
1696         {
1697            result = server.queryNames(null, query).size();
1698            if (result != expected)
1699               return;
1700         }
1701      }
1702   }
1703}
1704
Popular Tags