1 22 package org.jboss.test.jmx.compliance.monitor; 23 24 import java.io.ByteArrayInputStream ; 25 import java.io.ByteArrayOutputStream ; 26 import java.io.ObjectInputStream ; 27 import java.io.ObjectOutputStream ; 28 import java.util.ArrayList ; 29 import java.util.HashSet ; 30 import java.util.Iterator ; 31 32 import javax.management.Attribute ; 33 import javax.management.MBeanNotificationInfo ; 34 import javax.management.MBeanServer ; 35 import javax.management.MBeanServerFactory ; 36 import javax.management.Notification ; 37 import javax.management.NotificationListener ; 38 import javax.management.ObjectName ; 39 import javax.management.monitor.CounterMonitor ; 40 import javax.management.monitor.GaugeMonitor ; 41 import javax.management.monitor.Monitor ; 42 import javax.management.monitor.MonitorNotification ; 43 import javax.management.monitor.StringMonitor ; 44 45 import junit.framework.AssertionFailedError; 46 import junit.framework.TestCase; 47 48 import org.jboss.test.jmx.compliance.monitor.support.CounterSupport; 49 import org.jboss.test.jmx.compliance.monitor.support.GaugeSupport; 50 import org.jboss.test.jmx.compliance.monitor.support.MonitorSupport; 51 import org.jboss.test.jmx.compliance.monitor.support.StringSupport; 52 53 58 public class MonitorTestCase 59 extends TestCase 60 implements NotificationListener 61 { 62 64 66 69 MBeanServer server; 70 71 74 ObjectName monitorName; 75 76 79 Monitor monitor; 80 81 84 ObjectName monitoredName; 85 86 89 MonitorSupport monitored; 90 91 94 ArrayList notifications = new ArrayList (); 95 96 98 101 public MonitorTestCase(String s) 102 { 103 super(s); 104 } 105 106 108 111 public void testNotificationTypes() 112 { 113 assertEquals(MonitorNotification.OBSERVED_ATTRIBUTE_ERROR, 114 "jmx.monitor.error.attribute"); 115 assertEquals(MonitorNotification.OBSERVED_ATTRIBUTE_TYPE_ERROR, 116 "jmx.monitor.error.type"); 117 assertEquals(MonitorNotification.OBSERVED_OBJECT_ERROR, 118 "jmx.monitor.error.mbean"); 119 assertEquals(MonitorNotification.RUNTIME_ERROR, 120 "jmx.monitor.error.runtime"); 121 assertEquals(MonitorNotification.STRING_TO_COMPARE_VALUE_DIFFERED, 122 "jmx.monitor.string.differs"); 123 assertEquals(MonitorNotification.STRING_TO_COMPARE_VALUE_MATCHED, 124 "jmx.monitor.string.matches"); 125 assertEquals(MonitorNotification.THRESHOLD_ERROR, 126 "jmx.monitor.error.threshold"); 127 assertEquals(MonitorNotification.THRESHOLD_HIGH_VALUE_EXCEEDED, 128 "jmx.monitor.gauge.high"); 129 assertEquals(MonitorNotification.THRESHOLD_LOW_VALUE_EXCEEDED, 130 "jmx.monitor.gauge.low"); 131 assertEquals(MonitorNotification.THRESHOLD_VALUE_EXCEEDED, 132 "jmx.monitor.counter.threshold"); 133 } 134 135 137 140 public void testCounterNotificationInfo() 141 throws Exception 142 { 143 HashSet expected = new HashSet (); 144 expected.add(MonitorNotification.OBSERVED_ATTRIBUTE_ERROR); 145 expected.add(MonitorNotification.OBSERVED_ATTRIBUTE_TYPE_ERROR); 146 expected.add(MonitorNotification.OBSERVED_OBJECT_ERROR); 147 expected.add(MonitorNotification.RUNTIME_ERROR); 148 expected.add(MonitorNotification.THRESHOLD_ERROR); 149 expected.add(MonitorNotification.THRESHOLD_VALUE_EXCEEDED); 150 151 MBeanNotificationInfo [] mbni = new CounterMonitor ().getNotificationInfo(); 152 checkNotificationInfo("Counter", mbni, expected); 153 } 154 155 157 160 public void testNormalCounterThresholdExceededEarlyNoOffsetNoModulus() 161 throws Exception 162 { 163 initTest(); 164 try 165 { 166 initCounterMonitor(true, new Integer (10), 167 false, new Integer (0), new Integer (0)); 168 expectStartMonitor(new Integer (10), 169 MonitorNotification.THRESHOLD_VALUE_EXCEEDED); 170 } 171 finally 172 { 173 stopMonitor(); 174 MBeanServerFactory.releaseMBeanServer(server); 175 } 176 } 177 178 181 public void testNormalCounterThresholdExceededLateNoOffsetNoModulus() 182 throws Exception 183 { 184 initTest(); 185 try 186 { 187 initCounterMonitor(true, new Integer (10), 188 false, new Integer (0), new Integer (0)); 189 dontExpectStartMonitor(new Integer (0)); 190 expect(new Integer (10), 191 MonitorNotification.THRESHOLD_VALUE_EXCEEDED); 192 } 193 finally 194 { 195 stopMonitor(); 196 MBeanServerFactory.releaseMBeanServer(server); 197 } 198 } 199 200 203 public void testNormalCounterThresholdExceededManyNoOffsetNoModulus() 204 throws Exception 205 { 206 initTest(); 207 try 208 { 209 initCounterMonitor(true, new Integer (10), 210 false, new Integer (0), new Integer (0)); 211 dontExpectStartMonitor(new Integer (0)); 212 expect(new Integer (10), 213 MonitorNotification.THRESHOLD_VALUE_EXCEEDED); 214 dontExpect(new Integer (0)); 215 expect(new Integer (11), 216 MonitorNotification.THRESHOLD_VALUE_EXCEEDED); 217 } 218 finally 219 { 220 stopMonitor(); 221 MBeanServerFactory.releaseMBeanServer(server); 222 } 223 } 224 225 228 public void testNormalCounterThresholdNotExceededNoOffsetNoModulus() 229 throws Exception 230 { 231 initTest(); 232 try 233 { 234 initCounterMonitor(true, new Integer (10), 235 false, new Integer (0), new Integer (0)); 236 dontExpectStartMonitor(new Integer (0)); 237 dontExpect(new Integer (1)); 238 dontExpect(new Integer (-1)); 239 dontExpect(new Integer (9)); 240 expect(new Integer (10), 241 MonitorNotification.THRESHOLD_VALUE_EXCEEDED); 242 dontExpect(new Integer (10)); 243 dontExpect(new Integer (11)); 244 dontExpect(new Integer (9)); 245 expect(new Integer (10), 246 MonitorNotification.THRESHOLD_VALUE_EXCEEDED); 247 dontExpect(new Integer (9)); 248 } 249 finally 250 { 251 stopMonitor(); 252 MBeanServerFactory.releaseMBeanServer(server); 253 } 254 } 255 256 259 public void testNormalCounterThresholdExceededNoneNoOffsetNoModulus() 260 throws Exception 261 { 262 initTest(); 263 try 264 { 265 initCounterMonitor(false, new Integer (10), 266 false, new Integer (0), new Integer (0)); 267 dontExpectStartMonitor(new Integer (0)); 268 dontExpect(new Integer (10)); 269 dontExpect(new Integer (0)); 270 dontExpect(new Integer (10)); 271 } 272 finally 273 { 274 stopMonitor(); 275 MBeanServerFactory.releaseMBeanServer(server); 276 } 277 } 278 279 281 284 public void testNormalCounterThresholdExceededEarlyOffsetNoModulus() 285 throws Exception 286 { 287 initTest(); 288 try 289 { 290 initCounterMonitor(true, new Integer (10), 291 false, new Integer (10), new Integer (0)); 292 expectStartMonitor(new Integer (10), 293 MonitorNotification.THRESHOLD_VALUE_EXCEEDED); 294 } 295 finally 296 { 297 stopMonitor(); 298 MBeanServerFactory.releaseMBeanServer(server); 299 } 300 } 301 302 305 public void testNormalCounterThresholdExceededLateOffsetNoModulus() 306 throws Exception 307 { 308 initTest(); 309 try 310 { 311 initCounterMonitor(true, new Integer (10), 312 false, new Integer (10), new Integer (0)); 313 dontExpectStartMonitor(new Integer (0)); 314 expect(new Integer (10), 315 MonitorNotification.THRESHOLD_VALUE_EXCEEDED); 316 } 317 finally 318 { 319 stopMonitor(); 320 MBeanServerFactory.releaseMBeanServer(server); 321 } 322 } 323 324 327 public void testNormalCounterThresholdExceededManyOffsetNoModulus() 328 throws Exception 329 { 330 initTest(); 331 try 332 { 333 initCounterMonitor(true, new Integer (10), 334 false, new Integer (10), new Integer (0)); 335 dontExpectStartMonitor(new Integer (0)); 336 expect(new Integer (10), 337 MonitorNotification.THRESHOLD_VALUE_EXCEEDED); 338 dontExpect(new Integer (0)); 339 dontExpect(new Integer (11)); 340 expect(new Integer (20), 341 MonitorNotification.THRESHOLD_VALUE_EXCEEDED); 342 dontExpect(new Integer (29)); 343 expect(new Integer (30), 344 MonitorNotification.THRESHOLD_VALUE_EXCEEDED); 345 expect(new Integer (40), 346 MonitorNotification.THRESHOLD_VALUE_EXCEEDED); 347 } 348 finally 349 { 350 stopMonitor(); 351 MBeanServerFactory.releaseMBeanServer(server); 352 } 353 } 354 355 358 public void testNormalCounterThresholdNotExceededOffsetNoModulus() 359 throws Exception 360 { 361 initTest(); 362 try 363 { 364 initCounterMonitor(true, new Integer (10), 365 false, new Integer (10), new Integer (0)); 366 dontExpectStartMonitor(new Integer (0)); 367 dontExpect(new Integer (1)); 368 dontExpect(new Integer (-1)); 369 dontExpect(new Integer (9)); 370 expect(new Integer (10), 371 MonitorNotification.THRESHOLD_VALUE_EXCEEDED); 372 dontExpect(new Integer (10)); 373 dontExpect(new Integer (11)); 374 dontExpect(new Integer (9)); 375 expect(new Integer (20), 376 MonitorNotification.THRESHOLD_VALUE_EXCEEDED); 377 dontExpect(new Integer (19)); 378 } 379 finally 380 { 381 stopMonitor(); 382 MBeanServerFactory.releaseMBeanServer(server); 383 } 384 } 385 386 389 public void testNormalCounterThresholdExceededNoneOffsetNoModulus() 390 throws Exception 391 { 392 initTest(); 393 try 394 { 395 initCounterMonitor(false, new Integer (10), 396 false, new Integer (10), new Integer (0)); 397 dontExpectStartMonitor(new Integer (0)); 398 dontExpect(new Integer (10)); 399 dontExpect(new Integer (0)); 400 dontExpect(new Integer (20)); 401 } 402 finally 403 { 404 stopMonitor(); 405 MBeanServerFactory.releaseMBeanServer(server); 406 } 407 } 408 409 411 414 public void testNormalCounterThresholdExceededEarlyNoOffsetModulus() 415 throws Exception 416 { 417 initTest(); 418 try 419 { 420 initCounterMonitor(true, new Integer (10), 421 false, new Integer (0), new Integer (10)); 422 expectStartMonitor(new Integer (10), 423 MonitorNotification.THRESHOLD_VALUE_EXCEEDED); 424 } 425 finally 426 { 427 stopMonitor(); 428 MBeanServerFactory.releaseMBeanServer(server); 429 } 430 } 431 432 435 public void testNormalCounterThresholdExceededLateNoOffsetModulus() 436 throws Exception 437 { 438 initTest(); 439 try 440 { 441 initCounterMonitor(true, new Integer (10), 442 false, new Integer (0), new Integer (10)); 443 dontExpectStartMonitor(new Integer (0)); 444 expect(new Integer (10), 445 MonitorNotification.THRESHOLD_VALUE_EXCEEDED); 446 } 447 finally 448 { 449 stopMonitor(); 450 MBeanServerFactory.releaseMBeanServer(server); 451 } 452 } 453 454 457 public void testNormalCounterThresholdExceededManyNoOffsetModulus() 458 throws Exception 459 { 460 initTest(); 461 try 462 { 463 initCounterMonitor(true, new Integer (10), 464 false, new Integer (0), new Integer (10)); 465 dontExpectStartMonitor(new Integer (0)); 466 expect(new Integer (10), 467 MonitorNotification.THRESHOLD_VALUE_EXCEEDED); 468 dontExpect(new Integer (0)); 469 expect(new Integer (11), 470 MonitorNotification.THRESHOLD_VALUE_EXCEEDED); 471 expect(new Integer (12), 472 MonitorNotification.THRESHOLD_VALUE_EXCEEDED); 473 dontExpect(new Integer (0)); 474 expect(new Integer (10), 475 MonitorNotification.THRESHOLD_VALUE_EXCEEDED); 476 } 477 catch (AssertionFailedError e) 478 { 479 fail("FAILS IN RI: Modulus ignored with no offset???"); 480 } 481 finally 482 { 483 stopMonitor(); 484 MBeanServerFactory.releaseMBeanServer(server); 485 } 486 } 487 488 491 public void testNormalCounterThresholdNotExceededNoOffsetModulus() 492 throws Exception 493 { 494 initTest(); 495 try 496 { 497 initCounterMonitor(true, new Integer (10), 498 false, new Integer (0), new Integer (10)); 499 dontExpectStartMonitor(new Integer (0)); 500 dontExpect(new Integer (1)); 501 dontExpect(new Integer (-1)); 502 dontExpect(new Integer (9)); 503 expect(new Integer (10), 504 MonitorNotification.THRESHOLD_VALUE_EXCEEDED); 505 dontExpect(new Integer (9)); 506 expect(new Integer (10), 507 MonitorNotification.THRESHOLD_VALUE_EXCEEDED); 508 dontExpect(new Integer (9)); 509 } 510 finally 511 { 512 stopMonitor(); 513 MBeanServerFactory.releaseMBeanServer(server); 514 } 515 } 516 517 520 public void testNormalCounterThresholdExceededNoneNoOffsetModulus() 521 throws Exception 522 { 523 initTest(); 524 try 525 { 526 initCounterMonitor(false, new Integer (10), 527 false, new Integer (0), new Integer (10)); 528 dontExpectStartMonitor(new Integer (0)); 529 dontExpect(new Integer (10)); 530 dontExpect(new Integer (0)); 531 dontExpect(new Integer (10)); 532 } 533 finally 534 { 535 stopMonitor(); 536 MBeanServerFactory.releaseMBeanServer(server); 537 } 538 } 539 540 542 545 public void testNormalCounterThresholdExceededEarlyOffsetModulus() 546 throws Exception 547 { 548 initTest(); 549 try 550 { 551 initCounterMonitor(true, new Integer (10), 552 false, new Integer (10), new Integer (10)); 553 expectStartMonitor(new Integer (10), 554 MonitorNotification.THRESHOLD_VALUE_EXCEEDED); 555 } 556 finally 557 { 558 stopMonitor(); 559 MBeanServerFactory.releaseMBeanServer(server); 560 } 561 } 562 563 566 public void testNormalCounterThresholdExceededLateOffsetModulus() 567 throws Exception 568 { 569 initTest(); 570 try 571 { 572 initCounterMonitor(true, new Integer (10), 573 false, new Integer (10), new Integer (10)); 574 dontExpectStartMonitor(new Integer (0)); 575 expect(new Integer (10), 576 MonitorNotification.THRESHOLD_VALUE_EXCEEDED); 577 } 578 finally 579 { 580 stopMonitor(); 581 MBeanServerFactory.releaseMBeanServer(server); 582 } 583 } 584 585 588 public void testNormalCounterThresholdExceededManyOffsetModulus() 589 throws Exception 590 { 591 initTest(); 592 try 593 { 594 initCounterMonitor(true, new Integer (10), 595 false, new Integer (10), new Integer (20)); 596 dontExpectStartMonitor(new Integer (0)); 597 expect(new Integer (10), 598 MonitorNotification.THRESHOLD_VALUE_EXCEEDED); 599 dontExpect(new Integer (0)); 600 dontExpect(new Integer (12)); 601 expect(new Integer (20), 602 MonitorNotification.THRESHOLD_VALUE_EXCEEDED); 603 if (((CounterMonitor )monitor).getThreshold().equals(new Integer (30))) 604 fail("FAILS IN RI: Threshold 10, Offset 10, Modulus 20 should " + 605 " never get a threshold of 30"); 606 expect(new Integer (10), 607 MonitorNotification.THRESHOLD_VALUE_EXCEEDED); 608 dontExpect(new Integer (10)); 609 expect(new Integer (20), 610 MonitorNotification.THRESHOLD_VALUE_EXCEEDED); 611 expect(new Integer (20), 612 MonitorNotification.THRESHOLD_VALUE_EXCEEDED); 613 expect(new Integer (30), 614 MonitorNotification.THRESHOLD_VALUE_EXCEEDED); 615 } 616 finally 617 { 618 stopMonitor(); 619 MBeanServerFactory.releaseMBeanServer(server); 620 } 621 } 622 623 626 public void testNormalCounterThresholdNotExceededOffsetModulus() 627 throws Exception 628 { 629 initTest(); 630 try 631 { 632 initCounterMonitor(true, new Integer (10), 633 false, new Integer (10), new Integer (20)); 634 dontExpectStartMonitor(new Integer (0)); 635 dontExpect(new Integer (1)); 636 dontExpect(new Integer (-1)); 637 dontExpect(new Integer (9)); 638 expect(new Integer (10), 639 MonitorNotification.THRESHOLD_VALUE_EXCEEDED); 640 dontExpect(new Integer (10)); 641 dontExpect(new Integer (11)); 642 dontExpect(new Integer (9)); 643 expect(new Integer (20), 644 MonitorNotification.THRESHOLD_VALUE_EXCEEDED); 645 if (((CounterMonitor )monitor).getThreshold().equals(new Integer (30))) 646 fail("FAILS IN RI: Threshold 10, Offset 10, Modulus 20 should " + 647 " never get a threshold of 30"); 648 expect(new Integer (19), 649 MonitorNotification.THRESHOLD_VALUE_EXCEEDED); 650 } 651 finally 652 { 653 stopMonitor(); 654 MBeanServerFactory.releaseMBeanServer(server); 655 } 656 } 657 658 661 public void testNormalCounterThresholdExceededNoneOffsetModulus() 662 throws Exception 663 { 664 initTest(); 665 try 666 { 667 initCounterMonitor(false, new Integer (10), 668 false, new Integer (10), new Integer (10)); 669 dontExpectStartMonitor(new Integer (0)); 670 dontExpect(new Integer (10)); 671 dontExpect(new Integer (0)); 672 dontExpect(new Integer (20)); 673 } 674 finally 675 { 676 stopMonitor(); 677 MBeanServerFactory.releaseMBeanServer(server); 678 } 679 } 680 681 683 686 public void testDiffCounterThresholdExceededNoOffsetNoModulus() 687 throws Exception 688 { 689 initTest(); 690 try 691 { 692 initCounterMonitor(true, new Integer (10), 693 true, new Integer (0), new Integer (0)); 694 dontExpectStartMonitor(new Integer (0)); 695 expect(new Integer (10), 696 MonitorNotification.THRESHOLD_VALUE_EXCEEDED); 697 } 698 finally 699 { 700 stopMonitor(); 701 MBeanServerFactory.releaseMBeanServer(server); 702 } 703 } 704 705 708 public void testDiffCounterThresholdExceededManyNoOffsetNoModulus() 709 throws Exception 710 { 711 initTest(); 712 try 713 { 714 initCounterMonitor(true, new Integer (10), 715 true, new Integer (0), new Integer (0)); 716 dontExpectStartMonitor(new Integer (0)); 717 expect(new Integer (10), 718 MonitorNotification.THRESHOLD_VALUE_EXCEEDED); 719 expectDiff(new Integer (0), new Integer (11), 720 MonitorNotification.THRESHOLD_VALUE_EXCEEDED); 721 } 722 finally 723 { 724 stopMonitor(); 725 MBeanServerFactory.releaseMBeanServer(server); 726 } 727 } 728 729 732 public void testDiffCounterThresholdNotExceededNoOffsetNoModulus() 733 throws Exception 734 { 735 initTest(); 736 try 737 { 738 initCounterMonitor(true, new Integer (10), 739 true, new Integer (0), new Integer (0)); 740 dontExpectStartMonitorDiff(new Integer (0), new Integer (1)); 741 dontExpectDiff(new Integer (0), new Integer (9)); 742 dontExpectDiff(new Integer (1), new Integer (10)); 743 dontExpectDiff(new Integer (9), new Integer (11)); 744 dontExpectDiff(new Integer (9), new Integer (0)); 745 } 746 finally 747 { 748 stopMonitor(); 749 MBeanServerFactory.releaseMBeanServer(server); 750 } 751 } 752 753 756 public void testDiffCounterThresholdExceededNoneNoOffsetNoModulus() 757 throws Exception 758 { 759 initTest(); 760 try 761 { 762 initCounterMonitor(false, new Integer (10), 763 true, new Integer (0), new Integer (0)); 764 dontExpectStartMonitorDiff(new Integer (0), new Integer (10)); 765 dontExpectDiff(new Integer (0), new Integer (-10)); 766 dontExpectDiff(new Integer (0), new Integer (100)); 767 } 768 finally 769 { 770 stopMonitor(); 771 MBeanServerFactory.releaseMBeanServer(server); 772 } 773 } 774 775 777 780 public void testDiffCounterThresholdExceededOffsetNoModulus() 781 throws Exception 782 { 783 initTest(); 784 try 785 { 786 initCounterMonitor(true, new Integer (10), 787 true, new Integer (10), new Integer (0)); 788 dontExpectStartMonitor(new Integer (0)); 789 expect(new Integer (10), 790 MonitorNotification.THRESHOLD_VALUE_EXCEEDED); 791 } 792 finally 793 { 794 stopMonitor(); 795 MBeanServerFactory.releaseMBeanServer(server); 796 } 797 } 798 799 802 public void testDiffCounterThresholdExceededManyOffsetNoModulus() 803 throws Exception 804 { 805 initTest(); 806 try 807 { 808 initCounterMonitor(true, new Integer (10), 809 true, new Integer (10), new Integer (0)); 810 dontExpectStartMonitor(new Integer (0)); 811 expect(new Integer (10), 812 MonitorNotification.THRESHOLD_VALUE_EXCEEDED); 813 expectDiff(new Integer (0), new Integer (10), 814 MonitorNotification.THRESHOLD_VALUE_EXCEEDED); 815 expectDiff(new Integer (10), new Integer (30), 816 MonitorNotification.THRESHOLD_VALUE_EXCEEDED); 817 expectDiff(new Integer (30), new Integer (60), 818 MonitorNotification.THRESHOLD_VALUE_EXCEEDED); 819 expectDiff(new Integer (60), new Integer (100), 820 MonitorNotification.THRESHOLD_VALUE_EXCEEDED); 821 expectDiff(new Integer (0), new Integer (10), 822 MonitorNotification.THRESHOLD_VALUE_EXCEEDED); 823 } 824 finally 825 { 826 stopMonitor(); 827 MBeanServerFactory.releaseMBeanServer(server); 828 } 829 } 830 831 834 public void testDiffCounterThresholdNotExceededOffsetNoModulus() 835 throws Exception 836 { 837 initTest(); 838 try 839 { 840 initCounterMonitor(true, new Integer (10), 841 true, new Integer (10), new Integer (0)); 842 dontExpectStartMonitorDiff(new Integer (0), new Integer (1)); 843 expectDiff(new Integer (0), new Integer (10), 844 MonitorNotification.THRESHOLD_VALUE_EXCEEDED); 845 dontExpectDiff(new Integer (10), new Integer (20)); 846 expectDiff(new Integer (20), new Integer (40), 847 MonitorNotification.THRESHOLD_VALUE_EXCEEDED); 848 dontExpectDiff(new Integer (40), new Integer (69)); 849 } 850 finally 851 { 852 stopMonitor(); 853 MBeanServerFactory.releaseMBeanServer(server); 854 } 855 } 856 857 860 public void testDiffCounterThresholdExceededNoneOffsetNoModulus() 861 throws Exception 862 { 863 initTest(); 864 try 865 { 866 initCounterMonitor(false, new Integer (10), 867 true, new Integer (10), new Integer (0)); 868 dontExpectStartMonitorDiff(new Integer (0), new Integer (1)); 869 dontExpectDiff(new Integer (0), new Integer (10)); 870 dontExpectDiff(new Integer (10), new Integer (20)); 871 dontExpectDiff(new Integer (20), new Integer (40)); 872 dontExpectDiff(new Integer (40), new Integer (69)); 873 } 874 finally 875 { 876 stopMonitor(); 877 MBeanServerFactory.releaseMBeanServer(server); 878 } 879 } 880 881 883 886 public void testDiffCounterThresholdExceededNoOffsetModulus() 887 throws Exception 888 { 889 initTest(); 890 try 891 { 892 initCounterMonitor(true, new Integer (10), 893 true, new Integer (0), new Integer (10)); 894 dontExpectStartMonitor(new Integer (0)); 895 expect(new Integer (10), 896 MonitorNotification.THRESHOLD_VALUE_EXCEEDED); 897 } 898 finally 899 { 900 stopMonitor(); 901 MBeanServerFactory.releaseMBeanServer(server); 902 } 903 } 904 905 908 public void testDiffCounterThresholdExceededManyNoOffsetModulus() 909 throws Exception 910 { 911 initTest(); 912 try 913 { 914 initCounterMonitor(true, new Integer (10), 915 true, new Integer (0), new Integer (10)); 916 dontExpectStartMonitor(new Integer (0)); 917 expect(new Integer (10), 918 MonitorNotification.THRESHOLD_VALUE_EXCEEDED); 919 expectDiff(new Integer (0), new Integer (11), 920 MonitorNotification.THRESHOLD_VALUE_EXCEEDED); 921 expectDiff(new Integer (12), new Integer (22), 922 MonitorNotification.THRESHOLD_VALUE_EXCEEDED); 923 expectDiff(new Integer (0), new Integer (10), 924 MonitorNotification.THRESHOLD_VALUE_EXCEEDED); 925 } 926 finally 927 { 928 stopMonitor(); 929 MBeanServerFactory.releaseMBeanServer(server); 930 } 931 } 932 933 936 public void testDiffCounterThresholdNotExceededNoOffsetModulus() 937 throws Exception 938 { 939 initTest(); 940 try 941 { 942 initCounterMonitor(true, new Integer (10), 943 true, new Integer (0), new Integer (10)); 944 dontExpectStartMonitorDiff(new Integer (0), new Integer (1)); 945 dontExpectDiff(new Integer (0), new Integer (9)); 946 dontExpectDiff(new Integer (11), new Integer (20)); 947 dontExpectDiffModulus(new Integer (10), new Integer (-3), new Integer (10)); 948 } 949 finally 950 { 951 stopMonitor(); 952 MBeanServerFactory.releaseMBeanServer(server); 953 } 954 } 955 956 959 public void testDiffCounterThresholdExceededNoneNoOffsetModulus() 960 throws Exception 961 { 962 initTest(); 963 try 964 { 965 initCounterMonitor(false, new Integer (10), 966 true, new Integer (0), new Integer (10)); 967 dontExpectStartMonitorDiff(new Integer (0), new Integer (10)); 968 dontExpectDiffModulus(new Integer (0), new Integer (-10), new Integer (10)); 969 dontExpectDiff(new Integer (0), new Integer (100)); 970 } 971 finally 972 { 973 stopMonitor(); 974 MBeanServerFactory.releaseMBeanServer(server); 975 } 976 } 977 978 980 983 public void testDiffCounterThresholdExceededOffsetModulus() 984 throws Exception 985 { 986 initTest(); 987 try 988 { 989 initCounterMonitor(true, new Integer (10), 990 true, new Integer (10), new Integer (10)); 991 dontExpectStartMonitor(new Integer (0)); 992 expect(new Integer (10), 993 MonitorNotification.THRESHOLD_VALUE_EXCEEDED); 994 } 995 finally 996 { 997 stopMonitor(); 998 MBeanServerFactory.releaseMBeanServer(server); 999 } 1000 } 1001 1002 1005 public void testDiffCounterThresholdExceededManyOffsetModulus() 1006 throws Exception 1007 { 1008 initTest(); 1009 try 1010 { 1011 initCounterMonitor(true, new Integer (10), 1012 true, new Integer (10), new Integer (20)); 1013 dontExpectStartMonitor(new Integer (0)); 1014 expectDiff(new Integer (0), new Integer (11), 1015 MonitorNotification.THRESHOLD_VALUE_EXCEEDED); 1016 expectDiff(new Integer (11), new Integer (31), 1017 MonitorNotification.THRESHOLD_VALUE_EXCEEDED); 1018 expectDiff(new Integer (0), new Integer (10), 1019 MonitorNotification.THRESHOLD_VALUE_EXCEEDED); 1020 } 1021 finally 1022 { 1023 stopMonitor(); 1024 MBeanServerFactory.releaseMBeanServer(server); 1025 } 1026 } 1027 1028 1031 public void testDiffCounterThresholdNotExceededOffsetModulus() 1032 throws Exception 1033 { 1034 initTest(); 1035 try 1036 { 1037 initCounterMonitor(true, new Integer (10), 1038 true, new Integer (10), new Integer (20)); 1039 dontExpectStartMonitorDiff(new Integer (0), new Integer (1)); 1040 dontExpectDiff(new Integer (1), new Integer (10)); 1041 dontExpectDiffModulus(new Integer (10), new Integer (-13), new Integer (20)); 1042 } 1043 finally 1044 { 1045 stopMonitor(); 1046 MBeanServerFactory.releaseMBeanServer(server); 1047 } 1048 } 1049 1050 1053 public void testDiffCounterThresholdExceededNoneOffsetModulus() 1054 throws Exception 1055 { 1056 initTest(); 1057 try 1058 { 1059 initCounterMonitor(false, new Integer (10), 1060 true, new Integer (10), new Integer (20)); 1061 dontExpectStartMonitorDiff(new Integer (0), new Integer (10)); 1062 dontExpectDiffModulus(new Integer (0), new Integer (-10), new Integer (20)); 1063 dontExpectDiff(new Integer (0), new Integer (100)); 1064 } 1065 finally 1066 { 1067 stopMonitor(); 1068 MBeanServerFactory.releaseMBeanServer(server); 1069 } 1070 } 1071 1072 1074 1077 public void testCounterInvalidAttribute() 1078 throws Exception 1079 { 1080 initTest(); 1081 try 1082 { 1083 initCounterMonitor(true, new Integer (10), 1084 false, new Integer (10), new Integer (10)); 1085 attributeErrorStartMonitor(new Integer (0)); 1086 } 1087 finally 1088 { 1089 stopMonitor(); 1090 MBeanServerFactory.releaseMBeanServer(server); 1091 } 1092 } 1093 1094 1097 public void testCounterInvalidAttributeNull() 1098 throws Exception 1099 { 1100 initTest(); 1101 try 1102 { 1103 initCounterMonitor(true, new Integer (10), 1104 false, new Integer (10), new Integer (10)); 1105 attributeNullStartMonitor(new Integer (0)); 1106 } 1107 finally 1108 { 1109 stopMonitor(); 1110 MBeanServerFactory.releaseMBeanServer(server); 1111 } 1112 } 1113 1114 1117 public void testCounterInvalidAttributeType() 1118 throws Exception 1119 { 1120 initTest(); 1121 try 1122 { 1123 initCounterMonitor(true, new Integer (10), 1124 false, new Integer (10), new Integer (10)); 1125 attributeTypeStartMonitor(new Integer (0)); 1126 } 1127 finally 1128 { 1129 stopMonitor(); 1130 MBeanServerFactory.releaseMBeanServer(server); 1131 } 1132 } 1133 1134 1137 public void testCounterWriteOnly() 1138 throws Exception 1139 { 1140 initTest(); 1141 try 1142 { 1143 initCounterMonitor(true, new Integer (10), 1144 false, new Integer (10), new Integer (10)); 1145 attributeWriteStartMonitor(new Integer (0)); 1146 } 1147 finally 1148 { 1149 stopMonitor(); 1150 MBeanServerFactory.releaseMBeanServer(server); 1151 } 1152 } 1153 1154 1157 public void testCounterInvalidObjectName() 1158 throws Exception 1159 { 1160 initTest(); 1161 try 1162 { 1163 initCounterMonitor(true, new Integer (10), 1164 false, new Integer (10), new Integer (10)); 1165 objectNameStartMonitor(new Integer (0)); 1166 } 1167 finally 1168 { 1169 stopMonitor(); 1170 MBeanServerFactory.releaseMBeanServer(server); 1171 } 1172 } 1173 1174 1177 public void testCounterInvalidThreshold() 1178 throws Exception 1179 { 1180 initTest(); 1181 try 1182 { 1183 initCounterMonitor(true, new Long (10), 1184 false, new Integer (10), new Integer (10)); 1185 objectNameStartMonitor(new Integer (0)); 1186 } 1187 finally 1188 { 1189 stopMonitor(); 1190 MBeanServerFactory.releaseMBeanServer(server); 1191 } 1192 } 1193 1194 1197 public void testCounterInvalidOffset() 1198 throws Exception 1199 { 1200 initTest(); 1201 try 1202 { 1203 initCounterMonitor(true, new Integer (10), 1204 false, new Long (10), new Integer (10)); 1205 objectNameStartMonitor(new Integer (0)); 1206 } 1207 finally 1208 { 1209 stopMonitor(); 1210 MBeanServerFactory.releaseMBeanServer(server); 1211 } 1212 } 1213 1214 1217 public void testCounterInvalidModulus() 1218 throws Exception 1219 { 1220 initTest(); 1221 try 1222 { 1223 initCounterMonitor(true, new Integer (10), 1224 false, new Integer (10), new Long (10)); 1225 objectNameStartMonitor(new Integer (0)); 1226 } 1227 finally 1228 { 1229 stopMonitor(); 1230 MBeanServerFactory.releaseMBeanServer(server); 1231 } 1232 } 1233 1234 1237 public void testCounterRuntimeError() 1238 throws Exception 1239 { 1240 initTest(); 1241 try 1242 { 1243 initCounterMonitor(true, new Integer (10), 1244 false, new Integer (10), new Integer (10)); 1245 runtimeErrorStartMonitor(new Integer (0)); 1246 } 1247 finally 1248 { 1249 stopMonitor(); 1250 MBeanServerFactory.releaseMBeanServer(server); 1251 } 1252 } 1253 1254 1256 1259 public void testGaugeNotificationInfo() 1260 throws Exception 1261 { 1262 HashSet expected = new HashSet (); 1263 expected.add(MonitorNotification.OBSERVED_ATTRIBUTE_ERROR); 1264 expected.add(MonitorNotification.OBSERVED_ATTRIBUTE_TYPE_ERROR); 1265 expected.add(MonitorNotification.OBSERVED_OBJECT_ERROR); 1266 expected.add(MonitorNotification.RUNTIME_ERROR); 1267 expected.add(MonitorNotification.THRESHOLD_ERROR); 1268 expected.add(MonitorNotification.THRESHOLD_HIGH_VALUE_EXCEEDED); 1269 expected.add(MonitorNotification.THRESHOLD_LOW_VALUE_EXCEEDED); 1270 1271 MBeanNotificationInfo [] mbni = new GaugeMonitor ().getNotificationInfo(); 1272 checkNotificationInfo("Gauge", mbni, expected); 1273 } 1274 1275 1277 1280 public void testGaugeInvalidAttribute() 1281 throws Exception 1282 { 1283 initTest(); 1284 try 1285 { 1286 initGaugeMonitor(true, true, new Integer (10), new Integer (0), false); 1287 attributeErrorStartMonitor(new Integer (0)); 1288 } 1289 finally 1290 { 1291 stopMonitor(); 1292 MBeanServerFactory.releaseMBeanServer(server); 1293 } 1294 } 1295 1296 1299 public void testGaugeInvalidAttributeNull() 1300 throws Exception 1301 { 1302 initTest(); 1303 try 1304 { 1305 initGaugeMonitor(true, true, new Integer (10), new Integer (0), false); 1306 attributeNullStartMonitor(new Integer (0)); 1307 } 1308 finally 1309 { 1310 stopMonitor(); 1311 MBeanServerFactory.releaseMBeanServer(server); 1312 } 1313 } 1314 1315 1318 public void testGaugeInvalidAttributeType() 1319 throws Exception 1320 { 1321 initTest(); 1322 try 1323 { 1324 initGaugeMonitor(true, true, new Integer (10), new Integer (0), false); 1325 attributeTypeStartMonitor(new Integer (0)); 1326 } 1327 finally 1328 { 1329 stopMonitor(); 1330 MBeanServerFactory.releaseMBeanServer(server); 1331 } 1332 } 1333 1334 1337 public void testGaugeWriteOnly() 1338 throws Exception 1339 { 1340 initTest(); 1341 try 1342 { 1343 initGaugeMonitor(true, true, new Integer (10), new Integer (0), false); 1344 attributeWriteStartMonitor(new Integer (0)); 1345 } 1346 finally 1347 { 1348 stopMonitor(); 1349 MBeanServerFactory.releaseMBeanServer(server); 1350 } 1351 } 1352 1353 1356 public void testGaugeInvalidObjectName() 1357 throws Exception 1358 { 1359 initTest(); 1360 try 1361 { 1362 initGaugeMonitor(true, true, new Integer (10), new Integer (0), false); 1363 objectNameStartMonitor(new Integer (0)); 1364 } 1365 finally 1366 { 1367 stopMonitor(); 1368 MBeanServerFactory.releaseMBeanServer(server); 1369 } 1370 } 1371 1372 1375 public void testGaugeInvalidThreshold() 1376 throws Exception 1377 { 1378 initTest(); 1379 try 1380 { 1381 initGaugeMonitor(true, true, new Long (10), new Long (0), false); 1382 objectNameStartMonitor(new Integer (0)); 1383 } 1384 finally 1385 { 1386 stopMonitor(); 1387 MBeanServerFactory.releaseMBeanServer(server); 1388 } 1389 } 1390 1391 1394 public void testGaugeRuntimeError() 1395 throws Exception 1396 { 1397 initTest(); 1398 try 1399 { 1400 initGaugeMonitor(true, true, new Integer (10), new Integer (0), false); 1401 runtimeErrorStartMonitor(new Integer (0)); 1402 } 1403 finally 1404 { 1405 stopMonitor(); 1406 MBeanServerFactory.releaseMBeanServer(server); 1407 } 1408 } 1409 1410 1412 1415 public void testStringNotificationInfo() 1416 throws Exception 1417 { 1418 HashSet expected = new HashSet (); 1419 expected.add(MonitorNotification.OBSERVED_ATTRIBUTE_ERROR); 1420 expected.add(MonitorNotification.OBSERVED_ATTRIBUTE_TYPE_ERROR); 1421 expected.add(MonitorNotification.OBSERVED_OBJECT_ERROR); 1422 expected.add(MonitorNotification.RUNTIME_ERROR); 1423 expected.add(MonitorNotification.STRING_TO_COMPARE_VALUE_DIFFERED); 1424 expected.add(MonitorNotification.STRING_TO_COMPARE_VALUE_MATCHED); 1425 1426 MBeanNotificationInfo [] mbni = new StringMonitor ().getNotificationInfo(); 1427 checkNotificationInfo("String", mbni, expected); 1428 } 1429 1430 1432 1435 public void testStringDifferEarly() 1436 throws Exception 1437 { 1438 initTest(); 1439 try 1440 { 1441 initStringMonitor(true, false, "Hello"); 1442 expectStartMonitor("Goodbye", 1443 MonitorNotification.STRING_TO_COMPARE_VALUE_DIFFERED); 1444 } 1445 finally 1446 { 1447 stopMonitor(); 1448 MBeanServerFactory.releaseMBeanServer(server); 1449 } 1450 } 1451 1452 1455 public void testStringDifferLate() 1456 throws Exception 1457 { 1458 initTest(); 1459 try 1460 { 1461 initStringMonitor(true, false, "Hello"); 1462 dontExpectStartMonitor("Hello"); 1463 expect("Goodbye", 1464 MonitorNotification.STRING_TO_COMPARE_VALUE_DIFFERED); 1465 } 1466 finally 1467 { 1468 stopMonitor(); 1469 MBeanServerFactory.releaseMBeanServer(server); 1470 } 1471 } 1472 1473 1476 public void testStringDifferManyEarly() 1477 throws Exception 1478 { 1479 initTest(); 1480 try 1481 { 1482 initStringMonitor(true, false, "Hello"); 1483 expectStartMonitor("Goodbye", 1484 MonitorNotification.STRING_TO_COMPARE_VALUE_DIFFERED); 1485 dontExpect("Hello"); 1486 expect("Goodbye", 1487 MonitorNotification.STRING_TO_COMPARE_VALUE_DIFFERED); 1488 dontExpect("Hello"); 1489 expect("Goodbye", 1490 MonitorNotification.STRING_TO_COMPARE_VALUE_DIFFERED); 1491 dontExpect("Hello"); 1492 } 1493 finally 1494 { 1495 stopMonitor(); 1496 MBeanServerFactory.releaseMBeanServer(server); 1497 } 1498 } 1499 1500 1503 public void testStringDifferManyLate() 1504 throws Exception 1505 { 1506 initTest(); 1507 try 1508 { 1509 initStringMonitor(true, false, "Hello"); 1510 dontExpectStartMonitor("Hello"); 1511 expect("Goodbye", 1512 MonitorNotification.STRING_TO_COMPARE_VALUE_DIFFERED); 1513 dontExpect("Hello"); 1514 expect("Goodbye", 1515 MonitorNotification.STRING_TO_COMPARE_VALUE_DIFFERED); 1516 dontExpect("Hello"); 1517 expect("Goodbye", 1518 MonitorNotification.STRING_TO_COMPARE_VALUE_DIFFERED); 1519 dontExpect("Hello"); 1520 } 1521 finally 1522 { 1523 stopMonitor(); 1524 MBeanServerFactory.releaseMBeanServer(server); 1525 } 1526 } 1527 1528 1531 public void testStringMatchEarly() 1532 throws Exception 1533 { 1534 initTest(); 1535 try 1536 { 1537 initStringMonitor(false, true, "Hello"); 1538 expectStartMonitor("Hello", 1539 MonitorNotification.STRING_TO_COMPARE_VALUE_MATCHED); 1540 } 1541 finally 1542 { 1543 stopMonitor(); 1544 MBeanServerFactory.releaseMBeanServer(server); 1545 } 1546 } 1547 1548 1551 public void testStringMatchLate() 1552 throws Exception 1553 { 1554 initTest(); 1555 try 1556 { 1557 initStringMonitor(false, true, "Hello"); 1558 dontExpectStartMonitor("Goodbye"); 1559 expect("Hello", 1560 MonitorNotification.STRING_TO_COMPARE_VALUE_MATCHED); 1561 } 1562 finally 1563 { 1564 stopMonitor(); 1565 MBeanServerFactory.releaseMBeanServer(server); 1566 } 1567 } 1568 1569 1572 public void testStringMatchManyEarly() 1573 throws Exception 1574 { 1575 initTest(); 1576 try 1577 { 1578 initStringMonitor(false, true, "Hello"); 1579 expectStartMonitor("Hello", 1580 MonitorNotification.STRING_TO_COMPARE_VALUE_MATCHED); 1581 dontExpect("Goodbye"); 1582 expect("Hello", 1583 MonitorNotification.STRING_TO_COMPARE_VALUE_MATCHED); 1584 dontExpect("Goodbye"); 1585 expect("Hello", 1586 MonitorNotification.STRING_TO_COMPARE_VALUE_MATCHED); 1587 } 1588 finally 1589 { 1590 stopMonitor(); 1591 MBeanServerFactory.releaseMBeanServer(server); 1592 } 1593 } 1594 1595 1598 public void testStringMatchManyLate() 1599 throws Exception 1600 { 1601 initTest(); 1602 try 1603 { 1604 initStringMonitor(false, true, "Hello"); 1605 dontExpectStartMonitor("Goodbye"); 1606 expect("Hello", 1607 MonitorNotification.STRING_TO_COMPARE_VALUE_MATCHED); 1608 dontExpect("Goodbye"); 1609 expect("Hello", 1610 MonitorNotification.STRING_TO_COMPARE_VALUE_MATCHED); 1611 dontExpect("Goodbye"); 1612 expect("Hello", 1613 MonitorNotification.STRING_TO_COMPARE_VALUE_MATCHED); 1614 } 1615 finally 1616 { 1617 stopMonitor(); 1618 MBeanServerFactory.releaseMBeanServer(server); 1619 } 1620 } 1621 1622 1625 public void testStringBoth() 1626 throws Exception 1627 { 1628 initTest(); 1629 try 1630 { 1631 initStringMonitor(true, true, "Hello"); 1632 expectStartMonitor("Goodbye", 1633 MonitorNotification.STRING_TO_COMPARE_VALUE_DIFFERED); 1634 expect("Hello", 1635 MonitorNotification.STRING_TO_COMPARE_VALUE_MATCHED); 1636 } 1637 finally 1638 { 1639 stopMonitor(); 1640 MBeanServerFactory.releaseMBeanServer(server); 1641 } 1642 } 1643 1644 1647 public void testStringBothMany() 1648 throws Exception 1649 { 1650 initTest(); 1651 try 1652 { 1653 initStringMonitor(true, true, "Hello"); 1654 expectStartMonitor("Goodbye", 1655 MonitorNotification.STRING_TO_COMPARE_VALUE_DIFFERED); 1656 expect("Hello", 1657 MonitorNotification.STRING_TO_COMPARE_VALUE_MATCHED); 1658 expect("Goodbye", 1659 MonitorNotification.STRING_TO_COMPARE_VALUE_DIFFERED); 1660 expect("Hello", 1661 MonitorNotification.STRING_TO_COMPARE_VALUE_MATCHED); 1662 expect("Goodbye", 1663 MonitorNotification.STRING_TO_COMPARE_VALUE_DIFFERED); 1664 } 1665 finally 1666 { 1667 stopMonitor(); 1668 MBeanServerFactory.releaseMBeanServer(server); 1669 } 1670 } 1671 1672 1675 public void testStringNever() 1676 throws Exception 1677 { 1678 initTest(); 1679 try 1680 { 1681 initStringMonitor(false, false, "Hello"); 1682 dontExpectStartMonitor("Goodbye"); 1683 dontExpect("Hello"); 1684 dontExpect("Goodbye"); 1685 } 1686 finally 1687 { 1688 stopMonitor(); 1689 MBeanServerFactory.releaseMBeanServer(server); 1690 } 1691 } 1692 1693 1695 1698 public void testStringInvalidAttribute() 1699 throws Exception 1700 { 1701 initTest(); 1702 try 1703 { 1704 initStringMonitor(true, true, "Hello"); 1705 attributeErrorStartMonitor("Goodbye"); 1706 } 1707 finally 1708 { 1709 stopMonitor(); 1710 MBeanServerFactory.releaseMBeanServer(server); 1711 } 1712 } 1713 1714 1717 public void testStringInvalidAttributeNull() 1718 throws Exception 1719 { 1720 initTest(); 1721 try 1722 { 1723 initStringMonitor(true, true, "Hello"); 1724 attributeNullStartMonitor("Goodbye"); 1725 } 1726 finally 1727 { 1728 stopMonitor(); 1729 MBeanServerFactory.releaseMBeanServer(server); 1730 } 1731 } 1732 1733 1736 public void testStringInvalidAttributeType() 1737 throws Exception 1738 { 1739 initTest(); 1740 try 1741 { 1742 initStringMonitor(true, true, "Hello"); 1743 attributeTypeStartMonitor("Goodbye"); 1744 } 1745 finally 1746 { 1747 stopMonitor(); 1748 MBeanServerFactory.releaseMBeanServer(server); 1749 } 1750 } 1751 1752 1755 public void testStringWriteOnly() 1756 throws Exception 1757 { 1758 initTest(); 1759 try 1760 { 1761 initStringMonitor(true, true, "Hello"); 1762 attributeWriteStartMonitor("Goodbye"); 1763 } 1764 finally 1765 { 1766 stopMonitor(); 1767 MBeanServerFactory.releaseMBeanServer(server); 1768 } 1769 } 1770 1771 1774 public void testStringInvalidObjectName() 1775 throws Exception 1776 { 1777 initTest(); 1778 try 1779 { 1780 initStringMonitor(true, true, "Hello"); 1781 objectNameStartMonitor("Goodbye"); 1782 } 1783 finally 1784 { 1785 stopMonitor(); 1786 MBeanServerFactory.releaseMBeanServer(server); 1787 } 1788 } 1789 1790 1793 public void testStringRuntimeError() 1794 throws Exception 1795 { 1796 initTest(); 1797 try 1798 { 1799 initStringMonitor(true, true, "Hello"); 1800 runtimeErrorStartMonitor("Goodbye"); 1801 } 1802 finally 1803 { 1804 stopMonitor(); 1805 MBeanServerFactory.releaseMBeanServer(server); 1806 } 1807 } 1808 1809 1811 1814 private void initCounterMonitor(boolean notify, Number threshold, 1815 boolean differenceMode, Number offset, Number modulus) 1816 { 1817 try 1818 { 1819 CounterMonitor counterMonitor = new CounterMonitor (); 1820 counterMonitor.setNotify(notify); 1821 counterMonitor.setThreshold(threshold); 1822 counterMonitor.setDifferenceMode(differenceMode); 1823 counterMonitor.setOffset(offset); 1824 counterMonitor.setModulus(modulus); 1825 CounterSupport support = new CounterSupport(); 1826 monitor = counterMonitor; 1827 monitored = support; 1828 initMonitor(); 1829 } 1830 catch (Exception e) 1831 { 1832 fail(e.toString()); 1833 } 1834 } 1835 1836 1839 private void initGaugeMonitor(boolean notifyHigh, boolean notifyLow, 1840 Number thresholdHigh, Number thresholdLow, boolean differenceMode) 1841 { 1842 try 1843 { 1844 GaugeMonitor gaugeMonitor = new GaugeMonitor (); 1845 gaugeMonitor.setNotifyHigh(notifyHigh); 1846 gaugeMonitor.setNotifyLow(notifyLow); 1847 gaugeMonitor.setThresholds(thresholdHigh, thresholdLow); 1848 gaugeMonitor.setDifferenceMode(differenceMode); 1849 GaugeSupport support = new GaugeSupport(); 1850 monitor = gaugeMonitor; 1851 monitored = support; 1852 initMonitor(); 1853 } 1854 catch (Exception e) 1855 { 1856 fail(e.toString()); 1857 } 1858 } 1859 1860 1863 private void initStringMonitor(boolean differ, boolean match, String compare) 1864 { 1865 try 1866 { 1867 StringMonitor stringMonitor = new StringMonitor (); 1868 stringMonitor.setNotifyDiffer(differ); 1869 stringMonitor.setNotifyMatch(match); 1870 stringMonitor.setStringToCompare(compare); 1871 StringSupport support = new StringSupport(); 1872 monitor = stringMonitor; 1873 monitored = support; 1874 initMonitor(); 1875 } 1876 catch (Exception e) 1877 { 1878 fail(e.toString()); 1879 } 1880 } 1881 1882 1885 private void initTest() 1886 { 1887 notifications.clear(); 1888 server = MBeanServerFactory.createMBeanServer(); 1889 } 1890 1891 1894 private void initMonitor() throws Exception 1895 { 1896 monitorName = new ObjectName ("test:type=monitor"); 1897 monitoredName = new ObjectName ("test:type=monitored"); 1898 monitor.setObservedObject(monitoredName); 1899 monitor.setObservedAttribute("Value"); 1900 monitor.setGranularityPeriod(MonitorSUITE.GRANULARITY_TIME); 1901 server.registerMBean(monitor, monitorName); 1902 server.registerMBean(monitored, monitoredName); 1903 server.addNotificationListener(monitorName, this, null, null); 1904 } 1905 1906 1909 private void stopMonitor() 1910 { 1911 if (monitor != null) 1912 { 1913 monitored.end(); 1914 monitor.stop(); 1915 } 1916 } 1917 1918 1921 public void handleNotification(Notification n, Object ignored) 1922 { 1923 synchronized(notifications) 1924 { 1925 notifications.add(n); 1926 notifications.notifyAll(); 1927 } 1928 } 1929 1930 1933 private void sync(boolean getWorks) 1934 throws Exception 1935 { 1936 if (getWorks) 1938 waitCycle(); 1939 else 1940 { 1941 synchronized(notifications) 1942 { 1943 notifications.wait(MonitorSUITE.MAX_WAIT); 1944 } 1945 } 1946 } 1947 1948 1951 private void attributeErrorStartMonitor(Object initial) 1952 throws Exception 1953 { 1954 assertEquals(0, notifications.size()); 1955 monitor.setObservedAttribute("rubbish"); 1956 monitor.start(); 1957 sync(false); 1958 assertEquals(1, notifications.size()); 1959 MonitorNotification n = (MonitorNotification ) notifications.get(0); 1960 notifications.clear(); 1961 assertEquals(monitorName, n.getSource()); 1962 assertEquals(monitoredName, n.getObservedObject()); 1963 assertEquals("rubbish", n.getObservedAttribute()); 1964 assertEquals(MonitorNotification.OBSERVED_ATTRIBUTE_ERROR, n.getType()); 1965 n = serializeDeserialize(n); 1966 assertEquals(monitorName, n.getSource()); 1967 assertEquals(monitoredName, n.getObservedObject()); 1968 assertEquals("rubbish", n.getObservedAttribute()); 1969 assertEquals(MonitorNotification.OBSERVED_ATTRIBUTE_ERROR, n.getType()); 1970 } 1971 1972 1975 private void attributeNullStartMonitor(Object initial) 1976 throws Exception 1977 { 1978 assertEquals(0, notifications.size()); 1979 monitor.setObservedAttribute("WrongNull"); 1980 monitor.start(); 1981 sync(false); 1982 assertEquals(1, notifications.size()); 1983 MonitorNotification n = (MonitorNotification ) notifications.get(0); 1984 notifications.clear(); 1985 assertEquals(monitorName, n.getSource()); 1986 assertEquals(monitoredName, n.getObservedObject()); 1987 assertEquals("WrongNull", n.getObservedAttribute()); 1988 assertEquals(MonitorNotification.OBSERVED_ATTRIBUTE_TYPE_ERROR, 1989 n.getType()); 1990 n = serializeDeserialize(n); 1991 assertEquals(monitorName, n.getSource()); 1992 assertEquals(monitoredName, n.getObservedObject()); 1993 assertEquals("WrongNull", n.getObservedAttribute()); 1994 assertEquals(MonitorNotification.OBSERVED_ATTRIBUTE_TYPE_ERROR, 1995 n.getType()); 1996 } 1997 1998 2001 private void attributeTypeStartMonitor(Object initial) 2002 throws Exception 2003 { 2004 assertEquals(0, notifications.size()); 2005 monitor.setObservedAttribute("WrongType"); 2006 monitor.start(); 2007 sync(false); 2008 assertEquals(1, notifications.size()); 2009 MonitorNotification n = (MonitorNotification ) notifications.get(0); 2010 notifications.clear(); 2011 assertEquals(monitorName, n.getSource()); 2012 assertEquals(monitoredName, n.getObservedObject()); 2013 assertEquals("WrongType", n.getObservedAttribute()); 2014 assertEquals(MonitorNotification.OBSERVED_ATTRIBUTE_TYPE_ERROR, 2015 n.getType()); 2016 n = serializeDeserialize(n); 2017 assertEquals(monitoredName, n.getObservedObject()); 2018 assertEquals("WrongType", n.getObservedAttribute()); 2019 assertEquals(MonitorNotification.OBSERVED_ATTRIBUTE_TYPE_ERROR, 2020 n.getType()); 2021 } 2022 2023 2026 private void attributeWriteStartMonitor(Object initial) 2027 throws Exception 2028 { 2029 assertEquals(0, notifications.size()); 2030 monitor.setObservedAttribute("WriteOnly"); 2031 monitor.start(); 2032 sync(false); 2033 assertEquals(1, notifications.size()); 2034 MonitorNotification n = (MonitorNotification ) notifications.get(0); 2035 notifications.clear(); 2036 assertEquals(monitorName, n.getSource()); 2037 assertEquals(monitoredName, n.getObservedObject()); 2038 assertEquals("WriteOnly", n.getObservedAttribute()); 2039 assertEquals(MonitorNotification.OBSERVED_ATTRIBUTE_ERROR, 2040 n.getType()); 2041 n = serializeDeserialize(n); 2042 assertEquals(monitorName, n.getSource()); 2043 assertEquals(monitoredName, n.getObservedObject()); 2044 assertEquals("WriteOnly", n.getObservedAttribute()); 2045 assertEquals(MonitorNotification.OBSERVED_ATTRIBUTE_ERROR, 2046 n.getType()); 2047 } 2048 2049 2052 private void objectNameStartMonitor(Object initial) 2053 throws Exception 2054 { 2055 assertEquals(0, notifications.size()); 2056 monitoredName = new ObjectName ("rubbish:type=pants"); 2057 monitor.setObservedObject(monitoredName); 2058 monitor.start(); 2059 sync(false); 2060 assertEquals(1, notifications.size()); 2061 MonitorNotification n = (MonitorNotification ) notifications.get(0); 2062 notifications.clear(); 2063 assertEquals(monitorName, n.getSource()); 2064 assertEquals(monitoredName, n.getObservedObject()); 2065 assertEquals("Value", n.getObservedAttribute()); 2066 assertEquals(MonitorNotification.OBSERVED_OBJECT_ERROR, 2067 n.getType()); 2068 n = serializeDeserialize(n); 2069 assertEquals(monitorName, n.getSource()); 2070 assertEquals(monitoredName, n.getObservedObject()); 2071 assertEquals("Value", n.getObservedAttribute()); 2072 assertEquals(MonitorNotification.OBSERVED_OBJECT_ERROR, 2073 n.getType()); 2074 } 2075 2076 2079 private void runtimeErrorStartMonitor(Object initial) 2080 throws Exception 2081 { 2082 assertEquals(0, notifications.size()); 2083 monitor.setObservedAttribute("WrongException"); 2084 monitor.start(); 2085 sync(false); 2086 if (notifications.size() != 1) 2087 fail ("FAILS IN RI: Does not notify of error thrown by getter"); 2088 assertEquals(1, notifications.size()); 2089 MonitorNotification n = (MonitorNotification ) notifications.get(0); 2090 notifications.clear(); 2091 assertEquals(monitorName, n.getSource()); 2092 assertEquals(monitoredName, n.getObservedObject()); 2093 assertEquals("WrongException", n.getObservedAttribute()); 2094 assertEquals(MonitorNotification.RUNTIME_ERROR, 2095 n.getType()); 2096 n = serializeDeserialize(n); 2097 assertEquals(monitorName, n.getSource()); 2098 assertEquals(monitoredName, n.getObservedObject()); 2099 assertEquals("WrongException", n.getObservedAttribute()); 2100 assertEquals(MonitorNotification.RUNTIME_ERROR, 2101 n.getType()); 2102 } 2103 2104 2107 private void expectStartMonitor(Object expected, String type) 2108 throws Exception 2109 { 2110 assertEquals(0, notifications.size()); 2111 monitor.start(); 2112 setValue(expected); 2113 2114 sync(true); 2116 2117 checkGauge(expected); 2119 assertEquals(1, notifications.size()); 2120 MonitorNotification n = (MonitorNotification ) notifications.get(0); 2121 notifications.clear(); 2122 assertEquals(monitorName, n.getSource()); 2123 assertEquals(monitoredName, n.getObservedObject()); 2124 assertEquals("Value", n.getObservedAttribute()); 2125 assertEquals(type, n.getType()); 2126 assertEquals(expected, n.getDerivedGauge()); 2127 n = serializeDeserialize(n); 2128 assertEquals(monitorName, n.getSource()); 2129 assertEquals(monitoredName, n.getObservedObject()); 2130 assertEquals("Value", n.getObservedAttribute()); 2131 assertEquals(type, n.getType()); 2132 assertEquals(expected, n.getDerivedGauge()); 2133 } 2134 2135 2138 private void dontExpectStartMonitor(Object initial) 2139 throws Exception 2140 { 2141 assertEquals(0, notifications.size()); 2142 monitor.start(); 2143 setValue(initial); 2144 waitCycle(); 2145 assertEquals(0, notifications.size()); 2146 checkGauge(initial); 2147 } 2148 2149 2152 private void dontExpectStartMonitorDiff(Object value1, Object value2) 2153 throws Exception 2154 { 2155 monitor.start(); 2156 setValue(value1); 2157 waitCycle(); 2158 assertEquals(0, notifications.size()); 2159 setValue(value2); 2160 waitCycle(); 2161 assertEquals(0, notifications.size()); 2162 checkGauge(sub(value2, value1)); 2163 } 2164 2165 private void setValue(Object value) 2166 throws Exception 2167 { 2168 monitored.lock("set"); 2169 server.setAttribute(monitoredName, new Attribute ("Value", value)); 2170 } 2171 2172 2175 private void expect(Object expected, Object gauge, String type) 2176 throws Exception 2177 { 2178 assertEquals(0, notifications.size()); 2179 setValue(expected); 2180 2181 sync(true); 2183 2184 checkGauge(gauge); 2186 assertEquals(1, notifications.size()); 2187 MonitorNotification n = (MonitorNotification ) notifications.get(0); 2188 notifications.clear(); 2189 assertEquals(monitorName, n.getSource()); 2190 assertEquals(monitoredName, n.getObservedObject()); 2191 assertEquals("Value", n.getObservedAttribute()); 2192 assertEquals(type, n.getType()); 2193 assertEquals(gauge, n.getDerivedGauge()); 2194 } 2195 2196 2199 private void expectDiff(Object value1, Object value2, String type) 2200 throws Exception 2201 { 2202 waitCycle(); 2203 notifications.clear(); 2204 setValue(value1); 2205 waitCycle(); 2206 assertEquals(0, notifications.size()); 2207 setValue(value2); 2208 sync(true); 2209 2210 checkGauge(sub(value2, value1)); 2212 assertEquals(1, notifications.size()); 2213 MonitorNotification n = (MonitorNotification ) notifications.get(0); 2214 notifications.clear(); 2215 assertEquals(monitorName, n.getSource()); 2216 assertEquals(monitoredName, n.getObservedObject()); 2217 assertEquals("Value", n.getObservedAttribute()); 2218 assertEquals(type, n.getType()); 2219 assertEquals(sub(value2, value1), n.getDerivedGauge()); 2220 } 2221 2222 2225 private void expect(Object expected, String type) 2226 throws Exception 2227 { 2228 expect(expected, expected, type); 2229 } 2230 2231 2234 private void dontExpect(Object unexpected) 2235 throws Exception 2236 { 2237 assertEquals(0, notifications.size()); 2238 setValue(unexpected); 2239 waitCycle(); 2240 assertEquals(0, notifications.size()); 2241 checkGauge(unexpected); 2242 } 2243 2244 2247 private void dontExpectDiff(Object value1, Object value2) 2248 throws Exception 2249 { 2250 waitCycle(); 2251 notifications.clear(); 2252 setValue(value1); 2253 waitCycle(); 2254 assertEquals(0, notifications.size()); 2255 setValue(value2); 2256 waitCycle(); 2257 assertEquals(0, notifications.size()); 2258 checkGauge(sub(value2, value1)); 2259 } 2260 2261 2264 private void dontExpectDiffModulus(Object value1, Object value2, Object value3) 2265 throws Exception 2266 { 2267 waitCycle(); 2268 notifications.clear(); 2269 setValue(value1); 2270 waitCycle(); 2271 assertEquals(0, notifications.size()); 2272 setValue(value2); 2273 waitCycle(); 2274 assertEquals(0, notifications.size()); 2275 checkGauge(add(sub(value2, value1), value3)); 2276 } 2277 2278 2281 private void checkGauge(Object gauge) 2282 throws Exception 2283 { 2284 if (monitor instanceof StringMonitor ) 2285 assertEquals(gauge, ((StringMonitor )monitor).getDerivedGauge()); 2286 else if (monitor instanceof CounterMonitor ) 2287 assertEquals(gauge, ((CounterMonitor )monitor).getDerivedGauge()); 2288 else if (monitor instanceof GaugeMonitor ) 2289 assertEquals(gauge, ((GaugeMonitor )monitor).getDerivedGauge()); 2290 else 2291 fail("You idiot!"); 2292 } 2293 2294 2297 private void waitCycle() 2298 { 2299 monitored.unlock("set"); 2300 monitored.lock("set"); 2301 } 2302 2303 2309 private Number add(Object value1, Object value2) 2310 { 2311 if (value1 instanceof Byte ) 2312 return new Byte ((byte) (((Byte )value1).byteValue() + ((Byte )value2).byteValue())); 2313 if (value1 instanceof Integer ) 2314 return new Integer (((Integer )value1).intValue() + ((Integer )value2).intValue()); 2315 if (value1 instanceof Short ) 2316 return new Short ((short) (((Short )value1).shortValue() + ((Short )value2).shortValue())); 2317 if (value1 instanceof Long ) 2318 return new Long (((Long )value1).longValue() + ((Long )value2).longValue()); 2319 fail("You idiot!"); 2320 return null; 2321 } 2322 2323 2329 private Number sub(Object value1, Object value2) 2330 { 2331 if (value1 instanceof Byte ) 2332 return new Byte ((byte) (((Byte )value1).byteValue() - ((Byte )value2).byteValue())); 2333 if (value1 instanceof Integer ) 2334 return new Integer (((Integer )value1).intValue() - ((Integer )value2).intValue()); 2335 if (value1 instanceof Short ) 2336 return new Short ((short) (((Short )value1).shortValue() - ((Short )value2).shortValue())); 2337 if (value1 instanceof Long ) 2338 return new Long (((Long )value1).longValue() - ((Long )value2).longValue()); 2339 fail("You idiot!"); 2340 return null; 2341 } 2342 2343 2349 private void checkNotificationInfo(String type, 2350 MBeanNotificationInfo [] mbni, 2351 HashSet expected) 2352 { 2353 String [] types = mbni[0].getNotifTypes(); 2354 for (int i = 0; i < types.length; i++) 2355 if (expected.remove(types[i]) == false) 2356 fail(type + ": didn't expect notification type " + types[i]); 2357 Iterator iterator = expected.iterator(); 2358 while (iterator.hasNext()) 2359 fail(type + ": expected notification type " + iterator.next()); 2360 } 2361 2362 2368 private MonitorNotification serializeDeserialize(MonitorNotification n) 2369 throws Exception 2370 { 2371 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 2373 ObjectOutputStream oos = new ObjectOutputStream (baos); 2374 oos.writeObject(n); 2375 2376 ByteArrayInputStream bais = new ByteArrayInputStream (baos.toByteArray()); 2378 ObjectInputStream ois = new ObjectInputStream (bais); 2379 return (MonitorNotification ) ois.readObject(); 2380 } 2381} 2382 | Popular Tags |