1 22 package org.jboss.test.jmx.compliance.server; 23 24 import java.io.File ; 25 import java.net.URL ; 26 import java.util.Arrays ; 27 import java.util.List ; 28 29 import javax.management.Attribute ; 30 import javax.management.AttributeNotFoundException ; 31 import javax.management.InstanceNotFoundException ; 32 import javax.management.MBeanException ; 33 import javax.management.MBeanRegistrationException ; 34 import javax.management.MBeanServer ; 35 import javax.management.MBeanServerFactory ; 36 import javax.management.MBeanServerNotification ; 37 import javax.management.Notification ; 38 import javax.management.NotificationFilterSupport ; 39 import javax.management.NotificationListener ; 40 import javax.management.ObjectInstance ; 41 import javax.management.ObjectName ; 42 import javax.management.ReflectionException ; 43 import javax.management.RuntimeErrorException ; 44 import javax.management.RuntimeMBeanException ; 45 import javax.management.RuntimeOperationsException ; 46 import javax.management.loading.MLet ; 47 48 import junit.framework.AssertionFailedError; 49 import junit.framework.TestCase; 50 51 import org.jboss.test.jmx.compliance.server.support.BabarError; 52 import org.jboss.test.jmx.compliance.server.support.Base; 53 import org.jboss.test.jmx.compliance.server.support.BaseMBean; 54 import org.jboss.test.jmx.compliance.server.support.Broadcaster; 55 import org.jboss.test.jmx.compliance.server.support.Derived; 56 import org.jboss.test.jmx.compliance.server.support.Dynamic; 57 import org.jboss.test.jmx.compliance.server.support.ExceptionOnTheRun; 58 import org.jboss.test.jmx.compliance.server.support.LockedTest; 59 import org.jboss.test.jmx.compliance.server.support.LockedTest2; 60 import org.jboss.test.jmx.compliance.server.support.LockedTest3; 61 import org.jboss.test.jmx.compliance.server.support.MBeanListener; 62 import org.jboss.test.jmx.compliance.server.support.MyScreamingException; 63 import org.jboss.test.jmx.compliance.server.support.Test; 64 import org.jboss.test.jmx.compliance.server.support.Test2; 65 import org.jboss.test.jmx.compliance.server.support.Test3; 66 import org.jboss.test.jmx.compliance.server.support.Test4; 67 import org.jboss.test.jmx.compliance.server.support.Unrelated; 68 import org.jboss.test.jmx.compliance.server.support.UnrelatedMBean; 69 70 77 public class MBeanServerTEST 78 extends TestCase 79 { 80 URL location; 81 82 public MBeanServerTEST(String s) throws Exception 83 { 84 super(s); 85 86 location = getClass().getResource("/org/jboss/test/jmx/compliance/server/MBeanServerTEST.class"); 88 String jarPath = location.getPath(); 89 int i = jarPath.indexOf('!'); 90 if (i != -1) 91 { 92 jarPath = jarPath.substring(0, i); 93 location = new URL (jarPath); 94 location = new URL (location, "file:../"); 95 } 96 } 97 98 100 103 public void testInvokeWithPrimitiveBooleanReturn() throws Exception 104 { 105 MBeanServer server = MBeanServerFactory.newMBeanServer(); 106 ObjectName name = new ObjectName (":test=test"); 107 server.registerMBean(new Test(), name); 108 109 Boolean bool = (Boolean )server.invoke(name, "opWithPrimBooleanReturn", null, null); 110 111 assertTrue(bool.booleanValue() == true); 112 } 113 114 117 public void testInvokeWithPrimitiveLongArrayReturn() throws Exception 118 { 119 MBeanServer server = MBeanServerFactory.newMBeanServer(); 120 ObjectName name = new ObjectName (":test=test"); 121 server.registerMBean(new Test(), name); 122 123 long[] array = (long[])server.invoke(name, "opWithPrimLongArrayReturn", null, null); 124 125 assertTrue(array [0] == 1); 126 assertTrue(array [1] == 2); 127 assertTrue(array [2] == 3); 128 } 129 130 133 public void testInvokeWithLongArrayReturn() throws Exception 134 { 135 MBeanServer server = MBeanServerFactory.newMBeanServer(); 136 ObjectName name = new ObjectName (":test=test"); 137 server.registerMBean(new Test(), name); 138 139 Long [] array = (Long [])server.invoke(name, "opWithLongArrayReturn", null, null); 140 141 assertTrue(array [0].longValue() == 1); 142 assertTrue(array [1].longValue() == 2); 143 assertTrue(array [2].longValue() == 3); 144 } 145 146 149 public void testInvokeWithPrimitiveLongReturn() throws Exception 150 { 151 MBeanServer server = MBeanServerFactory.newMBeanServer(); 152 ObjectName name = new ObjectName (":test=test"); 153 server.registerMBean(new Test(), name); 154 155 Long l = (Long )server.invoke(name, "opWithPrimLongReturn", null, null); 156 157 assertTrue(l.longValue() == 1234567890123l); 158 } 159 160 163 public void testInvokeWithPrimitiveDoubleReturn() throws Exception 164 { 165 MBeanServer server = MBeanServerFactory.newMBeanServer(); 166 ObjectName name = new ObjectName (":test=test"); 167 server.registerMBean(new Test(), name); 168 169 Double d = (Double )server.invoke(name, "opWithPrimDoubleReturn", null, null); 170 171 assertTrue(d.doubleValue() == 0.1234567890123d); 172 } 173 174 177 public void testInvokeWithLongSignature() throws Exception 178 { 179 MBeanServer server = MBeanServerFactory.newMBeanServer(); 180 ObjectName name = new ObjectName (":test=test"); 181 server.registerMBean(new Test(), name); 182 183 server.invoke(name, "opWithLongSignature", 184 new Object [] { new Integer (1), new Integer (2), new Integer (3), new Integer (4), new Integer (5), 185 new Integer (6), new Integer (7), new Integer (8), new Integer (9), new Integer (10), 186 new Integer (11), new Integer (12), new Integer (13), new Integer (14), new Integer (15), 187 new Integer (16), new Integer (17), new Integer (18), new Integer (19), new Integer (20) }, 188 new String [] { "int", "int", "int", "int", "int", "int", "int", "int", "int", "int", 189 "int", "int", "int", "int", "int", "int", "int", "int", "int", "int" } 190 ); 191 } 192 193 197 public void testInvokeWithMixedSignature() throws Exception 198 { 199 MBeanServer server = MBeanServerFactory.newMBeanServer(); 200 ObjectName name = new ObjectName (":test=test"); 201 server.registerMBean(new Test(), name); 202 203 server.invoke(name, "opWithMixedSignature", 204 new Object [] { new Integer (1), new Double (2.2D), new Long (333L), new Boolean (true), new Byte ((byte)0x02), 205 new Short ((short)6), new long[]{7L, 8L}, new Long []{new Long (1L), new Long (2L)}, new Short ((short)9), new Byte ((byte)10), 206 new Long (11L), new Double (1.2D), new Integer (13), new Integer (14), new Integer (15), 207 new Integer (16), new Integer (17), new Integer (18), new Integer (19), new Integer (20) }, 208 new String [] { "int", "double", "long", "boolean", "byte", "short", "[J", "[Ljava.lang.Long;", "java.lang.Short", "java.lang.Byte", 209 "java.lang.Long", "java.lang.Double", "int", "int", "int", "int", "int", "int", "int", "int" } 210 ); 211 } 212 213 214 217 public void testInvokeWithNonExistantMBean() throws Exception 218 { 219 try 220 { 221 MBeanServer server = MBeanServerFactory.newMBeanServer(); 222 server.invoke(new ObjectName (":mbean=doesnotexist"), "noMethod", null, null); 223 224 fail("InstanceNotFoundException was not thrown from an invoke operation on a non-existant MBean."); 226 } 227 catch (InstanceNotFoundException e) 228 { 229 } 231 232 } 233 234 237 public void testInvokeWithBusinessException() throws Exception 238 { 239 try 240 { 241 MBeanServer server = MBeanServerFactory.newMBeanServer(); 242 ObjectName name = new ObjectName ("test:test=test"); 243 server.registerMBean(new Test(), name); 244 245 server.invoke(name, "operationWithException", null, null); 246 247 fail("MBeanException was not thrown."); 249 } 250 catch (MBeanException e) 251 { 252 assertTrue(e.getTargetException() instanceof MyScreamingException); 254 } 255 } 256 257 258 260 public void testGetAttributeWithNonExistingAttribute() throws Exception 261 { 262 try 263 { 264 MBeanServer server = MBeanServerFactory.newMBeanServer(); 265 server.getAttribute(new ObjectName ("JMImplementation:type=MBeanServerDelegate"), "Foo"); 266 267 fail("AttributeNotFoundexception was not thrown when invoking getAttribute() call on a non-existant attribute."); 269 } 270 catch (AttributeNotFoundException e) 271 { 272 } 274 } 275 276 public void testGetAttributeWithBusinessException() throws Exception 277 { 278 try 279 { 280 MBeanServer server = MBeanServerFactory.newMBeanServer(); 281 ObjectName name = new ObjectName ("test:test=test"); 282 server.registerMBean(new Test(), name); 283 284 server.getAttribute(name, "ThisWillScream"); 285 286 fail("Did not throw the screaming exception"); 288 } 289 catch (MBeanException e) 290 { 291 assertTrue(e.getTargetException() instanceof MyScreamingException); 293 } 294 } 295 296 public void testGetAttributeWithNonExistingMBean() throws Exception 297 { 298 try 299 { 300 MBeanServer server = MBeanServerFactory.newMBeanServer(); 301 ObjectName name = new ObjectName ("test:name=DoesNotExist"); 302 303 server.getAttribute(name, "Whatever"); 304 305 fail("InstanceNotFoundException was not thrown on a nonexistant MBean."); 307 } 308 catch (InstanceNotFoundException e) 309 { 310 } 312 } 313 314 public void testGetAttributeWithUncheckedException() throws Exception 315 { 316 try 317 { 318 MBeanServer server = MBeanServerFactory.newMBeanServer(); 319 ObjectName name = new ObjectName ("test:test=test"); 320 server.registerMBean(new Test(), name); 321 322 server.getAttribute(name, "ThrowUncheckedException"); 323 324 fail("RuntimeMBeanException was not thrown"); 326 } 327 catch (RuntimeMBeanException e) 328 { 329 assertTrue(e.getTargetException() instanceof ExceptionOnTheRun); 331 } 332 } 333 334 public void testGetAttributeWithError() throws Exception 335 { 336 try 337 { 338 MBeanServer server = MBeanServerFactory.newMBeanServer(); 339 ObjectName name = new ObjectName ("test:test=test"); 340 server.registerMBean(new Test(), name); 341 342 server.getAttribute(name, "Error"); 343 344 fail("Error was not thrown"); 346 } 347 catch (RuntimeErrorException e) 348 { 349 assertTrue(e.getTargetError() instanceof BabarError); 351 } 352 } 353 354 355 357 public void testSetAttributeWithNonExistingAttribute() throws Exception 358 { 359 try 360 { 361 MBeanServer server = MBeanServerFactory.newMBeanServer(); 362 server.setAttribute(new ObjectName ("JMImplementation:type=MBeanServerDelegate"), new Attribute ("Foo", "value")); 363 364 fail("AttributeNotFoundexception was not thrown when invoking getAttribute() call on a non-existant attribute."); 366 } 367 catch (AttributeNotFoundException e) 368 { 369 } 371 } 372 373 public void testSetAttributeWithBusinessException() throws Exception 374 { 375 try 376 { 377 MBeanServer server = MBeanServerFactory.newMBeanServer(); 378 ObjectName name = new ObjectName ("test:test=test"); 379 server.registerMBean(new Test(), name); 380 381 server.setAttribute(name, new Attribute ("ThisWillScream", "value")); 382 383 fail("Did not throw the screaming exception"); 385 } 386 catch (MBeanException e) 387 { 388 assertTrue(e.getTargetException() instanceof MyScreamingException); 390 } 391 } 392 393 public void testSetAttributeWithNonExistingMBean() throws Exception 394 { 395 try 396 { 397 MBeanServer server = MBeanServerFactory.newMBeanServer(); 398 ObjectName name = new ObjectName ("test:name=DoesNotExist"); 399 400 server.setAttribute(name, new Attribute ("Whatever", "nothing")); 401 402 fail("InstanceNotFoundException was not thrown on a nonexistant MBean."); 404 } 405 catch (InstanceNotFoundException e) 406 { 407 } 409 } 410 411 public void testSetAttributeWithUncheckedException() throws Exception 412 { 413 try 414 { 415 MBeanServer server = MBeanServerFactory.newMBeanServer(); 416 ObjectName name = new ObjectName ("test:test=test"); 417 server.registerMBean(new Test(), name); 418 419 server.setAttribute(name, new Attribute ("ThrowUncheckedException", "value")); 420 421 fail("RuntimeMBeanException was not thrown"); 423 } 424 catch (RuntimeMBeanException e) 425 { 426 assertTrue(e.getTargetException() instanceof ExceptionOnTheRun); 428 } 429 } 430 431 public void testSetAttributeWithError() throws Exception 432 { 433 try 434 { 435 MBeanServer server = MBeanServerFactory.newMBeanServer(); 436 ObjectName name = new ObjectName ("test:test=test"); 437 server.registerMBean(new Test(), name); 438 439 server.setAttribute(name, new Attribute ("Error", "value")); 440 441 fail("Error was not thrown"); 443 } 444 catch (RuntimeErrorException e) 445 { 446 assertTrue(e.getTargetError() instanceof BabarError); 448 } 449 } 450 451 452 454 457 public void testInstantiateWithDefaultConstructor() throws Exception 458 { 459 MBeanServer server = MBeanServerFactory.newMBeanServer(); 460 Object o = server.instantiate("org.jboss.test.jmx.compliance.server.support.Test"); 461 462 assertTrue(o instanceof org.jboss.test.jmx.compliance.server.support.Test); 463 } 464 465 469 public void testInstantiateWithDefaultConstructorAndApplicationException() throws Exception 470 { 471 try 472 { 473 MBeanServer server = MBeanServerFactory.newMBeanServer(); 474 server.instantiate("org.jboss.test.jmx.compliance.server.support.ConstructorTest"); 475 476 fail("Instantiate should have thrown an MBeanException."); 478 } 479 catch (MBeanException e) 480 { 481 } 483 } 484 485 489 public void testInstantiateWithDefaultConstructorAndRuntimeException() throws Exception 490 { 491 try 492 { 493 MBeanServer server = MBeanServerFactory.newMBeanServer(); 494 server.instantiate("org.jboss.test.jmx.compliance.server.support.ConstructorTest2"); 495 496 fail("Instantiate should have thrown a RuntimeMBeanException."); 498 } 499 catch (RuntimeMBeanException e) 500 { 501 } 503 } 504 505 509 public void testInstantiateWithDefaultConstructorAndError() throws Exception 510 { 511 try 512 { 513 MBeanServer server = MBeanServerFactory.newMBeanServer(); 514 server.instantiate("org.jboss.test.jmx.compliance.server.support.ConstructorTest3"); 515 516 fail("Instantiate should have thrown a RuntimeErrorException."); 518 } 519 catch (RuntimeErrorException e) 520 { 521 } 523 } 524 525 529 public void testInstantiateWithDefaultConstructorAndExceptionInInit() throws Exception 530 { 531 try 532 { 533 MBeanServer server = MBeanServerFactory.newMBeanServer(); 534 535 try 537 { 538 server.instantiate("org.jboss.test.jmx.compliance.server.support.ConstructorTest4"); 539 } 540 catch (ExceptionInInitializerError e) 541 { 542 fail("FAILS IN RI: MBeanServer fails to wrap an error or exception from a static initializer block correctly."); 549 } 550 551 fail("Instantiate should have thrown a RuntimeMBeanException."); 553 } 554 catch (RuntimeMBeanException e) 555 { 556 558 assertTrue(e.getTargetException() instanceof NullPointerException ); 559 } 560 } 561 562 566 public void testInstantiateWithDefaultConstructorAndErrorInInit() throws Exception 567 { 568 try 569 { 570 MBeanServer server = MBeanServerFactory.newMBeanServer(); 571 572 try 574 { 575 server.instantiate("org.jboss.test.jmx.compliance.server.support.ConstructorTest5"); 576 } 577 catch (BabarError e) 578 { 579 fail("FAILS IN RI: MBeanServer fails to wrap an error or exception from a static initializer block correctly."); 586 } 587 588 fail("Instantiate should have thrown a RuntimeErrorException."); 590 } 591 catch (RuntimeErrorException e) 592 { 593 595 assertTrue(e.getTargetError() instanceof org.jboss.test.jmx.compliance.server.support.BabarError); 596 } 597 } 598 599 602 public void testInstantiateWithDefaultConstructorAndUnknownClass() throws Exception 603 { 604 try 605 { 606 MBeanServer server = MBeanServerFactory.newMBeanServer(); 607 server.instantiate("foo.Bar"); 608 609 fail("Instantiate should have thrown a ReflectionException."); 611 } 612 catch (ReflectionException e) 613 { 614 assertTrue(e.getTargetException() instanceof ClassNotFoundException ); 616 } 617 } 618 619 622 public void testInstantiateWithMissingDefaultConstructor() throws Exception 623 { 624 try 625 { 626 MBeanServer server = MBeanServerFactory.newMBeanServer(); 627 server.instantiate("org.jboss.test.jmx.compliance.server.support.ConstructorTest6"); 628 629 fail("Instantiate should have thrown a ReflectionException."); 631 } 632 catch (ReflectionException e) 633 { 634 } 636 } 637 638 641 public void testInstantiateWithInaccessibleNoArgsConstructor() throws Exception 642 { 643 try 644 { 645 MBeanServer server = MBeanServerFactory.newMBeanServer(); 646 server.instantiate("org.jboss.test.jmx.compliance.server.support.ConstructorTest7"); 647 648 fail("Instantiate should have thrown a ReflectionException."); 650 } 651 catch (ReflectionException e) 652 { 653 } 655 } 656 657 661 public void testInstantiateWithNullClassName() throws Exception 662 { 663 try 664 { 665 MBeanServer server = MBeanServerFactory.newMBeanServer(); 666 server.instantiate(null); 667 668 fail("incorrect exception behavior"); 670 } 671 catch (RuntimeOperationsException e) 672 { 673 675 assertTrue(e.getTargetException() instanceof IllegalArgumentException ); 677 } 678 } 679 680 684 public void testInstantiateWithEmptyClassName() throws Exception 685 { 686 try 687 { 688 MBeanServer server = MBeanServerFactory.newMBeanServer(); 689 server.instantiate(""); 690 691 fail("incorrect exception/classloading behavior"); 693 } 694 catch (ReflectionException e) 695 { 696 698 assertTrue(e.getTargetException() instanceof ClassNotFoundException ); 700 } 701 } 702 703 707 public void testInstantiateWithNullClassName2() throws Exception 708 { 709 try 710 { 711 MBeanServer server = MBeanServerFactory.newMBeanServer(); 712 server.instantiate(null, null); 713 714 fail("incorrect exception behavior"); 716 } 717 catch (RuntimeOperationsException e) 718 { 719 721 assertTrue(e.getTargetException() instanceof IllegalArgumentException ); 723 } 724 } 725 726 730 public void testInstantiateWithEmptyClassName2() throws Exception 731 { 732 try 733 { 734 MBeanServer server = MBeanServerFactory.newMBeanServer(); 735 server.instantiate("", null); 736 737 fail("incorrect exception/classloading behavior"); 739 } 740 catch (ReflectionException e) 741 { 742 744 assertTrue(e.getTargetException() instanceof ClassNotFoundException ); 746 } 747 } 748 749 754 public void testInstantiateWithNullClassName3() throws Exception 755 { 756 try 757 { 758 MBeanServer server = MBeanServerFactory.newMBeanServer(); 759 server.instantiate(null, null, null); 760 761 fail("incorrect exception behavior"); 763 } 764 catch (RuntimeOperationsException e) 765 { 766 768 assertTrue(e.getTargetException() instanceof IllegalArgumentException ); 770 } 771 } 772 773 777 public void testInstantiateWithEmptyClassName3() throws Exception 778 { 779 try 780 { 781 MBeanServer server = MBeanServerFactory.newMBeanServer(); 782 server.instantiate("", null, null); 783 784 fail("incorrect exception/classloading behavior"); 786 } 787 catch (ReflectionException e) 788 { 789 791 assertTrue(e.getTargetException() instanceof ClassNotFoundException ); 793 } 794 } 795 796 801 public void testInstantiateWithNullClassName4() throws Exception 802 { 803 try 804 { 805 MBeanServer server = MBeanServerFactory.newMBeanServer(); 806 server.instantiate(null, null, null, null); 807 808 fail("incorrect exception behavior"); 810 } 811 catch (RuntimeOperationsException e) 812 { 813 815 assertTrue(e.getTargetException() instanceof IllegalArgumentException ); 817 } 818 } 819 820 824 public void testInstantiateWithEmptyClassName4() throws Exception 825 { 826 try 827 { 828 MBeanServer server = MBeanServerFactory.newMBeanServer(); 829 server.instantiate("", null, null, null); 830 831 fail("incorrect exception/classloading behavior"); 833 } 834 catch (ReflectionException e) 835 { 836 838 assertTrue(e.getTargetException() instanceof ClassNotFoundException ); 840 } 841 } 842 843 847 public void testInstantiateWithDefaultLoaderRepository() throws Exception 848 { 849 852 MBeanServer server = MBeanServerFactory.newMBeanServer(); 853 MLet mlet = new MLet (); 854 ObjectName name = new ObjectName (":test=test"); 855 856 try 858 { 859 mlet.addURL(new URL (location, "lib/jmxcompliance-Test.jar")); 860 server.registerMBean(mlet, name); 861 862 server.instantiate("org.jboss.test.jmx.compliance.server.support.AClass"); 863 } 864 finally 865 { 866 try 867 { 868 server.unregisterMBean(name); 869 } 870 catch (Exception ignored) {} 871 } 872 873 } 875 876 877 882 public void testInstantiateWithDefaultLoaderRepository2() throws Exception 883 { 884 try 885 { 886 889 MBeanServer server = MBeanServerFactory.newMBeanServer(); 890 MLet mlet = new MLet (); 891 892 mlet.addURL("file:./output/etc/test/compliance/server/Test.jar"); 894 896 server.instantiate("org.jboss.test.jmx.compliance.server.support.AClass"); 897 898 903 } 907 catch (ReflectionException e) 908 { 909 911 assertTrue(e.getTargetException() instanceof ClassNotFoundException ); 913 } 914 } 915 916 918 921 public void testRegisterNullObjectName() throws Exception 922 { 923 boolean caught = false; 924 try 925 { 926 MBeanServer server = MBeanServerFactory.newMBeanServer(); 927 server.registerMBean(new Test(), null); 928 } 929 catch (RuntimeOperationsException e) 930 { 931 if (e.getTargetException() instanceof IllegalArgumentException ) 932 caught = true; 933 else 934 fail("Wrong wrapped exception " + e.getTargetException()); 935 } 936 if (caught == false) 937 fail("Allowed to register with a null object name"); 938 } 939 940 943 public void testRegisterPatternObjectName() throws Exception 944 { 945 boolean caught = false; 946 try 947 { 948 MBeanServer server = MBeanServerFactory.newMBeanServer(); 949 server.registerMBean(new Test(), new ObjectName ("Domai?:type=test")); 950 } 951 catch (RuntimeOperationsException e) 952 { 953 if (e.getTargetException() instanceof IllegalArgumentException ) 954 caught = true; 955 else 956 fail("Wrong wrapped exception " + e.getTargetException()); 957 } 958 if (caught == false) 959 fail("Allowed to register with a pattern object name"); 960 } 961 962 965 public void testRegisterJMImplementationObjectName() throws Exception 966 { 967 boolean caught = false; 968 try 969 { 970 MBeanServer server = MBeanServerFactory.newMBeanServer(); 971 server.registerMBean(new Test(), new ObjectName ("JMImplementation:type=test")); 972 } 973 catch (RuntimeOperationsException e) 974 { 975 if (e.getTargetException() instanceof IllegalArgumentException ) 976 caught = true; 977 else 978 fail("Wrong wrapped exception " + e.getTargetException()); 979 } 980 if (caught == false) 981 fail("Allowed to register into JMImplementation"); 982 } 983 984 987 public void testRegisterJMImplementationDefaultDomainObjectName() throws Exception 988 { 989 boolean caught = false; 990 try 991 { 992 MBeanServer server = MBeanServerFactory.newMBeanServer("JMImplementation"); 993 server.registerMBean(new Test(), new ObjectName (":type=test")); 994 } 995 catch (RuntimeOperationsException e) 996 { 997 if (e.getTargetException() instanceof IllegalArgumentException ) 998 caught = true; 999 else 1000 fail("Wrong wrapped exception " + e.getTargetException()); 1001 } 1002 if (caught == false) 1003 fail("Allowed to register into JMImplementation"); 1004 } 1005 1006 1007 1010 public void testRegisterMBeanOnExceptionFromPreRegister() throws Exception 1011 { 1012 MBeanServer server = MBeanServerFactory.newMBeanServer(); 1013 ObjectName name = new ObjectName ("test:foo=bar"); 1014 1015 try 1016 { 1017 server.registerMBean(new Test2(), name); 1018 1019 fail("Test2 registered despite of throwing an exception from the preRegister() method."); 1021 } 1022 catch (MBeanRegistrationException e) 1023 { 1024 assertTrue(!server.isRegistered(name)); 1026 assertTrue(e.getTargetException() instanceof java.lang.RuntimeException ); 1027 } 1028 catch (RuntimeMBeanException e) 1029 { 1030 fail("FAILS IN RI: RuntimeMBeanException instead of MBeanRegistrationException?"); 1031 } 1032 } 1033 1034 1037 public void testRegisterMBeanOnExceptionFromPreRegister2() throws Exception 1038 { 1039 MBeanServer server = MBeanServerFactory.newMBeanServer(); 1040 ObjectName name = new ObjectName ("test:foo=bar"); 1041 1042 try 1043 { 1044 server.registerMBean(new Test3(), name); 1045 1046 fail("Test3 registered despite of throwin an exception from the preRegister() method"); 1048 } 1049 catch (MBeanRegistrationException e) 1050 { 1051 assertTrue(!server.isRegistered(name)); 1053 assertTrue(e.getTargetException() instanceof MyScreamingException); 1054 } 1055 } 1056 1057 1061 public void testRegisterMBeanOnExceptionFromPreRegister3() throws Exception 1062 { 1063 MBeanServer server = MBeanServerFactory.newMBeanServer(); 1064 ObjectName name = new ObjectName ("test:foo=bar"); 1065 1066 try 1067 { 1068 server.registerMBean(new Test4(), name); 1069 1070 fail("Test4 registered despite of throwing an exception from the preRegister() method."); 1072 } 1073 catch (MBeanRegistrationException e) 1074 { 1075 assertTrue(!server.isRegistered(name)); 1077 assertTrue(e.getTargetException() instanceof MyScreamingException); 1078 } 1079 } 1080 1081 1082 1084 1087 public void testUnregisterDelegate() throws Exception 1088 { 1089 boolean caught = false; 1090 try 1091 { 1092 MBeanServer server = MBeanServerFactory.newMBeanServer(); 1093 server.unregisterMBean(new ObjectName ("JMImplementation:type=MBeanServerDelegate")); 1094 } 1095 catch (RuntimeOperationsException e) 1099 { 1100 caught = true; 1101 } 1102 if (caught == false) 1103 fail("Allowed to unregister the delegate"); 1104 } 1105 1106 1109 public void testBasicUnregister() throws Exception 1110 { 1111 MBeanServer server = MBeanServerFactory.newMBeanServer(); 1112 ObjectName name = new ObjectName ("test:foo=bar"); 1113 1114 server.registerMBean(new Test(), name); 1115 server.unregisterMBean(name); 1116 } 1117 1118 1121 public void testUnregisterWithDefaultDomainName() throws Exception 1122 { 1123 try 1124 { 1125 MBeanServer server = MBeanServerFactory.newMBeanServer(); 1126 ObjectName name = new ObjectName (":foo=bar"); 1127 1128 server.registerMBean(new Test(), name); 1129 server.unregisterMBean(name); 1130 1131 } 1132 catch (InstanceNotFoundException e) 1133 { 1134 fail("FAILS IN RI: RI throws InstanceNotFoundException when an existing MBean is unregistered with an implicit default domain name."); 1137 } 1138 } 1139 1140 1143 public void testUnregisterWithObjectNameFromRegistration() throws Exception 1144 { 1145 try 1146 { 1147 MBeanServer server = MBeanServerFactory.newMBeanServer(); 1148 ObjectName name = new ObjectName (":foo=bar"); 1149 1150 ObjectInstance oi = server.registerMBean(new Test(), name); 1151 name = oi.getObjectName(); 1152 1153 server.unregisterMBean(name); 1154 1155 } 1156 catch (InstanceNotFoundException e) 1157 { 1158 fail("FAILS IN RI: RI throws InstanceNotFoundException when an existing MBean is unregistered with an implicit default domain name retrieved from the ObjectInstance returned at registration time."); 1161 } 1162 } 1163 1164 1168 public void testUnregisterMBeanOnExceptionFromPreDeregister() throws Exception 1169 { 1170 MBeanServer server = MBeanServerFactory.newMBeanServer(); 1171 ObjectName name = new ObjectName ("test:foo=bar"); 1172 1173 server.registerMBean(new LockedTest(), name); 1174 1175 try 1176 { 1177 server.unregisterMBean(name); 1178 1179 fail("LockedTest unregistered despite of throwing an exception from the preDeregister() method."); 1181 } 1182 catch (MBeanRegistrationException e) 1183 { 1184 assertTrue(server.isRegistered(name)); 1186 assertTrue(e.getTargetException() instanceof java.lang.RuntimeException ); 1187 } 1188 catch (RuntimeMBeanException e) 1189 { 1190 fail("FAILS IN RI: spec v1.0: any exception thrown from MBean's preDeregister() method should be wrapped in an MBeanRegistrationException by the agent."); 1193 } 1194 } 1195 1196 1200 public void testUnregisterMBeanOnExceptionFromPreDeregister2() throws Exception 1201 { 1202 MBeanServer server = MBeanServerFactory.newMBeanServer(); 1203 ObjectName name = new ObjectName ("test:foo=bar"); 1204 1205 server.registerMBean(new LockedTest2(), name); 1206 1207 try 1208 { 1209 1210 server.unregisterMBean(name); 1211 1212 fail("LockedTest2 unregistered despite of throwin an exception from the preDeregister() method"); 1214 } 1215 catch (MBeanRegistrationException e) 1216 { 1217 assertTrue(server.isRegistered(name)); 1219 assertTrue(e.getTargetException() instanceof MyScreamingException); 1220 } 1221 } 1222 1223 1228 public void testUnregisterMBeanOnExceptionFromPreDeregister3() throws Exception 1229 { 1230 MBeanServer server = MBeanServerFactory.newMBeanServer(); 1231 ObjectName name = new ObjectName ("test:foo=bar"); 1232 1233 server.registerMBean(new LockedTest3(), name); 1234 1235 try 1236 { 1237 server.unregisterMBean(name); 1238 1239 fail("LockedTest3 unregistered despite of throwing an exception from the preDeregister() method."); 1241 } 1242 catch (MBeanRegistrationException e) 1243 { 1244 assertTrue(server.isRegistered(name)); 1246 assertTrue(e.getTargetException() instanceof MyScreamingException); 1247 } 1248 } 1249 1250 1252 1255 public synchronized void testAddNotificationListenerToDelegate() throws Exception 1256 { 1257 MBeanServer server = MBeanServerFactory.newMBeanServer(); 1258 1259 class MyNotificationListener implements NotificationListener { 1260 1261 int notificationCount = 0; 1262 1263 public void handleNotification(Notification notification, Object handback) 1264 { 1265 try 1266 { 1267 notificationCount++; 1268 1269 assertTrue(handback instanceof String ); 1270 assertTrue(handback.equals("MyHandback")); 1271 assertTrue(notification.getSource().equals(new ObjectName ("JMImplementation:type=MBeanServerDelegate"))); 1272 } 1273 catch (Exception e) 1274 { 1275 fail("Unexpected error: " + e.toString()); 1276 } 1277 } 1278 } 1279 1280 MyNotificationListener listener = new MyNotificationListener(); 1281 1282 NotificationFilterSupport filter = new NotificationFilterSupport (); 1283 filter.enableType(MBeanServerNotification.REGISTRATION_NOTIFICATION); 1284 1285 server.addNotificationListener( 1286 new ObjectName ("JMImplementation:type=MBeanServerDelegate"), 1287 listener, filter, "MyHandback" 1288 ); 1289 1290 server.registerMBean(new Test(), new ObjectName (":foo=bar")); 1292 1293 assertTrue(listener.notificationCount == 1); 1294 } 1295 1296 1299 public synchronized void testAddMultipleListeners() 1300 throws Exception 1301 { 1302 MBeanServer server = MBeanServerFactory.newMBeanServer(); 1303 1304 class MyNotificationListener implements NotificationListener 1305 { 1306 Object handback; 1307 int result = 0; 1308 public MyNotificationListener(Object handback) 1309 { 1310 this.handback = handback; 1311 } 1312 public void handleNotification(Notification notification, Object handback) 1313 { 1314 result++; 1315 assertEquals(this.handback, handback); 1316 result++; 1317 } 1318 } 1319 1320 MyNotificationListener listener1 = new MyNotificationListener("handback1"); 1321 MyNotificationListener listener2 = new MyNotificationListener("handback2"); 1322 1323 server.addNotificationListener( 1324 new ObjectName ("JMImplementation:type=MBeanServerDelegate"), 1325 listener1, null, "handback1" 1326 ); 1327 server.addNotificationListener( 1328 new ObjectName ("JMImplementation:type=MBeanServerDelegate"), 1329 listener2, null, "handback2" 1330 ); 1331 1332 server.registerMBean(new Test(), new ObjectName (":foo=bar")); 1334 1335 assertTrue(listener1.result == 2); 1336 assertTrue(listener2.result == 2); 1337 } 1338 1339 1342 public synchronized void testAddListenerMultipleHandbacks() 1343 throws Exception 1344 { 1345 MBeanServer server = MBeanServerFactory.newMBeanServer(); 1346 1347 class MyNotificationListener implements NotificationListener 1348 { 1349 boolean result1 = false; 1350 boolean result2 = false; 1351 public void handleNotification(Notification notification, Object handback) 1352 { 1353 if (handback.equals("handback1")) 1354 result1 = true; 1355 else if (handback.equals("handback2")) 1356 result2 = true; 1357 else 1358 fail("Unexpected handback: " + handback); 1359 } 1360 } 1361 1362 MyNotificationListener listener = new MyNotificationListener(); 1363 1364 server.addNotificationListener( 1365 new ObjectName ("JMImplementation:type=MBeanServerDelegate"), 1366 listener, null, "handback1" 1367 ); 1368 server.addNotificationListener( 1369 new ObjectName ("JMImplementation:type=MBeanServerDelegate"), 1370 listener, null, "handback2" 1371 ); 1372 1373 server.registerMBean(new Test(), new ObjectName (":foo=bar")); 1375 1376 assertTrue(listener.result1); 1377 assertTrue(listener.result2); 1378 } 1379 1380 1383 public synchronized void testRemoveListener() 1384 throws Exception 1385 { 1386 MBeanServer server = MBeanServerFactory.newMBeanServer(); 1387 1388 class MyNotificationListener implements NotificationListener 1389 { 1390 Object handback; 1391 int result = 0; 1392 public MyNotificationListener(Object handback) 1393 { 1394 this.handback = handback; 1395 } 1396 public void handleNotification(Notification notification, Object handback) 1397 { 1398 result++; 1399 assertEquals(this.handback, handback); 1400 result++; 1401 } 1402 } 1403 1404 class MyOtherNotificationListener implements NotificationListener 1405 { 1406 boolean result1 = false; 1407 boolean result2 = false; 1408 public void handleNotification(Notification notification, Object handback) 1409 { 1410 if (handback.equals("handback1")) 1411 result1 = true; 1412 else if (handback.equals("handback2")) 1413 result2 = true; 1414 else 1415 fail("Unexpected handback: " + handback); 1416 } 1417 } 1418 1419 MyNotificationListener listener1 = new MyNotificationListener("handback1"); 1420 MyOtherNotificationListener listener2 = new MyOtherNotificationListener(); 1421 1422 server.addNotificationListener( 1423 new ObjectName ("JMImplementation:type=MBeanServerDelegate"), 1424 listener1, null, "handback1" 1425 ); 1426 server.addNotificationListener( 1427 new ObjectName ("JMImplementation:type=MBeanServerDelegate"), 1428 listener2, null, "handback2" 1429 ); 1430 server.addNotificationListener( 1431 new ObjectName ("JMImplementation:type=MBeanServerDelegate"), 1432 listener2, null, "handback3" 1433 ); 1434 server.removeNotificationListener( 1435 new ObjectName ("JMImplementation:type=MBeanServerDelegate"), 1436 listener2 1437 ); 1438 1439 server.registerMBean(new Test(), new ObjectName (":foo=bar")); 1441 1442 assertTrue(listener1.result == 2); 1443 assertTrue(listener2.result1 == false); 1444 assertTrue(listener2.result2 == false); 1445 } 1446 1447 1450 public synchronized void testRemoveTriplet() 1451 throws Exception 1452 { 1453 MBeanServer server = MBeanServerFactory.newMBeanServer(); 1454 1455 class MyNotificationListener implements NotificationListener 1456 { 1457 Object handback; 1458 int result = 0; 1459 public MyNotificationListener(Object handback) 1460 { 1461 this.handback = handback; 1462 } 1463 public void handleNotification(Notification notification, Object handback) 1464 { 1465 result++; 1466 assertEquals(this.handback, handback); 1467 result++; 1468 } 1469 } 1470 1471 MyNotificationListener listener1 = new MyNotificationListener("handback1"); 1472 MyNotificationListener listener2 = new MyNotificationListener("handback2"); 1473 1474 server.addNotificationListener( 1475 new ObjectName ("JMImplementation:type=MBeanServerDelegate"), 1476 listener1, null, "handback1" 1477 ); 1478 server.addNotificationListener( 1479 new ObjectName ("JMImplementation:type=MBeanServerDelegate"), 1480 listener2, null, "handback2" 1481 ); 1482 server.addNotificationListener( 1483 new ObjectName ("JMImplementation:type=MBeanServerDelegate"), 1484 listener2, null, "handback3" 1485 ); 1486 server.removeNotificationListener( 1487 new ObjectName ("JMImplementation:type=MBeanServerDelegate"), 1488 listener2, null, "handback3" 1489 ); 1490 1491 server.registerMBean(new Test(), new ObjectName (":foo=bar")); 1493 1494 assertTrue(listener1.result == 2); 1495 assertTrue(listener1.result == 2); 1496 } 1497 1498 1501 public synchronized void testRemoveBroadcaster() 1502 throws Exception 1503 { 1504 MBeanServer server = MBeanServerFactory.newMBeanServer(); 1505 1506 class MyNotificationListener implements NotificationListener 1507 { 1508 long result = 0; 1509 public MyNotificationListener() 1510 { 1511 } 1512 public void handleNotification(Notification notification, Object handback) 1513 { 1514 result = notification.getSequenceNumber(); 1515 } 1516 } 1517 1518 ObjectName broadcasterName = new ObjectName ("test:type=broadcaster"); 1520 Broadcaster broadcaster = new Broadcaster(); 1521 server.registerMBean(broadcaster, broadcasterName); 1522 1523 MyNotificationListener listener = new MyNotificationListener(); 1525 server.addNotificationListener(broadcasterName, listener, null, null); 1526 1527 broadcaster.doSomething(); 1529 assertEquals(1, listener.result); 1530 1531 server.unregisterMBean(broadcasterName); 1533 1534 broadcaster.doSomething(); 1536 try 1537 { 1538 assertEquals(1, listener.result); 1539 } 1540 catch (AssertionFailedError e) 1541 { 1542 fail("FAILS IN RI: Removing a notification broadcaster does not " + 1543 "remove the listeners registered against the object name."); 1544 } 1545 } 1546 1547 1550 public synchronized void testAddListenerToTwoBroadcasters() 1551 throws Exception 1552 { 1553 MBeanServer server = MBeanServerFactory.newMBeanServer(); 1554 1555 class MyNotificationListener implements NotificationListener 1556 { 1557 long result = 0; 1558 public MyNotificationListener() 1559 { 1560 } 1561 public void handleNotification(Notification notification, Object handback) 1562 { 1563 result++; 1564 } 1565 } 1566 1567 ObjectName broadcasterName = new ObjectName ("test:type=broadcaster"); 1569 Broadcaster broadcaster = new Broadcaster(); 1570 server.registerMBean(broadcaster, broadcasterName); 1571 1572 MyNotificationListener listener = new MyNotificationListener(); 1574 server.addNotificationListener(broadcasterName, listener, null, null); 1575 1576 server.addNotificationListener( 1578 new ObjectName ("JMImplementation:type=MBeanServerDelegate"), 1579 listener, null, null 1580 ); 1581 1582 broadcaster.doSomething(); 1584 assertEquals(1, listener.result); 1585 1586 server.registerMBean(new Test(), new ObjectName ("Test:foo=bar")); 1588 assertEquals(2, listener.result); 1589 1590 server.unregisterMBean(broadcasterName); 1592 assertEquals(3, listener.result); 1593 1594 server.unregisterMBean(new ObjectName ("Test:foo=bar")); 1596 assertEquals(4, listener.result); 1597 } 1598 1599 1602 public synchronized void testAddListenerToTwoBroadcastersRemoveOne() 1603 throws Exception 1604 { 1605 MBeanServer server = MBeanServerFactory.newMBeanServer(); 1606 1607 class MyNotificationListener implements NotificationListener 1608 { 1609 long result = 0; 1610 public MyNotificationListener() 1611 { 1612 } 1613 public void handleNotification(Notification notification, Object handback) 1614 { 1615 result++; 1616 } 1617 } 1618 1619 ObjectName broadcasterName = new ObjectName ("test:type=broadcaster"); 1621 Broadcaster broadcaster = new Broadcaster(); 1622 server.registerMBean(broadcaster, broadcasterName); 1623 1624 MyNotificationListener listener = new MyNotificationListener(); 1626 server.addNotificationListener(broadcasterName, listener, null, null); 1627 1628 server.addNotificationListener( 1630 new ObjectName ("JMImplementation:type=MBeanServerDelegate"), 1631 listener, null, null 1632 ); 1633 1634 server.removeNotificationListener(broadcasterName, listener); 1636 1637 broadcaster.doSomething(); 1639 assertEquals(0, listener.result); 1640 1641 server.registerMBean(new Test(), new ObjectName ("Test:foo=bar")); 1643 assertEquals(1, listener.result); 1644 } 1645 1646 1648 1651 public synchronized void testaddMBeanToDelegate() throws Exception 1652 { 1653 MBeanServer server = MBeanServerFactory.newMBeanServer(); 1654 1655 MBeanListener listener = new MBeanListener(); 1656 ObjectName listenerName = new ObjectName ("test:type=listener"); 1657 server.registerMBean(listener, listenerName); 1658 1659 NotificationFilterSupport filter = new NotificationFilterSupport (); 1660 filter.enableType(MBeanServerNotification.REGISTRATION_NOTIFICATION); 1661 1662 ObjectName delegateName = 1663 new ObjectName ("JMImplementation:type=MBeanServerDelegate"); 1664 server.addNotificationListener(delegateName, listenerName, filter, "MyHandback"); 1665 1666 server.registerMBean(new Test(), new ObjectName (":foo=bar")); 1668 1669 assertTrue(listener.count == 1); 1670 assertTrue(listener.source.equals(delegateName)); 1671 assertTrue(listener.handback.equals("MyHandback")); 1672 } 1673 1674 1677 public synchronized void testAddMBeanMultipleListeners() 1678 throws Exception 1679 { 1680 MBeanServer server = MBeanServerFactory.newMBeanServer(); 1681 1682 MBeanListener listener1 = new MBeanListener(); 1683 ObjectName listenerName1 = new ObjectName ("test:type=listener1"); 1684 server.registerMBean(listener1, listenerName1); 1685 MBeanListener listener2 = new MBeanListener(); 1686 ObjectName listenerName2 = new ObjectName ("test:type=listener2"); 1687 server.registerMBean(listener2, listenerName2); 1688 1689 ObjectName delegateName = 1690 new ObjectName ("JMImplementation:type=MBeanServerDelegate"); 1691 server.addNotificationListener(delegateName, listenerName1, null, "handback1"); 1692 server.addNotificationListener(delegateName, listenerName2, null, "handback2"); 1693 1694 server.registerMBean(new Test(), new ObjectName (":foo=bar")); 1696 1697 assertEquals(1, listener1.count); 1698 assertEquals(listener1.source,delegateName); 1699 assertEquals(listener1.handback,"handback1"); 1700 assertEquals(1, listener2.count); 1701 assertEquals(listener2.source,delegateName); 1702 assertEquals(listener2.handback,"handback2"); 1703 } 1704 1705 1708 public synchronized void testAddMBeanListenerMultipleHandbacks() 1709 throws Exception 1710 { 1711 MBeanServer server = MBeanServerFactory.newMBeanServer(); 1712 1713 MBeanListener listener = new MBeanListener("handback1", "handback2"); 1714 ObjectName listenerName = new ObjectName ("test:type=listener"); 1715 server.registerMBean(listener, listenerName); 1716 1717 ObjectName delegateName = 1718 new ObjectName ("JMImplementation:type=MBeanServerDelegate"); 1719 server.addNotificationListener(delegateName, listenerName, null, "handback1"); 1720 server.addNotificationListener(delegateName, listenerName, null, "handback2"); 1721 1722 server.registerMBean(new Test(), new ObjectName (":foo=bar")); 1724 1725 assertTrue(listener.count1 == 1); 1726 assertEquals(listener.source1,delegateName); 1727 assertEquals(listener.handback1,"handback1"); 1728 assertTrue(listener.count2 == 1); 1729 assertEquals(listener.source2,delegateName); 1730 assertEquals(listener.handback2,"handback2"); 1731 } 1732 1733 1736 public synchronized void testMBeanRemoveListener() 1737 throws Exception 1738 { 1739 MBeanServer server = MBeanServerFactory.newMBeanServer(); 1740 1741 MBeanListener listener1 = new MBeanListener(); 1742 ObjectName listenerName1 = new ObjectName ("test:type=listener1"); 1743 server.registerMBean(listener1, listenerName1); 1744 MBeanListener listener2 = new MBeanListener("handback2", "handback3"); 1745 ObjectName listenerName2 = new ObjectName ("test:type=listener2"); 1746 server.registerMBean(listener2, listenerName2); 1747 1748 ObjectName delegateName = 1749 new ObjectName ("JMImplementation:type=MBeanServerDelegate"); 1750 server.addNotificationListener(delegateName, listenerName1, null, "handback1"); 1751 server.addNotificationListener(delegateName, listenerName2, null, "handback2"); 1752 server.addNotificationListener(delegateName, listenerName2, null, "handback3"); 1753 server.removeNotificationListener(delegateName, listenerName2); 1754 1755 server.registerMBean(new Test(), new ObjectName (":foo=bar")); 1757 assertTrue("Listener1 should get a notification", listener1.count == 1); 1758 assertTrue("Source should be the delegate", listener1.source.equals(delegateName)); 1759 assertTrue("Listener1 should get handback1", listener1.handback.equals("handback1")); 1760 assertTrue("Listener2 should have no notiifcation", listener2.count == 0); 1761 assertTrue("Listener2 should have no notiifcation for handback2", listener2.count1 == 0); 1762 assertTrue("Listener2 should have no notiifcation for handback3", listener2.count2 == 0); 1763 } 1764 1765 1768 public synchronized void testMBeanRemoveTriplet() 1769 throws Exception 1770 { 1771 MBeanServer server = MBeanServerFactory.newMBeanServer(); 1772 1773 MBeanListener listener1 = new MBeanListener(); 1774 ObjectName listenerName1 = new ObjectName ("test:type=listener1"); 1775 server.registerMBean(listener1, listenerName1); 1776 MBeanListener listener2 = new MBeanListener("handback2", "handback3"); 1777 ObjectName listenerName2 = new ObjectName ("test:type=listener2"); 1778 server.registerMBean(listener2, listenerName2); 1779 1780 ObjectName delegateName = 1781 new ObjectName ("JMImplementation:type=MBeanServerDelegate"); 1782 server.addNotificationListener(delegateName, listenerName1, null, "handback1"); 1783 server.addNotificationListener(delegateName, listenerName2, null, "handback2"); 1784 server.addNotificationListener(delegateName, listenerName2, null, "handback3"); 1785 server.removeNotificationListener(delegateName, listenerName2, null, "handback3"); 1786 1787 server.registerMBean(new Test(), new ObjectName (":foo=bar")); 1789 assertTrue("Listener1 should get a notification", listener1.count == 1); 1790 assertTrue("Source should be the delegate", listener1.source.equals(delegateName)); 1791 assertTrue("Listener1 should get handback1", listener1.handback.equals("handback1")); 1792 assertTrue("Listener2 should get a notification", listener2.count1 == 1); 1793 assertTrue("Source should be the delegate", listener2.source1.equals(delegateName)); 1794 assertTrue("Listener2 should get handback2", listener2.handback1.equals("handback2")); 1795 assertTrue("Listener2 should have no notiifcation for handback3", listener2.count2 == 0); 1796 } 1797 1798 1801 public synchronized void testMBeanRedeployNotification() 1802 throws Exception 1803 { 1804 MBeanServer server = MBeanServerFactory.newMBeanServer(); 1805 1806 MBeanListener listener = new MBeanListener(); 1807 ObjectName listenerName = new ObjectName ("test:type=listener"); 1808 server.registerMBean(listener, listenerName); 1809 1810 ObjectName broadcasterName = new ObjectName ("test:type=Broadcaster"); 1811 server.registerMBean(new Broadcaster(), broadcasterName); 1812 1813 server.addNotificationListener(broadcasterName, listenerName, null, "handback1"); 1814 server.removeNotificationListener(broadcasterName, listenerName, null, "handback1"); 1815 server.unregisterMBean(broadcasterName); 1816 1817 Broadcaster broadcaster = new Broadcaster(); 1818 server.registerMBean(broadcaster, broadcasterName); 1819 server.addNotificationListener(broadcasterName, listenerName, null, "handback2"); 1820 1821 broadcaster.doSomething(); 1823 assertTrue("Listener should get a notification", listener.count == 1); 1824 assertTrue("Source should be the broadcaster", listener.source.equals(broadcasterName)); 1825 assertTrue("Listener should get handback2", listener.handback.equals("handback2")); 1826 } 1827 1828 1831 public synchronized void testMBeanRemoveBroadcaster() 1832 throws Exception 1833 { 1834 MBeanServer server = MBeanServerFactory.newMBeanServer(); 1835 1836 MBeanListener listener1 = new MBeanListener(); 1837 ObjectName listenerName1 = new ObjectName ("test:type=listener1"); 1838 server.registerMBean(listener1, listenerName1); 1839 1840 ObjectName broadcasterName = new ObjectName ("test:type=broadcaster"); 1842 Broadcaster broadcaster = new Broadcaster(); 1843 server.registerMBean(broadcaster, broadcasterName); 1844 1845 server.addNotificationListener(broadcasterName, listenerName1, null, null); 1847 1848 broadcaster.doSomething(); 1850 assertEquals(1, listener1.count); 1851 assertEquals(broadcasterName, listener1.source); 1852 1853 server.unregisterMBean(broadcasterName); 1855 1856 broadcaster.doSomething(); 1858 try 1859 { 1860 assertEquals(1, listener1.count); 1861 } 1862 catch (AssertionFailedError e) 1863 { 1864 fail("FAILS IN RI: Removing a notification broadcaster does not " + 1865 "remove the listeners registered against the object name."); 1866 } 1867 } 1868 1869 1872 public synchronized void testAddMBeanListenerToTwoBroadcasters() 1873 throws Exception 1874 { 1875 MBeanServer server = MBeanServerFactory.newMBeanServer(); 1876 1877 MBeanListener listener1 = new MBeanListener(); 1878 ObjectName listenerName1 = new ObjectName ("test:type=listener1"); 1879 server.registerMBean(listener1, listenerName1); 1880 1881 ObjectName broadcasterName = new ObjectName ("test:type=broadcaster"); 1883 Broadcaster broadcaster = new Broadcaster(); 1884 server.registerMBean(broadcaster, broadcasterName); 1885 1886 server.addNotificationListener(broadcasterName, listenerName1, null, null); 1888 1889 ObjectName delegateName = 1891 new ObjectName ("JMImplementation:type=MBeanServerDelegate"); 1892 server.addNotificationListener(delegateName,listenerName1, null, null); 1893 1894 broadcaster.doSomething(); 1896 assertEquals(1, listener1.count); 1897 assertEquals(broadcasterName, listener1.source); 1898 1899 try 1900 { 1901 server.registerMBean(new Test(), new ObjectName ("Test:foo=bar")); 1903 assertEquals(2, listener1.count); 1904 assertEquals(delegateName, listener1.source); 1905 1906 server.unregisterMBean(broadcasterName); 1908 assertEquals(3, listener1.count); 1909 assertEquals(delegateName, listener1.source); 1910 1911 server.unregisterMBean(new ObjectName ("Test:foo=bar")); 1913 assertEquals(4, listener1.count); 1914 assertEquals(delegateName, listener1.source); 1915 } 1916 catch (AssertionFailedError e) 1917 { 1918 fail("FAILS IN RI: Listener registered with ObjectName in MBeanServer " + 1919 "reports the wrong source for multiple broadcaster."); 1920 } 1921 } 1922 1923 1926 public synchronized void testAddMBeanListenerToTwoBroadcastersRemoveOne() 1927 throws Exception 1928 { 1929 MBeanServer server = MBeanServerFactory.newMBeanServer(); 1930 1931 MBeanListener listener1 = new MBeanListener(); 1932 ObjectName listenerName1 = new ObjectName ("test:type=listener1"); 1933 server.registerMBean(listener1, listenerName1); 1934 1935 ObjectName broadcasterName = new ObjectName ("test:type=broadcaster"); 1937 Broadcaster broadcaster = new Broadcaster(); 1938 server.registerMBean(broadcaster, broadcasterName); 1939 1940 server.addNotificationListener(broadcasterName, listenerName1, null, null); 1942 1943 ObjectName delegateName = 1945 new ObjectName ("JMImplementation:type=MBeanServerDelegate"); 1946 server.addNotificationListener(delegateName,listenerName1, null, null); 1947 1948 server.removeNotificationListener(broadcasterName, listener1); 1950 1951 broadcaster.doSomething(); 1953 assertEquals(0, listener1.count); 1954 1955 server.registerMBean(new Test(), new ObjectName ("Test:foo=bar")); 1957 assertEquals(1, listener1.count); 1958 try 1959 { 1960 assertEquals(delegateName, listener1.source); 1961 } 1962 catch (AssertionFailedError e) 1963 { 1964 fail("FAILS IN RI: Listener registered with ObjectName in MBeanServer " + 1965 "reports the wrong source for multiple broadcaster, " + 1966 "even when the broadcaster it reports has been removed."); 1967 } 1968 } 1969 1970 public void testGetDomains() 1971 throws Exception 1972 { 1973 MBeanServer server = MBeanServerFactory.newMBeanServer(); 1974 1975 assertTrue("Only one domain at the start", server.getDomains().length == 1); 1976 assertEquals(server.getDomains()[0], "JMImplementation"); 1977 1978 server.registerMBean(new Test(), new ObjectName ("Domain1:test=test1")); 1979 server.registerMBean(new Test(), new ObjectName ("Domain1:test=test2")); 1980 server.registerMBean(new Test(), new ObjectName ("Domain2:test=test1")); 1981 server.registerMBean(new Test(), new ObjectName ("Domain3:test=test1")); 1982 1983 assertTrue("Now four domains", server.getDomains().length == 4); 1984 List domains = Arrays.asList(server.getDomains()); 1985 assertTrue("server has JMImplementation", domains.contains("JMImplementation")); 1986 assertTrue("server has Domain1", domains.contains("Domain1")); 1987 assertTrue("server has Domain2", domains.contains("Domain2")); 1988 assertTrue("server has Domain3", domains.contains("Domain3")); 1989 1990 server.unregisterMBean(new ObjectName ("Domain3:test=test1")); 1991 1992 assertTrue("Now three domains", server.getDomains().length == 3); 1993 domains = Arrays.asList(server.getDomains()); 1994 assertTrue("server has JMImplementation", domains.contains("JMImplementation")); 1995 assertTrue("server has Domain1", domains.contains("Domain1")); 1996 assertTrue("server has Domain2", domains.contains("Domain2")); 1997 assertTrue("server no longer has Domain3", domains.contains("Domain3") == false); 1998 1999 server.unregisterMBean(new ObjectName ("Domain1:test=test1")); 2000 2001 assertTrue("Still three domains", server.getDomains().length == 3); 2002 domains = Arrays.asList(server.getDomains()); 2003 assertTrue("server has JMImplementation", domains.contains("JMImplementation")); 2004 assertTrue("server still has Domain1", domains.contains("Domain1")); 2005 assertTrue("server has Domain2", domains.contains("Domain2")); 2006 assertTrue("server no longer has Domain3", domains.contains("Domain3") == false); 2007 } 2008 2009 public void testIsInstanceOf() 2010 throws Exception 2011 { 2012 MBeanServer server = MBeanServerFactory.newMBeanServer(); 2013 ObjectName baseName = new ObjectName ("MBeanServerTEST:type=testIsInstanceOf,name=Base"); 2014 ObjectName derivedName = new ObjectName ("MBeanServerTEST:type=testIsInstanceOf,name=Derived"); 2015 ObjectName unrelatedName = new ObjectName ("MBeanServerTEST:type=testIsInstanceOf,name=Unrelated"); 2016 2017 server.registerMBean(new Base(), baseName); 2018 server.registerMBean(new Derived(), derivedName); 2019 server.registerMBean(new Unrelated(), unrelatedName); 2020 2021 assertTrue("Base is an instance Object", 2022 server.isInstanceOf(baseName, Object .class.getName())); 2023 assertTrue("Base is an instance BaseMBean", 2024 server.isInstanceOf(baseName, BaseMBean.class.getName())); 2025 assertTrue("Base is an instance Base", 2026 server.isInstanceOf(baseName, Base.class.getName())); 2027 assertTrue("Derived is an instance Object", 2028 server.isInstanceOf(derivedName, Object .class.getName())); 2029 assertTrue("Derived is an instance BaseMBean", 2030 server.isInstanceOf(derivedName, BaseMBean.class.getName())); 2031 assertTrue("Derived is an instance Base", 2032 server.isInstanceOf(derivedName, Base.class.getName())); 2033 assertTrue("Derived is an instance Derived", 2034 server.isInstanceOf(derivedName, Derived.class.getName())); 2035 assertTrue("Unrelated is an instance Object", 2036 server.isInstanceOf(unrelatedName, Object .class.getName())); 2037 assertTrue("Unrelated is an instance UnrelatedMBean", 2038 server.isInstanceOf(unrelatedName, UnrelatedMBean.class.getName())); 2039 assertTrue("Unrelated is an instance Unrelated", 2040 server.isInstanceOf(unrelatedName, Unrelated.class.getName())); 2041 2042 assertTrue("Base is an not instance Derived", 2043 server.isInstanceOf(baseName, Derived.class.getName()) == false); 2044 assertTrue("Base is an not instance UnrelatedMBean", 2045 server.isInstanceOf(baseName, UnrelatedMBean.class.getName()) == false); 2046 assertTrue("Base is an not instance Unrelated", 2047 server.isInstanceOf(baseName, Unrelated.class.getName()) == false); 2048 assertTrue("Derived is an not instance UnrelatedMBean", 2049 server.isInstanceOf(derivedName, UnrelatedMBean.class.getName()) == false); 2050 assertTrue("Dervied is an not instance Unrelated", 2051 server.isInstanceOf(derivedName, Unrelated.class.getName()) == false); 2052 assertTrue("Unrelated is an not instance BaseMBean", 2053 server.isInstanceOf(unrelatedName, BaseMBean.class.getName()) == false); 2054 assertTrue("Unrelated is an not instance Base", 2055 server.isInstanceOf(unrelatedName, Base.class.getName()) == false); 2056 assertTrue("Unrelated is an not instance Derived", 2057 server.isInstanceOf(unrelatedName, Derived.class.getName()) == false); 2058 } 2059 2060 public void testIsInstanceOfErrors() 2061 throws Exception 2062 { 2063 MBeanServer server = MBeanServerFactory.newMBeanServer(); 2064 ObjectName baseName = new ObjectName ("MBeanServerTEST:type=testIsInstanceOf,name=Base"); 2065 ObjectName dynamicName = new ObjectName ("MBeanServerTEST:type=testIsInstanceOf,name=Dynamic"); 2066 ObjectName doesNotExistName = new ObjectName ("MBeanServerTEST:type=testIsInstanceOf,name=DoesNotExist"); 2067 2068 server.registerMBean(new Base(), baseName); 2069 server.registerMBean(new Dynamic(), dynamicName); 2070 2071 assertTrue("Base is not an instance of a class that does not exist", 2072 server.isInstanceOf(baseName, "does.not.exist") == false); 2073 2074 assertTrue("FAILS IN RI: Not an instance if the getMBeanInfo reports a class name that does not exist", 2075 server.isInstanceOf(dynamicName, Object .class.getName()) == false); 2076 2077 boolean caught = false; 2078 try 2079 { 2080 server.isInstanceOf(doesNotExistName, Object .class.getName()); 2081 } 2082 catch (InstanceNotFoundException e) 2083 { 2084 caught = true; 2085 } 2086 assertTrue("Should get an instance not found for a non-existent mbean", caught); 2087 } 2088} 2089 | Popular Tags |