KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > compliance > relation > RelationServiceTestCase


1 /*
2  * JBoss, the OpenSource J2EE webOS
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7
8 package test.compliance.relation;
9
10 import test.compliance.relation.support.Trivial;
11
12 import junit.framework.TestCase;
13
14 import java.util.ArrayList;
15 import java.util.HashMap;
16 import java.util.HashSet;
17 import java.util.Iterator;
18 import java.util.List;
19 import java.util.Map;
20
21 import javax.management.InstanceNotFoundException;
22 import javax.management.MBeanNotificationInfo;
23 import javax.management.MBeanServer;
24 import javax.management.Notification;
25 import javax.management.NotificationListener;
26 import javax.management.MBeanServerFactory;
27 import javax.management.ObjectName;
28
29 import javax.management.relation.InvalidRelationIdException;
30 import javax.management.relation.InvalidRelationServiceException;
31 import javax.management.relation.InvalidRelationTypeException;
32 import javax.management.relation.InvalidRoleValueException;
33 import javax.management.relation.RelationNotFoundException;
34 import javax.management.relation.RelationNotification;
35 import javax.management.relation.RelationService;
36 import javax.management.relation.RelationServiceNotRegisteredException;
37 import javax.management.relation.RelationSupport;
38 import javax.management.relation.RelationTypeNotFoundException;
39 import javax.management.relation.RelationTypeSupport;
40 import javax.management.relation.Role;
41 import javax.management.relation.RoleInfo;
42 import javax.management.relation.RoleInfoNotFoundException;
43 import javax.management.relation.RoleList;
44 import javax.management.relation.RoleNotFoundException;
45 import javax.management.relation.RoleResult;
46 import javax.management.relation.RoleStatus;
47 import javax.management.relation.RoleUnresolved;
48 import javax.management.relation.RoleUnresolvedList;
49
50 /**
51  * Relation Service tests
52  *
53  * //TODO test internal relations
54  * //TODO test relations as mbeans in roles
55  * //TODO test multiple relation services (avoid future mods adding static data)
56  *
57  * @author <a HREF="mailto:Adrian.Brock@HappeningTimes.com">Adrian Brock</a>.
58  */

59 public class RelationServiceTestCase
60    extends TestCase
61 {
62    // Constants -----------------------------------------------------------------
63

64    // Attributes ----------------------------------------------------------------
65

66    HashMap services = new HashMap();
67
68    RoleList rolesA;
69    HashMap roleInfosA = new HashMap();
70    Role roleB1;
71    Role roleB2;
72    RoleList rolesB;
73    HashMap roleInfosB = new HashMap();
74    Role roleC1;
75    Role roleC2;
76    RoleList rolesC;
77    HashMap roleInfosC = new HashMap();
78    Role roleCX1;
79    Role roleCX2;
80    RoleList rolesCX;
81    HashMap roleInfosCX = new HashMap();
82    Role roleCZ2;
83    RoleList rolesCZ;
84    HashMap roleInfosCZ = new HashMap();
85    Role roleCZZ;
86    RoleList rolesCZZ;
87    HashMap roleInfosCZZ = new HashMap();
88    Role roleCZZZ;
89    RoleList rolesCZZZ;
90    HashMap roleInfosCZZZ = new HashMap();
91
92    // Constructor ---------------------------------------------------------------
93

94    /**
95     * Construct the test
96     */

97    public RelationServiceTestCase(String s)
98    {
99       super(s);
100    }
101
102    // Tests ---------------------------------------------------------------------
103

104    /**
105     * Test the constructor
106     */

107    public void testConstructor() throws Exception
108    {
109       RelationService rs = null;
110       rs = new RelationService(true);
111       assertEquals(true, rs.getPurgeFlag());
112
113       rs = new RelationService(false);
114       assertEquals(false, rs.getPurgeFlag());
115    }
116
117    /**
118     * Test add a relation
119     */

120    public void testAddRelation() throws Exception
121    {
122       MBeanServer server = MBeanServerFactory.createMBeanServer();
123       try
124       {
125          ObjectName service = createRelationService("test:type=service", server);
126          createRelationTypeB(service);
127          createRolesB(server);
128          RelationSupport support = null;
129          ObjectName rsupp = null;
130          String result = null;
131          Listener listener = new Listener(RelationNotification.RELATION_MBEAN_CREATION);
132          RelationService rs = (RelationService) services.get(service);
133          server.addNotificationListener(service, listener, null, null);
134          support = new RelationSupport("id", service, server, "relationTypeB",
135                                        rolesB);
136          rsupp = new ObjectName("test:add=relation");
137          server.registerMBean(support, rsupp);
138          rs.addRelation(rsupp);
139          result = rs.isRelation(rsupp);
140          assertEquals("id", result);
141          listener.check(1);
142       }
143       finally
144       {
145          MBeanServerFactory.releaseMBeanServer(server);
146       }
147    }
148
149    /**
150     * Test add a relation errors
151     */

152    public void testAddRelationErrors() throws Exception
153    {
154       MBeanServer server = MBeanServerFactory.createMBeanServer();
155       try
156       {
157          ObjectName service = createRelationService("test:type=service", null);
158          RelationService rs = (RelationService) services.get(service);
159          createRelationTypeB(service);
160          createRolesB(server);
161          RelationSupport support = null;
162          ObjectName name = null;
163          support = new RelationSupport("id", service, server, "relationTypeB",
164                                           rolesB);
165          name = new ObjectName("test:type=relation");
166          server.registerMBean(support, name);
167
168          boolean caught = false;
169          try
170          {
171             rs.addRelation(null);
172          }
173          catch (IllegalArgumentException e)
174          {
175             caught = true;
176          }
177          if (caught == false)
178             fail("addRelation allows null relation");
179
180          caught = false;
181          try
182          {
183             rs.addRelation(name);
184          }
185          catch (RelationServiceNotRegisteredException e)
186          {
187             caught = true;
188          }
189          if (caught == false)
190             fail("addRelation allowed when not registered");
191
192          ObjectName badRelation = null;
193          server.registerMBean(rs, service);
194          badRelation = new ObjectName("test:type=bad");
195          server.registerMBean(new Trivial(), badRelation);
196
197          caught = false;
198          try
199          {
200             rs.addRelation(badRelation);
201          }
202          catch (NoSuchMethodException e)
203          {
204             caught = true;
205          }
206          if (caught == false)
207             fail("addRelation allowed when not a relation");
208
209          caught = false;
210          try
211          {
212             rs.addRelation(name);
213             rs.addRelation(name);
214          }
215          catch (InvalidRelationIdException e)
216          {
217             caught = true;
218          }
219          if (caught == false)
220             fail("addRelation allows duplicate relation ids");
221
222          rs.removeRelation("id");
223          server.unregisterMBean(name);
224
225          caught = false;
226          try
227          {
228             rs.addRelation(name);
229          }
230          catch (InstanceNotFoundException e)
231          {
232             caught = true;
233          }
234          if (caught == false)
235             fail("addRelation allows unregistered relation");
236
237          ObjectName service2 = createRelationService("test:type=service2", null);
238          RelationService rs2 = (RelationService) services.get(service);
239          createRelationTypeB(service2);
240          RelationSupport support2 = null;
241          ObjectName name2 = null;
242          support = new RelationSupport("id", service2, server, "relationTypeB",
243                                           rolesB);
244          name2 = new ObjectName("test:type=relation2");
245          server.registerMBean(support, name2);
246
247          caught = false;
248          try
249          {
250             rs.addRelation(name2);
251          }
252          catch (InvalidRelationServiceException e)
253          {
254             caught = true;
255          }
256          if (caught == false)
257             fail("addRelation allows registration in the wrong relation service");
258
259          support = new RelationSupport("id", service, server, "relationTypeX",
260                                           rolesB);
261          name = new ObjectName("test:type=relationX");
262          server.registerMBean(support, name);
263
264          caught = false;
265          try
266          {
267             rs.addRelation(name);
268          }
269          catch (RelationTypeNotFoundException e)
270          {
271             caught = true;
272          }
273          if (caught == false)
274             fail("addRelation allows registration with invalid relation type");
275
276          createRelationTypeC(service);
277          createRolesC(server);
278          createRolesCZ(server);
279          support = new RelationSupport("idC", service, server, "relationTypeB",
280                                           rolesC);
281          name = new ObjectName("test:type=relationC");
282          server.registerMBean(support, name);
283
284          caught = false;
285          try
286          {
287             rs.addRelation(name);
288          }
289          catch (RoleNotFoundException e)
290          {
291             caught = true;
292          }
293          if (caught == false)
294             fail("addRelation allows registration with invalid role name");
295
296          support = new RelationSupport("idCZ", service, server, "relationTypeC",
297                                           rolesCZ);
298          name = new ObjectName("test:type=relationCZ");
299          server.registerMBean(support, name);
300
301          caught = false;
302          try
303          {
304             rs.addRelation(name);
305          }
306          catch (InvalidRoleValueException e)
307          {
308             caught = true;
309          }
310          if (caught == false)
311             fail("addRelation allows registration with invalid role value");
312       }
313       finally
314       {
315          MBeanServerFactory.releaseMBeanServer(server);
316       }
317    }
318
319    /**
320     * Test add a relation type
321     */

322    public void testAddRelationType() throws Exception
323    {
324       RoleInfo roleInfo1 = null;
325       RoleInfo roleInfo2 = null;
326       RoleInfo[] roleInfos = null;
327       RelationService rs = null;
328       ArrayList result = null;
329       RoleInfo result1 = null;
330       RoleInfo result2 = null;
331       roleInfo1 = new RoleInfo("roleInfo1", Trivial.class.getName());
332       roleInfo2 = new RoleInfo("roleInfo2", Trivial.class.getName());
333       roleInfos = new RoleInfo[] { roleInfo1, roleInfo2 };
334       RelationTypeSupport rtsupp = new RelationTypeSupport("RelationTypeName",
335                                                   roleInfos);
336       rs = new RelationService(true);
337       rs.addRelationType(rtsupp);
338       result = (ArrayList) rs.getRoleInfos("RelationTypeName");
339       result1 = rs.getRoleInfo("RelationTypeName", "roleInfo1");
340       result2 = rs.getRoleInfo("RelationTypeName", "roleInfo2");
341
342       // Check the roleInfos
343
assertEquals(2, result.size());
344       assertEquals(roleInfo1.toString(), result1.toString());
345       assertEquals(roleInfo2.toString(), result2.toString());
346    }
347
348    /**
349     * Test create relation type errors
350     */

351    public void testAddRelationTypeErrors() throws Exception
352    {
353       RoleInfo roleInfo1 = null;
354       RoleInfo roleInfo2 = null;
355       RoleInfo[] roleInfos = null;
356       RelationService rs = null;
357       RelationTypeSupport rtsupp = null;
358
359       // Null relation type
360
boolean caught = false;
361       try
362       {
363          roleInfo1 = new RoleInfo("roleInfo1", Trivial.class.getName());
364          roleInfo2 = new RoleInfo("roleInfo2", Trivial.class.getName());
365          roleInfos = new RoleInfo[] { roleInfo1, roleInfo2 };
366          rs = new RelationService(true);
367          rs.addRelationType(null);
368       }
369       catch (IllegalArgumentException e)
370       {
371          caught = true;
372       }
373       if (caught == false)
374          fail("addRelationType allows null relation type");
375
376       // Duplicate relation types
377
caught = false;
378       try
379       {
380          rtsupp = new RelationTypeSupport("RelationTypeName", roleInfos);
381          rs.addRelationType(rtsupp);
382          rs.addRelationType(rtsupp);
383       }
384       catch (InvalidRelationTypeException e)
385       {
386          caught = true;
387       }
388       if (caught == false)
389          fail("addRelationType allows duplication relation types");
390
391       // Duplicate role infos
392
caught = false;
393       try
394       {
395          roleInfos[1] = roleInfos[0];
396          rtsupp = new RelationTypeSupport("RelationTypeName1", roleInfos);
397          rs.addRelationType(rtsupp);
398       }
399       catch (InvalidRelationTypeException e)
400       {
401          caught = true;
402       }
403       if (caught == false)
404          fail("addRelationType allows duplicate role names");
405
406       // Null role info
407
caught = false;
408       try
409       {
410          roleInfos[1] = null;
411          rtsupp = new RelationTypeSupport("RelationTypeName1", roleInfos);
412          rs.addRelationType(rtsupp);
413       }
414       catch (InvalidRelationTypeException e)
415       {
416          caught = true;
417       }
418       if (caught == false)
419          fail("addRelationType allows null role info");
420
421       // No role info
422
caught = false;
423       try
424       {
425          rtsupp = new RelationTypeSupport("RelationTypeName1", new RoleInfo[0]);
426          rs.addRelationType(rtsupp);
427       }
428       catch (InvalidRelationTypeException e)
429       {
430          caught = true;
431       }
432       if (caught == false)
433          fail("addRelationType allows no role info");
434    }
435
436    /**
437     * Test check role reading
438     */

439    public void testCheckRoleReadingExternal() throws Exception
440    {
441       MBeanServer server = MBeanServerFactory.createMBeanServer();
442       try
443       {
444          ObjectName service = createRelationService("test:type=service", server);
445          createRelationTypeB(service);
446          createRolesB(server);
447          RelationSupport support = null;
448          Integer readB1 = null;
449          Integer readB2 = null;
450          RelationService rs = (RelationService) services.get(service);
451          support = new RelationSupport("id", service, server,
452                                           "relationTypeB", rolesB);
453          addRelation(server, service, support, "test:type=support");
454          readB1 = rs.checkRoleReading("roleB1", "relationTypeB");
455          readB2 = rs.checkRoleReading("roleB2", "relationTypeB");
456
457          assertEquals(0, readB1.intValue());
458          assertEquals(RoleStatus.ROLE_NOT_READABLE, readB2.intValue());
459       }
460       finally
461       {
462          MBeanServerFactory.releaseMBeanServer(server);
463       }
464    }
465
466    /**
467     * Test check role reading errors
468     */

469    public void testCheckRoleReadingErrors() throws Exception
470    {
471       ObjectName service = createRelationService("test:type=service", null);
472       RelationService rs = (RelationService) services.get(service);
473       createRelationTypeB(service);
474       createRolesB(null);
475
476       boolean caught = false;
477       try
478       {
479          rs.checkRoleReading(null, "relationTypeB");
480       }
481       catch(IllegalArgumentException e)
482       {
483          caught = true;
484       }
485       if (caught == false)
486          fail("checkRoleReading allows null role name");
487
488       caught = false;
489       try
490       {
491          rs.checkRoleReading("roleB1", null);
492       }
493       catch(IllegalArgumentException e)
494       {
495          caught = true;
496       }
497       if (caught == false)
498          fail("checkRoleReading allows null relation name");
499
500       caught = false;
501       try
502       {
503          rs.checkRoleReading("roleB1", "rubbish");
504       }
505       catch(RelationTypeNotFoundException e)
506       {
507          caught = true;
508       }
509       if (caught == false)
510          fail("checkRoleReading allows invalid relation type name");
511    }
512
513    /**
514     * Test check role writing
515     */

516    public void testCheckRoleWritingExternal() throws Exception
517    {
518       MBeanServer server = MBeanServerFactory.createMBeanServer();
519       try
520       {
521          ObjectName service = createRelationService("test:type=service", server);
522          createRelationTypeB(service);
523          createRolesB(server);
524          RelationSupport support = null;
525          Integer writeB1normal = null;
526          Integer writeB2normal = null;
527          Integer writeB1init = null;
528          Integer writeB2init = null;
529          RelationService rs = (RelationService) services.get(service);
530          support = new RelationSupport("id", service, server,
531                                        "relationTypeB", rolesB);
532          addRelation(server, service, support, "test:type=support");
533          writeB1normal = rs.checkRoleWriting(roleB1, "relationTypeB", new Boolean(false));
534          writeB2normal = rs.checkRoleWriting(roleB2, "relationTypeB", new Boolean(false));
535          writeB1init = rs.checkRoleWriting(roleB1, "relationTypeB", new Boolean(true));
536          writeB2init = rs.checkRoleWriting(roleB2, "relationTypeB", new Boolean(true));
537          assertEquals(RoleStatus.ROLE_NOT_WRITABLE, writeB1normal.intValue());
538          assertEquals(0, writeB2normal.intValue());
539          assertEquals(0, writeB1init.intValue());
540          assertEquals(0, writeB2init.intValue());
541       }
542       finally
543       {
544          MBeanServerFactory.releaseMBeanServer(server);
545       }
546    }
547
548    /**
549     * Test check role writing errors
550     */

551    public void testCheckRoleWritingErrors() throws Exception
552    {
553       ObjectName service = createRelationService("test:type=service", null);
554       RelationService rs = (RelationService) services.get(service);
555       createRelationTypeB(service);
556       createRolesB(null);
557
558       boolean caught = false;
559       try
560       {
561          rs.checkRoleWriting(null, "relationTypeB", new Boolean(true));
562       }
563       catch(IllegalArgumentException e)
564       {
565          caught = true;
566       }
567       if (caught == false)
568          fail("checkRoleWriting allows null role name");
569
570       caught = false;
571       try
572       {
573          rs.checkRoleWriting(roleB1, null, new Boolean(true));
574       }
575       catch(IllegalArgumentException e)
576       {
577          caught = true;
578       }
579       if (caught == false)
580          fail("checkRoleWriting allows null relation name");
581
582       caught = false;
583       try
584       {
585          rs.checkRoleWriting(roleB1, "relationTypeB", null);
586       }
587       catch(IllegalArgumentException e)
588       {
589          caught = true;
590       }
591       if (caught == false)
592          fail("checkRoleWriting allows null init flag");
593
594       caught = false;
595       try
596       {
597          rs.checkRoleWriting(roleB1, "rubbish", new Boolean(true));
598       }
599       catch(RelationTypeNotFoundException e)
600       {
601          caught = true;
602       }
603       if (caught == false)
604          fail("checkRoleWriting allows invalid relation type name");
605    }
606
607    /**
608     * Test create relation
609     */

610    public void testCreateRelation() throws Exception
611    {
612       MBeanServer server = MBeanServerFactory.createMBeanServer();
613       try
614       {
615          ObjectName service = createRelationService("test:type=service", server);
616          createRelationTypeB(service);
617          createRolesB(server);
618          RelationSupport support = null;
619          Listener listener = new Listener(RelationNotification.RELATION_BASIC_CREATION);
620          RelationService rs = (RelationService) services.get(service);
621          server.addNotificationListener(service, listener, null, null);
622          rs.createRelation("id", "relationTypeB", rolesB);
623          boolean result = rs.hasRelation("id").booleanValue();
624          assertEquals(true, result);
625          listener.check(1);
626       }
627       finally
628       {
629          MBeanServerFactory.releaseMBeanServer(server);
630       }
631    }
632
633    /**
634     * Test create relation errors
635     */

636    public void testCreateRelationErrors() throws Exception
637    {
638       MBeanServer server = MBeanServerFactory.createMBeanServer();
639       try
640       {
641          ObjectName service = createRelationService("test:type=service", server);
642          RelationService rs = (RelationService) services.get(service);
643          createRelationTypeB(service);
644          createRolesB(server);
645          createRelationTypeC(service);
646          createRolesC(server);
647          createRolesCZ(server);
648          createRolesCZZ(server);
649
650          boolean caught = false;
651          try
652          {
653             rs.createRelation(null, "relationTypeC", rolesC);
654          }
655          catch(IllegalArgumentException e)
656          {
657             caught = true;
658          }
659          if (caught == false)
660             fail("createRelation allows null relation id");
661         
662          caught = false;
663          try
664          {
665             rs.createRelation("relationId", null, rolesC);
666          }
667          catch(IllegalArgumentException e)
668          {
669             caught = true;
670          }
671          if (caught == false)
672             fail("createRelation allows null relation type name");
673         
674          caught = false;
675          try
676          {
677             rs.createRelation("relationId", "rubbish", rolesC);
678          }
679          catch(RelationTypeNotFoundException e)
680          {
681             caught = true;
682          }
683          if (caught == false)
684             fail("createRelation allows invalid relation type name");
685         
686          caught = false;
687          try
688          {
689             RoleList roleList = new RoleList();
690             roleList.add(roleC1);
691             roleList.add(roleB1);
692             rs.createRelation("relationId", "relationTypeC", roleList);
693          }
694          catch(RoleNotFoundException e)
695          {
696             caught = true;
697          }
698          if (caught == false)
699             fail("createRelation allows invalid role name");
700
701          caught = false;
702          try
703          {
704             RoleList roleList = new RoleList();
705             roleList.add(roleC1);
706             roleList.add(roleCZ2);
707             rs.createRelation("relationId", "relationTypeC", roleList);
708          }
709          catch(InvalidRoleValueException e)
710          {
711             caught = true;
712          }
713          if (caught == false)
714             fail("createRelation allows a role below the minimum");
715         
716          caught = false;
717          try
718          {
719             RoleList roleList = new RoleList();
720             roleList.add(roleC1);
721             roleList.add(roleCZZ);
722             rs.createRelation("relationId", "relationTypeC", roleList);
723          }
724          catch(InvalidRoleValueException e)
725          {
726             caught = true;
727          }
728          if (caught == false)
729             fail("createRelation allows a role above the maximum");
730         
731          caught = false;
732          try
733          {
734             RoleList roleList = new RoleList();
735             roleList.add(roleC1);
736             roleList.add(roleCZZZ);
737             rs.createRelation("relationId", "relationTypeC", roleList);
738          }
739          catch(InvalidRoleValueException e)
740          {
741             caught = true;
742          }
743          if (caught == false)
744             fail("createRelation allows a role with unregistered beans");
745         
746          caught = false;
747          try
748          {
749             rs.createRelation("relationId", "relationTypeC", rolesC);
750             rs.createRelation("relationId", "relationTypeC", rolesC);
751          }
752          catch(InvalidRelationIdException e)
753          {
754             caught = true;
755          }
756          if (caught == false)
757             fail("createRelation allows duplicate relation id");
758
759          server.unregisterMBean(service);
760          caught = false;
761          try
762          {
763             rs.createRelation("relationId2", "relationTypeC", rolesC);
764          }
765          catch(RelationServiceNotRegisteredException e)
766          {
767             caught = true;
768          }
769          if (caught == false)
770             fail("FAILS IN RI: createRelation allowed when not registered");
771       }
772       finally
773       {
774          MBeanServerFactory.releaseMBeanServer(server);
775       }
776    }
777
778    /**
779     * Test create relation type, getRoleInfo and getRoleInfos
780     */

781    public void testCreationRelationType() throws Exception
782    {
783       RoleInfo roleInfo1 = null;
784       RoleInfo roleInfo2 = null;
785       RoleInfo[] roleInfos = null;
786       RelationService rs = null;
787       ArrayList result = null;
788       RoleInfo result1 = null;
789       RoleInfo result2 = null;
790       roleInfo1 = new RoleInfo("roleInfo1", Trivial.class.getName());
791       roleInfo2 = new RoleInfo("roleInfo2", Trivial.class.getName());
792       roleInfos = new RoleInfo[] { roleInfo1, roleInfo2 };
793       rs = new RelationService(true);
794       rs.createRelationType("RelationTypeName", roleInfos);
795       result = (ArrayList) rs.getRoleInfos("RelationTypeName");
796       result1 = rs.getRoleInfo("RelationTypeName", "roleInfo1");
797       result2 = rs.getRoleInfo("RelationTypeName", "roleInfo2");
798
799       // Check the roleInfos
800
assertEquals(2, result.size());
801       assertEquals(roleInfo1.toString(), result1.toString());
802       assertEquals(roleInfo2.toString(), result2.toString());
803    }
804
805    /**
806     * Test create relation type errors
807     */

808    public void testCreateRelationTypeErrors() throws Exception
809    {
810       RoleInfo roleInfo1 = null;
811       RoleInfo roleInfo2 = null;
812       RoleInfo[] roleInfos = null;
813       RelationService rs = null;
814
815       // Null relation type name
816
boolean caught = false;
817       try
818       {
819          roleInfo1 = new RoleInfo("roleInfo1", Trivial.class.getName());
820          roleInfo2 = new RoleInfo("roleInfo2", Trivial.class.getName());
821          roleInfos = new RoleInfo[] { roleInfo1, roleInfo2 };
822          rs = new RelationService(true);
823          rs.createRelationType(null, roleInfos);
824       }
825       catch (IllegalArgumentException e)
826       {
827          caught = true;
828       }
829       if (caught == false)
830          fail("createRelationType allows null relation type name");
831
832       // Null role infos
833
caught = false;
834       try
835       {
836          rs.createRelationType("relationTypeName", null);
837       }
838       catch (IllegalArgumentException e)
839       {
840          caught = true;
841       }
842       if (caught == false)
843          fail("createRelationType allows null role infos");
844
845       // Duplicate relation types
846
caught = false;
847       try
848       {
849          rs.createRelationType("relationTypeName", roleInfos);
850          rs.createRelationType("relationTypeName", roleInfos);
851       }
852       catch (InvalidRelationTypeException e)
853       {
854          caught = true;
855       }
856       if (caught == false)
857          fail("createRelationType allows duplicate relation type names");
858
859       // Duplicate role infos
860
caught = false;
861       try
862       {
863          roleInfos[1] = roleInfos[0];
864          rs.createRelationType("relationTypeName1", roleInfos);
865       }
866       catch (InvalidRelationTypeException e)
867       {
868          caught = true;
869       }
870       if (caught == false)
871          fail("createRelationType allows duplicate role names");
872
873       // Null role info
874
caught = false;
875       try
876       {
877          roleInfos[1] = null;
878          rs.createRelationType("relationTypeName1", roleInfos);
879       }
880       catch (InvalidRelationTypeException e)
881       {
882          caught = true;
883       }
884       if (caught == false)
885          fail("createRelationType allows null role info");
886
887       // No role info
888
caught = false;
889       try
890       {
891          rs.createRelationType("relationTypeName1", new RoleInfo[0]);
892       }
893       catch (InvalidRelationTypeException e)
894       {
895          caught = true;
896       }
897       if (caught == false)
898          fail("createRelationType allows no role info");
899    }
900
901    /**
902     * Test getRoleInfo errors
903     */

904    public void testGetRoleInfoErrors() throws Exception
905    {
906       RoleInfo roleInfo1 = null;
907       RoleInfo roleInfo2 = null;
908       RoleInfo[] roleInfos = null;
909       RelationService rs = null;
910       ArrayList result = null;
911       RoleInfo result1 = null;
912       roleInfo1 = new RoleInfo("roleInfo1", Trivial.class.getName());
913       roleInfo2 = new RoleInfo("roleInfo2", Trivial.class.getName());
914       roleInfos = new RoleInfo[] { roleInfo1, roleInfo2 };
915       rs = new RelationService(true);
916       rs.createRelationType("RelationTypeName", roleInfos);
917
918       boolean caught = false;
919       try
920       {
921          rs.getRoleInfo(null, "roleInfo1");
922       }
923       catch (IllegalArgumentException e)
924       {
925          caught = true;
926       }
927       if (caught == false)
928          fail("getRoleInfo allows null relation type name");
929
930       caught = false;
931       try
932       {
933          rs.getRoleInfo("RelationTypeName", null);
934       }
935       catch (IllegalArgumentException e)
936       {
937          caught = true;
938       }
939       if (caught == false)
940          fail("getRoleInfo allows null role info name");
941
942       caught = false;
943       try
944       {
945          rs.getRoleInfo("RelationTypeNameX", "roleInfo1");
946       }
947       catch (RelationTypeNotFoundException e)
948       {
949          caught = true;
950       }
951       if (caught == false)
952          fail("getRoleInfo allows non-existent relation type name");
953
954       caught = false;
955       try
956       {
957          rs.getRoleInfo("RelationTypeName", "roleInfoX");
958       }
959       catch (RoleInfoNotFoundException e)
960       {
961          caught = true;
962       }
963       if (caught == false)
964          fail("getRoleInfo allows non-existent role info name");
965    }
966
967    /**
968     * Test getRoleInfos errors
969     */

970    public void testGetRoleInfosErrors() throws Exception
971    {
972       RoleInfo roleInfo1 = null;
973       RoleInfo roleInfo2 = null;
974       RoleInfo[] roleInfos = null;
975       RelationService rs = null;
976       ArrayList result = null;
977       RoleInfo result1 = null;
978       roleInfo1 = new RoleInfo("roleInfo1", Trivial.class.getName());
979       roleInfo2 = new RoleInfo("roleInfo2", Trivial.class.getName());
980       roleInfos = new RoleInfo[] { roleInfo1, roleInfo2 };
981       rs = new RelationService(true);
982       rs.createRelationType("RelationTypeName", roleInfos);
983
984       boolean caught = false;
985       try
986       {
987          rs.getRoleInfos(null);
988       }
989       catch (IllegalArgumentException e)
990       {
991          caught = true;
992       }
993       if (caught == false)
994          fail("getRoleInfos allows null relation type name");
995
996       caught = false;
997       try
998       {
999          rs.getRoleInfos("RelationTypeNameX");
1000      }
1001      catch (RelationTypeNotFoundException e)
1002      {
1003         caught = true;
1004      }
1005      if (caught == false)
1006         fail("getRoleInfos allows non-existent relation type name");
1007   }
1008
1009   /**
1010    * Test find associated mbeans
1011    *
1012    * REVIEW: This test needs more thought
1013    */

1014   public void testFindAssociatedMBeansExternal() throws Exception
1015   {
1016      MBeanServer server = MBeanServerFactory.createMBeanServer();
1017      try
1018      {
1019         ObjectName service = createRelationService("test:type=service", server);
1020         createRelationTypeC(service);
1021         createRolesC(server);
1022         createRelationTypeCX(service);
1023         createRolesCX(server);
1024         Map result1 = null;
1025         Map result2 = null;
1026         Map result3 = null;
1027         Map result4 = null;
1028         Map result5 = null;
1029         Map result6 = null;
1030         Map result7 = null;
1031         Map result8 = null;
1032         Map result9 = null;
1033         Map result10 = null;
1034         Map result11 = null;
1035         Map result12 = null;
1036         Map result13 = null;
1037         Map result14 = null;
1038         Map result15 = null;
1039         Map result16 = null;
1040         RelationSupport supportCX = new RelationSupport("idcx", service, server,
1041                                       "relationTypeCX", rolesCX);
1042         RelationSupport supportC = new RelationSupport("idc", service, server,
1043                                       "relationTypeC", rolesC);
1044         addRelation(server, service, supportCX, "test:type=supportCX");
1045         addRelation(server, service, supportC, "test:type=supportC");
1046         RelationService rs = (RelationService) services.get(service);
1047         result1 = rs.findAssociatedMBeans(new ObjectName("x:relation=c,role=1,bean=1"), null, null);
1048         result2 = rs.findAssociatedMBeans(new ObjectName("x:relation=c,role=1,bean=1"), null, null);
1049         result3 = rs.findAssociatedMBeans(new ObjectName("x:relation=c,role=1,bean=1"), "relationTypeCX", null);
1050         result4 = rs.findAssociatedMBeans(new ObjectName("x:relation=c,role=1,bean=1"), "relationTypeC", null);
1051         result5 = rs.findAssociatedMBeans(new ObjectName("x:relation=c,role=1,bean=1"), null, "roleC1");
1052         result6 = rs.findAssociatedMBeans(new ObjectName("x:relation=c,role=1,bean=1"), null, "roleC2");
1053         result7 = rs.findAssociatedMBeans(new ObjectName("x:relation=c,role=1,bean=1"), null, "roleC1");
1054         result8 = rs.findAssociatedMBeans(new ObjectName("x:relation=c,role=1,bean=1"), null, "roleC2");
1055         result9 = rs.findAssociatedMBeans(new ObjectName("x:relation=c,role=1,bean=1"), "relationTypeCX", "roleC1");
1056         result10 = rs.findAssociatedMBeans(new ObjectName("x:relation=c,role=1,bean=1"), "relationTypeC", "roleC1");
1057         result11 = rs.findAssociatedMBeans(new ObjectName("x:relation=c,role=1,bean=1"), "relationTypeCX", "roleC2");
1058         result12 = rs.findAssociatedMBeans(new ObjectName("x:relation=c,role=1,bean=1"), "relationTypeC", "roleC2");
1059         result13 = rs.findAssociatedMBeans(new ObjectName("x:relation=c,role=1,bean=1"), "relationTypeCX", "roleC1");
1060         result14 = rs.findAssociatedMBeans(new ObjectName("x:relation=c,role=1,bean=1"), "relationTypeC", "roleC1");
1061         result15 = rs.findAssociatedMBeans(new ObjectName("x:relation=c,role=1,bean=1"), "relationTypeCX", "roleC2");
1062         result16 = rs.findAssociatedMBeans(new ObjectName("x:relation=c,role=1,bean=1"), "relationTypeC", "roleC2");
1063         assertEquals(5, result1.size());
1064         assertEquals(5, result2.size());
1065         assertEquals(4, result3.size());
1066         assertEquals(2, result4.size());
1067         assertEquals(2, result5.size());
1068         assertEquals(5, result6.size());
1069         assertEquals(2, result7.size());
1070         assertEquals(5, result8.size());
1071         assertEquals(0, result9.size());
1072         assertEquals(2, result10.size());
1073         assertEquals(4, result11.size());
1074         assertEquals(2, result12.size());
1075         assertEquals(0, result13.size());
1076         assertEquals(2, result14.size());
1077         assertEquals(4, result15.size());
1078         assertEquals(2, result16.size());
1079      }
1080      finally
1081      {
1082         MBeanServerFactory.releaseMBeanServer(server);
1083      }
1084   }
1085
1086   /**
1087    * Test find associated mbeans errors
1088    */

1089   public void testFindAssociatedMBeansErrors() throws Exception
1090   {
1091      RelationService rs = new RelationService(true);
1092      
1093      boolean caught = false;
1094      try
1095      {
1096         rs.findAssociatedMBeans(null, null, null);
1097      }
1098      catch (IllegalArgumentException e)
1099      {
1100         caught = true;
1101      }
1102      if (caught == false)
1103         fail ("findAssociatedMBeans allows a null mbean name");
1104   }
1105
1106   /**
1107    * Test find referencing relations
1108    *
1109    * REVIEW: This test needs more thought
1110    */

1111   public void testFindReferencingRelationsExternal() throws Exception
1112   {
1113      MBeanServer server = MBeanServerFactory.createMBeanServer();
1114      try
1115      {
1116         ObjectName service = createRelationService("test:type=service", server);
1117         createRelationTypeC(service);
1118         createRolesC(server);
1119         createRelationTypeCX(service);
1120         createRolesCX(server);
1121         Map result1 = null;
1122         Map result2 = null;
1123         Map result3 = null;
1124         Map result4 = null;
1125         Map result5 = null;
1126         Map result6 = null;
1127         Map result7 = null;
1128         Map result8 = null;
1129         Map result9 = null;
1130         Map result10 = null;
1131         Map result11 = null;
1132         Map result12 = null;
1133         Map result13 = null;
1134         Map result14 = null;
1135         Map result15 = null;
1136         Map result16 = null;
1137         RelationSupport supportCX = new RelationSupport("idcx", service, server,
1138                                       "relationTypeCX", rolesCX);
1139         RelationSupport supportC = new RelationSupport("idc", service, server,
1140                                       "relationTypeC", rolesC);
1141         addRelation(server, service, supportCX, "test:type=supportCX");
1142         addRelation(server, service, supportC, "test:type=supportC");
1143         RelationService rs = (RelationService) services.get(service);
1144         result1 = rs.findReferencingRelations(new ObjectName("x:relation=c,role=1,bean=1"), null, null);
1145         result2 = rs.findReferencingRelations(new ObjectName("x:relation=c,role=1,bean=1"), null, null);
1146         result3 = rs.findReferencingRelations(new ObjectName("x:relation=c,role=1,bean=1"), "relationTypeCX", null);
1147         result4 = rs.findReferencingRelations(new ObjectName("x:relation=c,role=1,bean=1"), "relationTypeC", null);
1148         result5 = rs.findReferencingRelations(new ObjectName("x:relation=c,role=1,bean=1"), null, "roleC1");
1149         result6 = rs.findReferencingRelations(new ObjectName("x:relation=c,role=1,bean=1"), null, "roleC2");
1150         result7 = rs.findReferencingRelations(new ObjectName("x:relation=c,role=1,bean=1"), null, "roleC1");
1151         result8 = rs.findReferencingRelations(new ObjectName("x:relation=c,role=1,bean=1"), null, "roleC2");
1152         result9 = rs.findReferencingRelations(new ObjectName("x:relation=c,role=1,bean=1"), "relationTypeCX", "roleC1");
1153         result10 = rs.findReferencingRelations(new ObjectName("x:relation=c,role=1,bean=1"), "relationTypeC", "roleC1");
1154         result11 = rs.findReferencingRelations(new ObjectName("x:relation=c,role=1,bean=1"), "relationTypeCX", "roleC2");
1155         result12 = rs.findReferencingRelations(new ObjectName("x:relation=c,role=1,bean=1"), "relationTypeC", "roleC2");
1156         result13 = rs.findReferencingRelations(new ObjectName("x:relation=c,role=1,bean=1"), "relationTypeCX", "roleC1");
1157         result14 = rs.findReferencingRelations(new ObjectName("x:relation=c,role=1,bean=1"), "relationTypeC", "roleC1");
1158         result15 = rs.findReferencingRelations(new ObjectName("x:relation=c,role=1,bean=1"), "relationTypeCX", "roleC2");
1159         result16 = rs.findReferencingRelations(new ObjectName("x:relation=c,role=1,bean=1"), "relationTypeC", "roleC2");
1160         assertEquals(2, result1.size());
1161         assertEquals(2, result2.size());
1162         assertEquals(1, result3.size());
1163         assertEquals(1, result4.size());
1164         assertEquals(1, result5.size());
1165         assertEquals(2, result6.size());
1166         assertEquals(1, result7.size());
1167         assertEquals(2, result8.size());
1168         assertEquals(0, result9.size());
1169         assertEquals(1, result10.size());
1170         assertEquals(1, result11.size());
1171         assertEquals(1, result12.size());
1172         assertEquals(0, result13.size());
1173         assertEquals(1, result14.size());
1174         assertEquals(1, result15.size());
1175         assertEquals(1, result16.size());
1176      }
1177      finally
1178      {
1179         MBeanServerFactory.releaseMBeanServer(server);
1180      }
1181   }
1182
1183   /**
1184    * Test find referencing relations errors
1185    */

1186   public void testFindReferencingRelationsErrors() throws Exception
1187   {
1188      RelationService rs = new RelationService(true);
1189      
1190      boolean caught = false;
1191      try
1192      {
1193         rs.findReferencingRelations(null, null, null);
1194      }
1195      catch (IllegalArgumentException e)
1196      {
1197         caught = true;
1198      }
1199      if (caught == false)
1200         fail ("findReferencingRelations allows a null mbean name");
1201   }
1202
1203   /**
1204    * Test find relations of types
1205    *
1206    * REVIEW: This test needs more thought
1207    */

1208   public void testFindRelationsOfTypeExternal() throws Exception
1209   {
1210      MBeanServer server = MBeanServerFactory.createMBeanServer();
1211      try
1212      {
1213         ObjectName service = createRelationService("test:type=service", server);
1214         createRelationTypeA(service);
1215         createRolesA(server);
1216         createRelationTypeB(service);
1217         createRolesB(server);
1218         createRelationTypeC(service);
1219         createRolesC(server);
1220         createRelationTypeCX(service);
1221         createRolesCX(server);
1222         List result1 = null;
1223         List result2 = null;
1224         List result3 = null;
1225         List result4 = null;
1226         RelationSupport supportA1 = new RelationSupport("ida1", service, server,
1227                                          "relationTypeA", rolesA);
1228         RelationSupport supportA2 = new RelationSupport("ida2", service, server,
1229                                          "relationTypeA", rolesA);
1230         RelationSupport supportCX = new RelationSupport("idcx", service, server,
1231                                          "relationTypeCX", rolesCX);
1232         RelationSupport supportC = new RelationSupport("idc", service, server,
1233                                          "relationTypeC", rolesC);
1234         addRelation(server, service, supportA1, "test:type=supportA1");
1235         addRelation(server, service, supportA2, "test:type=supportA2");
1236         addRelation(server, service, supportCX, "test:type=supportCX");
1237         addRelation(server, service, supportC, "test:type=supportC");
1238         RelationService rs = (RelationService) services.get(service);
1239         result1 = rs.findRelationsOfType("relationTypeA");
1240         result2 = rs.findRelationsOfType("relationTypeB");
1241         result3 = rs.findRelationsOfType("relationTypeC");
1242         result4 = rs.findRelationsOfType("relationTypeCX");
1243         assertEquals(2, result1.size());
1244         assertEquals(0, result2.size());
1245         assertEquals(1, result3.size());
1246         assertEquals(1, result4.size());
1247      }
1248      finally
1249      {
1250         MBeanServerFactory.releaseMBeanServer(server);
1251      }
1252   }
1253
1254   /**
1255    * Test find relations of type errors
1256    */

1257   public void testFindRelationsOfTypeErrors() throws Exception
1258   {
1259      RelationService rs = new RelationService(true);
1260      
1261      boolean caught = false;
1262      try
1263      {
1264         rs.findRelationsOfType(null);
1265      }
1266      catch (IllegalArgumentException e)
1267      {
1268         caught = true;
1269      }
1270      if (caught == false)
1271         fail ("findRelationsOfType allows a null relation type name");
1272
1273      caught = false;
1274      try
1275      {
1276         rs.findRelationsOfType("rubbish");
1277      }
1278      catch (RelationTypeNotFoundException e)
1279      {
1280         caught = true;
1281      }
1282      if (caught == false)
1283         fail ("findRelationsOfType allows an invalid relation type name");
1284   }
1285
1286   /**
1287    * Test get all relations ids
1288    *
1289    * REVIEW: This test needs more thought
1290    */

1291   public void testGetAllRelationsIdsExternal() throws Exception
1292   {
1293      MBeanServer server = MBeanServerFactory.createMBeanServer();
1294      try
1295      {
1296         ObjectName service = createRelationService("test:type=service", server);
1297         createRelationTypeA(service);
1298         createRolesA(server);
1299         createRelationTypeB(service);
1300         createRolesB(server);
1301         createRelationTypeC(service);
1302         createRolesC(server);
1303         createRelationTypeCX(service);
1304         createRolesCX(server);
1305         List result = null;
1306         RelationSupport supportA1 = new RelationSupport("ida1", service, server,
1307                                          "relationTypeA", rolesA);
1308         RelationSupport supportA2 = new RelationSupport("ida2", service, server,
1309                                          "relationTypeA", rolesA);
1310         RelationSupport supportCX = new RelationSupport("idcx", service, server,
1311                                          "relationTypeCX", rolesCX);
1312         RelationSupport supportC = new RelationSupport("idc", service, server,
1313                                          "relationTypeC", rolesC);
1314         addRelation(server, service, supportA1, "test:type=supportA1");
1315         addRelation(server, service, supportA2, "test:type=supportA2");
1316         addRelation(server, service, supportCX, "test:type=supportCX");
1317         addRelation(server, service, supportC, "test:type=supportC");
1318         RelationService rs = (RelationService) services.get(service);
1319         result = rs.getAllRelationIds();
1320         assertEquals(4, result.size());
1321         assertEquals(true, result.contains("ida1"));
1322         assertEquals(true, result.contains("ida2"));
1323         assertEquals(true, result.contains("idcx"));
1324         assertEquals(true, result.contains("idc"));
1325      }
1326      finally
1327      {
1328         MBeanServerFactory.releaseMBeanServer(server);
1329      }
1330   }
1331
1332   /**
1333    * Test get all relation type names
1334    */

1335   public void testGetAllRelationTypeNames() throws Exception
1336   {
1337      RelationService rs = new RelationService(true);
1338      assertEquals(0, rs.getAllRelationTypeNames().size());
1339
1340      RoleInfo roleInfo1 = null;
1341      RoleInfo roleInfo2 = null;
1342      RoleInfo[] roleInfos = null;
1343      roleInfo1 = new RoleInfo("roleInfo1", Trivial.class.getName());
1344      roleInfo2 = new RoleInfo("roleInfo2", Trivial.class.getName());
1345      roleInfos = new RoleInfo[] { roleInfo1, roleInfo2 };
1346      rs.createRelationType("name1", roleInfos);
1347      rs.createRelationType("name2", roleInfos);
1348      rs.createRelationType("name3", roleInfos);
1349
1350      ArrayList result = (ArrayList) rs.getAllRelationTypeNames();
1351      assertEquals(3, result.size());
1352      assertEquals(true, result.contains("name1"));
1353      assertEquals(true, result.contains("name2"));
1354      assertEquals(true, result.contains("name3"));
1355   }
1356
1357   /**
1358    * Test get all roles
1359    */

1360   public void testGetAllRolesExternal() throws Exception
1361   {
1362      MBeanServer server = MBeanServerFactory.createMBeanServer();
1363      try
1364      {
1365         ObjectName service = createRelationService("test:type=service", server);
1366         createRelationTypeB(service);
1367         createRolesB(server);
1368         RoleResult result = null;
1369         RelationSupport support = new RelationSupport("id", service, server,
1370                                         "relationTypeB", rolesB);
1371         addRelation(server, service, support, "test:type=support");
1372         result = support.getAllRoles();
1373         checkResult(result, roleInfosB, rolesB);
1374      }
1375      finally
1376      {
1377         MBeanServerFactory.releaseMBeanServer(server);
1378      }
1379   }
1380
1381   /**
1382    * Test get all roles errors
1383    */

1384   public void testGetAllRolesErrors() throws Exception
1385   {
1386      MBeanServer server = MBeanServerFactory.createMBeanServer();
1387      try
1388      {
1389         ObjectName service = createRelationService("test:type=service", server);
1390         RelationService rs = (RelationService) services.get(service);
1391         createRelationTypeC(service);
1392         createRolesC(server);
1393         RoleList roles = new RoleList();
1394         roles.add(roleC1);
1395         roles.add(roleC2);
1396         rs.createRelation("relationId", "relationTypeC", roles);
1397         String[] roleNames = new String[] {"roleC1"};
1398
1399         boolean caught = false;
1400         try
1401         {
1402            rs.getAllRoles(null);
1403         }
1404         catch(IllegalArgumentException e)
1405         {
1406            caught = true;
1407         }
1408         if (caught == false)
1409            fail("getAllRoles allows null relation id");
1410        
1411         caught = false;
1412         try
1413         {
1414            rs.getAllRoles("rubbish");
1415         }
1416         catch(RelationNotFoundException e)
1417         {
1418            caught = true;
1419         }
1420         if (caught == false)
1421            fail("getAllRoles allows invalid relation id");
1422
1423         server.unregisterMBean(service);
1424         caught = false;
1425         try
1426         {
1427            rs.getAllRoles("relationId");
1428         }
1429         catch(RelationServiceNotRegisteredException e)
1430         {
1431            caught = true;
1432         }
1433         if (caught == false)
1434            fail("FAILS IN RI: getAllRoles allowed when not registered");
1435      }
1436      finally
1437      {
1438         MBeanServerFactory.releaseMBeanServer(server);
1439      }
1440   }
1441
1442   /**
1443    * Test get Notification info
1444    */

1445   public void testGetNotificationInfo()
1446   {
1447      RelationService rs = new RelationService(true);
1448      MBeanNotificationInfo[] mbni = rs.getNotificationInfo();
1449      assertEquals(1, mbni.length);
1450      HashSet types = new HashSet();
1451      types.add(RelationNotification.RELATION_BASIC_CREATION);
1452      types.add(RelationNotification.RELATION_BASIC_REMOVAL);
1453      types.add(RelationNotification.RELATION_BASIC_UPDATE);
1454      types.add(RelationNotification.RELATION_MBEAN_CREATION);
1455      types.add(RelationNotification.RELATION_MBEAN_REMOVAL);
1456      types.add(RelationNotification.RELATION_MBEAN_UPDATE);
1457      String[] mbniTypes = mbni[0].getNotifTypes();
1458      assertEquals(types.size(), mbniTypes.length);
1459      for (int i = 0; i < mbniTypes.length; i++)
1460      {
1461         if (types.contains(mbniTypes[i]) == false)
1462            fail("Unexpected relation notification type: " + mbniTypes[i]);
1463      }
1464   }
1465
1466   /**
1467    * Test get/set Purge Flag
1468    */

1469   public void testGetSetPurgeFlag() throws Exception
1470   {
1471      MBeanServer server = MBeanServerFactory.createMBeanServer();
1472      try
1473      {
1474         RelationService rs = null;
1475         ObjectName name = null;
1476         rs = new RelationService(true);
1477         name = new ObjectName("test:type = rs");
1478         server.registerMBean(rs, name);
1479         assertEquals(true, rs.getPurgeFlag());
1480         rs.setPurgeFlag(false);
1481         assertEquals(false, rs.getPurgeFlag());
1482         rs.setPurgeFlag(true);
1483         assertEquals(true, rs.getPurgeFlag());
1484      }
1485      finally
1486      {
1487         MBeanServerFactory.releaseMBeanServer(server);
1488      }
1489   }
1490
1491   /**
1492    * Test get Referenced MBeans
1493    */

1494   public void testGetReferencedMBeansExternal() throws Exception
1495   {
1496      MBeanServer server = MBeanServerFactory.createMBeanServer();
1497      try
1498      {
1499         ObjectName service = createRelationService("test:type=service", server);
1500         createRelationTypeC(service);
1501         createRolesC(server);
1502         Map result = null;
1503         RelationSupport support = new RelationSupport("id", service, server,
1504                                         "relationTypeC", rolesC);
1505         addRelation(server, service, support, "test:type=support");
1506         result = support.getReferencedMBeans();
1507         checkMBeans(result, rolesC);
1508      }
1509      finally
1510      {
1511         MBeanServerFactory.releaseMBeanServer(server);
1512      }
1513   }
1514
1515   /**
1516    * Test get referenced mbeans errors
1517    */

1518   public void testGetReferencedMBeansErrors() throws Exception
1519   {
1520      MBeanServer server = MBeanServerFactory.createMBeanServer();
1521      try
1522      {
1523         ObjectName service = createRelationService("test:type=service", server);
1524         RelationService rs = (RelationService) services.get(service);
1525         createRelationTypeC(service);
1526         createRolesC(server);
1527         RoleList roles = new RoleList();
1528         roles.add(roleC1);
1529         roles.add(roleC2);
1530         rs.createRelation("relationId", "relationTypeC", roles);
1531         String[] roleNames = new String[] {"roleC1"};
1532
1533         boolean caught = false;
1534         try
1535         {
1536            rs.getReferencedMBeans(null);
1537         }
1538         catch(IllegalArgumentException e)
1539         {
1540            caught = true;
1541         }
1542         if (caught == false)
1543            fail("getReferencedMBeans allows null relation id");
1544        
1545         caught = false;
1546         try
1547         {
1548            rs.getReferencedMBeans("rubbish");
1549         }
1550         catch(RelationNotFoundException e)
1551         {
1552            caught = true;
1553         }
1554         if (caught == false)
1555            fail("getReferencedMBeans allows invalid relation id");
1556      }
1557      finally
1558      {
1559         MBeanServerFactory.releaseMBeanServer(server);
1560      }
1561   }
1562
1563   /**
1564    * Test get Relation Type Names
1565    */

1566   public void testGetRelationTypeNameExternal() throws Exception
1567   {
1568      MBeanServer server = MBeanServerFactory.createMBeanServer();
1569      try
1570      {
1571         ObjectName service = createRelationService("test:type=service", server);
1572         RelationService rs = (RelationService) services.get(service);
1573         createRelationTypeB(service);
1574         createRolesB(server);
1575         createRelationTypeC(service);
1576         createRolesC(server);
1577         RelationSupport relB = null;
1578         RelationSupport relC = null;
1579         String resultB = null;
1580         String resultC = null;
1581         relB = new RelationSupport("idB", service, server,
1582                                       "relationTypeB", rolesB);
1583         addRelation(server, service, relB, "test:type=supportB");
1584         relC = new RelationSupport("idC", service, server,
1585                                       "relationTypeC", rolesC);
1586         addRelation(server, service, relC, "test:type=supportC");
1587         resultB = rs.getRelationTypeName("idB");
1588         resultC = rs.getRelationTypeName("idC");
1589         assertEquals("relationTypeB", resultB);
1590         assertEquals("relationTypeC", resultC);
1591      }
1592      finally
1593      {
1594         MBeanServerFactory.releaseMBeanServer(server);
1595      }
1596   }
1597
1598   /**
1599    * Test get relation type name errors
1600    */

1601   public void testGetRelationTypeNameErrors() throws Exception
1602   {
1603      MBeanServer server = MBeanServerFactory.createMBeanServer();
1604      try
1605      {
1606         ObjectName service = createRelationService("test:type=service", server);
1607         RelationService rs = (RelationService) services.get(service);
1608         createRelationTypeC(service);
1609         createRolesC(server);
1610         RoleList roles = new RoleList();
1611         roles.add(roleC1);
1612         roles.add(roleC2);
1613         rs.createRelation("relationId", "relationTypeC", roles);
1614         String[] roleNames = new String[] {"roleC1"};
1615
1616         boolean caught = false;
1617         try
1618         {
1619            rs.getRelationTypeName(null);
1620         }
1621         catch(IllegalArgumentException e)
1622         {
1623            caught = true;
1624         }
1625         if (caught == false)
1626            fail("getRelationTypeName allows null relation id");
1627        
1628         caught = false;
1629         try
1630         {
1631            rs.getRelationTypeName("rubbish");
1632         }
1633         catch(RelationNotFoundException e)
1634         {
1635            caught = true;
1636         }
1637         if (caught == false)
1638            fail("getRelationTypeName allows invalid relation id");
1639      }
1640      finally
1641      {
1642         MBeanServerFactory.releaseMBeanServer(server);
1643      }
1644   }
1645
1646   /**
1647    * Test get Role
1648    */

1649   public void testGetRoleExternal() throws Exception
1650   {
1651      MBeanServer server = MBeanServerFactory.createMBeanServer();
1652      try
1653      {
1654         ObjectName service = createRelationService("test:type=service", server);
1655         createRelationTypeC(service);
1656         createRolesC(server);
1657         List result = null;
1658         RelationSupport support = new RelationSupport("id", service, server,
1659                                         "relationTypeC", rolesC);
1660         addRelation(server, service, support, "test:type=support");
1661         result = support.getRole("roleC1");
1662         compareListOfObjectNames(getRole(rolesC, "roleC1").getRoleValue(), result);
1663      }
1664      finally
1665      {
1666         MBeanServerFactory.releaseMBeanServer(server);
1667      }
1668   }
1669
1670   /**
1671    * Test get role errors
1672    */

1673   public void testGetRoleErrors() throws Exception
1674   {
1675      MBeanServer server = MBeanServerFactory.createMBeanServer();
1676      try
1677      {
1678         ObjectName service = createRelationService("test:type=service", server);
1679         RelationService rs = (RelationService) services.get(service);
1680         createRelationTypeC(service);
1681         createRolesC(server);
1682         RoleList roles = new RoleList();
1683         roles.add(roleC1);
1684         roles.add(roleC2);
1685         rs.createRelation("relationId", "relationTypeC", roles);
1686
1687         boolean caught = false;
1688         try
1689         {
1690            rs.getRole(null, "roleC1");
1691         }
1692         catch(IllegalArgumentException e)
1693         {
1694            caught = true;
1695         }
1696         if (caught == false)
1697            fail("getRole allows null relation id");
1698        
1699         caught = false;
1700         try
1701         {
1702            rs.getRole("relationId", null);
1703         }
1704         catch(IllegalArgumentException e)
1705         {
1706            caught = true;
1707         }
1708         if (caught == false)
1709            fail("getRole allows null role");
1710        
1711         caught = false;
1712         try
1713         {
1714            rs.getRole("rubbish", "roleC1");
1715         }
1716         catch(RelationNotFoundException e)
1717         {
1718            caught = true;
1719         }
1720         if (caught == false)
1721            fail("getRole allows invalid relation id");
1722        
1723         caught = false;
1724         try
1725         {
1726            rs.getRole("relationId", "rubbish");
1727         }
1728         catch(RoleNotFoundException e)
1729         {
1730            caught = true;
1731         }
1732         if (caught == false)
1733            fail("getRole allows invalid role name");
1734        
1735         caught = false;
1736         try
1737         {
1738            rs.getRole("relationId", "roleC2");
1739         }
1740         catch(RoleNotFoundException e)
1741         {
1742            caught = true;
1743         }
1744         if (caught == false)
1745            fail("getRole allows unreadable role");
1746
1747         server.unregisterMBean(service);
1748         caught = false;
1749         try
1750         {
1751            rs.getRole("relationId", "roleC1");
1752         }
1753         catch(RelationServiceNotRegisteredException e)
1754         {
1755            caught = true;
1756         }
1757         if (caught == false)
1758            fail("FAILS IN RI: getRole allowed when not registered");
1759      }
1760      finally
1761      {
1762         MBeanServerFactory.releaseMBeanServer(server);
1763      }
1764   }
1765
1766   /**
1767    * Test get Role Cardinality
1768    */

1769   public void testGetRoleCardinalityExternal() throws Exception
1770   {
1771      MBeanServer server = MBeanServerFactory.createMBeanServer();
1772      try
1773      {
1774         ObjectName service = createRelationService("test:type=service", server);
1775         createRelationTypeC(service);
1776         createRolesC(server);
1777         Integer result = null;
1778         RelationSupport support = null;
1779         support = new RelationSupport("id", service, server,
1780                                          "relationTypeC", rolesC);
1781         addRelation(server, service, support, "test:type=support");
1782         result = support.getRoleCardinality("roleC1");
1783         assertEquals(2, result.intValue());
1784
1785         result = support.getRoleCardinality("roleC2");
1786         assertEquals(3, result.intValue());
1787      }
1788      finally
1789      {
1790         MBeanServerFactory.releaseMBeanServer(server);
1791      }
1792   }
1793
1794   /**
1795    * Test get role cardinality errors
1796    */

1797   public void testGetRoleCardinalityErrors() throws Exception
1798   {
1799      MBeanServer server = MBeanServerFactory.createMBeanServer();
1800      try
1801      {
1802         ObjectName service = createRelationService("test:type=service", server);
1803         RelationService rs = (RelationService) services.get(service);
1804         createRelationTypeC(service);
1805         createRolesC(server);
1806         RoleList roles = new RoleList();
1807         roles.add(roleC1);
1808         roles.add(roleC2);
1809         rs.createRelation("relationId", "relationTypeC", roles);
1810
1811         boolean caught = false;
1812         try
1813         {
1814            rs.getRoleCardinality(null, "roleC1");
1815         }
1816         catch(IllegalArgumentException e)
1817         {
1818            caught = true;
1819         }
1820         if (caught == false)
1821            fail("getRoleCardinality allows null relation id");
1822        
1823         caught = false;
1824         try
1825         {
1826            rs.getRoleCardinality("relationId", null);
1827         }
1828         catch(IllegalArgumentException e)
1829         {
1830            caught = true;
1831         }
1832         if (caught == false)
1833            fail("getRoleCardinality allows null role");
1834        
1835         caught = false;
1836         try
1837         {
1838            rs.getRoleCardinality("rubbish", "roleC1");
1839         }
1840         catch(RelationNotFoundException e)
1841         {
1842            caught = true;
1843         }
1844         if (caught == false)
1845            fail("getRoleCardinality allows invalid relation id");
1846        
1847         caught = false;
1848         try
1849         {
1850            rs.getRoleCardinality("relationId", "rubbish");
1851         }
1852         catch(RoleNotFoundException e)
1853         {
1854            caught = true;
1855         }
1856         if (caught == false)
1857            fail("getRoleCardinality allows invalid role name");
1858      }
1859      finally
1860      {
1861         MBeanServerFactory.releaseMBeanServer(server);
1862      }
1863   }
1864
1865   /**
1866    * Test get Roles
1867    */

1868   public void testGetRolesExternal() throws Exception
1869   {
1870      MBeanServer server = MBeanServerFactory.createMBeanServer();
1871      try
1872      {
1873         ObjectName service = createRelationService("test:type=service", server);
1874         RelationService rs = (RelationService) services.get(service);
1875         createRelationTypeC(service);
1876         createRolesC(server);
1877         RoleResult result = null;
1878         RelationSupport support = null;
1879         support = new RelationSupport("id", service, server,
1880                                       "relationTypeC", rolesC);
1881         addRelation(server, service, support, "test:type=support");
1882         result = rs.getRoles("id", new String[] {"roleC1", "roleC2" });
1883         checkResult(result, roleInfosC, rolesC);
1884
1885         result = rs.getRoles("id", new String[] {"roleC1" });
1886         RoleList resolved = result.getRoles();
1887         assertEquals(1, resolved.size());
1888         assertEquals(0, result.getRolesUnresolved().size());
1889         compare(getRole(rolesC, "roleC1"), (Role) resolved.get(0));
1890
1891         result = rs.getRoles("id", new String[] {"roleC2" });
1892         RoleUnresolvedList unresolved = result.getRolesUnresolved();
1893         assertEquals(0, result.getRoles().size());
1894         assertEquals(1, unresolved.size());
1895         assertEquals("roleC2", ((RoleUnresolved)unresolved.get(0)).getRoleName());
1896      }
1897      finally
1898      {
1899         MBeanServerFactory.releaseMBeanServer(server);
1900      }
1901   }
1902
1903   /**
1904    * Test get roles errors
1905    */

1906   public void testGetRolesErrors() throws Exception
1907   {
1908      MBeanServer server = MBeanServerFactory.createMBeanServer();
1909      try
1910      {
1911         ObjectName service = createRelationService("test:type=service", server);
1912         RelationService rs = (RelationService) services.get(service);
1913         createRelationTypeC(service);
1914         createRolesC(server);
1915         RoleList roles = new RoleList();
1916         roles.add(roleC1);
1917         roles.add(roleC2);
1918         rs.createRelation("relationId", "relationTypeC", roles);
1919         String[] roleNames = new String[] {"roleC1"};
1920
1921         boolean caught = false;
1922         try
1923         {
1924            rs.getRoles(null, roleNames);
1925         }
1926         catch(IllegalArgumentException e)
1927         {
1928            caught = true;
1929         }
1930         if (caught == false)
1931            fail("getRoles allows null relation id");
1932        
1933         caught = false;
1934         try
1935         {
1936            rs.getRoles("relationId", null);
1937         }
1938         catch(IllegalArgumentException e)
1939         {
1940            caught = true;
1941         }
1942         if (caught == false)
1943            fail("getRoles allows null role name array");
1944        
1945         caught = false;
1946         try
1947         {
1948            rs.getRoles("rubbish", roleNames);
1949         }
1950         catch(RelationNotFoundException e)
1951         {
1952            caught = true;
1953         }
1954         if (caught == false)
1955            fail("getRoles allows invalid relation id");
1956
1957         server.unregisterMBean(service);
1958         caught = false;
1959         try
1960         {
1961            rs.getRoles("relationId", roleNames);
1962         }
1963         catch(RelationServiceNotRegisteredException e)
1964         {
1965            caught = true;
1966         }
1967         if (caught == false)
1968            fail("FAILS IN RI: getRoles allowed when not registered");
1969      }
1970      finally
1971      {
1972         MBeanServerFactory.releaseMBeanServer(server);
1973      }
1974   }
1975
1976   /**
1977    * Test Has Relation
1978    */

1979   public void testHasRelationExternal() throws Exception
1980   {
1981      MBeanServer server = MBeanServerFactory.createMBeanServer();
1982      try
1983      {
1984         ObjectName service = createRelationService("test:type=service", server);
1985         createRelationTypeC(service);
1986         createRolesC(server);
1987         RelationSupport support = null;
1988         Boolean result1 = null;
1989         Boolean result2 = null;
1990         Boolean result3 = null;
1991         Boolean result4 = null;
1992         Boolean result5 = null;
1993         Boolean result6 = null;
1994         support = new RelationSupport("id1", service, server,
1995                                       "relationTypeC", rolesC);
1996         addRelation(server, service, support, "test:type=support1");
1997         support = new RelationSupport("id2", service, server,
1998                                       "relationTypeC", rolesC);
1999         addRelation(server, service, support, "test:type=support2");
2000         RelationService rs = (RelationService) services.get(service);
2001         result1 = rs.hasRelation("id1");
2002         result2 = rs.hasRelation("id2");
2003         result3 = rs.hasRelation("id3");
2004         rs.removeRelation("id2");
2005         result4 = rs.hasRelation("id1");
2006         result5 = rs.hasRelation("id2");
2007         result6 = rs.hasRelation("id3");
2008         assertEquals(true, result1.booleanValue());
2009         assertEquals(true, result2.booleanValue());
2010         assertEquals(false, result3.booleanValue());
2011         assertEquals(true, result4.booleanValue());
2012         assertEquals(false, result5.booleanValue());
2013         assertEquals(false, result6.booleanValue());
2014      }
2015      finally
2016      {
2017         MBeanServerFactory.releaseMBeanServer(server);
2018      }
2019   }
2020
2021   /**
2022    * Test Has Relation Errors
2023    */

2024   public void testHasRelationErrors() throws Exception
2025   {
2026      RelationService rs = new RelationService(true);
2027
2028      boolean caught = false;
2029      try
2030      {
2031         rs.hasRelation(null);
2032      }
2033      catch(IllegalArgumentException e)
2034      {
2035         caught = true;
2036      }
2037      if (caught == false)
2038         fail("hasRelation allows null relation id");
2039   }
2040
2041   /**
2042    * Test Is Active
2043    */

2044   public void testIsActive() throws Exception
2045   {
2046      MBeanServer server = MBeanServerFactory.createMBeanServer();
2047      try
2048      {
2049         RelationService rs = null;
2050         boolean caught = false;
2051         try
2052         {
2053            rs = new RelationService(true);
2054            rs.isActive();
2055         }
2056         catch (RelationServiceNotRegisteredException e)
2057         {
2058            caught = true;
2059         }
2060         catch (Exception e)
2061         {
2062            fail(e.toString());
2063         }
2064         assertEquals(true, caught);
2065
2066         ObjectName name = null;
2067         name = new ObjectName("test:type = rs");
2068         server.registerMBean(rs, name);
2069         rs.isActive();
2070
2071         caught = false;
2072         try
2073         {
2074            server.unregisterMBean(name);
2075            rs.isActive();
2076         }
2077         catch (RelationServiceNotRegisteredException e)
2078         {
2079            caught = true;
2080         }
2081         if (caught == false)
2082            fail("FAILS IN RI: Relation Service still reports itself active.");
2083      }
2084      finally
2085      {
2086         MBeanServerFactory.releaseMBeanServer(server);
2087      }
2088   }
2089
2090   /**
2091    * Test Is Relation
2092    */

2093   public void testIsRelationExternal() throws Exception
2094   {
2095      MBeanServer server = MBeanServerFactory.createMBeanServer();
2096      try
2097      {
2098         ObjectName service = createRelationService("test:type=service", server);
2099         createRelationTypeC(service);
2100         createRolesC(server);
2101         RelationSupport support = null;
2102         String result1 = null;
2103         String result2 = null;
2104         String result3 = null;
2105         String result4 = null;
2106         String result5 = null;
2107         String result6 = null;
2108         support = new RelationSupport("id1", service, server,
2109                                       "relationTypeC", rolesC);
2110         addRelation(server, service, support, "test:type=support1");
2111         support = new RelationSupport("id2", service, server,
2112                                       "relationTypeC", rolesC);
2113         addRelation(server, service, support, "test:type=support2");
2114         RelationService rs = (RelationService) services.get(service);
2115         result1 = rs.isRelation(new ObjectName("test:type=support1"));
2116         result2 = rs.isRelation(new ObjectName("test:type=support2"));
2117         result3 = rs.isRelation(new ObjectName("test:type=support3"));
2118         rs.removeRelation("id2");
2119         result4 = rs.isRelation(new ObjectName("test:type=support1"));
2120         result5 = rs.isRelation(new ObjectName("test:type=support2"));
2121         result6 = rs.isRelation(new ObjectName("test:type=support3"));
2122         assertEquals("id1", result1);
2123         assertEquals("id2", result2);
2124         assertEquals(null, result3);
2125         assertEquals("id1", result4);
2126         assertEquals(null, result5);
2127         assertEquals(null, result6);
2128      }
2129      finally
2130      {
2131         MBeanServerFactory.releaseMBeanServer(server);
2132      }
2133   }
2134
2135   /**
2136    * Test Is Relation Errors
2137    */

2138   public void testIsRelationErrors() throws Exception
2139   {
2140      RelationService rs = new RelationService(true);
2141
2142      boolean caught = false;
2143      try
2144      {
2145         rs.isRelation(null);
2146      }
2147      catch(IllegalArgumentException e)
2148      {
2149         caught = true;
2150      }
2151      if (caught == false)
2152         fail("isRelation allows null relation id");
2153   }
2154
2155   /**
2156    * Test Is Relation MBean
2157    */

2158   public void testIsRelationMBeanExternal() throws Exception
2159   {
2160      MBeanServer server = MBeanServerFactory.createMBeanServer();
2161      try
2162      {
2163         ObjectName service = createRelationService("test:type=service", server);
2164         createRelationTypeC(service);
2165         createRolesC(server);
2166         RelationSupport support = null;
2167         ObjectName result1 = null;
2168         ObjectName result2 = null;
2169         ObjectName on1 = null;
2170         ObjectName on2 = null;
2171         support = new RelationSupport("id1", service, server,
2172                                       "relationTypeC", rolesC);
2173         addRelation(server, service, support, "test:type=support1");
2174         support = new RelationSupport("id2", service, server,
2175                                       "relationTypeC", rolesC);
2176         addRelation(server, service, support, "test:type=support2");
2177         RelationService rs = (RelationService) services.get(service);
2178         result1 = rs.isRelationMBean("id1");
2179         result2 = rs.isRelationMBean("id2");
2180         on1 = new ObjectName("test:type=support1");
2181         on2 = new ObjectName("test:type=support2");
2182         assertEquals(on1, result1);
2183         assertEquals(on2, result2);
2184      }
2185      finally
2186      {
2187         MBeanServerFactory.releaseMBeanServer(server);
2188      }
2189   }
2190
2191   /**
2192    * Test Is Relation MBean Errors
2193    */

2194   public void testIsRelationMBeanErrors() throws Exception
2195   {
2196      RelationService rs = new RelationService(true);
2197
2198      boolean caught = false;
2199      try
2200      {
2201         rs.isRelationMBean(null);
2202      }
2203      catch(IllegalArgumentException e)
2204      {
2205         caught = true;
2206      }
2207      if (caught == false)
2208         fail("isRelationMBean allows null relation id");
2209
2210      caught = false;
2211      try
2212      {
2213         rs.isRelationMBean("rubbish");
2214      }
2215      catch(RelationNotFoundException e)
2216      {
2217         caught = true;
2218      }
2219      if (caught == false)
2220         fail("isRelationMBean allows non-existent relation");
2221   }
2222
2223   /**
2224    * Test purge Relations Automatically
2225    */

2226   public void testPurgeRelationsAutomaticExternal() throws Exception
2227   {
2228      MBeanServer server = MBeanServerFactory.createMBeanServer();
2229      try
2230      {
2231         ObjectName service = createRelationService("test:type=service", server);
2232         createRelationTypeC(service);
2233         createRolesC(server);
2234         RelationSupport support = null;
2235         ObjectName on = null;
2236         Listener listener = new Listener(RelationNotification.RELATION_MBEAN_REMOVAL);
2237         boolean result = false;
2238         support = new RelationSupport("id1", service, server,
2239                                       "relationTypeC", rolesC);
2240         addRelation(server, service, support, "test:type=support1");
2241         server.addNotificationListener(service, listener, null, null);
2242         RelationService rs = (RelationService) services.get(service);
2243         server.unregisterMBean(new ObjectName("x:relation=c,role=2,bean=1"));
2244         on = new ObjectName("test:type=support1");
2245         result = rs.hasRelation("id1").booleanValue();
2246         assertEquals(false, result);
2247         RelationNotification rn = listener.check(1);
2248         assertEquals(new ArrayList(), rn.getMBeansToUnregister());
2249         assertEquals(on, rn.getObjectName());
2250         assertEquals("id1", rn.getRelationId());
2251         assertEquals("relationTypeC", rn.getRelationTypeName());
2252      }
2253      finally
2254      {
2255         MBeanServerFactory.releaseMBeanServer(server);
2256      }
2257   }
2258
2259   /**
2260    * Test purge Relations Manually
2261    */

2262   public void testPurgeRelationsManuallyExternal() throws Exception
2263   {
2264      MBeanServer server = MBeanServerFactory.createMBeanServer();
2265      try
2266      {
2267         ObjectName service = createRelationService("test:type=service", server);
2268         createRelationTypeC(service);
2269         createRolesC(server);
2270         RelationSupport support = null;
2271         ObjectName on = null;
2272         Listener listener = new Listener(RelationNotification.RELATION_MBEAN_REMOVAL);
2273         boolean result = false;
2274         RelationService rs = null;
2275         support = new RelationSupport("id1", service, server,
2276                                       "relationTypeC", rolesC);
2277         addRelation(server, service, support, "test:type=support1");
2278         server.addNotificationListener(service, listener, null, null);
2279         rs = (RelationService) services.get(service);
2280         rs.setPurgeFlag(false);
2281         server.unregisterMBean(new ObjectName("x:relation=c,role=2,bean=1"));
2282         on = new ObjectName("test:type=support1");
2283         result = rs.hasRelation("id1").booleanValue();
2284 
2285         assertEquals(true, result);
2286         RelationNotification rn = listener.check(0);
2287
2288         rs.purgeRelations();
2289         result = rs.hasRelation("id1").booleanValue();
2290         assertEquals(false, result);
2291         rn = listener.check(1);
2292         assertEquals(new ArrayList(), rn.getMBeansToUnregister());
2293         assertEquals(on, rn.getObjectName());
2294         assertEquals("id1", rn.getRelationId());
2295         assertEquals("relationTypeC", rn.getRelationTypeName());
2296      }
2297      finally
2298      {
2299         MBeanServerFactory.releaseMBeanServer(server);
2300      }
2301   }
2302
2303   /**
2304    * Test purge relations errors
2305    */

2306   public void testPurgeRelationsErrors() throws Exception
2307   {
2308      RelationService rs = new RelationService(true);
2309
2310      boolean caught = false;
2311      try
2312      {
2313         rs.purgeRelations();
2314      }
2315      catch (RelationServiceNotRegisteredException e)
2316      {
2317         caught = true;
2318      }
2319      if (caught == false)
2320         fail("purgeRelations allowed when not registered");
2321   }
2322
2323   /**
2324    * Test remove relation
2325    */

2326   public void testRemoveRelationExternal() throws Exception
2327   {
2328      MBeanServer server = MBeanServerFactory.createMBeanServer();
2329      try
2330      {
2331         ObjectName service = createRelationService("test:type=service", server);
2332         createRelationTypeA(service);
2333         createRolesA(server);
2334         createRelationTypeB(service);
2335         createRolesB(server);
2336         createRelationTypeC(service);
2337         createRolesC(server);
2338         createRelationTypeCX(service);
2339         createRolesCX(server);
2340         List result = null;
2341         RelationSupport supportA1 = new RelationSupport("ida1", service, server,
2342                                          "relationTypeA", rolesA);
2343         RelationSupport supportA2 = new RelationSupport("ida2", service, server,
2344                                          "relationTypeA", rolesA);
2345         RelationSupport supportCX = new RelationSupport("idcx", service, server,
2346                                          "relationTypeCX", rolesCX);
2347         RelationSupport supportC = new RelationSupport("idc", service, server,
2348                                          "relationTypeC", rolesC);
2349         addRelation(server, service, supportA1, "test:type=supportA1");
2350         addRelation(server, service, supportA2, "test:type=supportA2");
2351         addRelation(server, service, supportCX, "test:type=supportCX");
2352         addRelation(server, service, supportC, "test:type=supportC");
2353         RelationService rs = (RelationService) services.get(service);
2354         rs.removeRelation("idcx");
2355         result = rs.getAllRelationIds();
2356         assertEquals(3, result.size());
2357         assertEquals(true, result.contains("ida1"));
2358         assertEquals(true, result.contains("ida2"));
2359         assertEquals(true, result.contains("idc"));
2360         assertEquals(false, result.contains("idcx"));
2361      }
2362      finally
2363      {
2364         MBeanServerFactory.releaseMBeanServer(server);
2365      }
2366   }
2367
2368   /**
2369    * Test remove relation errors
2370    */

2371   public void testRemoveRelationErrors() throws Exception
2372   {
2373      MBeanServer server = MBeanServerFactory.createMBeanServer();
2374      try
2375      {
2376         ObjectName service = createRelationService("test:type=serviceA", null);
2377         RelationService rs = null;
2378         rs = (RelationService) services.get(service);
2379
2380         boolean caught = false;
2381         try
2382         {
2383            rs.removeRelation("RelationId");
2384         }
2385         catch (RelationServiceNotRegisteredException e)
2386         {
2387            caught = true;
2388         }
2389         if (caught == false)
2390            fail("removeRelation allowed when not registered");
2391
2392         service = createRelationService("test:type=service", server);
2393         createRelationTypeA(service);
2394         createRolesA(server);
2395         RelationSupport supportA1 = new RelationSupport("ida1", service, server,
2396                                       "relationTypeA", rolesA);
2397         addRelation(server, service, supportA1, "test:type=supportA1");
2398         rs = (RelationService) services.get(service);
2399
2400         caught = false;
2401         try
2402         {
2403            rs.removeRelation(null);
2404         }
2405         catch (IllegalArgumentException e)
2406         {
2407            caught = true;
2408         }
2409         if (caught == false)
2410            fail("removeRelation accepts a null relation");
2411
2412         caught = false;
2413         try
2414         {
2415            rs.removeRelation("rubbish");
2416         }
2417         catch (RelationNotFoundException e)
2418         {
2419            caught = true;
2420         }
2421         if (caught == false)
2422            fail("removeRelation accepts a non existent relation");
2423      }
2424      finally
2425      {
2426         MBeanServerFactory.releaseMBeanServer(server);
2427      }
2428   }
2429
2430   /**
2431    * Test remove relation type
2432    */

2433   public void testRemoveRelationType() throws Exception
2434   {
2435      MBeanServer server = MBeanServerFactory.createMBeanServer();
2436      try
2437      {
2438         RelationService rs = new RelationService(true);
2439         assertEquals(0, rs.getAllRelationTypeNames().size());
2440
2441         RoleInfo roleInfo1 = null;
2442         RoleInfo roleInfo2 = null;
2443         RoleInfo[] roleInfos = null;
2444         ObjectName name = new ObjectName("test:type = rs");
2445         server.registerMBean(rs, name);
2446         roleInfo1 = new RoleInfo("roleInfo1", Trivial.class.getName());
2447         roleInfo2 = new RoleInfo("roleInfo2", Trivial.class.getName());
2448         roleInfos = new RoleInfo[] { roleInfo1, roleInfo2 };
2449         rs.createRelationType("name1", roleInfos);
2450         rs.createRelationType("name2", roleInfos);
2451         rs.createRelationType("name3", roleInfos);
2452         rs.removeRelationType("name3");
2453
2454         ArrayList result = (ArrayList) rs.getAllRelationTypeNames();
2455         assertEquals(2, result.size());
2456         assertEquals(true, result.contains("name1"));
2457         assertEquals(true, result.contains("name2"));
2458         assertEquals(false, result.contains("name3"));
2459      }
2460      finally
2461      {
2462         MBeanServerFactory.releaseMBeanServer(server);
2463      }
2464   }
2465
2466   /**
2467    * Test remove relation type errors
2468    */

2469   public void testRemoveRelationTypeErrors() throws Exception
2470   {
2471      MBeanServer server = MBeanServerFactory.createMBeanServer();
2472      try
2473      {
2474         RelationService rs = null;
2475         RoleInfo roleInfo1 = null;
2476         RoleInfo roleInfo2 = null;
2477         RoleInfo[] roleInfos = null;
2478         rs = new RelationService(true);
2479         roleInfo1 = new RoleInfo("roleInfo1", Trivial.class.getName());
2480         roleInfo2 = new RoleInfo("roleInfo2", Trivial.class.getName());
2481         roleInfos = new RoleInfo[] { roleInfo1, roleInfo2 };
2482         rs.createRelationType("name1", roleInfos);
2483
2484         boolean caught = false;
2485         try
2486         {
2487            rs.removeRelationType("name1");
2488         }
2489         catch (RelationServiceNotRegisteredException e)
2490         {
2491            caught = true;
2492         }
2493         if (caught == false)
2494            fail("Remove relation type allowed when not registered, why not?");
2495
2496         server.registerMBean(rs, new ObjectName("test:type=RelationService"));
2497
2498         caught = false;
2499         try
2500         {
2501            rs.removeRelationType(null);
2502         }
2503         catch (IllegalArgumentException e)
2504         {
2505            caught = true;
2506         }
2507         if (caught == false)
2508            fail("Remove relation type allows null relation type name");
2509
2510         caught = false;
2511         try
2512         {
2513            rs.removeRelationType("rubbish");
2514         }
2515         catch (RelationTypeNotFoundException e)
2516         {
2517            caught = true;
2518         }
2519         if (caught == false)
2520            fail("Remove relation type allows non-existent relation type name");
2521      }
2522      finally
2523      {
2524         MBeanServerFactory.releaseMBeanServer(server);
2525      }
2526   }
2527
2528   /**
2529    * Test send relation creation notification
2530    */

2531   public void testSendRelationCreationNotificationExternal() throws Exception
2532   {
2533      MBeanServer server = MBeanServerFactory.createMBeanServer();
2534      try
2535      {
2536         ObjectName service = createRelationService("test:type=service", server);
2537         createRelationTypeC(service);
2538         createRolesC(server);
2539         RelationSupport support = null;
2540         ObjectName on = null;
2541         Listener listener = new Listener(RelationNotification.RELATION_MBEAN_CREATION);
2542         support = new RelationSupport("id1", service, server,
2543                                       "relationTypeC", rolesC);
2544         addRelation(server, service, support, "test:type=support1");
2545         server.addNotificationListener(service, listener, null, null);
2546         RelationService rs = (RelationService) services.get(service);
2547         rs.sendRelationCreationNotification("id1");
2548         on = new ObjectName("test:type=support1");
2549         RelationNotification rn = listener.check(1);
2550         assertEquals(new ArrayList(), rn.getMBeansToUnregister());
2551         assertEquals(new ArrayList(), rn.getNewRoleValue());
2552         assertEquals(on, rn.getObjectName());
2553         assertEquals(new ArrayList(), rn.getOldRoleValue());
2554         assertEquals("id1", rn.getRelationId());
2555         assertEquals("relationTypeC", rn.getRelationTypeName());
2556         assertEquals(null, rn.getRoleName());
2557      }
2558      finally
2559      {
2560         MBeanServerFactory.releaseMBeanServer(server);
2561      }
2562   }
2563
2564   /**
2565    * Test send relation removal notification errors
2566    */

2567   public void testSendRelationCreationNotificationErrors() throws Exception
2568   {
2569      RelationService rs = new RelationService(true);
2570
2571      boolean caught = false;
2572      try
2573      {
2574         rs.sendRelationCreationNotification(null);
2575      }
2576      catch (IllegalArgumentException e)
2577      {
2578         caught = true;
2579      }
2580      if (caught == false)
2581         fail("sendRelationCreationNotification allows null relation id");
2582
2583      caught = false;
2584      try
2585      {
2586         rs.sendRelationCreationNotification("rubbish");
2587      }
2588      catch (RelationNotFoundException e)
2589      {
2590         caught = true;
2591      }
2592      if (caught == false)
2593         fail("sendRelationCreationNotification allows invalid relation id");
2594   }
2595
2596   /**
2597    * Test send relation removal notification
2598    */

2599   public void testSendRelationRemovalNotificationExternal() throws Exception
2600   {
2601      MBeanServer server = MBeanServerFactory.createMBeanServer();
2602      try
2603      {
2604         ObjectName service = createRelationService("test:type=service", server);
2605         createRelationTypeC(service);
2606         createRolesC(server);
2607         RelationSupport support = null;
2608         ObjectName on = null;
2609         ArrayList test = new ArrayList();
2610         Listener listener = new Listener(RelationNotification.RELATION_MBEAN_REMOVAL);
2611         support = new RelationSupport("id1", service, server,
2612                                          "relationTypeC", rolesC);
2613         addRelation(server, service, support, "test:type=support1");
2614         server.addNotificationListener(service, listener, null, null);
2615         RelationService rs = (RelationService) services.get(service);
2616         test.add(new ObjectName("test:type=test"));
2617         rs.sendRelationRemovalNotification("id1", test);
2618         on = new ObjectName("test:type=support1");
2619         RelationNotification rn = listener.check(1);
2620         assertEquals(test, rn.getMBeansToUnregister());
2621         assertEquals(new ArrayList(), rn.getNewRoleValue());
2622         assertEquals(on, rn.getObjectName());
2623         assertEquals(new ArrayList(), rn.getOldRoleValue());
2624         assertEquals("id1", rn.getRelationId());
2625         assertEquals("relationTypeC", rn.getRelationTypeName());
2626         assertEquals(null, rn.getRoleName());
2627      }
2628      finally
2629      {
2630         MBeanServerFactory.releaseMBeanServer(server);
2631      }
2632   }
2633
2634   /**
2635    * Test send relation removal notification errors
2636    */

2637   public void testSendRelationRemovalNotificationErrors() throws Exception
2638   {
2639      RelationService rs = new RelationService(true);
2640
2641      boolean caught = false;
2642      try
2643      {
2644         rs.sendRelationRemovalNotification(null, new ArrayList());
2645      }
2646      catch (IllegalArgumentException e)
2647      {
2648         caught = true;
2649      }
2650      if (caught == false)
2651         fail("sendRelationRemovalNotification allows null relation id");
2652
2653
2654      caught = false;
2655      try
2656      {
2657         rs.sendRelationRemovalNotification("rubbish", new ArrayList());
2658      }
2659      catch (RelationNotFoundException e)
2660      {
2661         caught = true;
2662      }
2663      if (caught == false)
2664         fail("sendRelationRemovalNotification allows invalid relation id");
2665   }
2666
2667   /**
2668    * Test send role update notification
2669    */

2670   public void testSendRoleUpdateNotificationExternal() throws Exception
2671   {
2672      MBeanServer server = MBeanServerFactory.createMBeanServer();
2673      try
2674      {
2675         ObjectName service = createRelationService("test:type=service", server);
2676         createRelationTypeC(service);
2677         createRolesB(server);
2678         createRolesC(server);
2679         RelationSupport support = null;
2680         ObjectName on = null;
2681         ArrayList test = new ArrayList();
2682         Listener listener = new Listener(RelationNotification.RELATION_MBEAN_UPDATE);
2683         support = new RelationSupport("id1", service, server,
2684                                          "relationTypeC", rolesC);
2685         addRelation(server, service, support, "test:type=support1");
2686         server.addNotificationListener(service, listener, null, null);
2687         RelationService rs = (RelationService) services.get(service);
2688         test.add(new ObjectName("test:type=test"));
2689         rs.sendRoleUpdateNotification("id1", roleB1, test);
2690         on = new ObjectName("test:type=support1");
2691         RelationNotification rn = listener.check(1);
2692         assertEquals(new ArrayList(), rn.getMBeansToUnregister());
2693         assertEquals(roleB1.getRoleValue(), rn.getNewRoleValue());
2694         assertEquals(on, rn.getObjectName());
2695         assertEquals(test, rn.getOldRoleValue());
2696         assertEquals("id1", rn.getRelationId());
2697         assertEquals("relationTypeC", rn.getRelationTypeName());
2698         assertEquals("roleB1", rn.getRoleName());
2699      }
2700      finally
2701      {
2702         MBeanServerFactory.releaseMBeanServer(server);
2703      }
2704   }
2705
2706   /**
2707    * Test send role update notification errors
2708    */

2709   public void testSendRoleUpdateNotificationErrors() throws Exception
2710   {
2711      MBeanServer server = MBeanServerFactory.createMBeanServer();
2712      try
2713      {
2714         ObjectName service = createRelationService("test:type=service", server);
2715         RelationService rs = (RelationService) services.get(service);
2716         createRelationTypeB(service);
2717         createRolesB(server);
2718
2719         boolean caught = false;
2720         try
2721         {
2722            rs.sendRoleUpdateNotification(null, roleB1, new ArrayList());
2723         }
2724         catch (IllegalArgumentException e)
2725         {
2726            caught = true;
2727         }
2728         if (caught == false)
2729            fail("sendRoleUpdateNotification allows null relation id");
2730
2731         RoleList roleList = new RoleList();
2732         roleList.add(roleB1);
2733         roleList.add(roleB2);
2734         rs.createRelation("relationId", "relationTypeB", roleList);
2735
2736         caught = false;
2737         try
2738         {
2739            rs.sendRoleUpdateNotification("relationId", null, new ArrayList());
2740         }
2741         catch (IllegalArgumentException e)
2742         {
2743            caught = true;
2744         }
2745         if (caught == false)
2746            fail("sendRoleUpdateNotification allows null role");
2747
2748
2749         caught = false;
2750         try
2751         {
2752            rs.sendRoleUpdateNotification("rubbish", roleB1, new ArrayList());
2753         }
2754         catch (RelationNotFoundException e)
2755         {
2756            caught = true;
2757         }
2758         if (caught == false)
2759            fail("sendRoleUpdateNotification allows invalid relation id");
2760      }
2761      finally
2762      {
2763         MBeanServerFactory.releaseMBeanServer(server);
2764      }
2765   }
2766
2767   /**
2768    * Test set a role
2769    */

2770   public void testSetRoleExternal() throws Exception
2771   {
2772      MBeanServer server = MBeanServerFactory.createMBeanServer();
2773      try
2774      {
2775         ObjectName service = createRelationService("test:type=service", server);
2776         createRelationTypeC(service);
2777         createRolesC(server);
2778         createRolesCX(server);
2779         RelationSupport support = null;
2780         ObjectName on = null;
2781         Listener listener = new Listener(RelationNotification.RELATION_MBEAN_UPDATE);
2782         support = new RelationSupport("id1", service, server,
2783                                          "relationTypeC", rolesC);
2784         addRelation(server, service, support, "test:type=support1");
2785         server.addNotificationListener(service, listener, null, null);
2786         RelationService rs = (RelationService) services.get(service);
2787         rs.setRole("id1", roleCX2);
2788         on = new ObjectName("test:type=support1");
2789         RoleList shouldBe = new RoleList();
2790         shouldBe.add(roleC1);
2791         shouldBe.add(roleCX2);
2792         compare(shouldBe, support.retrieveAllRoles());
2793         RelationNotification rn = listener.check(1);
2794         assertEquals(new ArrayList(), rn.getMBeansToUnregister());
2795         assertEquals(roleCX2.getRoleValue(), rn.getNewRoleValue());
2796         assertEquals(on, rn.getObjectName());
2797         assertEquals(roleC2.getRoleValue(), rn.getOldRoleValue());
2798         assertEquals("id1", rn.getRelationId());
2799         assertEquals("relationTypeC", rn.getRelationTypeName());
2800         assertEquals("roleC2", rn.getRoleName());
2801      }
2802      finally
2803      {
2804         MBeanServerFactory.releaseMBeanServer(server);
2805      }
2806   }
2807
2808   /**
2809    * Test set role errors
2810    */

2811   public void testSetRoleErrors() throws Exception
2812   {
2813      MBeanServer server = MBeanServerFactory.createMBeanServer();
2814      try
2815      {
2816         ObjectName service = createRelationService("test:type=service", server);
2817         RelationService rs = (RelationService) services.get(service);
2818         createRelationTypeB(service);
2819         createRolesB(server);
2820         createRelationTypeC(service);
2821         createRolesC(server);
2822         createRolesCZ(server);
2823         createRolesCZZ(server);
2824         RoleList roles = new RoleList();
2825         roles.add(roleC1);
2826         roles.add(roleC2);
2827         rs.createRelation("relationId", "relationTypeC", roles);
2828
2829         boolean caught = false;
2830         try
2831         {
2832            rs.setRole(null, roleC2);
2833         }
2834         catch(IllegalArgumentException e)
2835         {
2836            caught = true;
2837         }
2838         if (caught == false)
2839            fail("setRole allows null relation id");
2840        
2841         caught = false;
2842         try
2843         {
2844            rs.setRole("relationId", null);
2845         }
2846         catch(IllegalArgumentException e)
2847         {
2848            caught = true;
2849         }
2850         if (caught == false)
2851            fail("setRole allows null role");
2852        
2853         caught = false;
2854         try
2855         {
2856            rs.setRole("rubbish", roleC2);
2857         }
2858         catch(RelationNotFoundException e)
2859         {
2860            caught = true;
2861         }
2862         if (caught == false)
2863            fail("setRole allows invalid relation id");
2864        
2865         caught = false;
2866         try
2867         {
2868            rs.setRole("relationId", roleB1);
2869         }
2870         catch(RoleNotFoundException e)
2871         {
2872            caught = true;
2873         }
2874         if (caught == false)
2875            fail("setRole allows invalid role name");
2876        
2877         caught = false;
2878         try
2879         {
2880            rs.setRole("relationId", roleC1);
2881         }
2882         catch(RoleNotFoundException e)
2883         {
2884            caught = true;
2885         }
2886         if (caught == false)
2887            fail("setRole allows non-writable role");
2888
2889         caught = false;
2890         try
2891         {
2892            rs.setRole("relationId", roleCZ2);
2893         }
2894         catch(InvalidRoleValueException e)
2895         {
2896            caught = true;
2897         }
2898         if (caught == false)
2899            fail("setRole allows a role below the minimum");
2900        
2901         caught = false;
2902         try
2903         {
2904            rs.setRole("relationId", roleCZZ);
2905         }
2906         catch(InvalidRoleValueException e)
2907         {
2908            caught = true;
2909         }
2910         if (caught == false)
2911            fail("setRole allows a role above the maximum");
2912        
2913         caught = false;
2914         try
2915         {
2916            rs.setRole("relationId", roleCZZZ);
2917         }
2918         catch(InvalidRoleValueException e)
2919         {
2920            caught = true;
2921         }
2922         if (caught == false)
2923            fail("setRole allows a role with unregistered beans");
2924
2925         server.unregisterMBean(service);
2926         caught = false;
2927         try
2928         {
2929            rs.setRole("relationId", roleC2);
2930         }
2931         catch(RelationServiceNotRegisteredException e)
2932         {
2933            caught = true;
2934         }
2935         if (caught == false)
2936            fail("FAILS IN RI: setRole allowed when not registered");
2937      }
2938      finally
2939      {
2940         MBeanServerFactory.releaseMBeanServer(server);
2941      }
2942   }
2943
2944   /**
2945    * Test set roles
2946    * @info.todo different permutations
2947    */

2948   public void testSetRolesExternal() throws Exception
2949   {
2950      MBeanServer server = MBeanServerFactory.createMBeanServer();
2951      try
2952      {
2953         ObjectName service = createRelationService("test:type=service", server);
2954         createRelationTypeC(service);
2955         createRolesC(server);
2956         createRolesCX(server);
2957         RelationSupport support = null;
2958         ObjectName on = null;
2959         Listener listener = new Listener(RelationNotification.RELATION_MBEAN_UPDATE);
2960         RoleList shouldBe = new RoleList();
2961         shouldBe.add(roleC1);
2962         shouldBe.add(roleCX2);
2963         support = new RelationSupport("id1", service, server,
2964                                          "relationTypeC", rolesC);
2965         addRelation(server, service, support, "test:type=support1");
2966         server.addNotificationListener(service, listener, null, null);
2967         RelationService rs = (RelationService) services.get(service);
2968            
2969         rs.setRoles("id1", shouldBe);
2970         on = new ObjectName("test:type=support1");
2971         compare(shouldBe, support.retrieveAllRoles());
2972         RelationNotification rn = listener.check(1);
2973      }
2974      finally
2975      {
2976         MBeanServerFactory.releaseMBeanServer(server);
2977      }
2978   }
2979
2980   /**
2981    * Test set roles errors
2982    */

2983   public void testSetRolesErrors() throws Exception
2984   {
2985      MBeanServer server = MBeanServerFactory.createMBeanServer();
2986      try
2987      {
2988         ObjectName service = createRelationService("test:type=service", server);
2989         RelationService rs = (RelationService) services.get(service);
2990         createRelationTypeC(service);
2991         createRolesC(server);
2992         RoleList roles = new RoleList();
2993         roles.add(roleC1);
2994         roles.add(roleC2);
2995         rs.createRelation("relationId", "relationTypeC", roles);
2996         RoleList newRoles = new RoleList();
2997         newRoles.add(roleC2);
2998
2999         boolean caught = false;
3000         try
3001         {
3002            rs.setRoles(null, newRoles);
3003         }
3004         catch(IllegalArgumentException e)
3005         {
3006            caught = true;
3007         }
3008         if (caught == false)
3009            fail("setRoles allows null relation id");
3010        
3011         caught = false;
3012         try
3013         {
3014            rs.setRole("relationId", null);
3015         }
3016         catch(IllegalArgumentException e)
3017         {
3018            caught = true;
3019         }
3020         if (caught == false)
3021            fail("setRoles allows null role list");
3022        
3023         caught = false;
3024         try
3025         {
3026            rs.setRoles("rubbish", newRoles);
3027         }
3028         catch(RelationNotFoundException e)
3029         {
3030            caught = true;
3031         }
3032         if (caught == false)
3033            fail("setRoles allows invalid relation id");
3034
3035         server.unregisterMBean(service);
3036         caught = false;
3037         try
3038         {
3039            rs.setRoles("relationId", newRoles);
3040         }
3041         catch(RelationServiceNotRegisteredException e)
3042         {
3043            caught = true;
3044         }
3045         if (caught == false)
3046            fail("FAILS IN RI: setRoles allowed when not registered");
3047      }
3048      finally
3049      {
3050         MBeanServerFactory.releaseMBeanServer(server);
3051      }
3052   }
3053
3054   /**
3055    * Test update role map errors
3056    */

3057   public void testUpdateRoleMapErrors() throws Exception
3058   {
3059      MBeanServer server = MBeanServerFactory.createMBeanServer();
3060      try
3061      {
3062         ObjectName service = createRelationService("test:type=service", server);
3063         RelationService rs = (RelationService) services.get(service);
3064         createRelationTypeB(service);
3065         createRolesB(server);
3066         RoleList roleList = new RoleList();
3067         roleList.add(roleB1);
3068         roleList.add(roleB2);
3069         rs.createRelation("relationId", "relationTypeB", roleList);
3070
3071         boolean caught = false;
3072         try
3073         {
3074            rs.updateRoleMap(null, roleB1, new ArrayList());
3075         }
3076         catch (IllegalArgumentException e)
3077         {
3078            caught = true;
3079         }
3080         if (caught == false)
3081            fail("updateRoleMap allows null relation id");
3082
3083         caught = false;
3084         try
3085         {
3086            rs.updateRoleMap("relationId", null, new ArrayList());
3087         }
3088         catch (IllegalArgumentException e)
3089         {
3090            caught = true;
3091         }
3092         if (caught == false)
3093            fail("updateRoleMap allows null role");
3094
3095         caught = false;
3096         try
3097         {
3098            rs.updateRoleMap("rubbish", roleB1, new ArrayList());
3099         }
3100         catch (RelationNotFoundException e)
3101         {
3102            caught = true;
3103         }
3104         if (caught == false)
3105            fail("updateRoleMap allows invalid relation id");
3106
3107         server.unregisterMBean(service);
3108
3109         caught = false;
3110         try
3111         {
3112            rs.updateRoleMap("relationId", roleB1, new ArrayList());
3113         }
3114         catch (RelationServiceNotRegisteredException e)
3115         {
3116            caught = true;
3117         }
3118         if (caught == false)
3119            fail("FAILS IN RI: updateRoleMap allowed when not registered");
3120      }
3121      finally
3122      {
3123         MBeanServerFactory.releaseMBeanServer(server);
3124      }
3125   }
3126
3127   // Support -----------------------------------------------------------------
3128

3129   private ObjectName createRelationService(String name, MBeanServer server)
3130   {
3131      ObjectName result = null;
3132      RelationService relationService = new RelationService(true);
3133      try
3134      {
3135         result = new ObjectName(name);
3136         services.put(result, relationService);
3137         if (server !=null)
3138            server.registerMBean(relationService, result);
3139      }
3140      catch(Exception e)
3141      {
3142         fail(e.toString());
3143      }
3144      return result;
3145   }
3146
3147   private ObjectName addRelation(MBeanServer server, ObjectName service,
3148                                  RelationSupport support, String name)
3149   {
3150      ObjectName result = null;
3151      try
3152      {
3153         result = new ObjectName(name);
3154         server.registerMBean(support, result);
3155         if (service != null)
3156         {
3157            RelationService relationService = (RelationService) services.get(service);
3158            relationService.addRelation(result);
3159         }
3160      }
3161      catch(Exception e)
3162      {
3163         fail(e.toString());
3164      }
3165      return result;
3166   }
3167
3168   private RoleInfo createRoleInfo(String name, Class mbean,
3169                                   boolean read, boolean write,
3170                                   int min, int max)
3171   {
3172      RoleInfo result = null;
3173      try
3174      {
3175         result = new RoleInfo(name, mbean.getName(), read, write, min, max, "");
3176      }
3177      catch(Exception e)
3178      {
3179         fail(e.toString());
3180      }
3181      return result;
3182   }
3183
3184   private void createRelationType(ObjectName relationService, String name, RoleInfo[] roleInfos)
3185   {
3186      try
3187      {
3188         RelationService service = (RelationService) services.get(relationService);
3189         service.createRelationType(name, roleInfos);
3190      }
3191      catch(Exception e)
3192      {
3193         fail(e.toString());
3194      }
3195   }
3196
3197   private void compare(RoleList original, RoleList result)
3198   {
3199      assertEquals(original.size(), result.size());
3200      Iterator iterator = original.iterator();
3201      while (iterator.hasNext())
3202      {
3203         Role originalRole = (Role) iterator.next();
3204         Iterator iterator2 = result.iterator();
3205         while (iterator2.hasNext())
3206         {
3207            Role resultRole = (Role) iterator2.next();
3208            if (originalRole.getRoleName().equals(resultRole.getRoleName()))
3209            {
3210               compare(originalRole, resultRole);
3211               iterator2.remove();
3212            }
3213         }
3214      }
3215      assertEquals(0, result.size());
3216   }
3217
3218   private void compare(Role original, Role result)
3219   {
3220      assertEquals(original.getRoleName(), result.getRoleName());
3221      compareListOfObjectNames(original.getRoleValue(), result.getRoleValue());
3222   }
3223
3224   private void compareListOfObjectNames(List original, List result)
3225   {
3226      assertEquals(original.size(), result.size());
3227      Iterator iterator = original.iterator();
3228      while (iterator.hasNext())
3229      {
3230         ObjectName originalBean = (ObjectName) iterator.next();
3231         Iterator iterator2 = result.iterator();
3232         while (iterator2.hasNext())
3233         {
3234            ObjectName resultBean = (ObjectName) iterator2.next();
3235            if (originalBean.equals(resultBean))
3236            {
3237               iterator2.remove();
3238            }
3239         }
3240      }
3241      assertEquals(0, result.size());
3242   }
3243
3244   private void compareListOfStrings(List original, List result)
3245   {
3246      assertEquals(original.size(), result.size());
3247      Iterator iterator = original.iterator();
3248      while (iterator.hasNext())
3249      {
3250         String originalString = (String) iterator.next();
3251         Iterator iterator2 = result.iterator();
3252         while (iterator2.hasNext())
3253         {
3254            String resultString = (String) iterator2.next();
3255            if (originalString.equals(resultString))
3256            {
3257               iterator2.remove();
3258            }
3259         }
3260      }
3261      assertEquals(0, result.size());
3262   }
3263
3264   private ObjectName createRoleValueBean(String name, Class mbean, MBeanServer server)
3265   {
3266      ObjectName result = null;
3267      try
3268      {
3269         result = new ObjectName(name);
3270         if (server != null)
3271         {
3272            server.registerMBean(mbean.newInstance(), result);
3273         }
3274      }
3275      catch(Exception e)
3276      {
3277         fail(e.toString());
3278      }
3279      return result;
3280   }
3281
3282   private void checkResult(RoleResult result, HashMap infos, RoleList roles)
3283   {
3284      checkResolved(result.getRoles(), infos, roles);
3285      checkUnresolved(result.getRolesUnresolved(), infos, roles);
3286   }
3287
3288   private void checkResolved(RoleList resolved, HashMap infos, RoleList roles)
3289   {
3290      RoleList copy = (RoleList) roles.clone();
3291      Iterator iterator = resolved.iterator();
3292      while (iterator.hasNext())
3293      {
3294         Role role = (Role) iterator.next();
3295         String roleName = role.getRoleName();
3296         RoleInfo info = (RoleInfo) infos.get(roleName);
3297         if (info == null)
3298            fail("unknown role " + roleName);
3299         if (info.isReadable() == false)
3300            fail("role should not be readable " + roleName);
3301         Role original = removeRole(copy, roleName);
3302         compareListOfObjectNames(original.getRoleValue(), role.getRoleValue());
3303      }
3304
3305      iterator = copy.iterator();
3306      while (iterator.hasNext())
3307      {
3308         Role role = (Role) iterator.next();
3309         String roleName = role.getRoleName();
3310         RoleInfo info = (RoleInfo) infos.get(roleName);
3311         if (info.isReadable() == true)
3312            fail("missing role " + roleName);
3313      }
3314   }
3315
3316   private void checkUnresolved(RoleUnresolvedList unresolved, HashMap infos, RoleList roles)
3317   {
3318      RoleList copy = (RoleList) roles.clone();
3319      Iterator iterator = unresolved.iterator();
3320      while (iterator.hasNext())
3321      {
3322         RoleUnresolved roleUnresolved = (RoleUnresolved) iterator.next();
3323         String roleName = roleUnresolved.getRoleName();
3324         RoleInfo info = (RoleInfo) infos.get(roleName);
3325         if (info == null)
3326            fail("unknown role " + roleName);
3327         if (info.isReadable() == true)
3328            fail("role should be readable " + roleName);
3329         removeRole(copy, roleName);
3330      }
3331
3332      iterator = copy.iterator();
3333      while (iterator.hasNext())
3334      {
3335         Role role = (Role) iterator.next();
3336         String roleName = role.getRoleName();
3337         RoleInfo info = (RoleInfo) infos.get(roleName);
3338         if (info.isReadable() == false)
3339            fail("missing unresolved role " + roleName);
3340      }
3341   }
3342
3343   private Role removeRole(RoleList roles, String roleName)
3344   {
3345      Iterator iterator = roles.iterator();
3346      while (iterator.hasNext())
3347      {
3348         Role role = (Role) iterator.next();
3349         if (role.getRoleName().equals(roleName))
3350         {
3351            iterator.remove();
3352            return role;
3353         }
3354      }
3355      fail("role was not in the original " + roleName);
3356      return null;
3357   }
3358
3359   private Role getRole(RoleList roles, String roleName)
3360   {
3361      Iterator iterator = roles.iterator();
3362      while (iterator.hasNext())
3363      {
3364         Role role = (Role) iterator.next();
3365         if (role.getRoleName().equals(roleName))
3366         {
3367            return role;
3368         }
3369      }
3370      fail("role was not in the original " + roleName);
3371      return null;
3372   }
3373
3374   private void checkMBeans(Map result, RoleList roles)
3375   {
3376      // Construct what we think the value should be
3377
Map expected = calcMBeanRoleMap(roles);
3378   
3379      // Check the actual result
3380
Iterator iterator = result.entrySet().iterator();
3381      while (iterator.hasNext())
3382      {
3383         Map.Entry entry = (Map.Entry) iterator.next();
3384         ObjectName key = (ObjectName) entry.getKey();
3385         ArrayList roleNames = (ArrayList) entry.getValue();
3386         ArrayList expectedNames = (ArrayList) expected.get(key);
3387         if (expectedNames == null)
3388            fail("Unexpected object name " + key);
3389         compareListOfStrings(expectedNames, roleNames);
3390         expected.remove(key);
3391      }
3392      assertEquals(0, expected.size());
3393   }
3394
3395   private Map calcMBeanRoleMap(RoleList roles)
3396   {
3397      HashMap result = new HashMap();
3398      Iterator iterator = roles.iterator();
3399      while (iterator.hasNext())
3400      {
3401         Role role = (Role) iterator.next();
3402         String roleName = role.getRoleName();
3403         ArrayList mbeans = (ArrayList) role.getRoleValue();
3404         Iterator iterator2 = mbeans.iterator();
3405         while (iterator2.hasNext())
3406         {
3407            ObjectName objectName = (ObjectName) iterator2.next();
3408            ArrayList names = (ArrayList) result.get(objectName);
3409            if (names == null)
3410            {
3411               names = new ArrayList();
3412               result.put(objectName, names);
3413            }
3414            // It seems the role name should be duplicated?
3415
// Include the following test if this is a bug in RI.
3416
// if (names.contains(roleName) == false)
3417

3418            names.add(roleName);
3419         }
3420      }
3421      return result;
3422   }
3423
3424   private void createRolesA(MBeanServer server)
3425   {
3426      try
3427      {
3428         ArrayList roleA1Values = new ArrayList();
3429         roleA1Values.add(createRoleValueBean("x:relation=a,role=1,bean=1",
3430                                              Trivial.class, server));
3431         Role roleA1 = new Role("roleA1", roleA1Values);
3432         rolesA = new RoleList();
3433         rolesA.add(roleA1);
3434      }
3435      catch(Exception e)
3436      {
3437         fail(e.toString());
3438      }
3439   }
3440
3441   private void createRelationTypeA(ObjectName relationService)
3442   {
3443      try
3444      {
3445         RoleInfo roleInfoA1 = createRoleInfo("roleA1", Trivial.class, true, true, 1, 1);
3446         RoleInfo[] roleInfos = new RoleInfo[] { roleInfoA1 };
3447         createRelationType(relationService, "relationTypeA", roleInfos);
3448         for (int i=0; i < roleInfos.length; i++)
3449            roleInfosA.put(roleInfos[i].getName(), roleInfos[i]);
3450      }
3451      catch(Exception e)
3452      {
3453         fail(e.toString());
3454      }
3455   }
3456
3457   private void createRolesB(MBeanServer server)
3458   {
3459      try
3460      {
3461         ArrayList roleB1Values = new ArrayList();
3462         roleB1Values.add(createRoleValueBean("x:relation=b,role=1,bean=1",
3463                                              Trivial.class, server));
3464         roleB1Values.add(createRoleValueBean("x:relation=b,role=1,bean=2",
3465                                              Trivial.class, server));
3466         roleB1 = new Role("roleB1", roleB1Values);
3467
3468         ArrayList roleB2Values = new ArrayList();
3469         roleB2Values.add(createRoleValueBean("x:relation=b,role=2,bean=1",
3470                                              Trivial.class, server));
3471         roleB2Values.add(createRoleValueBean("x:relation=b,role=2,bean=2",
3472                                              Trivial.class, server));
3473         roleB2Values.add(createRoleValueBean("x:relation=b,role=2,bean=3",
3474                                              Trivial.class, server));
3475         roleB2 = new Role("roleB2", roleB2Values);
3476
3477         rolesB = new RoleList();
3478         rolesB.add(roleB1);
3479         rolesB.add(roleB2);
3480      }
3481      catch(Exception e)
3482      {
3483         fail(e.toString());
3484      }
3485   }
3486
3487   private void createRelationTypeB(ObjectName relationService)
3488   {
3489      try
3490      {
3491         RoleInfo roleInfoB1 = createRoleInfo("roleB1", Trivial.class, true, false, 1, 2);
3492         RoleInfo roleInfoB2 = createRoleInfo("roleB2", Trivial.class, false, true, 3, 4);
3493         RoleInfo[] roleInfos = new RoleInfo[] { roleInfoB1, roleInfoB2 };
3494         createRelationType(relationService, "relationTypeB", roleInfos );
3495         for (int i=0; i < roleInfos.length; i++)
3496         roleInfosB.put(roleInfos[i].getName(), roleInfos[i]);
3497      }
3498      catch(Exception e)
3499      {
3500         fail(e.toString());
3501      }
3502   }
3503
3504   private void createRolesC(MBeanServer server)
3505   {
3506      try
3507      {
3508         ArrayList roleC1Values = new ArrayList();
3509         roleC1Values.add(createRoleValueBean("x:relation=c,role=1,bean=1",
3510                                              Trivial.class, server));
3511         roleC1Values.add(createRoleValueBean("x:relation=c,role=1,bean=1",
3512                                              Trivial.class, null));
3513         roleC1 = new Role("roleC1", roleC1Values);
3514
3515         ArrayList roleC2Values = new ArrayList();
3516         roleC2Values.add(createRoleValueBean("x:relation=c,role=1,bean=1",
3517                                              Trivial.class, null));
3518         roleC2Values.add(createRoleValueBean("x:relation=c,role=2,bean=1",
3519                                              Trivial.class, server));
3520         roleC2Values.add(createRoleValueBean("x:relation=c,role=2,bean=2",
3521                                              Trivial.class, server));
3522         roleC2 = new Role("roleC2", roleC2Values);
3523
3524         rolesC = new RoleList();
3525         rolesC.add(roleC1);
3526         rolesC.add(roleC2);
3527      }
3528      catch(Exception e)
3529      {
3530         fail(e.toString());
3531      }
3532   }
3533
3534   private void createRelationTypeC(ObjectName relationService)
3535   {
3536      try
3537      {
3538         RoleInfo roleInfoC1 = createRoleInfo("roleC1", Trivial.class, true, false, 1, 2);
3539         RoleInfo roleInfoC2 = createRoleInfo("roleC2", Trivial.class, false, true, 3, 4);
3540         RoleInfo[] roleInfos = new RoleInfo[] { roleInfoC1, roleInfoC2 };
3541         createRelationType(relationService, "relationTypeC", roleInfos );
3542         for (int i=0; i < roleInfos.length; i++)
3543            roleInfosC.put(roleInfos[i].getName(), roleInfos[i]);
3544      }
3545      catch(Exception e)
3546      {
3547         fail(e.toString());
3548      }
3549   }
3550
3551   private void createRolesCX(MBeanServer server)
3552   {
3553      try
3554      {
3555         ArrayList roleCX1Values = new ArrayList();
3556         roleCX1Values.add(createRoleValueBean("x:relation=c,role=2,bean=1",
3557                                              Trivial.class, null));
3558         roleCX1 = new Role("roleC1", roleCX1Values);
3559
3560         ArrayList roleCX2Values = new ArrayList();
3561         roleCX2Values.add(createRoleValueBean("x:relation=c,role=1,bean=1",
3562                                              Trivial.class, null));
3563         roleCX2Values.add(createRoleValueBean("x:relation=c,role=1,bean=2",
3564                                              Trivial.class, server));
3565         roleCX2Values.add(createRoleValueBean("x:relation=c,role=1,bean=3",
3566                                              Trivial.class, server));
3567         roleCX2Values.add(createRoleValueBean("x:relation=c,role=1,bean=4",
3568                                              Trivial.class, server));
3569         roleCX2 = new Role("roleC2", roleCX2Values);
3570
3571         rolesCX = new RoleList();
3572         rolesCX.add(roleCX1);
3573         rolesCX.add(roleCX2);
3574      }
3575      catch(Exception e)
3576      {
3577         fail(e.toString());
3578      }
3579   }
3580
3581   private void createRelationTypeCX(ObjectName relationService)
3582   {
3583      try
3584      {
3585         RoleInfo roleInfoCX1 = createRoleInfo("roleC1", Trivial.class, true, false, 1, 2);
3586         RoleInfo roleInfoCX2 = createRoleInfo("roleC2", Trivial.class, false, true, 3, 4);
3587         RoleInfo[] roleInfos = new RoleInfo[] { roleInfoCX1, roleInfoCX2 };
3588         createRelationType(relationService, "relationTypeCX", roleInfos );
3589         for (int i=0; i < roleInfos.length; i++)
3590            roleInfosCX.put(roleInfos[i].getName(), roleInfos[i]);
3591      }
3592      catch(Exception e)
3593      {
3594         fail(e.toString());
3595      }
3596   }
3597
3598   private void createRolesCZ(MBeanServer server)
3599   {
3600      try
3601      {
3602         ArrayList roleCZ2Values = new ArrayList();
3603         roleCZ2Values.add(createRoleValueBean("x:relation=c,role=1,bean=1",
3604                                              Trivial.class, null));
3605         roleCZ2 = new Role("roleC2", roleCZ2Values);
3606
3607         rolesCZ = new RoleList();
3608         rolesCZ.add(roleCZ2);
3609      }
3610      catch(Exception e)
3611      {
3612         fail(e.toString());
3613      }
3614   }
3615
3616   private void createRolesCZZ(MBeanServer server)
3617   {
3618      try
3619      {
3620         ArrayList roleCZZValues = new ArrayList();
3621         roleCZZValues.add(createRoleValueBean("x:relation=c,role=1,bean=1",
3622                                              Trivial.class, null));
3623         roleCZZValues.add(createRoleValueBean("x:relation=c,role=1,bean=2",
3624                                              Trivial.class, null));
3625         roleCZZValues.add(createRoleValueBean("x:relation=c,role=1,bean=3",
3626                                              Trivial.class, null));
3627         roleCZZValues.add(createRoleValueBean("x:relation=c,role=1,bean=4",
3628                                              Trivial.class, null));
3629         roleCZZValues.add(createRoleValueBean("x:relation=c,role=1,bean=5",
3630                                              Trivial.class, server));
3631         roleCZZ = new Role("roleC2", roleCZZValues);
3632
3633         rolesCZZ = new RoleList();
3634         rolesCZZ.add(roleCZZ);
3635
3636         ArrayList roleCZZZValues = new ArrayList();
3637         roleCZZZValues.add(createRoleValueBean("x:relation=c,role=1x,bean=1",
3638                                              Trivial.class, null));
3639         roleCZZZValues.add(createRoleValueBean("x:relation=c,role=1x,bean=2",
3640                                              Trivial.class, null));
3641         roleCZZZValues.add(createRoleValueBean("x:relation=c,role=1x,bean=3",
3642                                              Trivial.class, null));
3643         roleCZZZ = new Role("roleC2", roleCZZZValues);
3644
3645         rolesCZZZ = new RoleList();
3646         rolesCZZZ.add(roleCZZZ);
3647      }
3648      catch(Exception e)
3649      {
3650         fail(e.toString());
3651      }
3652   }
3653
3654   private class Listener
3655      implements NotificationListener
3656   {
3657      String type;
3658      HashSet notifications = new HashSet();
3659      public Listener(String type)
3660      {
3661         this.type = type;
3662      }
3663      public void handleNotification(Notification n, Object h)
3664      {
3665         notifications.add(n);
3666      }
3667      public RelationNotification check(int size)
3668      {
3669         RelationNotification result = null;
3670         assertEquals(size, notifications.size());
3671         Iterator iterator = notifications.iterator();
3672         while (iterator.hasNext())
3673         {
3674            RelationNotification rn = (RelationNotification) iterator.next();
3675            assertEquals(type, rn.getType());
3676            result = rn;
3677         }
3678         return result;
3679      }
3680   }
3681}
3682
Popular Tags