1 22 package org.jboss.test.jmx.compliance.relation; 23 24 import java.util.ArrayList ; 25 import java.util.HashMap ; 26 import java.util.HashSet ; 27 import java.util.Iterator ; 28 import java.util.List ; 29 import java.util.Map ; 30 31 import javax.management.InstanceNotFoundException ; 32 import javax.management.MBeanNotificationInfo ; 33 import javax.management.MBeanServer ; 34 import javax.management.MBeanServerFactory ; 35 import javax.management.Notification ; 36 import javax.management.NotificationListener ; 37 import javax.management.ObjectName ; 38 import javax.management.relation.InvalidRelationIdException ; 39 import javax.management.relation.InvalidRelationServiceException ; 40 import javax.management.relation.InvalidRelationTypeException ; 41 import javax.management.relation.InvalidRoleValueException ; 42 import javax.management.relation.RelationNotFoundException ; 43 import javax.management.relation.RelationNotification ; 44 import javax.management.relation.RelationService ; 45 import javax.management.relation.RelationServiceNotRegisteredException ; 46 import javax.management.relation.RelationSupport ; 47 import javax.management.relation.RelationTypeNotFoundException ; 48 import javax.management.relation.RelationTypeSupport ; 49 import javax.management.relation.Role ; 50 import javax.management.relation.RoleInfo ; 51 import javax.management.relation.RoleInfoNotFoundException ; 52 import javax.management.relation.RoleList ; 53 import javax.management.relation.RoleNotFoundException ; 54 import javax.management.relation.RoleResult ; 55 import javax.management.relation.RoleStatus ; 56 import javax.management.relation.RoleUnresolved ; 57 import javax.management.relation.RoleUnresolvedList ; 58 59 import junit.framework.TestCase; 60 61 import org.jboss.test.jmx.compliance.relation.support.Trivial; 62 63 72 public class RelationServiceTestCase 73 extends TestCase 74 { 75 77 79 HashMap services = new HashMap (); 80 81 RoleList rolesA; 82 HashMap roleInfosA = new HashMap (); 83 Role roleB1; 84 Role roleB2; 85 RoleList rolesB; 86 HashMap roleInfosB = new HashMap (); 87 Role roleC1; 88 Role roleC2; 89 RoleList rolesC; 90 HashMap roleInfosC = new HashMap (); 91 Role roleCX1; 92 Role roleCX2; 93 RoleList rolesCX; 94 HashMap roleInfosCX = new HashMap (); 95 Role roleCZ2; 96 RoleList rolesCZ; 97 HashMap roleInfosCZ = new HashMap (); 98 Role roleCZZ; 99 RoleList rolesCZZ; 100 HashMap roleInfosCZZ = new HashMap (); 101 Role roleCZZZ; 102 RoleList rolesCZZZ; 103 HashMap roleInfosCZZZ = new HashMap (); 104 105 107 110 public RelationServiceTestCase(String s) 111 { 112 super(s); 113 } 114 115 117 120 public void testConstructor() throws Exception 121 { 122 RelationService rs = null; 123 rs = new RelationService (true); 124 assertEquals(true, rs.getPurgeFlag()); 125 126 rs = new RelationService (false); 127 assertEquals(false, rs.getPurgeFlag()); 128 } 129 130 133 public void testAddRelation() throws Exception 134 { 135 MBeanServer server = MBeanServerFactory.createMBeanServer(); 136 try 137 { 138 ObjectName service = createRelationService("test:type=service", server); 139 createRelationTypeB(service); 140 createRolesB(server); 141 RelationSupport support = null; 142 ObjectName rsupp = null; 143 String result = null; 144 Listener listener = new Listener (RelationNotification.RELATION_MBEAN_CREATION); 145 RelationService rs = (RelationService ) services.get(service); 146 server.addNotificationListener(service, listener, null, null); 147 support = new RelationSupport ("id", service, server, "relationTypeB", 148 rolesB); 149 rsupp = new ObjectName ("test:add=relation"); 150 server.registerMBean(support, rsupp); 151 rs.addRelation(rsupp); 152 result = rs.isRelation(rsupp); 153 assertEquals("id", result); 154 listener.check(1); 155 } 156 finally 157 { 158 MBeanServerFactory.releaseMBeanServer(server); 159 } 160 } 161 162 165 public void testAddRelationErrors() throws Exception 166 { 167 MBeanServer server = MBeanServerFactory.createMBeanServer(); 168 try 169 { 170 ObjectName service = createRelationService("test:type=service", null); 171 RelationService rs = (RelationService ) services.get(service); 172 createRelationTypeB(service); 173 createRolesB(server); 174 RelationSupport support = null; 175 ObjectName name = null; 176 support = new RelationSupport ("id", service, server, "relationTypeB", 177 rolesB); 178 name = new ObjectName ("test:type=relation"); 179 server.registerMBean(support, name); 180 181 boolean caught = false; 182 try 183 { 184 rs.addRelation(null); 185 } 186 catch (IllegalArgumentException e) 187 { 188 caught = true; 189 } 190 if (caught == false) 191 fail("addRelation allows null relation"); 192 193 caught = false; 194 try 195 { 196 rs.addRelation(name); 197 } 198 catch (RelationServiceNotRegisteredException e) 199 { 200 caught = true; 201 } 202 if (caught == false) 203 fail("addRelation allowed when not registered"); 204 205 ObjectName badRelation = null; 206 server.registerMBean(rs, service); 207 badRelation = new ObjectName ("test:type=bad"); 208 server.registerMBean(new Trivial(), badRelation); 209 210 caught = false; 211 try 212 { 213 rs.addRelation(badRelation); 214 } 215 catch (NoSuchMethodException e) 216 { 217 caught = true; 218 } 219 if (caught == false) 220 fail("addRelation allowed when not a relation"); 221 222 caught = false; 223 try 224 { 225 rs.addRelation(name); 226 rs.addRelation(name); 227 } 228 catch (InvalidRelationIdException e) 229 { 230 caught = true; 231 } 232 if (caught == false) 233 fail("addRelation allows duplicate relation ids"); 234 235 rs.removeRelation("id"); 236 server.unregisterMBean(name); 237 238 caught = false; 239 try 240 { 241 rs.addRelation(name); 242 } 243 catch (InstanceNotFoundException e) 244 { 245 caught = true; 246 } 247 if (caught == false) 248 fail("addRelation allows unregistered relation"); 249 250 ObjectName service2 = createRelationService("test:type=service2", null); 251 createRelationTypeB(service2); 252 ObjectName name2 = null; 253 support = new RelationSupport ("id", service2, server, "relationTypeB", 254 rolesB); 255 name2 = new ObjectName ("test:type=relation2"); 256 server.registerMBean(support, name2); 257 258 caught = false; 259 try 260 { 261 rs.addRelation(name2); 262 } 263 catch (InvalidRelationServiceException e) 264 { 265 caught = true; 266 } 267 if (caught == false) 268 fail("addRelation allows registration in the wrong relation service"); 269 270 support = new RelationSupport ("id", service, server, "relationTypeX", 271 rolesB); 272 name = new ObjectName ("test:type=relationX"); 273 server.registerMBean(support, name); 274 275 caught = false; 276 try 277 { 278 rs.addRelation(name); 279 } 280 catch (RelationTypeNotFoundException e) 281 { 282 caught = true; 283 } 284 if (caught == false) 285 fail("addRelation allows registration with invalid relation type"); 286 287 createRelationTypeC(service); 288 createRolesC(server); 289 createRolesCZ(server); 290 support = new RelationSupport ("idC", service, server, "relationTypeB", 291 rolesC); 292 name = new ObjectName ("test:type=relationC"); 293 server.registerMBean(support, name); 294 295 caught = false; 296 try 297 { 298 rs.addRelation(name); 299 } 300 catch (RoleNotFoundException e) 301 { 302 caught = true; 303 } 304 if (caught == false) 305 fail("addRelation allows registration with invalid role name"); 306 307 support = new RelationSupport ("idCZ", service, server, "relationTypeC", 308 rolesCZ); 309 name = new ObjectName ("test:type=relationCZ"); 310 server.registerMBean(support, name); 311 312 caught = false; 313 try 314 { 315 rs.addRelation(name); 316 } 317 catch (InvalidRoleValueException e) 318 { 319 caught = true; 320 } 321 if (caught == false) 322 fail("addRelation allows registration with invalid role value"); 323 } 324 finally 325 { 326 MBeanServerFactory.releaseMBeanServer(server); 327 } 328 } 329 330 333 public void testAddRelationType() throws Exception 334 { 335 RoleInfo roleInfo1 = null; 336 RoleInfo roleInfo2 = null; 337 RoleInfo [] roleInfos = null; 338 RelationService rs = null; 339 ArrayList result = null; 340 RoleInfo result1 = null; 341 RoleInfo result2 = null; 342 roleInfo1 = new RoleInfo ("roleInfo1", Trivial.class.getName()); 343 roleInfo2 = new RoleInfo ("roleInfo2", Trivial.class.getName()); 344 roleInfos = new RoleInfo [] { roleInfo1, roleInfo2 }; 345 RelationTypeSupport rtsupp = new RelationTypeSupport ("RelationTypeName", 346 roleInfos); 347 rs = new RelationService (true); 348 rs.addRelationType(rtsupp); 349 result = (ArrayList ) rs.getRoleInfos("RelationTypeName"); 350 result1 = rs.getRoleInfo("RelationTypeName", "roleInfo1"); 351 result2 = rs.getRoleInfo("RelationTypeName", "roleInfo2"); 352 353 assertEquals(2, result.size()); 355 assertEquals(roleInfo1.toString(), result1.toString()); 356 assertEquals(roleInfo2.toString(), result2.toString()); 357 } 358 359 362 public void testAddRelationTypeErrors() throws Exception 363 { 364 RoleInfo roleInfo1 = null; 365 RoleInfo roleInfo2 = null; 366 RoleInfo [] roleInfos = null; 367 RelationService rs = null; 368 RelationTypeSupport rtsupp = null; 369 370 boolean caught = false; 372 try 373 { 374 roleInfo1 = new RoleInfo ("roleInfo1", Trivial.class.getName()); 375 roleInfo2 = new RoleInfo ("roleInfo2", Trivial.class.getName()); 376 roleInfos = new RoleInfo [] { roleInfo1, roleInfo2 }; 377 rs = new RelationService (true); 378 rs.addRelationType(null); 379 } 380 catch (IllegalArgumentException e) 381 { 382 caught = true; 383 } 384 if (caught == false) 385 fail("addRelationType allows null relation type"); 386 387 caught = false; 389 try 390 { 391 rtsupp = new RelationTypeSupport ("RelationTypeName", roleInfos); 392 rs.addRelationType(rtsupp); 393 rs.addRelationType(rtsupp); 394 } 395 catch (InvalidRelationTypeException e) 396 { 397 caught = true; 398 } 399 if (caught == false) 400 fail("addRelationType allows duplication relation types"); 401 402 caught = false; 404 try 405 { 406 roleInfos[1] = roleInfos[0]; 407 rtsupp = new RelationTypeSupport ("RelationTypeName1", roleInfos); 408 rs.addRelationType(rtsupp); 409 } 410 catch (InvalidRelationTypeException e) 411 { 412 caught = true; 413 } 414 if (caught == false) 415 fail("addRelationType allows duplicate role names"); 416 417 caught = false; 419 try 420 { 421 roleInfos[1] = null; 422 rtsupp = new RelationTypeSupport ("RelationTypeName1", roleInfos); 423 rs.addRelationType(rtsupp); 424 } 425 catch (InvalidRelationTypeException e) 426 { 427 caught = true; 428 } 429 if (caught == false) 430 fail("addRelationType allows null role info"); 431 432 caught = false; 434 try 435 { 436 rtsupp = new RelationTypeSupport ("RelationTypeName1", new RoleInfo [0]); 437 rs.addRelationType(rtsupp); 438 } 439 catch (InvalidRelationTypeException e) 440 { 441 caught = true; 442 } 443 if (caught == false) 444 fail("addRelationType allows no role info"); 445 } 446 447 450 public void testCheckRoleReadingExternal() throws Exception 451 { 452 MBeanServer server = MBeanServerFactory.createMBeanServer(); 453 try 454 { 455 ObjectName service = createRelationService("test:type=service", server); 456 createRelationTypeB(service); 457 createRolesB(server); 458 RelationSupport support = null; 459 Integer readB1 = null; 460 Integer readB2 = null; 461 RelationService rs = (RelationService ) services.get(service); 462 support = new RelationSupport ("id", service, server, 463 "relationTypeB", rolesB); 464 addRelation(server, service, support, "test:type=support"); 465 readB1 = rs.checkRoleReading("roleB1", "relationTypeB"); 466 readB2 = rs.checkRoleReading("roleB2", "relationTypeB"); 467 468 assertEquals(0, readB1.intValue()); 469 assertEquals(RoleStatus.ROLE_NOT_READABLE, readB2.intValue()); 470 } 471 finally 472 { 473 MBeanServerFactory.releaseMBeanServer(server); 474 } 475 } 476 477 480 public void testCheckRoleReadingErrors() throws Exception 481 { 482 ObjectName service = createRelationService("test:type=service", null); 483 RelationService rs = (RelationService ) services.get(service); 484 createRelationTypeB(service); 485 createRolesB(null); 486 487 boolean caught = false; 488 try 489 { 490 rs.checkRoleReading(null, "relationTypeB"); 491 } 492 catch(IllegalArgumentException e) 493 { 494 caught = true; 495 } 496 if (caught == false) 497 fail("checkRoleReading allows null role name"); 498 499 caught = false; 500 try 501 { 502 rs.checkRoleReading("roleB1", null); 503 } 504 catch(IllegalArgumentException e) 505 { 506 caught = true; 507 } 508 if (caught == false) 509 fail("checkRoleReading allows null relation name"); 510 511 caught = false; 512 try 513 { 514 rs.checkRoleReading("roleB1", "rubbish"); 515 } 516 catch(RelationTypeNotFoundException e) 517 { 518 caught = true; 519 } 520 if (caught == false) 521 fail("checkRoleReading allows invalid relation type name"); 522 } 523 524 527 public void testCheckRoleWritingExternal() throws Exception 528 { 529 MBeanServer server = MBeanServerFactory.createMBeanServer(); 530 try 531 { 532 ObjectName service = createRelationService("test:type=service", server); 533 createRelationTypeB(service); 534 createRolesB(server); 535 RelationSupport support = null; 536 Integer writeB1normal = null; 537 Integer writeB2normal = null; 538 Integer writeB1init = null; 539 Integer writeB2init = null; 540 RelationService rs = (RelationService ) services.get(service); 541 support = new RelationSupport ("id", service, server, 542 "relationTypeB", rolesB); 543 addRelation(server, service, support, "test:type=support"); 544 writeB1normal = rs.checkRoleWriting(roleB1, "relationTypeB", new Boolean (false)); 545 writeB2normal = rs.checkRoleWriting(roleB2, "relationTypeB", new Boolean (false)); 546 writeB1init = rs.checkRoleWriting(roleB1, "relationTypeB", new Boolean (true)); 547 writeB2init = rs.checkRoleWriting(roleB2, "relationTypeB", new Boolean (true)); 548 assertEquals(RoleStatus.ROLE_NOT_WRITABLE, writeB1normal.intValue()); 549 assertEquals(0, writeB2normal.intValue()); 550 assertEquals(0, writeB1init.intValue()); 551 assertEquals(0, writeB2init.intValue()); 552 } 553 finally 554 { 555 MBeanServerFactory.releaseMBeanServer(server); 556 } 557 } 558 559 562 public void testCheckRoleWritingErrors() throws Exception 563 { 564 ObjectName service = createRelationService("test:type=service", null); 565 RelationService rs = (RelationService ) services.get(service); 566 createRelationTypeB(service); 567 createRolesB(null); 568 569 boolean caught = false; 570 try 571 { 572 rs.checkRoleWriting(null, "relationTypeB", new Boolean (true)); 573 } 574 catch(IllegalArgumentException e) 575 { 576 caught = true; 577 } 578 if (caught == false) 579 fail("checkRoleWriting allows null role name"); 580 581 caught = false; 582 try 583 { 584 rs.checkRoleWriting(roleB1, null, new Boolean (true)); 585 } 586 catch(IllegalArgumentException e) 587 { 588 caught = true; 589 } 590 if (caught == false) 591 fail("checkRoleWriting allows null relation name"); 592 593 caught = false; 594 try 595 { 596 rs.checkRoleWriting(roleB1, "relationTypeB", null); 597 } 598 catch(IllegalArgumentException e) 599 { 600 caught = true; 601 } 602 if (caught == false) 603 fail("checkRoleWriting allows null init flag"); 604 605 caught = false; 606 try 607 { 608 rs.checkRoleWriting(roleB1, "rubbish", new Boolean (true)); 609 } 610 catch(RelationTypeNotFoundException e) 611 { 612 caught = true; 613 } 614 if (caught == false) 615 fail("checkRoleWriting allows invalid relation type name"); 616 } 617 618 621 public void testCreateRelation() throws Exception 622 { 623 MBeanServer server = MBeanServerFactory.createMBeanServer(); 624 try 625 { 626 ObjectName service = createRelationService("test:type=service", server); 627 createRelationTypeB(service); 628 createRolesB(server); 629 Listener listener = new Listener (RelationNotification.RELATION_BASIC_CREATION); 630 RelationService rs = (RelationService ) services.get(service); 631 server.addNotificationListener(service, listener, null, null); 632 rs.createRelation("id", "relationTypeB", rolesB); 633 boolean result = rs.hasRelation("id").booleanValue(); 634 assertEquals(true, result); 635 listener.check(1); 636 } 637 finally 638 { 639 MBeanServerFactory.releaseMBeanServer(server); 640 } 641 } 642 643 646 public void testCreateRelationErrors() throws Exception 647 { 648 MBeanServer server = MBeanServerFactory.createMBeanServer(); 649 try 650 { 651 ObjectName service = createRelationService("test:type=service", server); 652 RelationService rs = (RelationService ) services.get(service); 653 createRelationTypeB(service); 654 createRolesB(server); 655 createRelationTypeC(service); 656 createRolesC(server); 657 createRolesCZ(server); 658 createRolesCZZ(server); 659 660 boolean caught = false; 661 try 662 { 663 rs.createRelation(null, "relationTypeC", rolesC); 664 } 665 catch(IllegalArgumentException e) 666 { 667 caught = true; 668 } 669 if (caught == false) 670 fail("createRelation allows null relation id"); 671 672 caught = false; 673 try 674 { 675 rs.createRelation("relationId", null, rolesC); 676 } 677 catch(IllegalArgumentException e) 678 { 679 caught = true; 680 } 681 if (caught == false) 682 fail("createRelation allows null relation type name"); 683 684 caught = false; 685 try 686 { 687 rs.createRelation("relationId", "rubbish", rolesC); 688 } 689 catch(RelationTypeNotFoundException e) 690 { 691 caught = true; 692 } 693 if (caught == false) 694 fail("createRelation allows invalid relation type name"); 695 696 caught = false; 697 try 698 { 699 RoleList roleList = new RoleList (); 700 roleList.add(roleC1); 701 roleList.add(roleB1); 702 rs.createRelation("relationId", "relationTypeC", roleList); 703 } 704 catch(RoleNotFoundException e) 705 { 706 caught = true; 707 } 708 if (caught == false) 709 fail("createRelation allows invalid role name"); 710 711 caught = false; 712 try 713 { 714 RoleList roleList = new RoleList (); 715 roleList.add(roleC1); 716 roleList.add(roleCZ2); 717 rs.createRelation("relationId", "relationTypeC", roleList); 718 } 719 catch(InvalidRoleValueException e) 720 { 721 caught = true; 722 } 723 if (caught == false) 724 fail("createRelation allows a role below the minimum"); 725 726 caught = false; 727 try 728 { 729 RoleList roleList = new RoleList (); 730 roleList.add(roleC1); 731 roleList.add(roleCZZ); 732 rs.createRelation("relationId", "relationTypeC", roleList); 733 } 734 catch(InvalidRoleValueException e) 735 { 736 caught = true; 737 } 738 if (caught == false) 739 fail("createRelation allows a role above the maximum"); 740 741 caught = false; 742 try 743 { 744 RoleList roleList = new RoleList (); 745 roleList.add(roleC1); 746 roleList.add(roleCZZZ); 747 rs.createRelation("relationId", "relationTypeC", roleList); 748 } 749 catch(InvalidRoleValueException e) 750 { 751 caught = true; 752 } 753 if (caught == false) 754 fail("createRelation allows a role with unregistered beans"); 755 756 caught = false; 757 try 758 { 759 rs.createRelation("relationId", "relationTypeC", rolesC); 760 rs.createRelation("relationId", "relationTypeC", rolesC); 761 } 762 catch(InvalidRelationIdException e) 763 { 764 caught = true; 765 } 766 if (caught == false) 767 fail("createRelation allows duplicate relation id"); 768 769 server.unregisterMBean(service); 770 caught = false; 771 try 772 { 773 rs.createRelation("relationId2", "relationTypeC", rolesC); 774 } 775 catch(RelationServiceNotRegisteredException e) 776 { 777 caught = true; 778 } 779 if (caught == false) 780 fail("FAILS IN RI: createRelation allowed when not registered"); 781 } 782 finally 783 { 784 MBeanServerFactory.releaseMBeanServer(server); 785 } 786 } 787 788 791 public void testCreationRelationType() throws Exception 792 { 793 RoleInfo roleInfo1 = null; 794 RoleInfo roleInfo2 = null; 795 RoleInfo [] roleInfos = null; 796 RelationService rs = null; 797 ArrayList result = null; 798 RoleInfo result1 = null; 799 RoleInfo result2 = null; 800 roleInfo1 = new RoleInfo ("roleInfo1", Trivial.class.getName()); 801 roleInfo2 = new RoleInfo ("roleInfo2", Trivial.class.getName()); 802 roleInfos = new RoleInfo [] { roleInfo1, roleInfo2 }; 803 rs = new RelationService (true); 804 rs.createRelationType("RelationTypeName", roleInfos); 805 result = (ArrayList ) rs.getRoleInfos("RelationTypeName"); 806 result1 = rs.getRoleInfo("RelationTypeName", "roleInfo1"); 807 result2 = rs.getRoleInfo("RelationTypeName", "roleInfo2"); 808 809 assertEquals(2, result.size()); 811 assertEquals(roleInfo1.toString(), result1.toString()); 812 assertEquals(roleInfo2.toString(), result2.toString()); 813 } 814 815 818 public void testCreateRelationTypeErrors() throws Exception 819 { 820 RoleInfo roleInfo1 = null; 821 RoleInfo roleInfo2 = null; 822 RoleInfo [] roleInfos = null; 823 RelationService rs = null; 824 825 boolean caught = false; 827 try 828 { 829 roleInfo1 = new RoleInfo ("roleInfo1", Trivial.class.getName()); 830 roleInfo2 = new RoleInfo ("roleInfo2", Trivial.class.getName()); 831 roleInfos = new RoleInfo [] { roleInfo1, roleInfo2 }; 832 rs = new RelationService (true); 833 rs.createRelationType(null, roleInfos); 834 } 835 catch (IllegalArgumentException e) 836 { 837 caught = true; 838 } 839 if (caught == false) 840 fail("createRelationType allows null relation type name"); 841 842 caught = false; 844 try 845 { 846 rs.createRelationType("relationTypeName", null); 847 } 848 catch (IllegalArgumentException e) 849 { 850 caught = true; 851 } 852 if (caught == false) 853 fail("createRelationType allows null role infos"); 854 855 caught = false; 857 try 858 { 859 rs.createRelationType("relationTypeName", roleInfos); 860 rs.createRelationType("relationTypeName", roleInfos); 861 } 862 catch (InvalidRelationTypeException e) 863 { 864 caught = true; 865 } 866 if (caught == false) 867 fail("createRelationType allows duplicate relation type names"); 868 869 caught = false; 871 try 872 { 873 roleInfos[1] = roleInfos[0]; 874 rs.createRelationType("relationTypeName1", roleInfos); 875 } 876 catch (InvalidRelationTypeException e) 877 { 878 caught = true; 879 } 880 if (caught == false) 881 fail("createRelationType allows duplicate role names"); 882 883 caught = false; 885 try 886 { 887 roleInfos[1] = null; 888 rs.createRelationType("relationTypeName1", roleInfos); 889 } 890 catch (InvalidRelationTypeException e) 891 { 892 caught = true; 893 } 894 if (caught == false) 895 fail("createRelationType allows null role info"); 896 897 caught = false; 899 try 900 { 901 rs.createRelationType("relationTypeName1", new RoleInfo [0]); 902 } 903 catch (InvalidRelationTypeException e) 904 { 905 caught = true; 906 } 907 if (caught == false) 908 fail("createRelationType allows no role info"); 909 } 910 911 914 public void testGetRoleInfoErrors() throws Exception 915 { 916 RoleInfo roleInfo1 = null; 917 RoleInfo roleInfo2 = null; 918 RoleInfo [] roleInfos = null; 919 RelationService rs = null; 920 roleInfo1 = new RoleInfo ("roleInfo1", Trivial.class.getName()); 921 roleInfo2 = new RoleInfo ("roleInfo2", Trivial.class.getName()); 922 roleInfos = new RoleInfo [] { roleInfo1, roleInfo2 }; 923 rs = new RelationService (true); 924 rs.createRelationType("RelationTypeName", roleInfos); 925 926 boolean caught = false; 927 try 928 { 929 rs.getRoleInfo(null, "roleInfo1"); 930 } 931 catch (IllegalArgumentException e) 932 { 933 caught = true; 934 } 935 if (caught == false) 936 fail("getRoleInfo allows null relation type name"); 937 938 caught = false; 939 try 940 { 941 rs.getRoleInfo("RelationTypeName", null); 942 } 943 catch (IllegalArgumentException e) 944 { 945 caught = true; 946 } 947 if (caught == false) 948 fail("getRoleInfo allows null role info name"); 949 950 caught = false; 951 try 952 { 953 rs.getRoleInfo("RelationTypeNameX", "roleInfo1"); 954 } 955 catch (RelationTypeNotFoundException e) 956 { 957 caught = true; 958 } 959 if (caught == false) 960 fail("getRoleInfo allows non-existent relation type name"); 961 962 caught = false; 963 try 964 { 965 rs.getRoleInfo("RelationTypeName", "roleInfoX"); 966 } 967 catch (RoleInfoNotFoundException e) 968 { 969 caught = true; 970 } 971 if (caught == false) 972 fail("getRoleInfo allows non-existent role info name"); 973 } 974 975 978 public void testGetRoleInfosErrors() throws Exception 979 { 980 RoleInfo roleInfo1 = null; 981 RoleInfo roleInfo2 = null; 982 RoleInfo [] roleInfos = null; 983 RelationService rs = null; 984 roleInfo1 = new RoleInfo ("roleInfo1", Trivial.class.getName()); 985 roleInfo2 = new RoleInfo ("roleInfo2", Trivial.class.getName()); 986 roleInfos = new RoleInfo [] { roleInfo1, roleInfo2 }; 987 rs = new RelationService (true); 988 rs.createRelationType("RelationTypeName", roleInfos); 989 990 boolean caught = false; 991 try 992 { 993 rs.getRoleInfos(null); 994 } 995 catch (IllegalArgumentException e) 996 { 997 caught = true; 998 } 999 if (caught == false) 1000 fail("getRoleInfos allows null relation type name"); 1001 1002 caught = false; 1003 try 1004 { 1005 rs.getRoleInfos("RelationTypeNameX"); 1006 } 1007 catch (RelationTypeNotFoundException e) 1008 { 1009 caught = true; 1010 } 1011 if (caught == false) 1012 fail("getRoleInfos allows non-existent relation type name"); 1013 } 1014 1015 1020 public void testFindAssociatedMBeansExternal() throws Exception 1021 { 1022 MBeanServer server = MBeanServerFactory.createMBeanServer(); 1023 try 1024 { 1025 ObjectName service = createRelationService("test:type=service", server); 1026 createRelationTypeC(service); 1027 createRolesC(server); 1028 createRelationTypeCX(service); 1029 createRolesCX(server); 1030 Map result1 = null; 1031 Map result2 = null; 1032 Map result3 = null; 1033 Map result4 = null; 1034 Map result5 = null; 1035 Map result6 = null; 1036 Map result7 = null; 1037 Map result8 = null; 1038 Map result9 = null; 1039 Map result10 = null; 1040 Map result11 = null; 1041 Map result12 = null; 1042 Map result13 = null; 1043 Map result14 = null; 1044 Map result15 = null; 1045 Map result16 = null; 1046 RelationSupport supportCX = new RelationSupport ("idcx", service, server, 1047 "relationTypeCX", rolesCX); 1048 RelationSupport supportC = new RelationSupport ("idc", service, server, 1049 "relationTypeC", rolesC); 1050 addRelation(server, service, supportCX, "test:type=supportCX"); 1051 addRelation(server, service, supportC, "test:type=supportC"); 1052 RelationService rs = (RelationService ) services.get(service); 1053 result1 = rs.findAssociatedMBeans(new ObjectName ("x:relation=c,role=1,bean=1"), null, null); 1054 result2 = rs.findAssociatedMBeans(new ObjectName ("x:relation=c,role=1,bean=1"), null, null); 1055 result3 = rs.findAssociatedMBeans(new ObjectName ("x:relation=c,role=1,bean=1"), "relationTypeCX", null); 1056 result4 = rs.findAssociatedMBeans(new ObjectName ("x:relation=c,role=1,bean=1"), "relationTypeC", null); 1057 result5 = rs.findAssociatedMBeans(new ObjectName ("x:relation=c,role=1,bean=1"), null, "roleC1"); 1058 result6 = rs.findAssociatedMBeans(new ObjectName ("x:relation=c,role=1,bean=1"), null, "roleC2"); 1059 result7 = rs.findAssociatedMBeans(new ObjectName ("x:relation=c,role=1,bean=1"), null, "roleC1"); 1060 result8 = rs.findAssociatedMBeans(new ObjectName ("x:relation=c,role=1,bean=1"), null, "roleC2"); 1061 result9 = rs.findAssociatedMBeans(new ObjectName ("x:relation=c,role=1,bean=1"), "relationTypeCX", "roleC1"); 1062 result10 = rs.findAssociatedMBeans(new ObjectName ("x:relation=c,role=1,bean=1"), "relationTypeC", "roleC1"); 1063 result11 = rs.findAssociatedMBeans(new ObjectName ("x:relation=c,role=1,bean=1"), "relationTypeCX", "roleC2"); 1064 result12 = rs.findAssociatedMBeans(new ObjectName ("x:relation=c,role=1,bean=1"), "relationTypeC", "roleC2"); 1065 result13 = rs.findAssociatedMBeans(new ObjectName ("x:relation=c,role=1,bean=1"), "relationTypeCX", "roleC1"); 1066 result14 = rs.findAssociatedMBeans(new ObjectName ("x:relation=c,role=1,bean=1"), "relationTypeC", "roleC1"); 1067 result15 = rs.findAssociatedMBeans(new ObjectName ("x:relation=c,role=1,bean=1"), "relationTypeCX", "roleC2"); 1068 result16 = rs.findAssociatedMBeans(new ObjectName ("x:relation=c,role=1,bean=1"), "relationTypeC", "roleC2"); 1069 assertEquals(5, result1.size()); 1070 assertEquals(5, result2.size()); 1071 assertEquals(4, result3.size()); 1072 assertEquals(2, result4.size()); 1073 assertEquals(2, result5.size()); 1074 assertEquals(5, result6.size()); 1075 assertEquals(2, result7.size()); 1076 assertEquals(5, result8.size()); 1077 assertEquals(0, result9.size()); 1078 assertEquals(2, result10.size()); 1079 assertEquals(4, result11.size()); 1080 assertEquals(2, result12.size()); 1081 assertEquals(0, result13.size()); 1082 assertEquals(2, result14.size()); 1083 assertEquals(4, result15.size()); 1084 assertEquals(2, result16.size()); 1085 } 1086 finally 1087 { 1088 MBeanServerFactory.releaseMBeanServer(server); 1089 } 1090 } 1091 1092 1095 public void testFindAssociatedMBeansErrors() throws Exception 1096 { 1097 RelationService rs = new RelationService (true); 1098 1099 boolean caught = false; 1100 try 1101 { 1102 rs.findAssociatedMBeans(null, null, null); 1103 } 1104 catch (IllegalArgumentException e) 1105 { 1106 caught = true; 1107 } 1108 if (caught == false) 1109 fail ("findAssociatedMBeans allows a null mbean name"); 1110 } 1111 1112 1117 public void testFindReferencingRelationsExternal() throws Exception 1118 { 1119 MBeanServer server = MBeanServerFactory.createMBeanServer(); 1120 try 1121 { 1122 ObjectName service = createRelationService("test:type=service", server); 1123 createRelationTypeC(service); 1124 createRolesC(server); 1125 createRelationTypeCX(service); 1126 createRolesCX(server); 1127 Map result1 = null; 1128 Map result2 = null; 1129 Map result3 = null; 1130 Map result4 = null; 1131 Map result5 = null; 1132 Map result6 = null; 1133 Map result7 = null; 1134 Map result8 = null; 1135 Map result9 = null; 1136 Map result10 = null; 1137 Map result11 = null; 1138 Map result12 = null; 1139 Map result13 = null; 1140 Map result14 = null; 1141 Map result15 = null; 1142 Map result16 = null; 1143 RelationSupport supportCX = new RelationSupport ("idcx", service, server, 1144 "relationTypeCX", rolesCX); 1145 RelationSupport supportC = new RelationSupport ("idc", service, server, 1146 "relationTypeC", rolesC); 1147 addRelation(server, service, supportCX, "test:type=supportCX"); 1148 addRelation(server, service, supportC, "test:type=supportC"); 1149 RelationService rs = (RelationService ) services.get(service); 1150 result1 = rs.findReferencingRelations(new ObjectName ("x:relation=c,role=1,bean=1"), null, null); 1151 result2 = rs.findReferencingRelations(new ObjectName ("x:relation=c,role=1,bean=1"), null, null); 1152 result3 = rs.findReferencingRelations(new ObjectName ("x:relation=c,role=1,bean=1"), "relationTypeCX", null); 1153 result4 = rs.findReferencingRelations(new ObjectName ("x:relation=c,role=1,bean=1"), "relationTypeC", null); 1154 result5 = rs.findReferencingRelations(new ObjectName ("x:relation=c,role=1,bean=1"), null, "roleC1"); 1155 result6 = rs.findReferencingRelations(new ObjectName ("x:relation=c,role=1,bean=1"), null, "roleC2"); 1156 result7 = rs.findReferencingRelations(new ObjectName ("x:relation=c,role=1,bean=1"), null, "roleC1"); 1157 result8 = rs.findReferencingRelations(new ObjectName ("x:relation=c,role=1,bean=1"), null, "roleC2"); 1158 result9 = rs.findReferencingRelations(new ObjectName ("x:relation=c,role=1,bean=1"), "relationTypeCX", "roleC1"); 1159 result10 = rs.findReferencingRelations(new ObjectName ("x:relation=c,role=1,bean=1"), "relationTypeC", "roleC1"); 1160 result11 = rs.findReferencingRelations(new ObjectName ("x:relation=c,role=1,bean=1"), "relationTypeCX", "roleC2"); 1161 result12 = rs.findReferencingRelations(new ObjectName ("x:relation=c,role=1,bean=1"), "relationTypeC", "roleC2"); 1162 result13 = rs.findReferencingRelations(new ObjectName ("x:relation=c,role=1,bean=1"), "relationTypeCX", "roleC1"); 1163 result14 = rs.findReferencingRelations(new ObjectName ("x:relation=c,role=1,bean=1"), "relationTypeC", "roleC1"); 1164 result15 = rs.findReferencingRelations(new ObjectName ("x:relation=c,role=1,bean=1"), "relationTypeCX", "roleC2"); 1165 result16 = rs.findReferencingRelations(new ObjectName ("x:relation=c,role=1,bean=1"), "relationTypeC", "roleC2"); 1166 assertEquals(2, result1.size()); 1167 assertEquals(2, result2.size()); 1168 assertEquals(1, result3.size()); 1169 assertEquals(1, result4.size()); 1170 assertEquals(1, result5.size()); 1171 assertEquals(2, result6.size()); 1172 assertEquals(1, result7.size()); 1173 assertEquals(2, result8.size()); 1174 assertEquals(0, result9.size()); 1175 assertEquals(1, result10.size()); 1176 assertEquals(1, result11.size()); 1177 assertEquals(1, result12.size()); 1178 assertEquals(0, result13.size()); 1179 assertEquals(1, result14.size()); 1180 assertEquals(1, result15.size()); 1181 assertEquals(1, result16.size()); 1182 } 1183 finally 1184 { 1185 MBeanServerFactory.releaseMBeanServer(server); 1186 } 1187 } 1188 1189 1192 public void testFindReferencingRelationsErrors() throws Exception 1193 { 1194 RelationService rs = new RelationService (true); 1195 1196 boolean caught = false; 1197 try 1198 { 1199 rs.findReferencingRelations(null, null, null); 1200 } 1201 catch (IllegalArgumentException e) 1202 { 1203 caught = true; 1204 } 1205 if (caught == false) 1206 fail ("findReferencingRelations allows a null mbean name"); 1207 } 1208 1209 1214 public void testFindRelationsOfTypeExternal() throws Exception 1215 { 1216 MBeanServer server = MBeanServerFactory.createMBeanServer(); 1217 try 1218 { 1219 ObjectName service = createRelationService("test:type=service", server); 1220 createRelationTypeA(service); 1221 createRolesA(server); 1222 createRelationTypeB(service); 1223 createRolesB(server); 1224 createRelationTypeC(service); 1225 createRolesC(server); 1226 createRelationTypeCX(service); 1227 createRolesCX(server); 1228 List result1 = null; 1229 List result2 = null; 1230 List result3 = null; 1231 List result4 = null; 1232 RelationSupport supportA1 = new RelationSupport ("ida1", service, server, 1233 "relationTypeA", rolesA); 1234 RelationSupport supportA2 = new RelationSupport ("ida2", service, server, 1235 "relationTypeA", rolesA); 1236 RelationSupport supportCX = new RelationSupport ("idcx", service, server, 1237 "relationTypeCX", rolesCX); 1238 RelationSupport supportC = new RelationSupport ("idc", service, server, 1239 "relationTypeC", rolesC); 1240 addRelation(server, service, supportA1, "test:type=supportA1"); 1241 addRelation(server, service, supportA2, "test:type=supportA2"); 1242 addRelation(server, service, supportCX, "test:type=supportCX"); 1243 addRelation(server, service, supportC, "test:type=supportC"); 1244 RelationService rs = (RelationService ) services.get(service); 1245 result1 = rs.findRelationsOfType("relationTypeA"); 1246 result2 = rs.findRelationsOfType("relationTypeB"); 1247 result3 = rs.findRelationsOfType("relationTypeC"); 1248 result4 = rs.findRelationsOfType("relationTypeCX"); 1249 assertEquals(2, result1.size()); 1250 assertEquals(0, result2.size()); 1251 assertEquals(1, result3.size()); 1252 assertEquals(1, result4.size()); 1253 } 1254 finally 1255 { 1256 MBeanServerFactory.releaseMBeanServer(server); 1257 } 1258 } 1259 1260 1263 public void testFindRelationsOfTypeErrors() throws Exception 1264 { 1265 RelationService rs = new RelationService (true); 1266 1267 boolean caught = false; 1268 try 1269 { 1270 rs.findRelationsOfType(null); 1271 } 1272 catch (IllegalArgumentException e) 1273 { 1274 caught = true; 1275 } 1276 if (caught == false) 1277 fail ("findRelationsOfType allows a null relation type name"); 1278 1279 caught = false; 1280 try 1281 { 1282 rs.findRelationsOfType("rubbish"); 1283 } 1284 catch (RelationTypeNotFoundException e) 1285 { 1286 caught = true; 1287 } 1288 if (caught == false) 1289 fail ("findRelationsOfType allows an invalid relation type name"); 1290 } 1291 1292 1297 public void testGetAllRelationsIdsExternal() throws Exception 1298 { 1299 MBeanServer server = MBeanServerFactory.createMBeanServer(); 1300 try 1301 { 1302 ObjectName service = createRelationService("test:type=service", server); 1303 createRelationTypeA(service); 1304 createRolesA(server); 1305 createRelationTypeB(service); 1306 createRolesB(server); 1307 createRelationTypeC(service); 1308 createRolesC(server); 1309 createRelationTypeCX(service); 1310 createRolesCX(server); 1311 List result = null; 1312 RelationSupport supportA1 = new RelationSupport ("ida1", service, server, 1313 "relationTypeA", rolesA); 1314 RelationSupport supportA2 = new RelationSupport ("ida2", service, server, 1315 "relationTypeA", rolesA); 1316 RelationSupport supportCX = new RelationSupport ("idcx", service, server, 1317 "relationTypeCX", rolesCX); 1318 RelationSupport supportC = new RelationSupport ("idc", service, server, 1319 "relationTypeC", rolesC); 1320 addRelation(server, service, supportA1, "test:type=supportA1"); 1321 addRelation(server, service, supportA2, "test:type=supportA2"); 1322 addRelation(server, service, supportCX, "test:type=supportCX"); 1323 addRelation(server, service, supportC, "test:type=supportC"); 1324 RelationService rs = (RelationService ) services.get(service); 1325 result = rs.getAllRelationIds(); 1326 assertEquals(4, result.size()); 1327 assertEquals(true, result.contains("ida1")); 1328 assertEquals(true, result.contains("ida2")); 1329 assertEquals(true, result.contains("idcx")); 1330 assertEquals(true, result.contains("idc")); 1331 } 1332 finally 1333 { 1334 MBeanServerFactory.releaseMBeanServer(server); 1335 } 1336 } 1337 1338 1341 public void testGetAllRelationTypeNames() throws Exception 1342 { 1343 RelationService rs = new RelationService (true); 1344 assertEquals(0, rs.getAllRelationTypeNames().size()); 1345 1346 RoleInfo roleInfo1 = null; 1347 RoleInfo roleInfo2 = null; 1348 RoleInfo [] roleInfos = null; 1349 roleInfo1 = new RoleInfo ("roleInfo1", Trivial.class.getName()); 1350 roleInfo2 = new RoleInfo ("roleInfo2", Trivial.class.getName()); 1351 roleInfos = new RoleInfo [] { roleInfo1, roleInfo2 }; 1352 rs.createRelationType("name1", roleInfos); 1353 rs.createRelationType("name2", roleInfos); 1354 rs.createRelationType("name3", roleInfos); 1355 1356 ArrayList result = (ArrayList ) rs.getAllRelationTypeNames(); 1357 assertEquals(3, result.size()); 1358 assertEquals(true, result.contains("name1")); 1359 assertEquals(true, result.contains("name2")); 1360 assertEquals(true, result.contains("name3")); 1361 } 1362 1363 1366 public void testGetAllRolesExternal() throws Exception 1367 { 1368 MBeanServer server = MBeanServerFactory.createMBeanServer(); 1369 try 1370 { 1371 ObjectName service = createRelationService("test:type=service", server); 1372 createRelationTypeB(service); 1373 createRolesB(server); 1374 RoleResult result = null; 1375 RelationSupport support = new RelationSupport ("id", service, server, 1376 "relationTypeB", rolesB); 1377 addRelation(server, service, support, "test:type=support"); 1378 result = support.getAllRoles(); 1379 checkResult(result, roleInfosB, rolesB); 1380 } 1381 finally 1382 { 1383 MBeanServerFactory.releaseMBeanServer(server); 1384 } 1385 } 1386 1387 1390 public void testGetAllRolesErrors() throws Exception 1391 { 1392 MBeanServer server = MBeanServerFactory.createMBeanServer(); 1393 try 1394 { 1395 ObjectName service = createRelationService("test:type=service", server); 1396 RelationService rs = (RelationService ) services.get(service); 1397 createRelationTypeC(service); 1398 createRolesC(server); 1399 RoleList roles = new RoleList (); 1400 roles.add(roleC1); 1401 roles.add(roleC2); 1402 rs.createRelation("relationId", "relationTypeC", roles); 1403 1404 boolean caught = false; 1405 try 1406 { 1407 rs.getAllRoles(null); 1408 } 1409 catch(IllegalArgumentException e) 1410 { 1411 caught = true; 1412 } 1413 if (caught == false) 1414 fail("getAllRoles allows null relation id"); 1415 1416 caught = false; 1417 try 1418 { 1419 rs.getAllRoles("rubbish"); 1420 } 1421 catch(RelationNotFoundException e) 1422 { 1423 caught = true; 1424 } 1425 if (caught == false) 1426 fail("getAllRoles allows invalid relation id"); 1427 1428 server.unregisterMBean(service); 1429 caught = false; 1430 try 1431 { 1432 rs.getAllRoles("relationId"); 1433 } 1434 catch(RelationServiceNotRegisteredException e) 1435 { 1436 caught = true; 1437 } 1438 if (caught == false) 1439 fail("FAILS IN RI: getAllRoles allowed when not registered"); 1440 } 1441 finally 1442 { 1443 MBeanServerFactory.releaseMBeanServer(server); 1444 } 1445 } 1446 1447 1450 public void testGetNotificationInfo() 1451 { 1452 RelationService rs = new RelationService (true); 1453 MBeanNotificationInfo [] mbni = rs.getNotificationInfo(); 1454 assertEquals(1, mbni.length); 1455 HashSet types = new HashSet (); 1456 types.add(RelationNotification.RELATION_BASIC_CREATION); 1457 types.add(RelationNotification.RELATION_BASIC_REMOVAL); 1458 types.add(RelationNotification.RELATION_BASIC_UPDATE); 1459 types.add(RelationNotification.RELATION_MBEAN_CREATION); 1460 types.add(RelationNotification.RELATION_MBEAN_REMOVAL); 1461 types.add(RelationNotification.RELATION_MBEAN_UPDATE); 1462 String [] mbniTypes = mbni[0].getNotifTypes(); 1463 assertEquals(types.size(), mbniTypes.length); 1464 for (int i = 0; i < mbniTypes.length; i++) 1465 { 1466 if (types.contains(mbniTypes[i]) == false) 1467 fail("Unexpected relation notification type: " + mbniTypes[i]); 1468 } 1469 } 1470 1471 1474 public void testGetSetPurgeFlag() throws Exception 1475 { 1476 MBeanServer server = MBeanServerFactory.createMBeanServer(); 1477 try 1478 { 1479 RelationService rs = null; 1480 ObjectName name = null; 1481 rs = new RelationService (true); 1482 name = new ObjectName ("test:type = rs"); 1483 server.registerMBean(rs, name); 1484 assertEquals(true, rs.getPurgeFlag()); 1485 rs.setPurgeFlag(false); 1486 assertEquals(false, rs.getPurgeFlag()); 1487 rs.setPurgeFlag(true); 1488 assertEquals(true, rs.getPurgeFlag()); 1489 } 1490 finally 1491 { 1492 MBeanServerFactory.releaseMBeanServer(server); 1493 } 1494 } 1495 1496 1499 public void testGetReferencedMBeansExternal() throws Exception 1500 { 1501 MBeanServer server = MBeanServerFactory.createMBeanServer(); 1502 try 1503 { 1504 ObjectName service = createRelationService("test:type=service", server); 1505 createRelationTypeC(service); 1506 createRolesC(server); 1507 Map result = null; 1508 RelationSupport support = new RelationSupport ("id", service, server, 1509 "relationTypeC", rolesC); 1510 addRelation(server, service, support, "test:type=support"); 1511 result = support.getReferencedMBeans(); 1512 checkMBeans(result, rolesC); 1513 } 1514 finally 1515 { 1516 MBeanServerFactory.releaseMBeanServer(server); 1517 } 1518 } 1519 1520 1523 public void testGetReferencedMBeansErrors() throws Exception 1524 { 1525 MBeanServer server = MBeanServerFactory.createMBeanServer(); 1526 try 1527 { 1528 ObjectName service = createRelationService("test:type=service", server); 1529 RelationService rs = (RelationService ) services.get(service); 1530 createRelationTypeC(service); 1531 createRolesC(server); 1532 RoleList roles = new RoleList (); 1533 roles.add(roleC1); 1534 roles.add(roleC2); 1535 rs.createRelation("relationId", "relationTypeC", roles); 1536 1537 boolean caught = false; 1538 try 1539 { 1540 rs.getReferencedMBeans(null); 1541 } 1542 catch(IllegalArgumentException e) 1543 { 1544 caught = true; 1545 } 1546 if (caught == false) 1547 fail("getReferencedMBeans allows null relation id"); 1548 1549 caught = false; 1550 try 1551 { 1552 rs.getReferencedMBeans("rubbish"); 1553 } 1554 catch(RelationNotFoundException e) 1555 { 1556 caught = true; 1557 } 1558 if (caught == false) 1559 fail("getReferencedMBeans allows invalid relation id"); 1560 } 1561 finally 1562 { 1563 MBeanServerFactory.releaseMBeanServer(server); 1564 } 1565 } 1566 1567 1570 public void testGetRelationTypeNameExternal() throws Exception 1571 { 1572 MBeanServer server = MBeanServerFactory.createMBeanServer(); 1573 try 1574 { 1575 ObjectName service = createRelationService("test:type=service", server); 1576 RelationService rs = (RelationService ) services.get(service); 1577 createRelationTypeB(service); 1578 createRolesB(server); 1579 createRelationTypeC(service); 1580 createRolesC(server); 1581 RelationSupport relB = null; 1582 RelationSupport relC = null; 1583 String resultB = null; 1584 String resultC = null; 1585 relB = new RelationSupport ("idB", service, server, 1586 "relationTypeB", rolesB); 1587 addRelation(server, service, relB, "test:type=supportB"); 1588 relC = new RelationSupport ("idC", service, server, 1589 "relationTypeC", rolesC); 1590 addRelation(server, service, relC, "test:type=supportC"); 1591 resultB = rs.getRelationTypeName("idB"); 1592 resultC = rs.getRelationTypeName("idC"); 1593 assertEquals("relationTypeB", resultB); 1594 assertEquals("relationTypeC", resultC); 1595 } 1596 finally 1597 { 1598 MBeanServerFactory.releaseMBeanServer(server); 1599 } 1600 } 1601 1602 1605 public void testGetRelationTypeNameErrors() throws Exception 1606 { 1607 MBeanServer server = MBeanServerFactory.createMBeanServer(); 1608 try 1609 { 1610 ObjectName service = createRelationService("test:type=service", server); 1611 RelationService rs = (RelationService ) services.get(service); 1612 createRelationTypeC(service); 1613 createRolesC(server); 1614 RoleList roles = new RoleList (); 1615 roles.add(roleC1); 1616 roles.add(roleC2); 1617 rs.createRelation("relationId", "relationTypeC", roles); 1618 1619 boolean caught = false; 1620 try 1621 { 1622 rs.getRelationTypeName(null); 1623 } 1624 catch(IllegalArgumentException e) 1625 { 1626 caught = true; 1627 } 1628 if (caught == false) 1629 fail("getRelationTypeName allows null relation id"); 1630 1631 caught = false; 1632 try 1633 { 1634 rs.getRelationTypeName("rubbish"); 1635 } 1636 catch(RelationNotFoundException e) 1637 { 1638 caught = true; 1639 } 1640 if (caught == false) 1641 fail("getRelationTypeName allows invalid relation id"); 1642 } 1643 finally 1644 { 1645 MBeanServerFactory.releaseMBeanServer(server); 1646 } 1647 } 1648 1649 1652 public void testGetRoleExternal() throws Exception 1653 { 1654 MBeanServer server = MBeanServerFactory.createMBeanServer(); 1655 try 1656 { 1657 ObjectName service = createRelationService("test:type=service", server); 1658 createRelationTypeC(service); 1659 createRolesC(server); 1660 List result = null; 1661 RelationSupport support = new RelationSupport ("id", service, server, 1662 "relationTypeC", rolesC); 1663 addRelation(server, service, support, "test:type=support"); 1664 result = support.getRole("roleC1"); 1665 compareListOfObjectNames(getRole(rolesC, "roleC1").getRoleValue(), result); 1666 } 1667 finally 1668 { 1669 MBeanServerFactory.releaseMBeanServer(server); 1670 } 1671 } 1672 1673 1676 public void testGetRoleErrors() throws Exception 1677 { 1678 MBeanServer server = MBeanServerFactory.createMBeanServer(); 1679 try 1680 { 1681 ObjectName service = createRelationService("test:type=service", server); 1682 RelationService rs = (RelationService ) services.get(service); 1683 createRelationTypeC(service); 1684 createRolesC(server); 1685 RoleList roles = new RoleList (); 1686 roles.add(roleC1); 1687 roles.add(roleC2); 1688 rs.createRelation("relationId", "relationTypeC", roles); 1689 1690 boolean caught = false; 1691 try 1692 { 1693 rs.getRole(null, "roleC1"); 1694 } 1695 catch(IllegalArgumentException e) 1696 { 1697 caught = true; 1698 } 1699 if (caught == false) 1700 fail("getRole allows null relation id"); 1701 1702 caught = false; 1703 try 1704 { 1705 rs.getRole("relationId", null); 1706 } 1707 catch(IllegalArgumentException e) 1708 { 1709 caught = true; 1710 } 1711 if (caught == false) 1712 fail("getRole allows null role"); 1713 1714 caught = false; 1715 try 1716 { 1717 rs.getRole("rubbish", "roleC1"); 1718 } 1719 catch(RelationNotFoundException e) 1720 { 1721 caught = true; 1722 } 1723 if (caught == false) 1724 fail("getRole allows invalid relation id"); 1725 1726 caught = false; 1727 try 1728 { 1729 rs.getRole("relationId", "rubbish"); 1730 } 1731 catch(RoleNotFoundException e) 1732 { 1733 caught = true; 1734 } 1735 if (caught == false) 1736 fail("getRole allows invalid role name"); 1737 1738 caught = false; 1739 try 1740 { 1741 rs.getRole("relationId", "roleC2"); 1742 } 1743 catch(RoleNotFoundException e) 1744 { 1745 caught = true; 1746 } 1747 if (caught == false) 1748 fail("getRole allows unreadable role"); 1749 1750 server.unregisterMBean(service); 1751 caught = false; 1752 try 1753 { 1754 rs.getRole("relationId", "roleC1"); 1755 } 1756 catch(RelationServiceNotRegisteredException e) 1757 { 1758 caught = true; 1759 } 1760 if (caught == false) 1761 fail("FAILS IN RI: getRole allowed when not registered"); 1762 } 1763 finally 1764 { 1765 MBeanServerFactory.releaseMBeanServer(server); 1766 } 1767 } 1768 1769 1772 public void testGetRoleCardinalityExternal() throws Exception 1773 { 1774 MBeanServer server = MBeanServerFactory.createMBeanServer(); 1775 try 1776 { 1777 ObjectName service = createRelationService("test:type=service", server); 1778 createRelationTypeC(service); 1779 createRolesC(server); 1780 Integer result = null; 1781 RelationSupport support = null; 1782 support = new RelationSupport ("id", service, server, 1783 "relationTypeC", rolesC); 1784 addRelation(server, service, support, "test:type=support"); 1785 result = support.getRoleCardinality("roleC1"); 1786 assertEquals(2, result.intValue()); 1787 1788 result = support.getRoleCardinality("roleC2"); 1789 assertEquals(3, result.intValue()); 1790 } 1791 finally 1792 { 1793 MBeanServerFactory.releaseMBeanServer(server); 1794 } 1795 } 1796 1797 1800 public void testGetRoleCardinalityErrors() throws Exception 1801 { 1802 MBeanServer server = MBeanServerFactory.createMBeanServer(); 1803 try 1804 { 1805 ObjectName service = createRelationService("test:type=service", server); 1806 RelationService rs = (RelationService ) services.get(service); 1807 createRelationTypeC(service); 1808 createRolesC(server); 1809 RoleList roles = new RoleList (); 1810 roles.add(roleC1); 1811 roles.add(roleC2); 1812 rs.createRelation("relationId", "relationTypeC", roles); 1813 1814 boolean caught = false; 1815 try 1816 { 1817 rs.getRoleCardinality(null, "roleC1"); 1818 } 1819 catch(IllegalArgumentException e) 1820 { 1821 caught = true; 1822 } 1823 if (caught == false) 1824 fail("getRoleCardinality allows null relation id"); 1825 1826 caught = false; 1827 try 1828 { 1829 rs.getRoleCardinality("relationId", null); 1830 } 1831 catch(IllegalArgumentException e) 1832 { 1833 caught = true; 1834 } 1835 if (caught == false) 1836 fail("getRoleCardinality allows null role"); 1837 1838 caught = false; 1839 try 1840 { 1841 rs.getRoleCardinality("rubbish", "roleC1"); 1842 } 1843 catch(RelationNotFoundException e) 1844 { 1845 caught = true; 1846 } 1847 if (caught == false) 1848 fail("getRoleCardinality allows invalid relation id"); 1849 1850 caught = false; 1851 try 1852 { 1853 rs.getRoleCardinality("relationId", "rubbish"); 1854 } 1855 catch(RoleNotFoundException e) 1856 { 1857 caught = true; 1858 } 1859 if (caught == false) 1860 fail("getRoleCardinality allows invalid role name"); 1861 } 1862 finally 1863 { 1864 MBeanServerFactory.releaseMBeanServer(server); 1865 } 1866 } 1867 1868 1871 public void testGetRolesExternal() throws Exception 1872 { 1873 MBeanServer server = MBeanServerFactory.createMBeanServer(); 1874 try 1875 { 1876 ObjectName service = createRelationService("test:type=service", server); 1877 RelationService rs = (RelationService ) services.get(service); 1878 createRelationTypeC(service); 1879 createRolesC(server); 1880 RoleResult result = null; 1881 RelationSupport support = null; 1882 support = new RelationSupport ("id", service, server, 1883 "relationTypeC", rolesC); 1884 addRelation(server, service, support, "test:type=support"); 1885 result = rs.getRoles("id", new String [] {"roleC1", "roleC2" }); 1886 checkResult(result, roleInfosC, rolesC); 1887 1888 result = rs.getRoles("id", new String [] {"roleC1" }); 1889 RoleList resolved = result.getRoles(); 1890 assertEquals(1, resolved.size()); 1891 assertEquals(0, result.getRolesUnresolved().size()); 1892 compare(getRole(rolesC, "roleC1"), (Role ) resolved.get(0)); 1893 1894 result = rs.getRoles("id", new String [] {"roleC2" }); 1895 RoleUnresolvedList unresolved = result.getRolesUnresolved(); 1896 assertEquals(0, result.getRoles().size()); 1897 assertEquals(1, unresolved.size()); 1898 assertEquals("roleC2", ((RoleUnresolved )unresolved.get(0)).getRoleName()); 1899 } 1900 finally 1901 { 1902 MBeanServerFactory.releaseMBeanServer(server); 1903 } 1904 } 1905 1906 1909 public void testGetRolesErrors() throws Exception 1910 { 1911 MBeanServer server = MBeanServerFactory.createMBeanServer(); 1912 try 1913 { 1914 ObjectName service = createRelationService("test:type=service", server); 1915 RelationService rs = (RelationService ) services.get(service); 1916 createRelationTypeC(service); 1917 createRolesC(server); 1918 RoleList roles = new RoleList (); 1919 roles.add(roleC1); 1920 roles.add(roleC2); 1921 rs.createRelation("relationId", "relationTypeC", roles); 1922 String [] roleNames = new String [] {"roleC1"}; 1923 1924 boolean caught = false; 1925 try 1926 { 1927 rs.getRoles(null, roleNames); 1928 } 1929 catch(IllegalArgumentException e) 1930 { 1931 caught = true; 1932 } 1933 if (caught == false) 1934 fail("getRoles allows null relation id"); 1935 1936 caught = false; 1937 try 1938 { 1939 rs.getRoles("relationId", null); 1940 } 1941 catch(IllegalArgumentException e) 1942 { 1943 caught = true; 1944 } 1945 if (caught == false) 1946 fail("getRoles allows null role name array"); 1947 1948 caught = false; 1949 try 1950 { 1951 rs.getRoles("rubbish", roleNames); 1952 } 1953 catch(RelationNotFoundException e) 1954 { 1955 caught = true; 1956 } 1957 if (caught == false) 1958 fail("getRoles allows invalid relation id"); 1959 1960 server.unregisterMBean(service); 1961 caught = false; 1962 try 1963 { 1964 rs.getRoles("relationId", roleNames); 1965 } 1966 catch(RelationServiceNotRegisteredException e) 1967 { 1968 caught = true; 1969 } 1970 if (caught == false) 1971 fail("FAILS IN RI: getRoles allowed when not registered"); 1972 } 1973 finally 1974 { 1975 MBeanServerFactory.releaseMBeanServer(server); 1976 } 1977 } 1978 1979 1982 public void testHasRelationExternal() throws Exception 1983 { 1984 MBeanServer server = MBeanServerFactory.createMBeanServer(); 1985 try 1986 { 1987 ObjectName service = createRelationService("test:type=service", server); 1988 createRelationTypeC(service); 1989 createRolesC(server); 1990 RelationSupport support = null; 1991 Boolean result1 = null; 1992 Boolean result2 = null; 1993 Boolean result3 = null; 1994 Boolean result4 = null; 1995 Boolean result5 = null; 1996 Boolean result6 = null; 1997 support = new RelationSupport ("id1", service, server, 1998 "relationTypeC", rolesC); 1999 addRelation(server, service, support, "test:type=support1"); 2000 support = new RelationSupport ("id2", service, server, 2001 "relationTypeC", rolesC); 2002 addRelation(server, service, support, "test:type=support2"); 2003 RelationService rs = (RelationService ) services.get(service); 2004 result1 = rs.hasRelation("id1"); 2005 result2 = rs.hasRelation("id2"); 2006 result3 = rs.hasRelation("id3"); 2007 rs.removeRelation("id2"); 2008 result4 = rs.hasRelation("id1"); 2009 result5 = rs.hasRelation("id2"); 2010 result6 = rs.hasRelation("id3"); 2011 assertEquals(true, result1.booleanValue()); 2012 assertEquals(true, result2.booleanValue()); 2013 assertEquals(false, result3.booleanValue()); 2014 assertEquals(true, result4.booleanValue()); 2015 assertEquals(false, result5.booleanValue()); 2016 assertEquals(false, result6.booleanValue()); 2017 } 2018 finally 2019 { 2020 MBeanServerFactory.releaseMBeanServer(server); 2021 } 2022 } 2023 2024 2027 public void testHasRelationErrors() throws Exception 2028 { 2029 RelationService rs = new RelationService (true); 2030 2031 boolean caught = false; 2032 try 2033 { 2034 rs.hasRelation(null); 2035 } 2036 catch(IllegalArgumentException e) 2037 { 2038 caught = true; 2039 } 2040 if (caught == false) 2041 fail("hasRelation allows null relation id"); 2042 } 2043 2044 2047 public void testIsActive() throws Exception 2048 { 2049 MBeanServer server = MBeanServerFactory.createMBeanServer(); 2050 try 2051 { 2052 RelationService rs = null; 2053 boolean caught = false; 2054 try 2055 { 2056 rs = new RelationService (true); 2057 rs.isActive(); 2058 } 2059 catch (RelationServiceNotRegisteredException e) 2060 { 2061 caught = true; 2062 } 2063 catch (Exception e) 2064 { 2065 fail(e.toString()); 2066 } 2067 assertEquals(true, caught); 2068 2069 ObjectName name = null; 2070 name = new ObjectName ("test:type = rs"); 2071 server.registerMBean(rs, name); 2072 rs.isActive(); 2073 2074 caught = false; 2075 try 2076 { 2077 server.unregisterMBean(name); 2078 rs.isActive(); 2079 } 2080 catch (RelationServiceNotRegisteredException e) 2081 { 2082 caught = true; 2083 } 2084 if (caught == false) 2085 fail("FAILS IN RI: Relation Service still reports itself active."); 2086 } 2087 finally 2088 { 2089 MBeanServerFactory.releaseMBeanServer(server); 2090 } 2091 } 2092 2093 2096 public void testIsRelationExternal() throws Exception 2097 { 2098 MBeanServer server = MBeanServerFactory.createMBeanServer(); 2099 try 2100 { 2101 ObjectName service = createRelationService("test:type=service", server); 2102 createRelationTypeC(service); 2103 createRolesC(server); 2104 RelationSupport support = null; 2105 String result1 = null; 2106 String result2 = null; 2107 String result3 = null; 2108 String result4 = null; 2109 String result5 = null; 2110 String result6 = null; 2111 support = new RelationSupport ("id1", service, server, 2112 "relationTypeC", rolesC); 2113 addRelation(server, service, support, "test:type=support1"); 2114 support = new RelationSupport ("id2", service, server, 2115 "relationTypeC", rolesC); 2116 addRelation(server, service, support, "test:type=support2"); 2117 RelationService rs = (RelationService ) services.get(service); 2118 result1 = rs.isRelation(new ObjectName ("test:type=support1")); 2119 result2 = rs.isRelation(new ObjectName ("test:type=support2")); 2120 result3 = rs.isRelation(new ObjectName ("test:type=support3")); 2121 rs.removeRelation("id2"); 2122 result4 = rs.isRelation(new ObjectName ("test:type=support1")); 2123 result5 = rs.isRelation(new ObjectName ("test:type=support2")); 2124 result6 = rs.isRelation(new ObjectName ("test:type=support3")); 2125 assertEquals("id1", result1); 2126 assertEquals("id2", result2); 2127 assertEquals(null, result3); 2128 assertEquals("id1", result4); 2129 assertEquals(null, result5); 2130 assertEquals(null, result6); 2131 } 2132 finally 2133 { 2134 MBeanServerFactory.releaseMBeanServer(server); 2135 } 2136 } 2137 2138 2141 public void testIsRelationErrors() throws Exception 2142 { 2143 RelationService rs = new RelationService (true); 2144 2145 boolean caught = false; 2146 try 2147 { 2148 rs.isRelation(null); 2149 } 2150 catch(IllegalArgumentException e) 2151 { 2152 caught = true; 2153 } 2154 if (caught == false) 2155 fail("isRelation allows null relation id"); 2156 } 2157 2158 2161 public void testIsRelationMBeanExternal() throws Exception 2162 { 2163 MBeanServer server = MBeanServerFactory.createMBeanServer(); 2164 try 2165 { 2166 ObjectName service = createRelationService("test:type=service", server); 2167 createRelationTypeC(service); 2168 createRolesC(server); 2169 RelationSupport support = null; 2170 ObjectName result1 = null; 2171 ObjectName result2 = null; 2172 ObjectName on1 = null; 2173 ObjectName on2 = null; 2174 support = new RelationSupport ("id1", service, server, 2175 "relationTypeC", rolesC); 2176 addRelation(server, service, support, "test:type=support1"); 2177 support = new RelationSupport ("id2", service, server, 2178 "relationTypeC", rolesC); 2179 addRelation(server, service, support, "test:type=support2"); 2180 RelationService rs = (RelationService ) services.get(service); 2181 result1 = rs.isRelationMBean("id1"); 2182 result2 = rs.isRelationMBean("id2"); 2183 on1 = new ObjectName ("test:type=support1"); 2184 on2 = new ObjectName ("test:type=support2"); 2185 assertEquals(on1, result1); 2186 assertEquals(on2, result2); 2187 } 2188 finally 2189 { 2190 MBeanServerFactory.releaseMBeanServer(server); 2191 } 2192 } 2193 2194 2197 public void testIsRelationMBeanErrors() throws Exception 2198 { 2199 RelationService rs = new RelationService (true); 2200 2201 boolean caught = false; 2202 try 2203 { 2204 rs.isRelationMBean(null); 2205 } 2206 catch(IllegalArgumentException e) 2207 { 2208 caught = true; 2209 } 2210 if (caught == false) 2211 fail("isRelationMBean allows null relation id"); 2212 2213 caught = false; 2214 try 2215 { 2216 rs.isRelationMBean("rubbish"); 2217 } 2218 catch(RelationNotFoundException e) 2219 { 2220 caught = true; 2221 } 2222 if (caught == false) 2223 fail("isRelationMBean allows non-existent relation"); 2224 } 2225 2226 2229 public void testPurgeRelationsAutomaticExternal() throws Exception 2230 { 2231 MBeanServer server = MBeanServerFactory.createMBeanServer(); 2232 try 2233 { 2234 ObjectName service = createRelationService("test:type=service", server); 2235 createRelationTypeC(service); 2236 createRolesC(server); 2237 RelationSupport support = null; 2238 ObjectName on = null; 2239 Listener listener = new Listener (RelationNotification.RELATION_MBEAN_REMOVAL); 2240 boolean result = false; 2241 support = new RelationSupport ("id1", service, server, 2242 "relationTypeC", rolesC); 2243 addRelation(server, service, support, "test:type=support1"); 2244 server.addNotificationListener(service, listener, null, null); 2245 RelationService rs = (RelationService ) services.get(service); 2246 server.unregisterMBean(new ObjectName ("x:relation=c,role=2,bean=1")); 2247 on = new ObjectName ("test:type=support1"); 2248 result = rs.hasRelation("id1").booleanValue(); 2249 assertEquals(false, result); 2250 RelationNotification rn = listener.check(1); 2251 assertEquals(new ArrayList (), rn.getMBeansToUnregister()); 2252 assertEquals(on, rn.getObjectName()); 2253 assertEquals("id1", rn.getRelationId()); 2254 assertEquals("relationTypeC", rn.getRelationTypeName()); 2255 } 2256 finally 2257 { 2258 MBeanServerFactory.releaseMBeanServer(server); 2259 } 2260 } 2261 2262 2265 public void testPurgeRelationsManuallyExternal() throws Exception 2266 { 2267 MBeanServer server = MBeanServerFactory.createMBeanServer(); 2268 try 2269 { 2270 ObjectName service = createRelationService("test:type=service", server); 2271 createRelationTypeC(service); 2272 createRolesC(server); 2273 RelationSupport support = null; 2274 ObjectName on = null; 2275 Listener listener = new Listener (RelationNotification.RELATION_MBEAN_REMOVAL); 2276 boolean result = false; 2277 RelationService rs = null; 2278 support = new RelationSupport ("id1", service, server, 2279 "relationTypeC", rolesC); 2280 addRelation(server, service, support, "test:type=support1"); 2281 server.addNotificationListener(service, listener, null, null); 2282 rs = (RelationService ) services.get(service); 2283 rs.setPurgeFlag(false); 2284 server.unregisterMBean(new ObjectName ("x:relation=c,role=2,bean=1")); 2285 on = new ObjectName ("test:type=support1"); 2286 result = rs.hasRelation("id1").booleanValue(); 2287 2288 assertEquals(true, result); 2289 RelationNotification rn = listener.check(0); 2290 2291 rs.purgeRelations(); 2292 result = rs.hasRelation("id1").booleanValue(); 2293 assertEquals(false, result); 2294 rn = listener.check(1); 2295 assertEquals(new ArrayList (), rn.getMBeansToUnregister()); 2296 assertEquals(on, rn.getObjectName()); 2297 assertEquals("id1", rn.getRelationId()); 2298 assertEquals("relationTypeC", rn.getRelationTypeName()); 2299 } 2300 finally 2301 { 2302 MBeanServerFactory.releaseMBeanServer(server); 2303 } 2304 } 2305 2306 2309 public void testPurgeRelationsErrors() throws Exception 2310 { 2311 RelationService rs = new RelationService (true); 2312 2313 boolean caught = false; 2314 try 2315 { 2316 rs.purgeRelations(); 2317 } 2318 catch (RelationServiceNotRegisteredException e) 2319 { 2320 caught = true; 2321 } 2322 if (caught == false) 2323 fail("purgeRelations allowed when not registered"); 2324 } 2325 2326 2329 public void testRemoveRelationExternal() throws Exception 2330 { 2331 MBeanServer server = MBeanServerFactory.createMBeanServer(); 2332 try 2333 { 2334 ObjectName service = createRelationService("test:type=service", server); 2335 createRelationTypeA(service); 2336 createRolesA(server); 2337 createRelationTypeB(service); 2338 createRolesB(server); 2339 createRelationTypeC(service); 2340 createRolesC(server); 2341 createRelationTypeCX(service); 2342 createRolesCX(server); 2343 List result = null; 2344 RelationSupport supportA1 = new RelationSupport ("ida1", service, server, 2345 "relationTypeA", rolesA); 2346 RelationSupport supportA2 = new RelationSupport ("ida2", service, server, 2347 "relationTypeA", rolesA); 2348 RelationSupport supportCX = new RelationSupport ("idcx", service, server, 2349 "relationTypeCX", rolesCX); 2350 RelationSupport supportC = new RelationSupport ("idc", service, server, 2351 "relationTypeC", rolesC); 2352 addRelation(server, service, supportA1, "test:type=supportA1"); 2353 addRelation(server, service, supportA2, "test:type=supportA2"); 2354 addRelation(server, service, supportCX, "test:type=supportCX"); 2355 addRelation(server, service, supportC, "test:type=supportC"); 2356 RelationService rs = (RelationService ) services.get(service); 2357 rs.removeRelation("idcx"); 2358 result = rs.getAllRelationIds(); 2359 assertEquals(3, result.size()); 2360 assertEquals(true, result.contains("ida1")); 2361 assertEquals(true, result.contains("ida2")); 2362 assertEquals(true, result.contains("idc")); 2363 assertEquals(false, result.contains("idcx")); 2364 } 2365 finally 2366 { 2367 MBeanServerFactory.releaseMBeanServer(server); 2368 } 2369 } 2370 2371 2374 public void testRemoveRelationErrors() throws Exception 2375 { 2376 MBeanServer server = MBeanServerFactory.createMBeanServer(); 2377 try 2378 { 2379 ObjectName service = createRelationService("test:type=serviceA", null); 2380 RelationService rs = null; 2381 rs = (RelationService ) services.get(service); 2382 2383 boolean caught = false; 2384 try 2385 { 2386 rs.removeRelation("RelationId"); 2387 } 2388 catch (RelationServiceNotRegisteredException e) 2389 { 2390 caught = true; 2391 } 2392 if (caught == false) 2393 fail("removeRelation allowed when not registered"); 2394 2395 service = createRelationService("test:type=service", server); 2396 createRelationTypeA(service); 2397 createRolesA(server); 2398 RelationSupport supportA1 = new RelationSupport ("ida1", service, server, 2399 "relationTypeA", rolesA); 2400 addRelation(server, service, supportA1, "test:type=supportA1"); 2401 rs = (RelationService ) services.get(service); 2402 2403 caught = false; 2404 try 2405 { 2406 rs.removeRelation(null); 2407 } 2408 catch (IllegalArgumentException e) 2409 { 2410 caught = true; 2411 } 2412 if (caught == false) 2413 fail("removeRelation accepts a null relation"); 2414 2415 caught = false; 2416 try 2417 { 2418 rs.removeRelation("rubbish"); 2419 } 2420 catch (RelationNotFoundException e) 2421 { 2422 caught = true; 2423 } 2424 if (caught == false) 2425 fail("removeRelation accepts a non existent relation"); 2426 } 2427 finally 2428 { 2429 MBeanServerFactory.releaseMBeanServer(server); 2430 } 2431 } 2432 2433 2436 public void testRemoveRelationType() throws Exception 2437 { 2438 MBeanServer server = MBeanServerFactory.createMBeanServer(); 2439 try 2440 { 2441 RelationService rs = new RelationService (true); 2442 assertEquals(0, rs.getAllRelationTypeNames().size()); 2443 2444 RoleInfo roleInfo1 = null; 2445 RoleInfo roleInfo2 = null; 2446 RoleInfo [] roleInfos = null; 2447 ObjectName name = new ObjectName ("test:type = rs"); 2448 server.registerMBean(rs, name); 2449 roleInfo1 = new RoleInfo ("roleInfo1", Trivial.class.getName()); 2450 roleInfo2 = new RoleInfo ("roleInfo2", Trivial.class.getName()); 2451 roleInfos = new RoleInfo [] { roleInfo1, roleInfo2 }; 2452 rs.createRelationType("name1", roleInfos); 2453 rs.createRelationType("name2", roleInfos); 2454 rs.createRelationType("name3", roleInfos); 2455 rs.removeRelationType("name3"); 2456 2457 ArrayList result = (ArrayList ) rs.getAllRelationTypeNames(); 2458 assertEquals(2, result.size()); 2459 assertEquals(true, result.contains("name1")); 2460 assertEquals(true, result.contains("name2")); 2461 assertEquals(false, result.contains("name3")); 2462 } 2463 finally 2464 { 2465 MBeanServerFactory.releaseMBeanServer(server); 2466 } 2467 } 2468 2469 2472 public void testRemoveRelationTypeErrors() throws Exception 2473 { 2474 MBeanServer server = MBeanServerFactory.createMBeanServer(); 2475 try 2476 { 2477 RelationService rs = null; 2478 RoleInfo roleInfo1 = null; 2479 RoleInfo roleInfo2 = null; 2480 RoleInfo [] roleInfos = null; 2481 rs = new RelationService (true); 2482 roleInfo1 = new RoleInfo ("roleInfo1", Trivial.class.getName()); 2483 roleInfo2 = new RoleInfo ("roleInfo2", Trivial.class.getName()); 2484 roleInfos = new RoleInfo [] { roleInfo1, roleInfo2 }; 2485 rs.createRelationType("name1", roleInfos); 2486 2487 boolean caught = false; 2488 try 2489 { 2490 rs.removeRelationType("name1"); 2491 } 2492 catch (RelationServiceNotRegisteredException e) 2493 { 2494 caught = true; 2495 } 2496 if (caught == false) 2497 fail("Remove relation type allowed when not registered, why not?"); 2498 2499 server.registerMBean(rs, new ObjectName ("test:type=RelationService")); 2500 2501 caught = false; 2502 try 2503 { 2504 rs.removeRelationType(null); 2505 } 2506 catch (IllegalArgumentException e) 2507 { 2508 caught = true; 2509 } 2510 if (caught == false) 2511 fail("Remove relation type allows null relation type name"); 2512 2513 caught = false; 2514 try 2515 { 2516 rs.removeRelationType("rubbish"); 2517 } 2518 catch (RelationTypeNotFoundException e) 2519 { 2520 caught = true; 2521 } 2522 if (caught == false) 2523 fail("Remove relation type allows non-existent relation type name"); 2524 } 2525 finally 2526 { 2527 MBeanServerFactory.releaseMBeanServer(server); 2528 } 2529 } 2530 2531 2534 public void testSendRelationCreationNotificationExternal() throws Exception 2535 { 2536 MBeanServer server = MBeanServerFactory.createMBeanServer(); 2537 try 2538 { 2539 ObjectName service = createRelationService("test:type=service", server); 2540 createRelationTypeC(service); 2541 createRolesC(server); 2542 RelationSupport support = null; 2543 ObjectName on = null; 2544 Listener listener = new Listener (RelationNotification.RELATION_MBEAN_CREATION); 2545 support = new RelationSupport ("id1", service, server, 2546 "relationTypeC", rolesC); 2547 addRelation(server, service, support, "test:type=support1"); 2548 server.addNotificationListener(service, listener, null, null); 2549 RelationService rs = (RelationService ) services.get(service); 2550 rs.sendRelationCreationNotification("id1"); 2551 on = new ObjectName ("test:type=support1"); 2552 RelationNotification rn = listener.check(1); 2553 assertEquals(new ArrayList (), rn.getMBeansToUnregister()); 2554 assertEquals(new ArrayList (), rn.getNewRoleValue()); 2555 assertEquals(on, rn.getObjectName()); 2556 assertEquals(new ArrayList (), rn.getOldRoleValue()); 2557 assertEquals("id1", rn.getRelationId()); 2558 assertEquals("relationTypeC", rn.getRelationTypeName()); 2559 assertEquals(null, rn.getRoleName()); 2560 } 2561 finally 2562 { 2563 MBeanServerFactory.releaseMBeanServer(server); 2564 } 2565 } 2566 2567 2570 public void testSendRelationCreationNotificationErrors() throws Exception 2571 { 2572 RelationService rs = new RelationService (true); 2573 2574 boolean caught = false; 2575 try 2576 { 2577 rs.sendRelationCreationNotification(null); 2578 } 2579 catch (IllegalArgumentException e) 2580 { 2581 caught = true; 2582 } 2583 if (caught == false) 2584 fail("sendRelationCreationNotification allows null relation id"); 2585 2586 caught = false; 2587 try 2588 { 2589 rs.sendRelationCreationNotification("rubbish"); 2590 } 2591 catch (RelationNotFoundException e) 2592 { 2593 caught = true; 2594 } 2595 if (caught == false) 2596 fail("sendRelationCreationNotification allows invalid relation id"); 2597 } 2598 2599 2602 public void testSendRelationRemovalNotificationExternal() throws Exception 2603 { 2604 MBeanServer server = MBeanServerFactory.createMBeanServer(); 2605 try 2606 { 2607 ObjectName service = createRelationService("test:type=service", server); 2608 createRelationTypeC(service); 2609 createRolesC(server); 2610 RelationSupport support = null; 2611 ObjectName on = null; 2612 ArrayList test = new ArrayList (); 2613 Listener listener = new Listener (RelationNotification.RELATION_MBEAN_REMOVAL); 2614 support = new RelationSupport ("id1", service, server, 2615 "relationTypeC", rolesC); 2616 addRelation(server, service, support, "test:type=support1"); 2617 server.addNotificationListener(service, listener, null, null); 2618 RelationService rs = (RelationService ) services.get(service); 2619 test.add(new ObjectName ("test:type=test")); 2620 rs.sendRelationRemovalNotification("id1", test); 2621 on = new ObjectName ("test:type=support1"); 2622 RelationNotification rn = listener.check(1); 2623 assertEquals(test, rn.getMBeansToUnregister()); 2624 assertEquals(new ArrayList (), rn.getNewRoleValue()); 2625 assertEquals(on, rn.getObjectName()); 2626 assertEquals(new ArrayList (), rn.getOldRoleValue()); 2627 assertEquals("id1", rn.getRelationId()); 2628 assertEquals("relationTypeC", rn.getRelationTypeName()); 2629 assertEquals(null, rn.getRoleName()); 2630 } 2631 finally 2632 { 2633 MBeanServerFactory.releaseMBeanServer(server); 2634 } 2635 } 2636 2637 2640 public void testSendRelationRemovalNotificationErrors() throws Exception 2641 { 2642 RelationService rs = new RelationService (true); 2643 2644 boolean caught = false; 2645 try 2646 { 2647 rs.sendRelationRemovalNotification(null, new ArrayList ()); 2648 } 2649 catch (IllegalArgumentException e) 2650 { 2651 caught = true; 2652 } 2653 if (caught == false) 2654 fail("sendRelationRemovalNotification allows null relation id"); 2655 2656 2657 caught = false; 2658 try 2659 { 2660 rs.sendRelationRemovalNotification("rubbish", new ArrayList ()); 2661 } 2662 catch (RelationNotFoundException e) 2663 { 2664 caught = true; 2665 } 2666 if (caught == false) 2667 fail("sendRelationRemovalNotification allows invalid relation id"); 2668 } 2669 2670 2673 public void testSendRoleUpdateNotificationExternal() throws Exception 2674 { 2675 MBeanServer server = MBeanServerFactory.createMBeanServer(); 2676 try 2677 { 2678 ObjectName service = createRelationService("test:type=service", server); 2679 createRelationTypeC(service); 2680 createRolesB(server); 2681 createRolesC(server); 2682 RelationSupport support = null; 2683 ObjectName on = null; 2684 ArrayList test = new ArrayList (); 2685 Listener listener = new Listener (RelationNotification.RELATION_MBEAN_UPDATE); 2686 support = new RelationSupport ("id1", service, server, 2687 "relationTypeC", rolesC); 2688 addRelation(server, service, support, "test:type=support1"); 2689 server.addNotificationListener(service, listener, null, null); 2690 RelationService rs = (RelationService ) services.get(service); 2691 test.add(new ObjectName ("test:type=test")); 2692 rs.sendRoleUpdateNotification("id1", roleB1, test); 2693 on = new ObjectName ("test:type=support1"); 2694 RelationNotification rn = listener.check(1); 2695 assertEquals(new ArrayList (), rn.getMBeansToUnregister()); 2696 assertEquals(roleB1.getRoleValue(), rn.getNewRoleValue()); 2697 assertEquals(on, rn.getObjectName()); 2698 assertEquals(test, rn.getOldRoleValue()); 2699 assertEquals("id1", rn.getRelationId()); 2700 assertEquals("relationTypeC", rn.getRelationTypeName()); 2701 assertEquals("roleB1", rn.getRoleName()); 2702 } 2703 finally 2704 { 2705 MBeanServerFactory.releaseMBeanServer(server); 2706 } 2707 } 2708 2709 2712 public void testSendRoleUpdateNotificationErrors() throws Exception 2713 { 2714 MBeanServer server = MBeanServerFactory.createMBeanServer(); 2715 try 2716 { 2717 ObjectName service = createRelationService("test:type=service", server); 2718 RelationService rs = (RelationService ) services.get(service); 2719 createRelationTypeB(service); 2720 createRolesB(server); 2721 2722 boolean caught = false; 2723 try 2724 { 2725 rs.sendRoleUpdateNotification(null, roleB1, new ArrayList ()); 2726 } 2727 catch (IllegalArgumentException e) 2728 { 2729 caught = true; 2730 } 2731 if (caught == false) 2732 fail("sendRoleUpdateNotification allows null relation id"); 2733 2734 RoleList roleList = new RoleList (); 2735 roleList.add(roleB1); 2736 roleList.add(roleB2); 2737 rs.createRelation("relationId", "relationTypeB", roleList); 2738 2739 caught = false; 2740 try 2741 { 2742 rs.sendRoleUpdateNotification("relationId", null, new ArrayList ()); 2743 } 2744 catch (IllegalArgumentException e) 2745 { 2746 caught = true; 2747 } 2748 if (caught == false) 2749 fail("sendRoleUpdateNotification allows null role"); 2750 2751 2752 caught = false; 2753 try 2754 { 2755 rs.sendRoleUpdateNotification("rubbish", roleB1, new ArrayList ()); 2756 } 2757 catch (RelationNotFoundException e) 2758 { 2759 caught = true; 2760 } 2761 if (caught == false) 2762 fail("sendRoleUpdateNotification allows invalid relation id"); 2763 } 2764 finally 2765 { 2766 MBeanServerFactory.releaseMBeanServer(server); 2767 } 2768 } 2769 2770 2773 public void testSetRoleExternal() throws Exception 2774 { 2775 MBeanServer server = MBeanServerFactory.createMBeanServer(); 2776 try 2777 { 2778 ObjectName service = createRelationService("test:type=service", server); 2779 createRelationTypeC(service); 2780 createRolesC(server); 2781 createRolesCX(server); 2782 RelationSupport support = null; 2783 ObjectName on = null; 2784 Listener listener = new Listener (RelationNotification.RELATION_MBEAN_UPDATE); 2785 support = new RelationSupport ("id1", service, server, 2786 "relationTypeC", rolesC); 2787 addRelation(server, service, support, "test:type=support1"); 2788 server.addNotificationListener(service, listener, null, null); 2789 RelationService rs = (RelationService ) services.get(service); 2790 rs.setRole("id1", roleCX2); 2791 on = new ObjectName ("test:type=support1"); 2792 RoleList shouldBe = new RoleList (); 2793 shouldBe.add(roleC1); 2794 shouldBe.add(roleCX2); 2795 compare(shouldBe, support.retrieveAllRoles()); 2796 RelationNotification rn = listener.check(1); 2797 assertEquals(new ArrayList (), rn.getMBeansToUnregister()); 2798 assertEquals(roleCX2.getRoleValue(), rn.getNewRoleValue()); 2799 assertEquals(on, rn.getObjectName()); 2800 assertEquals(roleC2.getRoleValue(), rn.getOldRoleValue()); 2801 assertEquals("id1", rn.getRelationId()); 2802 assertEquals("relationTypeC", rn.getRelationTypeName()); 2803 assertEquals("roleC2", rn.getRoleName()); 2804 } 2805 finally 2806 { 2807 MBeanServerFactory.releaseMBeanServer(server); 2808 } 2809 } 2810 2811 2814 public void testSetRoleErrors() throws Exception 2815 { 2816 MBeanServer server = MBeanServerFactory.createMBeanServer(); 2817 try 2818 { 2819 ObjectName service = createRelationService("test:type=service", server); 2820 RelationService rs = (RelationService ) services.get(service); 2821 createRelationTypeB(service); 2822 createRolesB(server); 2823 createRelationTypeC(service); 2824 createRolesC(server); 2825 createRolesCZ(server); 2826 createRolesCZZ(server); 2827 RoleList roles = new RoleList (); 2828 roles.add(roleC1); 2829 roles.add(roleC2); 2830 rs.createRelation("relationId", "relationTypeC", roles); 2831 2832 boolean caught = false; 2833 try 2834 { 2835 rs.setRole(null, roleC2); 2836 } 2837 catch(IllegalArgumentException e) 2838 { 2839 caught = true; 2840 } 2841 if (caught == false) 2842 fail("setRole allows null relation id"); 2843 2844 caught = false; 2845 try 2846 { 2847 rs.setRole("relationId", null); 2848 } 2849 catch(IllegalArgumentException e) 2850 { 2851 caught = true; 2852 } 2853 if (caught == false) 2854 fail("setRole allows null role"); 2855 2856 caught = false; 2857 try 2858 { 2859 rs.setRole("rubbish", roleC2); 2860 } 2861 catch(RelationNotFoundException e) 2862 { 2863 caught = true; 2864 } 2865 if (caught == false) 2866 fail("setRole allows invalid relation id"); 2867 2868 caught = false; 2869 try 2870 { 2871 rs.setRole("relationId", roleB1); 2872 } 2873 catch(RoleNotFoundException e) 2874 { 2875 caught = true; 2876 } 2877 if (caught == false) 2878 fail("setRole allows invalid role name"); 2879 2880 caught = false; 2881 try 2882 { 2883 rs.setRole("relationId", roleC1); 2884 } 2885 catch(RoleNotFoundException e) 2886 { 2887 caught = true; 2888 } 2889 if (caught == false) 2890 fail("setRole allows non-writable role"); 2891 2892 caught = false; 2893 try 2894 { 2895 rs.setRole("relationId", roleCZ2); 2896 } 2897 catch(InvalidRoleValueException e) 2898 { 2899 caught = true; 2900 } 2901 if (caught == false) 2902 fail("setRole allows a role below the minimum"); 2903 2904 caught = false; 2905 try 2906 { 2907 rs.setRole("relationId", roleCZZ); 2908 } 2909 catch(InvalidRoleValueException e) 2910 { 2911 caught = true; 2912 } 2913 if (caught == false) 2914 fail("setRole allows a role above the maximum"); 2915 2916 caught = false; 2917 try 2918 { 2919 rs.setRole("relationId", roleCZZZ); 2920 } 2921 catch(InvalidRoleValueException e) 2922 { 2923 caught = true; 2924 } 2925 if (caught == false) 2926 fail("setRole allows a role with unregistered beans"); 2927 2928 server.unregisterMBean(service); 2929 caught = false; 2930 try 2931 { 2932 rs.setRole("relationId", roleC2); 2933 } 2934 catch(RelationServiceNotRegisteredException e) 2935 { 2936 caught = true; 2937 } 2938 if (caught == false) 2939 fail("FAILS IN RI: setRole allowed when not registered"); 2940 } 2941 finally 2942 { 2943 MBeanServerFactory.releaseMBeanServer(server); 2944 } 2945 } 2946 2947 2951 public void testSetRolesExternal() throws Exception 2952 { 2953 MBeanServer server = MBeanServerFactory.createMBeanServer(); 2954 try 2955 { 2956 ObjectName service = createRelationService("test:type=service", server); 2957 createRelationTypeC(service); 2958 createRolesC(server); 2959 createRolesCX(server); 2960 RelationSupport support = null; 2961 Listener listener = new Listener (RelationNotification.RELATION_MBEAN_UPDATE); 2962 RoleList shouldBe = new RoleList (); 2963 shouldBe.add(roleC1); 2964 shouldBe.add(roleCX2); 2965 support = new RelationSupport ("id1", service, server, 2966 "relationTypeC", rolesC); 2967 addRelation(server, service, support, "test:type=support1"); 2968 server.addNotificationListener(service, listener, null, null); 2969 RelationService rs = (RelationService ) services.get(service); 2970 2971 rs.setRoles("id1", shouldBe); 2972 compare(shouldBe, support.retrieveAllRoles()); 2973 listener.check(1); 2974 } 2975 finally 2976 { 2977 MBeanServerFactory.releaseMBeanServer(server); 2978 } 2979 } 2980 2981 2984 public void testSetRolesErrors() throws Exception 2985 { 2986 MBeanServer server = MBeanServerFactory.createMBeanServer(); 2987 try 2988 { 2989 ObjectName service = createRelationService("test:type=service", server); 2990 RelationService rs = (RelationService ) services.get(service); 2991 createRelationTypeC(service); 2992 createRolesC(server); 2993 RoleList roles = new RoleList (); 2994 roles.add(roleC1); 2995 roles.add(roleC2); 2996 rs.createRelation("relationId", "relationTypeC", roles); 2997 RoleList newRoles = new RoleList (); 2998 newRoles.add(roleC2); 2999 3000 boolean caught = false; 3001 try 3002 { 3003 rs.setRoles(null, newRoles); 3004 } 3005 catch(IllegalArgumentException e) 3006 { 3007 caught = true; 3008 } 3009 if (caught == false) 3010 fail("setRoles allows null relation id"); 3011 3012 caught = false; 3013 try 3014 { 3015 rs.setRole("relationId", null); 3016 } 3017 catch(IllegalArgumentException e) 3018 { 3019 caught = true; 3020 } 3021 if (caught == false) 3022 fail("setRoles allows null role list"); 3023 3024 caught = false; 3025 try 3026 { 3027 rs.setRoles("rubbish", newRoles); 3028 } 3029 catch(RelationNotFoundException e) 3030 { 3031 caught = true; 3032 } 3033 if (caught == false) 3034 fail("setRoles allows invalid relation id"); 3035 3036 server.unregisterMBean(service); 3037 caught = false; 3038 try 3039 { 3040 rs.setRoles("relationId", newRoles); 3041 } 3042 catch(RelationServiceNotRegisteredException e) 3043 { 3044 caught = true; 3045 } 3046 if (caught == false) 3047 fail("FAILS IN RI: setRoles allowed when not registered"); 3048 } 3049 finally 3050 { 3051 MBeanServerFactory.releaseMBeanServer(server); 3052 } 3053 } 3054 3055 3058 public void testUpdateRoleMapErrors() throws Exception 3059 { 3060 MBeanServer server = MBeanServerFactory.createMBeanServer(); 3061 try 3062 { 3063 ObjectName service = createRelationService("test:type=service", server); 3064 RelationService rs = (RelationService ) services.get(service); 3065 createRelationTypeB(service); 3066 createRolesB(server); 3067 RoleList roleList = new RoleList (); 3068 roleList.add(roleB1); 3069 roleList.add(roleB2); 3070 rs.createRelation("relationId", "relationTypeB", roleList); 3071 3072 boolean caught = false; 3073 try 3074 { 3075 rs.updateRoleMap(null, roleB1, new ArrayList ()); 3076 } 3077 catch (IllegalArgumentException e) 3078 { 3079 caught = true; 3080 } 3081 if (caught == false) 3082 fail("updateRoleMap allows null relation id"); 3083 3084 caught = false; 3085 try 3086 { 3087 rs.updateRoleMap("relationId", null, new ArrayList ()); 3088 } 3089 catch (IllegalArgumentException e) 3090 { 3091 caught = true; 3092 } 3093 if (caught == false) 3094 fail("updateRoleMap allows null role"); 3095 3096 caught = false; 3097 try 3098 { 3099 rs.updateRoleMap("rubbish", roleB1, new ArrayList ()); 3100 } 3101 catch (RelationNotFoundException e) 3102 { 3103 caught = true; 3104 } 3105 if (caught == false) 3106 fail("updateRoleMap allows invalid relation id"); 3107 3108 server.unregisterMBean(service); 3109 3110 caught = false; 3111 try 3112 { 3113 rs.updateRoleMap("relationId", roleB1, new ArrayList ()); 3114 } 3115 catch (RelationServiceNotRegisteredException e) 3116 { 3117 caught = true; 3118 } 3119 if (caught == false) 3120 fail("FAILS IN RI: updateRoleMap allowed when not registered"); 3121 } 3122 finally 3123 { 3124 MBeanServerFactory.releaseMBeanServer(server); 3125 } 3126 } 3127 3128 3130 private ObjectName createRelationService(String name, MBeanServer server) 3131 { 3132 ObjectName result = null; 3133 RelationService relationService = new RelationService (true); 3134 try 3135 { 3136 result = new ObjectName (name); 3137 services.put(result, relationService); 3138 if (server !=null) 3139 server.registerMBean(relationService, result); 3140 } 3141 catch(Exception e) 3142 { 3143 fail(e.toString()); 3144 } 3145 return result; 3146 } 3147 3148 private ObjectName addRelation(MBeanServer server, ObjectName service, 3149 RelationSupport support, String name) 3150 { 3151 ObjectName result = null; 3152 try 3153 { 3154 result = new ObjectName (name); 3155 server.registerMBean(support, result); 3156 if (service != null) 3157 { 3158 RelationService relationService = (RelationService ) services.get(service); 3159 relationService.addRelation(result); 3160 } 3161 } 3162 catch(Exception e) 3163 { 3164 fail(e.toString()); 3165 } 3166 return result; 3167 } 3168 3169 private RoleInfo createRoleInfo(String name, Class mbean, 3170 boolean read, boolean write, 3171 int min, int max) 3172 { 3173 RoleInfo result = null; 3174 try 3175 { 3176 result = new RoleInfo (name, mbean.getName(), read, write, min, max, ""); 3177 } 3178 catch(Exception e) 3179 { 3180 fail(e.toString()); 3181 } 3182 return result; 3183 } 3184 3185 private void createRelationType(ObjectName relationService, String name, RoleInfo [] roleInfos) 3186 { 3187 try 3188 { 3189 RelationService service = (RelationService ) services.get(relationService); 3190 service.createRelationType(name, roleInfos); 3191 } 3192 catch(Exception e) 3193 { 3194 fail(e.toString()); 3195 } 3196 } 3197 3198 private void compare(RoleList original, RoleList result) 3199 { 3200 assertEquals(original.size(), result.size()); 3201 Iterator iterator = original.iterator(); 3202 while (iterator.hasNext()) 3203 { 3204 Role originalRole = (Role ) iterator.next(); 3205 Iterator iterator2 = result.iterator(); 3206 while (iterator2.hasNext()) 3207 { 3208 Role resultRole = (Role ) iterator2.next(); 3209 if (originalRole.getRoleName().equals(resultRole.getRoleName())) 3210 { 3211 compare(originalRole, resultRole); 3212 iterator2.remove(); 3213 } 3214 } 3215 } 3216 assertEquals(0, result.size()); 3217 } 3218 3219 private void compare(Role original, Role result) 3220 { 3221 assertEquals(original.getRoleName(), result.getRoleName()); 3222 compareListOfObjectNames(original.getRoleValue(), result.getRoleValue()); 3223 } 3224 3225 private void compareListOfObjectNames(List original, List result) 3226 { 3227 assertEquals(original.size(), result.size()); 3228 Iterator iterator = original.iterator(); 3229 while (iterator.hasNext()) 3230 { 3231 ObjectName originalBean = (ObjectName ) iterator.next(); 3232 Iterator iterator2 = result.iterator(); 3233 while (iterator2.hasNext()) 3234 { 3235 ObjectName resultBean = (ObjectName ) iterator2.next(); 3236 if (originalBean.equals(resultBean)) 3237 { 3238 iterator2.remove(); 3239 } 3240 } 3241 } 3242 assertEquals(0, result.size()); 3243 } 3244 3245 private void compareListOfStrings(List original, List result) 3246 { 3247 assertEquals(original.size(), result.size()); 3248 Iterator iterator = original.iterator(); 3249 while (iterator.hasNext()) 3250 { 3251 String originalString = (String ) iterator.next(); 3252 Iterator iterator2 = result.iterator(); 3253 while (iterator2.hasNext()) 3254 { 3255 String resultString = (String ) iterator2.next(); 3256 if (originalString.equals(resultString)) 3257 { 3258 iterator2.remove(); 3259 } 3260 } 3261 } 3262 assertEquals(0, result.size()); 3263 } 3264 3265 private ObjectName createRoleValueBean(String name, Class mbean, MBeanServer server) 3266 { 3267 ObjectName result = null; 3268 try 3269 { 3270 result = new ObjectName (name); 3271 if (server != null) 3272 { 3273 server.registerMBean(mbean.newInstance(), result); 3274 } 3275 } 3276 catch(Exception e) 3277 { 3278 fail(e.toString()); 3279 } 3280 return result; 3281 } 3282 3283 private void checkResult(RoleResult result, HashMap infos, RoleList roles) 3284 { 3285 checkResolved(result.getRoles(), infos, roles); 3286 checkUnresolved(result.getRolesUnresolved(), infos, roles); 3287 } 3288 3289 private void checkResolved(RoleList resolved, HashMap infos, RoleList roles) 3290 { 3291 RoleList copy = (RoleList ) roles.clone(); 3292 Iterator iterator = resolved.iterator(); 3293 while (iterator.hasNext()) 3294 { 3295 Role role = (Role ) iterator.next(); 3296 String roleName = role.getRoleName(); 3297 RoleInfo info = (RoleInfo ) infos.get(roleName); 3298 if (info == null) 3299 fail("unknown role " + roleName); 3300 if (info.isReadable() == false) 3301 fail("role should not be readable " + roleName); 3302 Role original = removeRole(copy, roleName); 3303 compareListOfObjectNames(original.getRoleValue(), role.getRoleValue()); 3304 } 3305 3306 iterator = copy.iterator(); 3307 while (iterator.hasNext()) 3308 { 3309 Role role = (Role ) iterator.next(); 3310 String roleName = role.getRoleName(); 3311 RoleInfo info = (RoleInfo ) infos.get(roleName); 3312 if (info.isReadable() == true) 3313 fail("missing role " + roleName); 3314 } 3315 } 3316 3317 private void checkUnresolved(RoleUnresolvedList unresolved, HashMap infos, RoleList roles) 3318 { 3319 RoleList copy = (RoleList ) roles.clone(); 3320 Iterator iterator = unresolved.iterator(); 3321 while (iterator.hasNext()) 3322 { 3323 RoleUnresolved roleUnresolved = (RoleUnresolved ) iterator.next(); 3324 String roleName = roleUnresolved.getRoleName(); 3325 RoleInfo info = (RoleInfo ) infos.get(roleName); 3326 if (info == null) 3327 fail("unknown role " + roleName); 3328 if (info.isReadable() == true) 3329 fail("role should be readable " + roleName); 3330 removeRole(copy, roleName); 3331 } 3332 3333 iterator = copy.iterator(); 3334 while (iterator.hasNext()) 3335 { 3336 Role role = (Role ) iterator.next(); 3337 String roleName = role.getRoleName(); 3338 RoleInfo info = (RoleInfo ) infos.get(roleName); 3339 if (info.isReadable() == false) 3340 fail("missing unresolved role " + roleName); 3341 } 3342 } 3343 3344 private Role removeRole(RoleList roles, String roleName) 3345 { 3346 Iterator iterator = roles.iterator(); 3347 while (iterator.hasNext()) 3348 { 3349 Role role = (Role ) iterator.next(); 3350 if (role.getRoleName().equals(roleName)) 3351 { 3352 iterator.remove(); 3353 return role; 3354 } 3355 } 3356 fail("role was not in the original " + roleName); 3357 return null; 3358 } 3359 3360 private Role getRole(RoleList roles, String roleName) 3361 { 3362 Iterator iterator = roles.iterator(); 3363 while (iterator.hasNext()) 3364 { 3365 Role role = (Role ) iterator.next(); 3366 if (role.getRoleName().equals(roleName)) 3367 { 3368 return role; 3369 } 3370 } 3371 fail("role was not in the original " + roleName); 3372 return null; 3373 } 3374 3375 private void checkMBeans(Map result, RoleList roles) 3376 { 3377 Map expected = calcMBeanRoleMap(roles); 3379 3380 Iterator iterator = result.entrySet().iterator(); 3382 while (iterator.hasNext()) 3383 { 3384 Map.Entry entry = (Map.Entry ) iterator.next(); 3385 ObjectName key = (ObjectName ) entry.getKey(); 3386 ArrayList roleNames = (ArrayList ) entry.getValue(); 3387 ArrayList expectedNames = (ArrayList ) expected.get(key); 3388 if (expectedNames == null) 3389 fail("Unexpected object name " + key); 3390 compareListOfStrings(expectedNames, roleNames); 3391 expected.remove(key); 3392 } 3393 assertEquals(0, expected.size()); 3394 } 3395 3396 private Map calcMBeanRoleMap(RoleList roles) 3397 { 3398 HashMap result = new HashMap (); 3399 Iterator iterator = roles.iterator(); 3400 while (iterator.hasNext()) 3401 { 3402 Role role = (Role ) iterator.next(); 3403 String roleName = role.getRoleName(); 3404 ArrayList mbeans = (ArrayList ) role.getRoleValue(); 3405 Iterator iterator2 = mbeans.iterator(); 3406 while (iterator2.hasNext()) 3407 { 3408 ObjectName objectName = (ObjectName ) iterator2.next(); 3409 ArrayList names = (ArrayList ) result.get(objectName); 3410 if (names == null) 3411 { 3412 names = new ArrayList (); 3413 result.put(objectName, names); 3414 } 3415 3419 names.add(roleName); 3420 } 3421 } 3422 return result; 3423 } 3424 3425 private void createRolesA(MBeanServer server) 3426 { 3427 try 3428 { 3429 ArrayList roleA1Values = new ArrayList (); 3430 roleA1Values.add(createRoleValueBean("x:relation=a,role=1,bean=1", 3431 Trivial.class, server)); 3432 Role roleA1 = new Role ("roleA1", roleA1Values); 3433 rolesA = new RoleList (); 3434 rolesA.add(roleA1); 3435 } 3436 catch(Exception e) 3437 { 3438 fail(e.toString()); 3439 } 3440 } 3441 3442 private void createRelationTypeA(ObjectName relationService) 3443 { 3444 try 3445 { 3446 RoleInfo roleInfoA1 = createRoleInfo("roleA1", Trivial.class, true, true, 1, 1); 3447 RoleInfo [] roleInfos = new RoleInfo [] { roleInfoA1 }; 3448 createRelationType(relationService, "relationTypeA", roleInfos); 3449 for (int i=0; i < roleInfos.length; i++) 3450 roleInfosA.put(roleInfos[i].getName(), roleInfos[i]); 3451 } 3452 catch(Exception e) 3453 { 3454 fail(e.toString()); 3455 } 3456 } 3457 3458 private void createRolesB(MBeanServer server) 3459 { 3460 try 3461 { 3462 ArrayList roleB1Values = new ArrayList (); 3463 roleB1Values.add(createRoleValueBean("x:relation=b,role=1,bean=1", 3464 Trivial.class, server)); 3465 roleB1Values.add(createRoleValueBean("x:relation=b,role=1,bean=2", 3466 Trivial.class, server)); 3467 roleB1 = new Role ("roleB1", roleB1Values); 3468 3469 ArrayList roleB2Values = new ArrayList (); 3470 roleB2Values.add(createRoleValueBean("x:relation=b,role=2,bean=1", 3471 Trivial.class, server)); 3472 roleB2Values.add(createRoleValueBean("x:relation=b,role=2,bean=2", 3473 Trivial.class, server)); 3474 roleB2Values.add(createRoleValueBean("x:relation=b,role=2,bean=3", 3475 Trivial.class, server)); 3476 roleB2 = new Role ("roleB2", roleB2Values); 3477 3478 rolesB = new RoleList (); 3479 rolesB.add(roleB1); 3480 rolesB.add(roleB2); 3481 } 3482 catch(Exception e) 3483 { 3484 fail(e.toString()); 3485 } 3486 } 3487 3488 private void createRelationTypeB(ObjectName relationService) 3489 { 3490 try 3491 { 3492 RoleInfo roleInfoB1 = createRoleInfo("roleB1", Trivial.class, true, false, 1, 2); 3493 RoleInfo roleInfoB2 = createRoleInfo("roleB2", Trivial.class, false, true, 3, 4); 3494 RoleInfo [] roleInfos = new RoleInfo [] { roleInfoB1, roleInfoB2 }; 3495 createRelationType(relationService, "relationTypeB", roleInfos ); 3496 for (int i=0; i < roleInfos.length; i++) 3497 roleInfosB.put(roleInfos[i].getName(), roleInfos[i]); 3498 } 3499 catch(Exception e) 3500 { 3501 fail(e.toString()); 3502 } 3503 } 3504 3505 private void createRolesC(MBeanServer server) 3506 { 3507 try 3508 { 3509 ArrayList roleC1Values = new ArrayList (); 3510 roleC1Values.add(createRoleValueBean("x:relation=c,role=1,bean=1", 3511 Trivial.class, server)); 3512 roleC1Values.add(createRoleValueBean("x:relation=c,role=1,bean=1", 3513 Trivial.class, null)); 3514 roleC1 = new Role ("roleC1", roleC1Values); 3515 3516 ArrayList roleC2Values = new ArrayList (); 3517 roleC2Values.add(createRoleValueBean("x:relation=c,role=1,bean=1", 3518 Trivial.class, null)); 3519 roleC2Values.add(createRoleValueBean("x:relation=c,role=2,bean=1", 3520 Trivial.class, server)); 3521 roleC2Values.add(createRoleValueBean("x:relation=c,role=2,bean=2", 3522 Trivial.class, server)); 3523 roleC2 = new Role ("roleC2", roleC2Values); 3524 3525 rolesC = new RoleList (); 3526 rolesC.add(roleC1); 3527 rolesC.add(roleC2); 3528 } 3529 catch(Exception e) 3530 { 3531 fail(e.toString()); 3532 } 3533 } 3534 3535 private void createRelationTypeC(ObjectName relationService) 3536 { 3537 try 3538 { 3539 RoleInfo roleInfoC1 = createRoleInfo("roleC1", Trivial.class, true, false, 1, 2); 3540 RoleInfo roleInfoC2 = createRoleInfo("roleC2", Trivial.class, false, true, 3, 4); 3541 RoleInfo [] roleInfos = new RoleInfo [] { roleInfoC1, roleInfoC2 }; 3542 createRelationType(relationService, "relationTypeC", roleInfos ); 3543 for (int i=0; i < roleInfos.length; i++) 3544 roleInfosC.put(roleInfos[i].getName(), roleInfos[i]); 3545 } 3546 catch(Exception e) 3547 { 3548 fail(e.toString()); 3549 } 3550 } 3551 3552 private void createRolesCX(MBeanServer server) 3553 { 3554 try 3555 { 3556 ArrayList roleCX1Values = new ArrayList (); 3557 roleCX1Values.add(createRoleValueBean("x:relation=c,role=2,bean=1", 3558 Trivial.class, null)); 3559 roleCX1 = new Role ("roleC1", roleCX1Values); 3560 3561 ArrayList roleCX2Values = new ArrayList (); 3562 roleCX2Values.add(createRoleValueBean("x:relation=c,role=1,bean=1", 3563 Trivial.class, null)); 3564 roleCX2Values.add(createRoleValueBean("x:relation=c,role=1,bean=2", 3565 Trivial.class, server)); 3566 roleCX2Values.add(createRoleValueBean("x:relation=c,role=1,bean=3", 3567 Trivial.class, server)); 3568 roleCX2Values.add(createRoleValueBean("x:relation=c,role=1,bean=4", 3569 Trivial.class, server)); 3570 roleCX2 = new Role ("roleC2", roleCX2Values); 3571 3572 rolesCX = new RoleList (); 3573 rolesCX.add(roleCX1); 3574 rolesCX.add(roleCX2); 3575 } 3576 catch(Exception e) 3577 { 3578 fail(e.toString()); 3579 } 3580 } 3581 3582 private void createRelationTypeCX(ObjectName relationService) 3583 { 3584 try 3585 { 3586 RoleInfo roleInfoCX1 = createRoleInfo("roleC1", Trivial.class, true, false, 1, 2); 3587 RoleInfo roleInfoCX2 = createRoleInfo("roleC2", Trivial.class, false, true, 3, 4); 3588 RoleInfo [] roleInfos = new RoleInfo [] { roleInfoCX1, roleInfoCX2 }; 3589 createRelationType(relationService, "relationTypeCX", roleInfos ); 3590 for (int i=0; i < roleInfos.length; i++) 3591 roleInfosCX.put(roleInfos[i].getName(), roleInfos[i]); 3592 } 3593 catch(Exception e) 3594 { 3595 fail(e.toString()); 3596 } 3597 } 3598 3599 private void createRolesCZ(MBeanServer server) 3600 { 3601 try 3602 { 3603 ArrayList roleCZ2Values = new ArrayList (); 3604 roleCZ2Values.add(createRoleValueBean("x:relation=c,role=1,bean=1", 3605 Trivial.class, null)); 3606 roleCZ2 = new Role ("roleC2", roleCZ2Values); 3607 3608 rolesCZ = new RoleList (); 3609 rolesCZ.add(roleCZ2); 3610 } 3611 catch(Exception e) 3612 { 3613 fail(e.toString()); 3614 } 3615 } 3616 3617 private void createRolesCZZ(MBeanServer server) 3618 { 3619 try 3620 { 3621 ArrayList roleCZZValues = new ArrayList (); 3622 roleCZZValues.add(createRoleValueBean("x:relation=c,role=1,bean=1", 3623 Trivial.class, null)); 3624 roleCZZValues.add(createRoleValueBean("x:relation=c,role=1,bean=2", 3625 Trivial.class, null)); 3626 roleCZZValues.add(createRoleValueBean("x:relation=c,role=1,bean=3", 3627 Trivial.class, null)); 3628 roleCZZValues.add(createRoleValueBean("x:relation=c,role=1,bean=4", 3629 Trivial.class, null)); 3630 roleCZZValues.add(createRoleValueBean("x:relation=c,role=1,bean=5", 3631 Trivial.class, server)); 3632 roleCZZ = new Role ("roleC2", roleCZZValues); 3633 3634 rolesCZZ = new RoleList (); 3635 rolesCZZ.add(roleCZZ); 3636 3637 ArrayList roleCZZZValues = new ArrayList (); 3638 roleCZZZValues.add(createRoleValueBean("x:relation=c,role=1x,bean=1", 3639 Trivial.class, null)); 3640 roleCZZZValues.add(createRoleValueBean("x:relation=c,role=1x,bean=2", 3641 Trivial.class, null)); 3642 roleCZZZValues.add(createRoleValueBean("x:relation=c,role=1x,bean=3", 3643 Trivial.class, null)); 3644 roleCZZZ = new Role ("roleC2", roleCZZZValues); 3645 3646 rolesCZZZ = new RoleList (); 3647 rolesCZZZ.add(roleCZZZ); 3648 } 3649 catch(Exception e) 3650 { 3651 fail(e.toString()); 3652 } 3653 } 3654 3655 private class Listener 3656 implements NotificationListener 3657 { 3658 String type; 3659 HashSet notifications = new HashSet (); 3660 public Listener(String type) 3661 { 3662 this.type = type; 3663 } 3664 public void handleNotification(Notification n, Object h) 3665 { 3666 notifications.add(n); 3667 } 3668 public RelationNotification check(int size) 3669 { 3670 RelationNotification result = null; 3671 assertEquals(size, notifications.size()); 3672 Iterator iterator = notifications.iterator(); 3673 while (iterator.hasNext()) 3674 { 3675 RelationNotification rn = (RelationNotification ) iterator.next(); 3676 assertEquals(type, rn.getType()); 3677 result = rn; 3678 } 3679 return result; 3680 } 3681 } 3682} 3683 | Popular Tags |