KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.HashSet JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.Set JavaDoc;
27
28 import javax.management.AttributeValueExp JavaDoc;
29 import javax.management.MBeanServer JavaDoc;
30 import javax.management.MBeanServerFactory JavaDoc;
31 import javax.management.ObjectInstance JavaDoc;
32 import javax.management.ObjectName JavaDoc;
33 import javax.management.Query JavaDoc;
34 import javax.management.QueryExp JavaDoc;
35 import javax.management.ValueExp JavaDoc;
36
37 import org.jboss.test.jmx.compliance.query.support.BooleanTest;
38 import org.jboss.test.jmx.compliance.query.support.NumberTest;
39 import org.jboss.test.jmx.compliance.query.support.StringTest;
40 import org.jboss.test.jmx.compliance.query.support.Trivial;
41
42 import junit.framework.AssertionFailedError;
43 import junit.framework.TestCase;
44
45 /**
46  * Query unit tests
47  *
48  * @author <a HREF="mailto:Adrian.Brock@HappeningTimes.com">Adrian Brock</a>.
49  */

50 public class QueryTestCase
51   extends TestCase
52 {
53    // Attributes ----------------------------------------------------------------
54

55    // Constructor ---------------------------------------------------------------
56

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

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

67    /**
68     * Test a boolean
69     */

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

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

173    public void testDoubleObject() throws Exception JavaDoc
174    {
175       ValueExp JavaDoc one = Query.value(new Double JavaDoc(10d));
176       ValueExp JavaDoc two = Query.value(new Double JavaDoc(20d));
177       ValueExp JavaDoc div = Query.value(new Double JavaDoc(2d));
178       ValueExp JavaDoc minus = Query.value(new Double JavaDoc(-10d));
179       ValueExp JavaDoc mult = Query.value(new Double JavaDoc(200d));
180       ValueExp JavaDoc plus = Query.value(new Double JavaDoc(30d));
181       equalsTEST(one, two);
182       operationTEST(one, two, div, minus, mult, plus);
183       comparisonTEST(one, two);
184       betweenTEST(one, two, plus);
185       attrTEST(new NumberTest(new Double JavaDoc(10d)), Query.attr("Number"), one, two);
186       attrTEST(new NumberTest(new Double JavaDoc(10d)), Query.attr(NumberTest.class.getName(), "Number"), one, two);
187       inTEST(one, two, div, minus, mult, plus);
188    }
189
190    /**
191     * Test a float
192     */

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

213    public void testFloatObject() throws Exception JavaDoc
214    {
215       ValueExp JavaDoc one = Query.value(new Float JavaDoc(10f));
216       ValueExp JavaDoc two = Query.value(new Float JavaDoc(20f));
217       ValueExp JavaDoc div = Query.value(new Double JavaDoc(2f));
218       ValueExp JavaDoc minus = Query.value(new Double JavaDoc(-10f));
219       ValueExp JavaDoc mult = Query.value(new Double JavaDoc(200f));
220       ValueExp JavaDoc plus = Query.value(new Double JavaDoc(30f));
221       equalsTEST(one, two);
222       operationTEST(one, two, div, minus, mult, plus);
223       comparisonTEST(one, two);
224       betweenTEST(one, two, plus);
225       attrTEST(new NumberTest(new Float JavaDoc(10f)), Query.attr("Number"), one, two);
226       attrTEST(new NumberTest(new Float JavaDoc(10f)), Query.attr(NumberTest.class.getName(), "Number"), one, two);
227       inTEST(one, two, div, minus, mult, plus);
228    }
229
230    /**
231     * Test a int
232     */

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

253    public void testIntegerObject() throws Exception JavaDoc
254    {
255       ValueExp JavaDoc one = Query.value(new Integer JavaDoc(10));
256       ValueExp JavaDoc two = Query.value(new Integer JavaDoc(20));
257       ValueExp JavaDoc div = Query.value(new Double JavaDoc(2));
258       ValueExp JavaDoc minus = Query.value(new Double JavaDoc(-10));
259       ValueExp JavaDoc mult = Query.value(new Double JavaDoc(200));
260       ValueExp JavaDoc plus = Query.value(new Double JavaDoc(30));
261       equalsTEST(one, two);
262       operationTEST(one, two, div, minus, mult, plus);
263       comparisonTEST(one, two);
264       betweenTEST(one, two, plus);
265       attrTEST(new NumberTest(new Integer JavaDoc(10)), Query.attr("Number"), one, two);
266       attrTEST(new NumberTest(new Integer JavaDoc(10)), Query.attr(NumberTest.class.getName(), "Number"), one, two);
267       inTEST(one, two, div, minus, mult, plus);
268    }
269
270    /**
271     * Test a long
272     */

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

293    public void testLongObject() throws Exception JavaDoc
294    {
295       ValueExp JavaDoc one = Query.value(new Long JavaDoc(10l));
296       ValueExp JavaDoc two = Query.value(new Long JavaDoc(20l));
297       ValueExp JavaDoc div = Query.value(new Double JavaDoc(2l));
298       ValueExp JavaDoc minus = Query.value(new Double JavaDoc(-10l));
299       ValueExp JavaDoc mult = Query.value(new Double JavaDoc(200l));
300       ValueExp JavaDoc plus = Query.value(new Double JavaDoc(30l));
301       equalsTEST(one, two);
302       operationTEST(one, two, div, minus, mult, plus);
303       comparisonTEST(one, two);
304       betweenTEST(one, two, plus);
305       attrTEST(new NumberTest(new Long JavaDoc(10l)), Query.attr("Number"), one, two);
306       attrTEST(new NumberTest(new Long JavaDoc(10l)), Query.attr(NumberTest.class.getName(), "Number"), one, two);
307       inTEST(one, two, div, minus, mult, plus);
308    }
309
310    /**
311     * Test a String
312     */

313    public void testString() throws Exception JavaDoc
314    {
315       ValueExp JavaDoc one = Query.value("Hello");
316       ValueExp JavaDoc two = Query.value("Goodbye");
317       ValueExp JavaDoc cat = Query.value("HelloGoodbye");
318       ValueExp JavaDoc three = Query.value("ZZZZZZ");
319       ValueExp JavaDoc four = Query.value("Hi");
320       ValueExp JavaDoc five = Query.value("See ya");
321       ValueExp JavaDoc six = Query.value("Laytaz");
322       equalsTEST(one, two);
323       catTEST(one, two, cat);
324       comparisonTEST(two, one);
325       betweenTEST(two, one, three);
326       attrTEST(new StringTest("Hello"), Query.attr("String"), one, two);
327       attrTEST(new StringTest("Hello"), Query.attr(StringTest.class.getName(), "String"), one, two);
328       inTEST(one, two, three, four, five, six);
329    }
330
331    /**
332     * Test and is true both
333     */

334    public void testAndTrueBoth() throws Exception JavaDoc
335    {
336       new QueryTEST(
337          new MBean[]
338          {
339             new MBean(new NumberTest(0), "Domain1:type=instance1")
340          },
341          new MBean[0],
342          Query.and
343          (
344             Query.eq
345             (
346                Query.value(10), Query.value(10)
347             ),
348             Query.eq
349             (
350                Query.value("Hello"), Query.value("Hello")
351             )
352          )
353       ).test();
354    }
355
356    /**
357     * Test and is false first parameter
358     */

359    public void testAndFalseFirst() throws Exception JavaDoc
360    {
361       new QueryTEST(
362          new MBean[0],
363          new MBean[]
364          {
365             new MBean(new NumberTest(0), "Domain1:type=instance1")
366          },
367          Query.and
368          (
369             Query.eq
370             (
371                Query.value(10), Query.value(20)
372             ),
373             Query.eq
374             (
375                Query.value("Hello"), Query.value("Hello")
376             )
377          )
378       ).test();
379    }
380
381    /**
382     * Test and is false second parameter
383     */

384    public void testAndFalseSecond() throws Exception JavaDoc
385    {
386       new QueryTEST(
387          new MBean[0],
388          new MBean[]
389          {
390             new MBean(new NumberTest(0), "Domain1:type=instance1")
391          },
392          Query.and
393          (
394             Query.eq
395             (
396                Query.value(10), Query.value(10)
397             ),
398             Query.eq
399             (
400                Query.value("Hello"), Query.value("Goodbye")
401             )
402          )
403       ).test();
404    }
405
406    /**
407     * Test and is false both parameters
408     */

409    public void testAndFalseBoth() throws Exception JavaDoc
410    {
411       new QueryTEST(
412          new MBean[0],
413          new MBean[]
414          {
415             new MBean(new NumberTest(0), "Domain1:type=instance1")
416          },
417          Query.and
418          (
419             Query.eq
420             (
421                Query.value(10), Query.value(20)
422             ),
423             Query.eq
424             (
425                Query.value("Hello"), Query.value("Goodbye")
426             )
427          )
428       ).test();
429    }
430
431    /**
432     * Test or is true both
433     */

434    public void testOrTrueBoth() throws Exception JavaDoc
435    {
436       new QueryTEST(
437          new MBean[]
438          {
439             new MBean(new NumberTest(0), "Domain1:type=instance1")
440          },
441          new MBean[0],
442          Query.or
443          (
444             Query.eq
445             (
446                Query.value(10), Query.value(10)
447             ),
448             Query.eq
449             (
450                Query.value("Hello"), Query.value("Hello")
451             )
452          )
453       ).test();
454    }
455
456    /**
457     * Test or is false first parameter
458     */

459    public void testOrFalseFirst() throws Exception JavaDoc
460    {
461       new QueryTEST(
462          new MBean[]
463          {
464             new MBean(new NumberTest(0), "Domain1:type=instance1")
465          },
466          new MBean[0],
467          Query.or
468          (
469             Query.eq
470             (
471                Query.value(10), Query.value(20)
472             ),
473             Query.eq
474             (
475                Query.value("Hello"), Query.value("Hello")
476             )
477          )
478       ).test();
479    }
480
481    /**
482     * Test or is false second parameter
483     */

484    public void testOrFalseSecond() throws Exception JavaDoc
485    {
486       new QueryTEST(
487          new MBean[0],
488          new MBean[]
489          {
490             new MBean(new NumberTest(0), "Domain1:type=instance1")
491          },
492          Query.and
493          (
494             Query.eq
495             (
496                Query.value(10), Query.value(10)
497             ),
498             Query.eq
499             (
500                Query.value("Hello"), Query.value("Goodbye")
501             )
502          )
503       ).test();
504    }
505
506    /**
507     * Test or is false both parameters
508     */

509    public void testOrFalseBoth() throws Exception JavaDoc
510    {
511       new QueryTEST(
512          new MBean[0],
513          new MBean[]
514          {
515             new MBean(new NumberTest(0), "Domain1:type=instance1")
516          },
517          Query.or
518          (
519             Query.eq
520             (
521                Query.value(10), Query.value(20)
522             ),
523             Query.eq
524             (
525                Query.value("Hello"), Query.value("Goodbye")
526             )
527          )
528       ).test();
529    }
530
531    /**
532     * Test not
533     */

534    public void testNot() throws Exception JavaDoc
535    {
536       new QueryTEST(
537          new MBean[0],
538          new MBean[]
539          {
540             new MBean(new NumberTest(0), "Domain1:type=instance1")
541          },
542          Query.not
543          (
544             Query.eq
545             (
546                Query.value("Hello"), Query.value("Hello")
547             )
548          )
549       ).test();
550    }
551
552    /**
553     * Test not not
554     */

555    public void testNotNot() throws Exception JavaDoc
556    {
557       new QueryTEST(
558          new MBean[]
559          {
560             new MBean(new NumberTest(0), "Domain1:type=instance1")
561          },
562          new MBean[0],
563          Query.not
564          (
565             Query.eq
566             (
567                Query.value("Hello"), Query.value("Goodbye")
568             )
569          )
570       ).test();
571    }
572
573    /**
574     * Test class attribute
575     */

576    public void testClassAttribute() throws Exception JavaDoc
577    {
578       new QueryTEST(
579          new MBean[]
580          {
581             new MBean(new NumberTest(0), "Domain1:type=instance1")
582          },
583          new MBean[]
584          {
585             new MBean(new Trivial(), "Domain1:type=instance2")
586          },
587          Query.eq
588          (
589             Query.classattr(), Query.value(NumberTest.class.getName())
590          )
591       ).test();
592    }
593
594    /**
595     * Test simple object name
596     */

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

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

633    public void testPropertyPatternObjectName() throws Exception JavaDoc
634    {
635       new QueryTEST(
636          new MBean[]
637          {
638             new MBean(new Trivial(), "Domain1:type=instance1"),
639             new MBean(new Trivial(), "Domain1:type=instance2")
640          },
641          new MBean[]
642          {
643             new MBean(new Trivial(), "Domain2:type=instance1")
644          },
645          new ObjectName JavaDoc("Domain1:*")
646       ).test();
647    }
648
649    /**
650     * Test multiple property pattern object name
651     */

652    public void testMultiplePropertyPatternObjectName() throws Exception JavaDoc
653    {
654       new QueryTEST(
655          new MBean[]
656          {
657             new MBean(new Trivial(), "Domain1:type=instance1,extra=true")
658          },
659          new MBean[]
660          {
661             new MBean(new Trivial(), "Domain1:type=instance2")
662          },
663          new ObjectName JavaDoc("Domain1:extra=true,*")
664       ).test();
665    }
666
667    /**
668     * Test invalid name passed to ObjectName
669     */

670    public void testInvalidNamePassedToObjectName() throws Exception JavaDoc
671    {
672       if (new ObjectName JavaDoc("*:*").apply(new ObjectName JavaDoc("*:type=patternNotAllowedHere")))
673          fail("Patterns should not be matched");
674    }
675
676    /**
677     * Test any substring
678     */

679    public void testAnySubstring() throws Exception JavaDoc
680    {
681       new QueryTEST(
682          new MBean[]
683          {
684             new MBean(new StringTest("Hello"), "Domain1:type=instance1"),
685             new MBean(new StringTest("ellbeginning"), "Domain1:type=instance2"),
686             new MBean(new StringTest("endell"), "Domain1:type=instance3"),
687             new MBean(new StringTest("ell"), "Domain1:type=instance4")
688          },
689          new MBean[]
690          {
691             new MBean(new StringTest("Goodbye"), "Domain2:type=instance1"),
692             new MBean(new StringTest("el"), "Domain2:type=instance2"),
693             new MBean(new StringTest("ll"), "Domain2:type=instance3"),
694             new MBean(new StringTest("e ll"), "Domain2:type=instance4"),
695             new MBean(new StringTest("ELL"), "Domain2:type=instance5"),
696             new MBean(new StringTest("Ell"), "Domain2:type=instance6")
697          },
698          Query.anySubString(Query.attr("String"), Query.value("ell"))
699       ).test();
700    }
701
702    /**
703     * Test final substring
704     */

705    public void testFinalSubstring() throws Exception JavaDoc
706    {
707       new QueryTEST(
708          new MBean[]
709          {
710             new MBean(new StringTest("endell"), "Domain1:type=instance1"),
711             new MBean(new StringTest("ell"), "Domain1:type=instance2")
712          },
713          new MBean[]
714          {
715             new MBean(new StringTest("Hello"), "Domain2:type=instance1"),
716             new MBean(new StringTest("ellbeginning"), "Domain2:type=instance2"),
717             new MBean(new StringTest("Goodbye"), "Domain2:type=instance3"),
718             new MBean(new StringTest("el"), "Domain2:type=instance4"),
719             new MBean(new StringTest("ll"), "Domain2:type=instance5"),
720             new MBean(new StringTest("e ll"), "Domain2:type=instance6"),
721             new MBean(new StringTest("ELL"), "Domain2:type=instance7"),
722             new MBean(new StringTest("Ell"), "Domain2:type=instance8")
723          },
724          Query.finalSubString(Query.attr("String"), Query.value("ell"))
725       ).test();
726    }
727
728    /**
729     * Test initial substring
730     */

731    public void testInitialSubstring() throws Exception JavaDoc
732    {
733       new QueryTEST(
734          new MBean[]
735          {
736             new MBean(new StringTest("ellbeginning"), "Domain1:type=instance1"),
737             new MBean(new StringTest("ell"), "Domain1:type=instance2")
738          },
739          new MBean[]
740          {
741             new MBean(new StringTest("Hello"), "Domain2:type=instance1"),
742             new MBean(new StringTest("endell"), "Domain2:type=instance2"),
743             new MBean(new StringTest("Goodbye"), "Domain2:type=instance3"),
744             new MBean(new StringTest("el"), "Domain2:type=instance4"),
745             new MBean(new StringTest("ll"), "Domain2:type=instance5"),
746             new MBean(new StringTest("e ll"), "Domain2:type=instance6"),
747             new MBean(new StringTest("ELL"), "Domain2:type=instance7"),
748             new MBean(new StringTest("Ell"), "Domain2:type=instance8")
749          },
750          Query.initialSubString(Query.attr("String"), Query.value("ell"))
751       ).test();
752    }
753
754    /**
755     * Test match asterisk beginning
756     */

757    public void testMatchAsteriskBeginning() throws Exception JavaDoc
758    {
759       new QueryTEST(
760          new MBean[]
761          {
762             new MBean(new StringTest("endell"), "Domain1:type=instance1"),
763             new MBean(new StringTest("ell"), "Domain1:type=instance2")
764          },
765          new MBean[]
766          {
767             new MBean(new StringTest("Hello"), "Domain2:type=instance1"),
768             new MBean(new StringTest("ellbeginning"), "Domain2:type=instance2"),
769             new MBean(new StringTest("Goodbye"), "Domain2:type=instance3"),
770             new MBean(new StringTest("el"), "Domain2:type=instance4"),
771             new MBean(new StringTest("ll"), "Domain2:type=instance5"),
772             new MBean(new StringTest("e ll"), "Domain2:type=instance6"),
773             new MBean(new StringTest("ELL"), "Domain2:type=instance7"),
774             new MBean(new StringTest("Ell"), "Domain2:type=instance8")
775          },
776          Query.match(Query.attr("String"), Query.value("*ell"))
777       ).test();
778    }
779
780    /**
781     * Test match asterisk end
782     */

783    public void testMatchAsteriskEnd() throws Exception JavaDoc
784    {
785       new QueryTEST(
786          new MBean[]
787          {
788             new MBean(new StringTest("ellbeginning"), "Domain1:type=instance1"),
789             new MBean(new StringTest("ell"), "Domain1:type=instance2")
790          },
791          new MBean[]
792          {
793             new MBean(new StringTest("Hello"), "Domain2:type=instance1"),
794             new MBean(new StringTest("beginningell"), "Domain2:type=instance2"),
795             new MBean(new StringTest("Goodbye"), "Domain2:type=instance3"),
796             new MBean(new StringTest("el"), "Domain2:type=instance4"),
797             new MBean(new StringTest("ll"), "Domain2:type=instance5"),
798             new MBean(new StringTest("e ll"), "Domain2:type=instance6"),
799             new MBean(new StringTest("ELL"), "Domain2:type=instance7"),
800             new MBean(new StringTest("Ell"), "Domain2:type=instance8")
801          },
802          Query.match(Query.attr("String"), Query.value("ell*"))
803       ).test();
804    }
805
806    /**
807     * Test any match asterisk beginning and end
808     */

809    public void testMatchAsteriskBeginningAndEnd() throws Exception JavaDoc
810    {
811       new QueryTEST(
812          new MBean[]
813          {
814             new MBean(new StringTest("Hello"), "Domain1:type=instance1"),
815             new MBean(new StringTest("ell"), "Domain1:type=instance2"),
816             new MBean(new StringTest("endell"), "Domain1:type=instance3"),
817             new MBean(new StringTest("beginningell"), "Domain1:type=instance4")
818          },
819          new MBean[]
820          {
821             new MBean(new StringTest("Goodbye"), "Domain2:type=instance1"),
822             new MBean(new StringTest("el"), "Domain2:type=instance2"),
823             new MBean(new StringTest("ll"), "Domain2:type=instance3"),
824             new MBean(new StringTest("e ll"), "Domain2:type=instance4"),
825             new MBean(new StringTest("ELL"), "Domain2:type=instance5"),
826             new MBean(new StringTest("Ell"), "Domain2:type=instance6")
827          },
828          Query.match(Query.attr("String"), Query.value("*ell*"))
829       ).test();
830    }
831
832    /**
833     * Test match asterisk embedded
834     */

835    public void testMatchAsteriskEmbedded() throws Exception JavaDoc
836    {
837       new QueryTEST(
838          new MBean[]
839          {
840             new MBean(new StringTest("Hello"), "Domain1:type=instance1"),
841          },
842          new MBean[]
843          {
844             new MBean(new StringTest("ell"), "Domain2:type=instance1"),
845             new MBean(new StringTest("endell"), "Domain2:type=instance2"),
846             new MBean(new StringTest("beginningell"), "Domain2:type=instance3"),
847             new MBean(new StringTest("Goodbye"), "Domain2:type=instance4"),
848             new MBean(new StringTest("el"), "Domain2:type=instance5"),
849             new MBean(new StringTest("ll"), "Domain2:type=instance6"),
850             new MBean(new StringTest("e ll"), "Domain4:type=instance7"),
851             new MBean(new StringTest("ELL"), "Domain2:type=instance8"),
852             new MBean(new StringTest("Ell"), "Domain2:type=instance9")
853          },
854          Query.match(Query.attr("String"), Query.value("H*o"))
855       ).test();
856    }
857
858    /**
859     * Test match question beginning
860     */

861    public void testMatchQuestionBeginning() throws Exception JavaDoc
862    {
863       new QueryTEST(
864          new MBean[]
865          {
866             new MBean(new StringTest("Hello"), "Domain1:type=instance1"),
867             new MBean(new StringTest("hello"), "Domain1:type=instance2"),
868             new MBean(new StringTest(" ello"), "Domain1:type=instance3")
869          },
870          new MBean[]
871          {
872             new MBean(new StringTest("ello"), "Domain2:type=instance1"),
873             new MBean(new StringTest("Ello"), "Domain2:type=instance2"),
874             new MBean(new StringTest("hhello"), "Domain2:type=instance3"),
875          },
876          Query.match(Query.attr("String"), Query.value("?ello"))
877       ).test();
878    }
879
880    /**
881     * Test match question end
882     */

883    public void testMatchQuestionEnd() throws Exception JavaDoc
884    {
885       new QueryTEST(
886          new MBean[]
887          {
888             new MBean(new StringTest("Hello"), "Domain1:type=instance1"),
889             new MBean(new StringTest("HellO"), "Domain1:type=instance2"),
890             new MBean(new StringTest("Hell "), "Domain1:type=instance3")
891          },
892          new MBean[]
893          {
894             new MBean(new StringTest("hell"), "Domain2:type=instance1"),
895             new MBean(new StringTest("helL"), "Domain2:type=instance2"),
896             new MBean(new StringTest("Helloo"), "Domain2:type=instance3"),
897          },
898          Query.match(Query.attr("String"), Query.value("Hell?"))
899       ).test();
900    }
901
902    /**
903     * Test match question beginning and end
904     */

905    public void testMatchQuestionBeginningEnd() throws Exception JavaDoc
906    {
907       new QueryTEST(
908          new MBean[]
909          {
910             new MBean(new StringTest("Hello"), "Domain1:type=instance1"),
911             new MBean(new StringTest("HellO"), "Domain1:type=instance2"),
912             new MBean(new StringTest("hello"), "Domain1:type=instance3"),
913             new MBean(new StringTest("hellO"), "Domain1:type=instance4"),
914             new MBean(new StringTest(" ell "), "Domain1:type=instance5")
915          },
916          new MBean[]
917          {
918             new MBean(new StringTest("hell"), "Domain2:type=instance1"),
919             new MBean(new StringTest("helL"), "Domain2:type=instance2"),
920             new MBean(new StringTest("ello"), "Domain2:type=instance3"),
921             new MBean(new StringTest("Ello"), "Domain2:type=instance4"),
922             new MBean(new StringTest("ell"), "Domain2:type=instance5"),
923             new MBean(new StringTest("Helloo"), "Domain2:type=instance6"),
924             new MBean(new StringTest("HHello"), "Domain2:type=instance7"),
925             new MBean(new StringTest("HHelloo"), "Domain2:type=instance8"),
926          },
927          Query.match(Query.attr("String"), Query.value("?ell?"))
928       ).test();
929    }
930
931    /**
932     * Test match question embedded
933     */

934    public void testMatchQuestionEmbedded() throws Exception JavaDoc
935    {
936       new QueryTEST(
937          new MBean[]
938          {
939             new MBean(new StringTest("Hello"), "Domain1:type=instance1"),
940             new MBean(new StringTest("HeLlo"), "Domain1:type=instance2"),
941             new MBean(new StringTest("He lo"), "Domain1:type=instance3")
942          },
943          new MBean[]
944          {
945             new MBean(new StringTest("hell"), "Domain2:type=instance1"),
946             new MBean(new StringTest("ello"), "Domain2:type=instance2"),
947             new MBean(new StringTest("ell"), "Domain2:type=instance3"),
948             new MBean(new StringTest("Helloo"), "Domain2:type=instance4"),
949             new MBean(new StringTest("HHello"), "Domain2:type=instance5"),
950             new MBean(new StringTest("HHelloo"), "Domain2:type=instance6"),
951          },
952          Query.match(Query.attr("String"), Query.value("He?lo"))
953       ).test();
954    }
955
956    /**
957     * Test match character set
958     */

959    public void testMatchCharacterSet() throws Exception JavaDoc
960    {
961       try
962       {
963          new QueryTEST(
964             new MBean[]
965             {
966                new MBean(new StringTest("Hello"), "Domain1:type=instance1"),
967                new MBean(new StringTest("HeLlo"), "Domain1:type=instance2"),
968             },
969             new MBean[]
970             {
971                new MBean(new StringTest("hell"), "Domain2:type=instance1"),
972                new MBean(new StringTest("ello"), "Domain2:type=instance2"),
973                new MBean(new StringTest("ell"), "Domain2:type=instance3"),
974                new MBean(new StringTest("Helloo"), "Domain2:type=instance4"),
975                new MBean(new StringTest("HHello"), "Domain2:type=instance5"),
976                new MBean(new StringTest("HHelloo"), "Domain2:type=instance6"),
977             },
978             Query.match(Query.attr("String"), Query.value("He[lL]lo"))
979          ).test();
980       }
981       catch (AssertionFailedError e)
982       {
983           fail("FAILS IN RI: expected Hello to match He[lL]lo");
984       }
985    }
986
987    /**
988     * Test match character range
989     */

990    public void testMatchCharacterRange() throws Exception JavaDoc
991    {
992       try
993       {
994         new QueryTEST(
995             new MBean[]
996             {
997                new MBean(new StringTest("Hello"), "Domain1:type=instance1"),
998                new MBean(new StringTest("Hemlo"), "Domain1:type=instance2"),
999             },
1000            new MBean[]
1001            {
1002               new MBean(new StringTest("hell"), "Domain2:type=instance1"),
1003               new MBean(new StringTest("He lo"), "Domain2:type=instance2"),
1004               new MBean(new StringTest("Heklo"), "Domain2:type=instance3"),
1005               new MBean(new StringTest("Henlo"), "Domain2:type=instance4"),
1006               new MBean(new StringTest("HeLlo"), "Domain2:type=instance5"),
1007               new MBean(new StringTest("HeMlo"), "Domain2:type=instance6"),
1008            },
1009            Query.match(Query.attr("String"), Query.value("He[l-m]lo"))
1010         ).test();
1011      }
1012      catch (AssertionFailedError e)
1013      {
1014          fail("FAILS IN RI: didn't expected HeMlo to match He[l-m]lo");
1015      }
1016   }
1017
1018   /**
1019    * Test match escaping question mark
1020    */

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

1039   public void testEscapingAsterisk() throws Exception JavaDoc
1040   {
1041      new QueryTEST(
1042         new MBean[]
1043         {
1044            new MBean(new StringTest("Hello*"), "Domain1:type=instance1"),
1045         },
1046         new MBean[]
1047         {
1048            new MBean(new StringTest("Helloz"), "Domain2:type=instance1"),
1049         },
1050         Query.match(Query.attr("String"), Query.value("Hello\\*"))
1051      ).test();
1052   }
1053
1054   /**
1055    * Test match escaping open bracket
1056    */

1057   public void testEscapingOpenBracket() throws Exception JavaDoc
1058   {
1059      new QueryTEST(
1060         new MBean[]
1061         {
1062            new MBean(new StringTest("Hello[ab]"), "Domain1:type=instance1"),
1063         },
1064         new MBean[]
1065         {
1066            new MBean(new StringTest("Helloa"), "Domain2:type=instance1"),
1067            new MBean(new StringTest("Hello\\a"), "Domain2:type=instance2"),
1068         },
1069         Query.match(Query.attr("String"), Query.value("Hello\\[ab]"))
1070      ).test();
1071   }
1072
1073   /**
1074    * Test match minus in character set
1075    */

1076   public void testMinusInCharacterSet() throws Exception JavaDoc
1077   {
1078      try
1079      {
1080         new QueryTEST(
1081            new MBean[]
1082            {
1083               new MBean(new StringTest("Hello-"), "Domain1:type=instance1"),
1084            },
1085            new MBean[]
1086            {
1087               new MBean(new StringTest("Hello[ab-]"), "Domain2:type=instance1"),
1088            },
1089            Query.match(Query.attr("String"), Query.value("Hello[ab-]"))
1090         ).test();
1091      }
1092      catch (AssertionFailedError e)
1093      {
1094          fail("FAILS IN RI: expected Hello- to match Hello[ab-]");
1095      }
1096   }
1097
1098   /**
1099    * Test threading, tests that running the same query in multiple threads
1100    * works. This test might not catch a threading problem on every run.
1101    */

1102   public void testThreading() throws Exception JavaDoc
1103   {
1104      MBeanServer JavaDoc server1 = MBeanServerFactory.createMBeanServer("server1");
1105      MBeanServer JavaDoc server2 = MBeanServerFactory.createMBeanServer("server2");
1106      try
1107      {
1108         ObjectName JavaDoc name = new ObjectName JavaDoc("Domain1:type=instance1");
1109         NumberTest bean1 = new NumberTest(1);
1110         NumberTest bean2 = new NumberTest(2);
1111         server1.registerMBean(bean1, name);
1112         server2.registerMBean(bean2, name);
1113         QueryExp JavaDoc query = Query.eq(Query.attr("Number"), Query.value(2));
1114         QueryThread thread1 = new QueryThread(server1, query, 0);
1115         QueryThread thread2 = new QueryThread(server2, query, 1);
1116         thread1.start();
1117         thread2.start();
1118         thread1.join(10000);
1119         thread1.check();
1120         thread2.join(10000);
1121         thread2.check();
1122      }
1123      finally
1124      {
1125         MBeanServerFactory.releaseMBeanServer(server1);
1126         MBeanServerFactory.releaseMBeanServer(server2);
1127      }
1128   }
1129
1130   /**
1131    * Test pathological
1132    */

1133   public void testPathological() throws Exception JavaDoc
1134   {
1135      try
1136      {
1137         new QueryTEST(
1138            new MBean[]
1139            {
1140               new MBean(new StringTest("Hello(?:.)"), "Domain1:type=instance1"),
1141            },
1142            new MBean[]
1143            {
1144               new MBean(new StringTest("Hellox"), "Domain2:type=instance1"),
1145            },
1146            Query.match(Query.attr("String"), Query.value("Hello(?:.)"))
1147         ).test();
1148      }
1149      catch (AssertionFailedError e)
1150      {
1151          fail("FAILS IN JBossMX: expected Hello(?:.) to match Hello(?:.)");
1152      }
1153   }
1154
1155   // Support ----------------------------------------------------------------
1156

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