1 22 package org.jboss.test.jmx.compliance.query; 23 24 import java.util.HashSet ; 25 import java.util.Iterator ; 26 import java.util.Set ; 27 28 import javax.management.AttributeValueExp ; 29 import javax.management.MBeanServer ; 30 import javax.management.MBeanServerFactory ; 31 import javax.management.ObjectInstance ; 32 import javax.management.ObjectName ; 33 import javax.management.Query ; 34 import javax.management.QueryExp ; 35 import javax.management.ValueExp ; 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 50 public class QueryTestCase 51 extends TestCase 52 { 53 55 57 60 public QueryTestCase(String s) 61 { 62 super(s); 63 } 64 65 67 70 public void testBoolean() throws Exception 71 { 72 ValueExp one = Query.value(true); 73 ValueExp 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 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 [] 90 { 91 one, two, two 92 } 93 ) 94 ).test(); 95 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 [] 106 { 107 two, two, one 108 } 109 ) 110 ).test(); 111 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 [] 122 { 123 two, one, two 124 } 125 ) 126 ).test(); 127 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 [] 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 153 public void testDouble() throws Exception 154 { 155 ValueExp one = Query.value(10d); 156 ValueExp two = Query.value(20d); 157 ValueExp div = Query.value(2d); 158 ValueExp minus = Query.value(-10d); 159 ValueExp mult = Query.value(200d); 160 ValueExp 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 173 public void testDoubleObject() throws Exception 174 { 175 ValueExp one = Query.value(new Double (10d)); 176 ValueExp two = Query.value(new Double (20d)); 177 ValueExp div = Query.value(new Double (2d)); 178 ValueExp minus = Query.value(new Double (-10d)); 179 ValueExp mult = Query.value(new Double (200d)); 180 ValueExp plus = Query.value(new Double (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 (10d)), Query.attr("Number"), one, two); 186 attrTEST(new NumberTest(new Double (10d)), Query.attr(NumberTest.class.getName(), "Number"), one, two); 187 inTEST(one, two, div, minus, mult, plus); 188 } 189 190 193 public void testFloat() throws Exception 194 { 195 ValueExp one = Query.value(10f); 196 ValueExp two = Query.value(20f); 197 ValueExp div = Query.value(2f); 198 ValueExp minus = Query.value(-10f); 199 ValueExp mult = Query.value(200f); 200 ValueExp 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 213 public void testFloatObject() throws Exception 214 { 215 ValueExp one = Query.value(new Float (10f)); 216 ValueExp two = Query.value(new Float (20f)); 217 ValueExp div = Query.value(new Double (2f)); 218 ValueExp minus = Query.value(new Double (-10f)); 219 ValueExp mult = Query.value(new Double (200f)); 220 ValueExp plus = Query.value(new Double (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 (10f)), Query.attr("Number"), one, two); 226 attrTEST(new NumberTest(new Float (10f)), Query.attr(NumberTest.class.getName(), "Number"), one, two); 227 inTEST(one, two, div, minus, mult, plus); 228 } 229 230 233 public void testInteger() throws Exception 234 { 235 ValueExp one = Query.value(10); 236 ValueExp two = Query.value(20); 237 ValueExp div = Query.value(2); 238 ValueExp minus = Query.value(-10); 239 ValueExp mult = Query.value(200); 240 ValueExp 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 253 public void testIntegerObject() throws Exception 254 { 255 ValueExp one = Query.value(new Integer (10)); 256 ValueExp two = Query.value(new Integer (20)); 257 ValueExp div = Query.value(new Double (2)); 258 ValueExp minus = Query.value(new Double (-10)); 259 ValueExp mult = Query.value(new Double (200)); 260 ValueExp plus = Query.value(new Double (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 (10)), Query.attr("Number"), one, two); 266 attrTEST(new NumberTest(new Integer (10)), Query.attr(NumberTest.class.getName(), "Number"), one, two); 267 inTEST(one, two, div, minus, mult, plus); 268 } 269 270 273 public void testLong() throws Exception 274 { 275 ValueExp one = Query.value(10l); 276 ValueExp two = Query.value(20l); 277 ValueExp div = Query.value(2l); 278 ValueExp minus = Query.value(-10l); 279 ValueExp mult = Query.value(200l); 280 ValueExp 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 293 public void testLongObject() throws Exception 294 { 295 ValueExp one = Query.value(new Long (10l)); 296 ValueExp two = Query.value(new Long (20l)); 297 ValueExp div = Query.value(new Double (2l)); 298 ValueExp minus = Query.value(new Double (-10l)); 299 ValueExp mult = Query.value(new Double (200l)); 300 ValueExp plus = Query.value(new Double (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 (10l)), Query.attr("Number"), one, two); 306 attrTEST(new NumberTest(new Long (10l)), Query.attr(NumberTest.class.getName(), "Number"), one, two); 307 inTEST(one, two, div, minus, mult, plus); 308 } 309 310 313 public void testString() throws Exception 314 { 315 ValueExp one = Query.value("Hello"); 316 ValueExp two = Query.value("Goodbye"); 317 ValueExp cat = Query.value("HelloGoodbye"); 318 ValueExp three = Query.value("ZZZZZZ"); 319 ValueExp four = Query.value("Hi"); 320 ValueExp five = Query.value("See ya"); 321 ValueExp 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 334 public void testAndTrueBoth() throws Exception 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 359 public void testAndFalseFirst() throws Exception 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 384 public void testAndFalseSecond() throws Exception 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 409 public void testAndFalseBoth() throws Exception 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 434 public void testOrTrueBoth() throws Exception 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 459 public void testOrFalseFirst() throws Exception 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 484 public void testOrFalseSecond() throws Exception 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 509 public void testOrFalseBoth() throws Exception 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 534 public void testNot() throws Exception 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 555 public void testNotNot() throws Exception 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 576 public void testClassAttribute() throws Exception 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 597 public void testSimpleObjectName() throws Exception 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 ("Domain1:type=instance1") 609 ).test(); 610 } 611 612 615 public void testDomainPatternObjectName() throws Exception 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 ("*:type=instance1") 627 ).test(); 628 } 629 630 633 public void testPropertyPatternObjectName() throws Exception 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 ("Domain1:*") 646 ).test(); 647 } 648 649 652 public void testMultiplePropertyPatternObjectName() throws Exception 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 ("Domain1:extra=true,*") 664 ).test(); 665 } 666 667 670 public void testInvalidNamePassedToObjectName() throws Exception 671 { 672 if (new ObjectName ("*:*").apply(new ObjectName ("*:type=patternNotAllowedHere"))) 673 fail("Patterns should not be matched"); 674 } 675 676 679 public void testAnySubstring() throws Exception 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 705 public void testFinalSubstring() throws Exception 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 731 public void testInitialSubstring() throws Exception 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 757 public void testMatchAsteriskBeginning() throws Exception 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 783 public void testMatchAsteriskEnd() throws Exception 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 809 public void testMatchAsteriskBeginningAndEnd() throws Exception 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 835 public void testMatchAsteriskEmbedded() throws Exception 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 861 public void testMatchQuestionBeginning() throws Exception 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 883 public void testMatchQuestionEnd() throws Exception 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 905 public void testMatchQuestionBeginningEnd() throws Exception 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 934 public void testMatchQuestionEmbedded() throws Exception 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 959 public void testMatchCharacterSet() throws Exception 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 990 public void testMatchCharacterRange() throws Exception 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 1021 public void testEscapingQuestion() throws Exception 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 1039 public void testEscapingAsterisk() throws Exception 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 1057 public void testEscapingOpenBracket() throws Exception 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 1076 public void testMinusInCharacterSet() throws Exception 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 1102 public void testThreading() throws Exception 1103 { 1104 MBeanServer server1 = MBeanServerFactory.createMBeanServer("server1"); 1105 MBeanServer server2 = MBeanServerFactory.createMBeanServer("server2"); 1106 try 1107 { 1108 ObjectName name = new ObjectName ("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 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 1133 public void testPathological() throws Exception 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 1157 private void equalsTEST(ValueExp value1, ValueExp value2) 1158 throws Exception 1159 { 1160 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 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 value1, ValueExp value2, ValueExp div, 1187 ValueExp minus, ValueExp mult, ValueExp plus) 1188 throws Exception 1189 { 1190 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 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 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 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 value1, ValueExp value2, ValueExp cat) 1245 throws Exception 1246 { 1247 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 value1, ValueExp value2) 1263 throws Exception 1264 { 1265 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 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 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 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 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 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 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 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 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 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 value1, ValueExp value2, ValueExp value3) 1388 throws Exception 1389 { 1390 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 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 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 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 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 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 mbean, AttributeValueExp attr, ValueExp value1, ValueExp value2) 1465 throws Exception 1466 { 1467 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 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 value1, ValueExp value2, ValueExp value3, 1497 ValueExp value4, ValueExp value5, ValueExp value6) 1498 throws Exception 1499 { 1500 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 [] 1511 { 1512 value1, value2, value3, value4, value5, value6 1513 } 1514 ) 1515 ).test(); 1516 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 [] 1527 { 1528 value1, value2, value3, value4, value5, value6 1529 } 1530 ) 1531 ).test(); 1532 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 [] 1543 { 1544 value1, value2, value3, value4, value5, value6 1545 } 1546 ) 1547 ).test(); 1548 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 [] 1559 { 1560 value2, value3, value4, value5, value6 1561 } 1562 ) 1563 ).test(); 1564 } 1565 1566 private class MBean 1567 { 1568 public Object mbean; 1569 public ObjectName objectName; 1570 public MBean(Object object, String name) 1571 throws Exception 1572 { 1573 this.mbean = object; 1574 this.objectName = new ObjectName (name); 1575 } 1576 } 1577 1578 private class QueryTEST 1579 { 1580 HashSet expectedInstances = new HashSet (); 1581 HashSet expectedNames = new HashSet (); 1582 QueryExp queryExp; 1583 MBeanServer server; 1584 1585 public QueryTEST(MBean[] expected, MBean[] others, QueryExp queryExp) 1586 throws Exception 1587 { 1588 this.queryExp = queryExp; 1589 1590 server = MBeanServerFactory.createMBeanServer(); 1591 1592 for (int i = 0; i < expected.length; i++) 1593 { 1594 ObjectInstance 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 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 1619 { 1620 Set result = server.queryMBeans(null, queryExp); 1621 1622 Iterator iterator = result.iterator(); 1623 while (iterator.hasNext()) 1624 { 1625 ObjectInstance instance = (ObjectInstance ) iterator.next(); 1626 Iterator 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 instance = (ObjectInstance ) iterator.next(); 1647 fail("Expected instance " + instance.getObjectName() 1648 + "\nfor query " + queryExp); 1649 } 1650 } 1651 1652 public void testQueryNames() 1653 throws Exception 1654 { 1655 Set result = server.queryNames(null, queryExp); 1656 1657 Iterator iterator = result.iterator(); 1658 while (iterator.hasNext()) 1659 { 1660 ObjectName name = (ObjectName ) iterator.next(); 1661 Iterator 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 1689 { 1690 MBeanServer server; 1691 QueryExp query; 1692 int expected; 1693 int result; 1694 public QueryThread(MBeanServer server, QueryExp 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 |