| 1 7 8 package test.compliance.server; 9 10 import java.util.Arrays; 11 import java.util.List; 12 13 import javax.management.Attribute; 14 import javax.management.AttributeNotFoundException; 15 import javax.management.InstanceNotFoundException; 16 import javax.management.MBeanException; 17 import javax.management.MBeanRegistrationException; 18 import javax.management.MBeanServer; 19 import javax.management.MBeanServerFactory; 20 import javax.management.MBeanServerNotification; 21 import javax.management.Notification; 22 import javax.management.NotificationFilterSupport; 23 import javax.management.NotificationListener; 24 import javax.management.ObjectInstance; 25 import javax.management.ObjectName; 26 import javax.management.ReflectionException; 27 import javax.management.RuntimeErrorException; 28 import javax.management.RuntimeMBeanException; 29 import javax.management.RuntimeOperationsException; 30 import javax.management.loading.MLet; 31 32 import junit.framework.AssertionFailedError; 33 import junit.framework.TestCase; 34 import test.compliance.server.support.BabarError; 35 import test.compliance.server.support.Base; 36 import test.compliance.server.support.BaseMBean; 37 import test.compliance.server.support.Broadcaster; 38 import test.compliance.server.support.Derived; 39 import test.compliance.server.support.Dynamic; 40 import test.compliance.server.support.ExceptionOnTheRun; 41 import test.compliance.server.support.LockedTest; 42 import test.compliance.server.support.LockedTest2; 43 import test.compliance.server.support.LockedTest3; 44 import test.compliance.server.support.MBeanListener; 45 import test.compliance.server.support.MyScreamingException; 46 import test.compliance.server.support.Test; 47 import test.compliance.server.support.Test2; 48 import test.compliance.server.support.Test3; 49 import test.compliance.server.support.Test4; 50 import test.compliance.server.support.Unrelated; 51 import test.compliance.server.support.UnrelatedMBean; 52 53 60 public class MBeanServerTEST 61 extends TestCase 62 { 63 public MBeanServerTEST(String s) 64 { 65 super(s); 66 } 67 68 69 71 74 public void testInvokeWithPrimitiveBooleanReturn() throws Exception 75 { 76 MBeanServer server = MBeanServerFactory.newMBeanServer(); 77 ObjectName name = new ObjectName(":test=test"); 78 server.registerMBean(new Test(), name); 79 80 Boolean bool = (Boolean)server.invoke(name, "opWithPrimBooleanReturn", null, null); 81 82 assertTrue(bool.booleanValue() == true); 83 } 84 85 88 public void testInvokeWithPrimitiveLongArrayReturn() throws Exception 89 { 90 MBeanServer server = MBeanServerFactory.newMBeanServer(); 91 ObjectName name = new ObjectName(":test=test"); 92 server.registerMBean(new Test(), name); 93 94 long[] array = (long[])server.invoke(name, "opWithPrimLongArrayReturn", null, null); 95 96 assertTrue(array [0] == 1); 97 assertTrue(array [1] == 2); 98 assertTrue(array [2] == 3); 99 } 100 101 104 public void testInvokeWithLongArrayReturn() throws Exception 105 { 106 MBeanServer server = MBeanServerFactory.newMBeanServer(); 107 ObjectName name = new ObjectName(":test=test"); 108 server.registerMBean(new Test(), name); 109 110 Long[] array = (Long[])server.invoke(name, "opWithLongArrayReturn", null, null); 111 112 assertTrue(array [0].longValue() == 1); 113 assertTrue(array [1].longValue() == 2); 114 assertTrue(array [2].longValue() == 3); 115 } 116 117 120 public void testInvokeWithPrimitiveLongReturn() throws Exception 121 { 122 MBeanServer server = MBeanServerFactory.newMBeanServer(); 123 ObjectName name = new ObjectName(":test=test"); 124 server.registerMBean(new Test(), name); 125 126 Long l = (Long)server.invoke(name, "opWithPrimLongReturn", null, null); 127 128 assertTrue(l.longValue() == 1234567890123l); 129 } 130 131 134 public void testInvokeWithPrimitiveDoubleReturn() throws Exception 135 { 136 MBeanServer server = MBeanServerFactory.newMBeanServer(); 137 ObjectName name = new ObjectName(":test=test"); 138 server.registerMBean(new Test(), name); 139 140 Double d = (Double)server.invoke(name, "opWithPrimDoubleReturn", null, null); 141 142 assertTrue(d.doubleValue() == 0.1234567890123d); 143 } 144 145 148 public void testInvokeWithLongSignature() throws Exception 149 { 150 MBeanServer server = MBeanServerFactory.newMBeanServer(); 151 ObjectName name = new ObjectName(":test=test"); 152 server.registerMBean(new Test(), name); 153 154 server.invoke(name, "opWithLongSignature", 155 new Object[] { new Integer(1), new Integer(2), new Integer(3), new Integer(4), new Integer(5), 156 new Integer(6), new Integer(7), new Integer(8), new Integer(9), new Integer(10), 157 new Integer(11), new Integer(12), new Integer(13), new Integer(14), new Integer(15), 158 new Integer(16), new Integer(17), new Integer(18), new Integer(19), new Integer(20) }, 159 new String[] { "int", "int", "int", "int", "int", "int", "int", "int", "int", "int", 160 "int", "int", "int", "int", "int", "int", "int", "int", "int", "int" } 161 ); 162 } 163 164 168 public void testInvokeWithMixedSignature() throws Exception 169 { 170 MBeanServer server = MBeanServerFactory.newMBeanServer(); 171 ObjectName name = new ObjectName(":test=test"); 172 server.registerMBean(new Test(), name); 173 174 server.invoke(name, "opWithMixedSignature", 175 new Object[] { new Integer(1), new Double(2.2D), new Long(333L), new Boolean(true), new Byte((byte)0x02), 176 new Short((short)6), new long[]{7L, 8L}, new Long[]{new Long(1L), new Long(2L)}, new Short((short)9), new Byte((byte)10), 177 new Long(11L), new Double(1.2D), new Integer(13), new Integer(14), new Integer(15), 178 new Integer(16), new Integer(17), new Integer(18), new Integer(19), new Integer(20) }, 179 new String[] { "int", "double", "long", "boolean", "byte", "short", "[J", "[Ljava.lang.Long;", "java.lang.Short", "java.lang.Byte", 180 "java.lang.Long", "java.lang.Double", "int", "int", "int", "int", "int", "int", "int", "int" } 181 ); 182 } 183 184 185 188 public void testInvokeWithNonExistantMBean() throws Exception 189 { 190 try 191 { 192 MBeanServer server = MBeanServerFactory.newMBeanServer(); 193 server.invoke(new ObjectName(":mbean=doesnotexist"), "noMethod", null, null); 194 195 fail("InstanceNotFoundException was not thrown from an invoke operation on a non-existant MBean."); 197 } 198 catch (InstanceNotFoundException e) 199 { 200 } 202 203 } 204 205 208 public void testInvokeWithBusinessException() throws Exception 209 { 210 try 211 { 212 MBeanServer server = MBeanServerFactory.newMBeanServer(); 213 ObjectName name = new ObjectName("test:test=test"); 214 server.registerMBean(new Test(), name); 215 216 server.invoke(name, "operationWithException", null, null); 217 218 fail("MBeanException was not thrown."); 220 } 221 catch (MBeanException e) 222 { 223 assertTrue(e.getTargetException() instanceof MyScreamingException); 225 } 226 } 227 228 229 231 public void testGetAttributeWithNonExistingAttribute() throws Exception 232 { 233 try 234 { 235 MBeanServer server = MBeanServerFactory.newMBeanServer(); 236 Object foo = server.getAttribute(new ObjectName("JMImplementation:type=MBeanServerDelegate"), "Foo"); 237 238 fail("AttributeNotFoundexception was not thrown when invoking getAttribute() call on a non-existant attribute."); 240 } 241 catch (AttributeNotFoundException e) 242 { 243 } 245 } 246 247 public void testGetAttributeWithBusinessException() throws Exception 248 { 249 try 250 { 251 MBeanServer server = MBeanServerFactory.newMBeanServer(); 252 ObjectName name = new ObjectName("test:test=test"); 253 server.registerMBean(new Test(), name); 254 255 Object foo = server.getAttribute(name, "ThisWillScream"); 256 257 fail("Did not throw the screaming exception"); 259 } 260 catch (MBeanException e) 261 { 262 assertTrue(e.getTargetException() instanceof MyScreamingException); 264 } 265 } 266 267 public void testGetAttributeWithNonExistingMBean() throws Exception 268 { 269 try 270 { 271 MBeanServer server = MBeanServerFactory.newMBeanServer(); 272 ObjectName name = new ObjectName("test:name=DoesNotExist"); 273 274 server.getAttribute(name, "Whatever"); 275 276 fail("InstanceNotFoundException was not thrown on a nonexistant MBean."); 278 } 279 catch (InstanceNotFoundException e) 280 { 281 } 283 } 284 285 public void testGetAttributeWithUncheckedException() throws Exception 286 { 287 try 288 { 289 MBeanServer server = MBeanServerFactory.newMBeanServer(); 290 ObjectName name = new ObjectName("test:test=test"); 291 server.registerMBean(new Test(), name); 292 293 server.getAttribute(name, "ThrowUncheckedException"); 294 295 fail("RuntimeMBeanException was not thrown"); 297 } 298 catch (RuntimeMBeanException e) 299 { 300 assertTrue(e.getTargetException() instanceof ExceptionOnTheRun); 302 } 303 } 304 305 public void testGetAttributeWithError() throws Exception 306 { 307 try 308 { 309 MBeanServer server = MBeanServerFactory.newMBeanServer(); 310 ObjectName name = new ObjectName("test:test=test"); 311 server.registerMBean(new Test(), name); 312 313 server.getAttribute(name, "Error"); 314 315 fail("Error was not thrown"); 317 } 318 catch (RuntimeErrorException e) 319 { 320 assertTrue(e.getTargetError() instanceof BabarError); 322 } 323 } 324 325 326 328 public void testSetAttributeWithNonExistingAttribute() throws Exception 329 { 330 try 331 { 332 MBeanServer server = MBeanServerFactory.newMBeanServer(); 333 server.setAttribute(new ObjectName("JMImplementation:type=MBeanServerDelegate"), new Attribute("Foo", "value")); 334 335 fail("AttributeNotFoundexception was not thrown when invoking getAttribute() call on a non-existant attribute."); 337 } 338 catch (AttributeNotFoundException e) 339 { 340 } 342 } 343 344 public void testSetAttributeWithBusinessException() throws Exception 345 { 346 try 347 { 348 MBeanServer server = MBeanServerFactory.newMBeanServer(); 349 ObjectName name = new ObjectName("test:test=test"); 350 server.registerMBean(new Test(), name); 351 352 server.setAttribute(name, new Attribute("ThisWillScream", "value")); 353 354 fail("Did not throw the screaming exception"); 356 } 357 catch (MBeanException e) 358 { 359 assertTrue(e.getTargetException() instanceof MyScreamingException); 361 } 362 } 363 364 public void testSetAttributeWithNonExistingMBean() throws Exception 365 { 366 try 367 { 368 MBeanServer server = MBeanServerFactory.newMBeanServer(); 369 ObjectName name = new ObjectName("test:name=DoesNotExist"); 370 371 server.setAttribute(name, new Attribute("Whatever", "nothing")); 372 373 fail("InstanceNotFoundException was not thrown on a nonexistant MBean."); 375 } 376 catch (InstanceNotFoundException e) 377 { 378 } 380 } 381 382 public void testSetAttributeWithUncheckedException() throws Exception 383 { 384 try 385 { 386 MBeanServer server = MBeanServerFactory.newMBeanServer(); 387 ObjectName name = new ObjectName("test:test=test"); 388 server.registerMBean(new Test(), name); 389 390 server.setAttribute(name, new Attribute("ThrowUncheckedException", "value")); 391 392 fail("RuntimeMBeanException was not thrown"); 394 } 395 catch (RuntimeMBeanException e) 396 { 397 assertTrue(e.getTargetException() instanceof ExceptionOnTheRun); 399 } 400 } 401 402 public void testSetAttributeWithError() throws Exception 403 { 404 try 405 { 406 MBeanServer server = MBeanServerFactory.newMBeanServer(); 407 ObjectName name = new ObjectName("test:test=test"); 408 server.registerMBean(new Test(), name); 409 410 server.setAttribute(name, new Attribute("Error", "value")); 411 412 fail("Error was not thrown"); 414 } 415 catch (RuntimeErrorException e) 416 { 417 assertTrue(e.getTargetError() instanceof BabarError); 419 } 420 } 421 422 423 425 428 public void testInstantiateWithDefaultConstructor() throws Exception 429 { 430 MBeanServer server = MBeanServerFactory.newMBeanServer(); 431 Object o = server.instantiate("test.compliance.server.support.Test"); 432 433 assertTrue(o instanceof test.compliance.server.support.Test); 434 } 435 436 440 public void testInstantiateWithDefaultConstructorAndApplicationException() throws Exception 441 { 442 try 443 { 444 MBeanServer server = MBeanServerFactory.newMBeanServer(); 445 Object o = server.instantiate("test.compliance.server.support.ConstructorTest"); 446 447 fail("Instantiate should have thrown an MBeanException."); 449 } 450 catch (MBeanException e) 451 { 452 } 454 } 455 456 460 public void testInstantiateWithDefaultConstructorAndRuntimeException() throws Exception 461 { 462 try 463 { 464 MBeanServer server = MBeanServerFactory.newMBeanServer(); 465 Object o = server.instantiate("test.compliance.server.support.ConstructorTest2"); 466 467 fail("Instantiate should have thrown a RuntimeMBeanException."); 469 } 470 catch (RuntimeMBeanException e) 471 { 472 } 474 } 475 476 480 public void testInstantiateWithDefaultConstructorAndError() throws Exception 481 { 482 try 483 { 484 MBeanServer server = MBeanServerFactory.newMBeanServer(); 485 Object o = server.instantiate("test.compliance.server.support.ConstructorTest3"); 486 487 fail("Instantiate should have thrown a RuntimeErrorException."); 489 } 490 catch (RuntimeErrorException e) 491 { 492 } 494 } 495 496 500 public void testInstantiateWithDefaultConstructorAndExceptionInInit() throws Exception 501 { 502 try 503 { 504 MBeanServer server = MBeanServerFactory.newMBeanServer(); 505 506 try 508 { 509 Object o = server.instantiate("test.compliance.server.support.ConstructorTest4"); 510 } 511 catch (ExceptionInInitializerError e) 512 { 513 fail("FAILS IN RI: MBeanServer fails to wrap an error or exception from a static initializer block correctly."); 520 } 521 522 fail("Instantiate should have thrown a RuntimeMBeanException."); 524 } 525 catch (RuntimeMBeanException e) 526 { 527 529 assertTrue(e.getTargetException() instanceof NullPointerException); 530 } 531 } 532 533 537 public void testInstantiateWithDefaultConstructorAndErrorInInit() throws Exception 538 { 539 try 540 { 541 MBeanServer server = MBeanServerFactory.newMBeanServer(); 542 543 try 545 { 546 Object o = server.instantiate("test.compliance.server.support.ConstructorTest5"); 547 } 548 catch (BabarError e) 549 { 550 fail("FAILS IN RI: MBeanServer fails to wrap an error or exception from a static initializer block correctly."); 557 } 558 559 fail("Instantiate should have thrown a RuntimeErrorException."); 561 } 562 catch (RuntimeErrorException e) 563 { 564 566 assertTrue(e.getTargetError() instanceof test.compliance.server.support.BabarError); 567 } 568 } 569 570 573 public void testInstantiateWithDefaultConstructorAndUnknownClass() throws Exception 574 { 575 try 576 { 577 MBeanServer server = MBeanServerFactory.newMBeanServer(); 578 Object o = server.instantiate("foo.Bar"); 579 580 fail("Instantiate should have thrown a ReflectionException."); 582 } 583 catch (ReflectionException e) 584 { 585 assertTrue(e.getTargetException() instanceof ClassNotFoundException); 587 } 588 } 589 590 593 public void testInstantiateWithMissingDefaultConstructor() throws Exception 594 { 595 try 596 { 597 MBeanServer server = MBeanServerFactory.newMBeanServer(); 598 Object o = server.instantiate("test.compliance.server.support.ConstructorTest6"); 599 600 fail("Instantiate should have thrown a ReflectionException."); 602 } 603 catch (ReflectionException e) 604 { 605 } 607 } 608 609 612 public void testInstantiateWithInaccessibleNoArgsConstructor() throws Exception 613 { 614 try 615 { 616 MBeanServer server = MBeanServerFactory.newMBeanServer(); 617 Object o = server.instantiate("test.compliance.server.support.ConstructorTest7"); 618 619 fail("Instantiate should have thrown a ReflectionException."); 621 } 622 catch (ReflectionException e) 623 { 624 } 626 } 627 628 632 public void testInstantiateWithNullClassName() throws Exception 633 { 634 try 635 { 636 MBeanServer server = MBeanServerFactory.newMBeanServer(); 637 Object o = server.instantiate(null); 638 639 fail("incorrect exception behavior"); 641 } 642 catch (RuntimeOperationsException e) 643 { 644 646 assertTrue(e.getTargetException() instanceof IllegalArgumentException); 648 } 649 } 650 651 655 public void testInstantiateWithEmptyClassName() throws Exception 656 { 657 try 658 { 659 MBeanServer server = MBeanServerFactory.newMBeanServer(); 660 Object o = server.instantiate(""); 661 662 fail("incorrect exception/classloading behavior"); 664 } 665 catch (ReflectionException e) 666 { 667 669 assertTrue(e.getTargetException() instanceof ClassNotFoundException); 671 } 672 } 673 674 678 public void testInstantiateWithNullClassName2() throws Exception 679 { 680 try 681 { 682 MBeanServer server = MBeanServerFactory.newMBeanServer(); 683 Object o = server.instantiate(null, null); 684 685 fail("incorrect exception behavior"); 687 } 688 catch (RuntimeOperationsException e) 689 { 690 692 assertTrue(e.getTargetException() instanceof IllegalArgumentException); 694 } 695 } 696 697 701 public void testInstantiateWithEmptyClassName2() throws Exception 702 { 703 try 704 { 705 MBeanServer server = MBeanServerFactory.newMBeanServer(); 706 Object o = server.instantiate("", null); 707 708 fail("incorrect exception/classloading behavior"); 710 } 711 catch (ReflectionException e) 712 { 713 715 assertTrue(e.getTargetException() instanceof ClassNotFoundException); 717 } 718 } 719 720 725 public void testInstantiateWithNullClassName3() throws Exception 726 { 727 try 728 { 729 MBeanServer server = MBeanServerFactory.newMBeanServer(); 730 Object o = server.instantiate(null, null, null); 731 732 fail("incorrect exception behavior"); 734 } 735 catch (RuntimeOperationsException e) 736 { 737 739 assertTrue(e.getTargetException() instanceof IllegalArgumentException); 741 } 742 } 743 744 748 public void testInstantiateWithEmptyClassName3() throws Exception 749 { 750 try 751 { 752 MBeanServer server = MBeanServerFactory.newMBeanServer(); 753 Object o = server.instantiate("", null, null); 754 755 fail("incorrect exception/classloading behavior"); 757 } 758 catch (ReflectionException e) 759 { 760 762 assertTrue(e.getTargetException() instanceof ClassNotFoundException); 764 } 765 } 766 767 772 public void testInstantiateWithNullClassName4() throws Exception 773 { 774 try 775 { 776 MBeanServer server = MBeanServerFactory.newMBeanServer(); 777 Object o = server.instantiate(null, null, null, null); 778 779 fail("incorrect exception behavior"); 781 } 782 catch (RuntimeOperationsException e) 783 { 784 786 assertTrue(e.getTargetException() instanceof IllegalArgumentException); 788 } 789 } 790 791 795 public void testInstantiateWithEmptyClassName4() throws Exception 796 { 797 try 798 { 799 MBeanServer server = MBeanServerFactory.newMBeanServer(); 800 Object o = server.instantiate("", null, null, null); 801 802 fail("incorrect exception/classloading behavior"); 804 } 805 catch (ReflectionException e) 806 { 807 809 assertTrue(e.getTargetException() instanceof ClassNotFoundException); 811 } 812 } 813 814 818 public void testInstantiateWithDefaultLoaderRepository() throws Exception 819 { 820 823 MBeanServer server = MBeanServerFactory.newMBeanServer(); 824 MLet mlet = new MLet(); 825 ObjectName name = new ObjectName(":test=test"); 826 827 try 829 { 830 mlet.addURL("file:./output/etc/test/compliance/server/Test.jar"); 831 server.registerMBean(mlet, name); 832 833 Object o = server.instantiate("test.compliance.server.support.AClass"); 834 } 835 finally 836 { 837 try 838 { 839 server.unregisterMBean(name); 840 } 841 catch (Exception ignored) {} 842 } 843 844 } 846 847 848 853 public void testInstantiateWithDefaultLoaderRepository2() throws Exception 854 { 855 try 856 { 857 860 MBeanServer server = MBeanServerFactory.newMBeanServer(); 861 MLet mlet = new MLet(); 862 863 mlet.addURL("file:./output/etc/test/compliance/server/Test.jar"); 865 867 Object o = server.instantiate("test.compliance.server.support.AClass"); 868 869 874 } 878 catch (ReflectionException e) 879 { 880 882 assertTrue(e.getTargetException() instanceof ClassNotFoundException); 884 } 885 } 886 887 889 892 public void testRegisterNullObjectName() throws Exception 893 { 894 boolean caught = false; 895 try 896 { 897 MBeanServer server = MBeanServerFactory.newMBeanServer(); 898 server.registerMBean(new Test(), null); 899 } 900 catch (RuntimeOperationsException e) 901 { 902 if (e.getTargetException() instanceof IllegalArgumentException) 903 caught = true; 904 else 905 fail("Wrong wrapped exception " + e.getTargetException()); 906 } 907 if (caught == false) 908 fail("Allowed to register with a null object name"); 909 } 910
|