KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.ArrayList;
11 import java.util.HashMap;
12 import java.util.HashSet;
13 import java.util.Iterator;
14 import java.util.List;
15 import java.util.Map;
16
17 import javax.management.MBeanServer;
18 import javax.management.MBeanServerFactory;
19 import javax.management.Notification;
20 import javax.management.NotificationListener;
21 import javax.management.ObjectName;
22 import javax.management.relation.RelationNotification;
23 import javax.management.relation.RelationService;
24 import javax.management.relation.RelationSupport;
25 import javax.management.relation.Role;
26 import javax.management.relation.RoleInfo;
27 import javax.management.relation.RoleList;
28 import javax.management.relation.RoleResult;
29 import javax.management.relation.RoleUnresolved;
30 import javax.management.relation.RoleUnresolvedList;
31
32 import junit.framework.TestCase;
33 import test.compliance.relation.support.Trivial;
34
35 /**
36  * Relation Support tests
37  *
38  * @author <a HREF="mailto:Adrian.Brock@HappeningTimes.com">Adrian Brock</a>.
39  */

40 public class RelationSupportTestCase
41   extends TestCase
42 {
43
44   // Constants -----------------------------------------------------------------
45

46   // Attributes ----------------------------------------------------------------
47

48   HashMap services = new HashMap();
49
50   RoleList rolesA;
51   HashMap roleInfosA = new HashMap();
52   RoleList rolesB;
53   HashMap roleInfosB = new HashMap();
54   Role roleC1;
55   Role roleC2;
56   RoleList rolesC;
57   HashMap roleInfosC = new HashMap();
58   Role roleCX1;
59   Role roleCX2;
60   RoleList rolesCX;
61   HashMap roleInfosCX = new HashMap();
62
63   // Constructor ---------------------------------------------------------------
64

65   /**
66    * Construct the test
67    */

68   public RelationSupportTestCase(String s)
69   {
70     super(s);
71   }
72
73   // Tests ---------------------------------------------------------------------
74

75   /**
76    * Test the delegate constructor
77    */

78   public void testDelegateConstructor()
79   {
80     MBeanServer server = createMBeanServer();
81     ObjectName service = createRelationService("test:type=service", null);
82     createRolesB(null);
83     RelationSupport support = null;
84     try
85     {
86       support = new RelationSupport("id", service, server, "relationTypeB",
87                                     rolesB);
88     }
89     catch(Exception e)
90     {
91       fail(e.toString());
92     }
93     assertEquals("id", support.getRelationId());
94     assertEquals("relationTypeB", support.getRelationTypeName());
95     assertEquals("test:type=service", support.getRelationServiceName().toString());
96     RoleList roleList = support.retrieveAllRoles();
97     compare(rolesB, roleList);
98   }
99
100   /**
101    * Test get all roles
102    */

103   public void testGetAllRoles()
104   {
105     MBeanServer server = createMBeanServer();
106     ObjectName service = createRelationService("test:type=service", server);
107     createRelationTypeB(service);
108     createRolesB(server);
109     RoleResult result = null;
110     try
111     {
112       RelationSupport support = new RelationSupport("id", service, server,
113                                   "relationTypeB", rolesB);
114       addRelation(server, service, support, "test:type=support");
115       result = support.getAllRoles();
116     }
117     catch(Exception e)
118     {
119       fail(e.toString());
120     }
121     checkResult(result, roleInfosB, rolesB);
122   }
123
124   /**
125    * Test get referenced mbeans when not registered in service
126    */

127   public void testGetReferencedMBeansNotRegistered()
128   {
129     MBeanServer server = createMBeanServer();
130     ObjectName service = createRelationService("test:type=service", null);
131     createRelationTypeC(service);
132     createRolesC(server);
133     Map result = null;
134     try
135     {
136       RelationSupport support = new RelationSupport("id", service, server,
137                                   "relationTypeC", rolesC);
138       result = support.getReferencedMBeans();
139     }
140     catch(Exception e)
141     {
142       fail(e.toString());
143     }
144     checkMBeans(result, rolesC);
145   }
146
147   /**
148    * Test get referenced mbeans when registered in service
149    */

150   public void testGetReferencedMBeansWhenRegistered()
151   {
152     MBeanServer server = createMBeanServer();
153     ObjectName service = createRelationService("test:type=service", server);
154     createRelationTypeC(service);
155     createRolesC(server);
156     Map result = null;
157     try
158     {
159       RelationSupport support = new RelationSupport("id", service, server,
160                                   "relationTypeC", rolesC);
161       addRelation(server, service, support, "test:type=support");
162       result = support.getReferencedMBeans();
163     }
164     catch(Exception e)
165     {
166       fail(e.toString());
167     }
168     checkMBeans(result, rolesC);
169   }
170
171   /**
172    * Test get Role
173    */

174   public void testGetRole()
175   {
176     MBeanServer server = createMBeanServer();
177     ObjectName service = createRelationService("test:type=service", server);
178     createRelationTypeC(service);
179     createRolesC(server);
180     List result = null;
181     try
182     {
183       RelationSupport support = new RelationSupport("id", service, server,
184                                   "relationTypeC", rolesC);
185       addRelation(server, service, support, "test:type=support");
186       result = support.getRole("roleC1");
187     }
188     catch(Exception e)
189     {
190       fail(e.toString());
191     }
192     compareListOfObjectNames(getRole(rolesC, "roleC1").getRoleValue(), result);
193   }
194
195   /**
196    * Test get Role cardinality when not registered
197    */

198   public void testGetRoleCardinalityUnregistered()
199   {
200     MBeanServer server = createMBeanServer();
201     ObjectName service = createRelationService("test:type=service", null);
202     createRolesC(null);
203     Integer result = null;
204     RelationSupport support = null;
205     try
206     {
207       support = new RelationSupport("id", service, server,
208                                   "relationTypeC", rolesC);
209       result = support.getRoleCardinality("roleC1");
210     }
211     catch(Exception e)
212     {
213       fail(e.toString());
214     }
215     assertEquals(2, result.intValue());
216
217     try
218     {
219       result = support.getRoleCardinality("roleC2");
220     }
221     catch(Exception e)
222     {
223       fail(e.toString());
224     }
225     assertEquals(3, result.intValue());
226   }
227
228   /**
229    * Test get Role cardinality registered
230    */

231   public void testGetRoleCardinalityRegistered()
232   {
233     MBeanServer server = createMBeanServer();
234     ObjectName service = createRelationService("test:type=service", server);
235     createRelationTypeC(service);
236     createRolesC(server);
237     Integer result = null;
238     RelationSupport support = null;
239     try
240     {
241       support = new RelationSupport("id", service, server,
242                                   "relationTypeC", rolesC);
243       addRelation(server, service, support, "test:type=support");
244       result = support.getRoleCardinality("roleC1");
245     }
246     catch(Exception e)
247     {
248       fail(e.toString());
249     }
250     assertEquals(2, result.intValue());
251
252     try
253     {
254       result = support.getRoleCardinality("roleC2");
255     }
256     catch(Exception e)
257     {
258       fail(e.toString());
259     }
260     assertEquals(3, result.intValue());
261   }
262
263   /**
264    * Test get Roles
265    */

266   public void testGetRoles()
267   {
268     MBeanServer server = createMBeanServer();
269     ObjectName service = createRelationService("test:type=service", server);
270     createRelationTypeC(service);
271     createRolesC(server);
272     RoleResult result = null;
273     RelationSupport support = null;
274     try
275     {
276       support = new RelationSupport("id", service, server,
277                                   "relationTypeC", rolesC);
278       addRelation(server, service, support, "test:type=support");
279       result = support.getRoles(new String[] {"roleC1", "roleC2" });
280     }
281     catch(Exception e)
282     {
283       fail(e.toString());
284     }
285     checkResult(result, roleInfosC, rolesC);
286
287     try
288     {
289       result = support.getRoles(new String[] {"roleC1" });
290     }
291     catch(Exception e)
292     {
293       fail(e.toString());
294     }
295     RoleList resolved = result.getRoles();
296     assertEquals(1, resolved.size());
297     assertEquals(0, result.getRolesUnresolved().size());
298     compare(getRole(rolesC, "roleC1"), (Role) resolved.get(0));
299
300     try
301     {
302       result = support.getRoles(new String[] {"roleC2" });
303     }
304     catch(Exception e)
305     {
306       fail(e.toString());
307     }
308     RoleUnresolvedList unresolved = result.getRolesUnresolved();
309     assertEquals(0, result.getRoles().size());
310     assertEquals(1, unresolved.size());
311     assertEquals("roleC2", ((RoleUnresolved)unresolved.get(0)).getRoleName());
312   }
313
314   /**
315    * Test relation service flag
316    */

317   public void testRelationServiceFlag()
318   {
319     MBeanServer server = createMBeanServer();
320     ObjectName service = createRelationService("test:type=service", null);
321     createRolesB(null);
322     RelationSupport support = null;
323     try
324     {
325       support = new RelationSupport("id", service, server, "relationTypeB",
326                                     rolesB);
327     }
328     catch(Exception e)
329     {
330       fail(e.toString());
331     }
332     assertEquals(false, support.isInRelationService().booleanValue());
333     support.setRelationServiceManagementFlag(new Boolean(true));
334     assertEquals(true, support.isInRelationService().booleanValue());
335     support.setRelationServiceManagementFlag(new Boolean(false));
336     assertEquals(false, support.isInRelationService().booleanValue());
337   }
338
339   /**
340    * Test retrieve all roles when unregistered
341    */

342   public void testRetrieveAllRolesUnRegistered()
343   {
344     MBeanServer server = createMBeanServer();
345     ObjectName service = createRelationService("test:type=service", null);
346     createRolesB(null);
347     RoleList result = null;
348     try
349     {
350       RelationSupport support = new RelationSupport("id", service, server,
351                                   "relationTypeB", rolesB);
352       result = support.retrieveAllRoles();
353     }
354     catch(Exception e)
355     {
356       fail(e.toString());
357     }
358     compare(rolesB, result);
359   }
360
361   /**
362    * Test retrieve all roles registered
363    */

364   public void testRetrieveAllRolesRegistered()
365   {
366     MBeanServer server = createMBeanServer();
367     ObjectName service = createRelationService("test:type=service", server);
368     createRelationTypeB(service);
369     createRolesB(server);
370     RoleList result = null;
371     try
372     {
373       RelationSupport support = new RelationSupport("id", service, server,
374                                   "relationTypeB", rolesB);
375       addRelation(server, service, support, "test:type=support");
376       result = support.retrieveAllRoles();
377     }
378     catch(Exception e)
379     {
380       fail(e.toString());
381     }
382     compare(rolesB, result);
383   }
384
385   /**
386    * Test set a role
387    */

388   public void testSetRole()
389   {
390       MBeanServer server = MBeanServerFactory.createMBeanServer();
391       try
392       {
393          ObjectName service = createRelationService("test:type=service", server);
394          createRelationTypeC(service);
395          createRolesC(server);
396          createRolesCX(server);
397          RelationSupport support = null;
398          ObjectName on = null;
399          Listener listener = new Listener(RelationNotification.RELATION_MBEAN_UPDATE);
400          try
401          {
402             support = new RelationSupport("id1", service, server,
403                                           "relationTypeC", rolesC);
404             addRelation(server, service, support, "test:type=support1");
405             server.addNotificationListener(service, listener, null, null);
406             support.setRole(roleCX2);
407             on = new ObjectName("test:type=support1");
408          }
409          catch(Exception e)
410          {
411             fail(e.toString());
412          }
413          RoleList shouldBe = new RoleList();
414          shouldBe.add(roleC1);
415          shouldBe.add(roleCX2);
416          compare(shouldBe, support.retrieveAllRoles());
417          RelationNotification rn = listener.check(1);
418          assertEquals(new ArrayList(), rn.getMBeansToUnregister());
419          assertEquals(roleCX2.getRoleValue(), rn.getNewRoleValue());
420          assertEquals(on, rn.getObjectName());
421          assertEquals(roleC2.getRoleValue(), rn.getOldRoleValue());
422          assertEquals("id1", rn.getRelationId());
423          assertEquals("relationTypeC", rn.getRelationTypeName());
424          assertEquals("roleC2", rn.getRoleName());
425       }
426       finally
427       {
428          MBeanServerFactory.releaseMBeanServer(server);
429       }
430   }
431
432   /**
433    * Test set roles
434    * @info.todo different permutations
435    */

436   public void testSetRoles()
437   {
438       MBeanServer server = MBeanServerFactory.createMBeanServer();
439       try
440       {
441          ObjectName service = createRelationService("test:type=service", server);
442          createRelationTypeC(service);
443          createRolesC(server);
444          createRolesCX(server);
445          RelationSupport support = null;
446          ObjectName on = null;
447          Listener listener = new Listener(RelationNotification.RELATION_MBEAN_UPDATE);
448          RoleList shouldBe = new RoleList();
449          shouldBe.add(roleC1);
450          shouldBe.add(roleCX2);
451          try
452          {
453             support = new RelationSupport("id1", service, server,
454                                           "relationTypeC", rolesC);
455             addRelation(server, service, support, "test:type=support1");
456             server.addNotificationListener(service, listener, null, null);
457             
458             support.setRoles(shouldBe);
459             on = new ObjectName("test:type=support1");
460          }
461          catch(Exception e)
462          {
463             fail(e.toString());
464          }
465          compare(shouldBe, support.retrieveAllRoles());
466          RelationNotification rn = listener.check(1);
467       }
468       finally
469       {
470          MBeanServerFactory.releaseMBeanServer(server);
471       }
472   }
473
474   /**
475    * Test error handling
476    */

477   public void testErrors()
478   {
479 // TODO !!!!!!!!
480
}
481
482   // Support -------------------------------------------------------------------
483

484   private MBeanServer createMBeanServer()
485   {
486       return MBeanServerFactory.createMBeanServer();
487   }
488
489   private ObjectName createRelationService(String name, MBeanServer server)
490   {
491     ObjectName result = null;
492     RelationService relationService = new RelationService(true);
493     try
494     {
495       result = new ObjectName(name);
496       services.put(result, relationService);
497       if (server !=null)
498         server.registerMBean(relationService, result);
499     }
500     catch(Exception e)
501     {
502       fail(e.toString());
503     }
504     return result;
505   }
506
507   private ObjectName addRelation(MBeanServer server, ObjectName service,
508                                  RelationSupport support, String name)
509   {
510     ObjectName result = null;
511     try
512     {
513       result = new ObjectName(name);
514       server.registerMBean(support, result);
515       if (service != null)
516       {
517         RelationService relationService = (RelationService) services.get(service);
518         relationService.addRelation(result);
519       }
520     }
521     catch(Exception e)
522     {
523       fail(e.toString());
524     }
525     return result;
526   }
527
528   private RoleInfo createRoleInfo(String name, Class mbean,
529                                   boolean read, boolean write,
530                                   int min, int max)
531   {
532     RoleInfo result = null;
533     try
534     {
535       result = new RoleInfo(name, mbean.getName(), read, write, min, max, "");
536     }
537     catch(Exception e)
538     {
539       fail(e.toString());
540     }
541     return result;
542   }
543
544   private void createRelationType(ObjectName relationService, String name, RoleInfo[] roleInfos)
545   {
546     try
547     {
548       RelationService service = (RelationService) services.get(relationService);
549       service.createRelationType(name, roleInfos);
550     }
551     catch(Exception e)
552     {
553       fail(e.toString());
554     }
555   }
556
557   private void compare(RoleList original, RoleList result)
558   {
559     assertEquals(original.size(), result.size());
560     Iterator iterator = original.iterator();
561     while (iterator.hasNext())
562     {
563       Role originalRole = (Role) iterator.next();
564       Iterator iterator2 = result.iterator();
565       while (iterator2.hasNext())
566       {
567         Role resultRole = (Role) iterator2.next();
568         if (originalRole.getRoleName().equals(resultRole.getRoleName()))
569         {
570           compare(originalRole, resultRole);
571           iterator2.remove();
572         }
573       }
574     }
575     assertEquals(0, result.size());
576   }
577
578   private void compare(Role original, Role result)
579   {
580     assertEquals(original.getRoleName(), result.getRoleName());
581     compareListOfObjectNames(original.getRoleValue(), result.getRoleValue());
582   }
583
584   private void compareListOfObjectNames(List original, List result)
585   {
586     assertEquals(original.size(), result.size());
587     Iterator iterator = original.iterator();
588     while (iterator.hasNext())
589     {
590       ObjectName originalBean = (ObjectName) iterator.next();
591       Iterator iterator2 = result.iterator();
592       while (iterator2.hasNext())
593       {
594         ObjectName resultBean = (ObjectName) iterator2.next();
595         if (originalBean.equals(resultBean))
596         {
597           iterator2.remove();
598         }
599       }
600     }
601     assertEquals(0, result.size());
602   }
603
604   private void compareListOfStrings(List original, List result)
605   {
606     assertEquals(original.size(), result.size());
607     Iterator iterator = original.iterator();
608     while (iterator.hasNext())
609     {
610       String originalString = (String) iterator.next();
611       Iterator iterator2 = result.iterator();
612       while (iterator2.hasNext())
613       {
614         String resultString = (String) iterator2.next();
615         if (originalString.equals(resultString))
616         {
617           iterator2.remove();
618         }
619       }
620     }
621     assertEquals(0, result.size());
622   }
623
624   private ObjectName createRoleValueBean(String name, Class mbean, MBeanServer server)
625   {
626     ObjectName result = null;
627     try
628     {
629       result = new ObjectName(name);
630       if (server != null)
631       {
632         server.registerMBean(mbean.newInstance(), result);
633       }
634     }
635     catch(Exception e)
636     {
637       fail(e.toString());
638     }
639     return result;
640   }
641
642   private void checkResult(RoleResult result, HashMap infos, RoleList roles)
643   {
644     checkResolved(result.getRoles(), infos, roles);
645     checkUnresolved(result.getRolesUnresolved(), infos, roles);
646   }
647
648   private void checkResolved(RoleList resolved, HashMap infos, RoleList roles)
649   {
650     RoleList copy = (RoleList) roles.clone();
651     Iterator iterator = resolved.iterator();
652     while (iterator.hasNext())
653     {
654       Role role = (Role) iterator.next();
655       String roleName = role.getRoleName();
656       RoleInfo info = (RoleInfo) infos.get(roleName);
657       if (info == null)
658         fail("unknown role " + roleName);
659       if (info.isReadable() == false)
660         fail("role should not be readable " + roleName);
661       Role original = removeRole(copy, roleName);
662       compareListOfObjectNames(original.getRoleValue(), role.getRoleValue());
663     }
664
665     iterator = copy.iterator();
666     while (iterator.hasNext())
667     {
668       Role role = (Role) iterator.next();
669       String roleName = role.getRoleName();
670       RoleInfo info = (RoleInfo) infos.get(roleName);
671       if (info.isReadable() == true)
672         fail("missing role " + roleName);
673     }
674   }
675
676   private void checkUnresolved(RoleUnresolvedList unresolved, HashMap infos, RoleList roles)
677   {
678     RoleList copy = (RoleList) roles.clone();
679     Iterator iterator = unresolved.iterator();
680     while (iterator.hasNext())
681     {
682       RoleUnresolved roleUnresolved = (RoleUnresolved) iterator.next();
683       String roleName = roleUnresolved.getRoleName();
684       RoleInfo info = (RoleInfo) infos.get(roleName);
685       if (info == null)
686         fail("unknown role " + roleName);
687       if (info.isReadable() == true)
688         fail("role should be readable " + roleName);
689       removeRole(copy, roleName);
690     }
691
692     iterator = copy.iterator();
693     while (iterator.hasNext())
694     {
695       Role role = (Role) iterator.next();
696       String roleName = role.getRoleName();
697       RoleInfo info = (RoleInfo) infos.get(roleName);
698       if (info.isReadable() == false)
699         fail("missing unresolved role " + roleName);
700     }
701   }
702
703   private Role removeRole(RoleList roles, String roleName)
704   {
705     Iterator iterator = roles.iterator();
706     while (iterator.hasNext())
707     {
708       Role role = (Role) iterator.next();
709       if (role.getRoleName().equals(roleName))
710       {
711         iterator.remove();
712         return role;
713       }
714     }
715     fail("role was not in the original " + roleName);
716     return null;
717   }
718
719   private Role getRole(RoleList roles, String roleName)
720   {
721     Iterator iterator = roles.iterator();
722     while (iterator.hasNext())
723     {
724       Role role = (Role) iterator.next();
725       if (role.getRoleName().equals(roleName))
726       {
727         return role;
728       }
729     }
730     fail("role was not in the original " + roleName);
731     return null;
732   }
733
734   private void checkMBeans(Map result, RoleList roles)
735   {
736     // Construct what we think the value should be
737
Map expected = calcMBeanRoleMap(roles);
738     
739     // Check the actual result
740
Iterator iterator = result.entrySet().iterator();
741     while (iterator.hasNext())
742     {
743        Map.Entry entry = (Map.Entry) iterator.next();
744        ObjectName key = (ObjectName) entry.getKey();
745        ArrayList roleNames = (ArrayList) entry.getValue();
746        ArrayList expectedNames = (ArrayList) expected.get(key);
747        if (expectedNames == null)
748          fail("Unexpected object name " + key);
749        compareListOfStrings(expectedNames, roleNames);
750        expected.remove(key);
751     }
752     assertEquals(0, expected.size());
753   }
754
755   private Map calcMBeanRoleMap(RoleList roles)
756   {
757     HashMap result = new HashMap();
758     Iterator iterator = roles.iterator();
759     while (iterator.hasNext())
760     {
761       Role role = (Role) iterator.next();
762       String roleName = role.getRoleName();
763       ArrayList mbeans = (ArrayList) role.getRoleValue();
764       Iterator iterator2 = mbeans.iterator();
765       while (iterator2.hasNext())
766       {
767         ObjectName objectName = (ObjectName) iterator2.next();
768         ArrayList names = (ArrayList) result.get(objectName);
769         if (names == null)
770         {
771           names = new ArrayList();
772           result.put(objectName, names);
773         }
774         // It seems the role name should be duplicated?
775
// Include the following test if this is a bug in RI.
776
// if (names.contains(roleName) == false)
777

778         names.add(roleName);
779       }
780     }
781     return result;
782   }
783
784   private void createRolesA(MBeanServer server)
785   {
786     try
787     {
788       ArrayList roleA1Values = new ArrayList();
789       roleA1Values.add(createRoleValueBean("x:relation=a,role=1,bean=1",
790                                             Trivial.class, server));
791       Role roleA1 = new Role("roleA1", roleA1Values);
792       rolesA = new RoleList();
793       rolesA.add(roleA1);
794     }
795     catch(Exception e)
796     {
797       fail(e.toString());
798     }
799   }
800
801   private void createRelationTypeA(ObjectName relationService)
802   {
803     try
804     {
805       RoleInfo roleInfoA1 = createRoleInfo("roleA1", Trivial.class, true, true, 1, 1);
806       RoleInfo[] roleInfos = new RoleInfo[] { roleInfoA1 };
807       createRelationType(relationService, "relationTypeA", roleInfos);
808       for (int i=0; i < roleInfos.length; i++)
809         roleInfosA.put(roleInfos[i].getName(), roleInfos[i]);
810     }
811     catch(Exception e)
812     {
813       fail(e.toString());
814     }
815   }
816
817   private void createRolesB(MBeanServer server)
818   {
819     try
820     {
821       ArrayList roleB1Values = new ArrayList();
822       roleB1Values.add(createRoleValueBean("x:relation=b,role=1,bean=1",
823                                             Trivial.class, server));
824       roleB1Values.add(createRoleValueBean("x:relation=b,role=1,bean=2",
825                                             Trivial.class, server));
826       Role roleB1 = new Role("roleB1", roleB1Values);
827
828       ArrayList roleB2Values = new ArrayList();
829       roleB2Values.add(createRoleValueBean("x:relation=b,role=2,bean=1",
830                                             Trivial.class, server));
831       roleB2Values.add(createRoleValueBean("x:relation=b,role=2,bean=2",
832                                             Trivial.class, server));
833       roleB2Values.add(createRoleValueBean("x:relation=b,role=2,bean=3",
834                                             Trivial.class, server));
835       Role roleB2 = new Role("roleB2", roleB2Values);
836
837       rolesB = new RoleList();
838       rolesB.add(roleB1);
839       rolesB.add(roleB2);
840     }
841     catch(Exception e)
842     {
843       fail(e.toString());
844     }
845   }
846
847   private void createRelationTypeB(ObjectName relationService)
848   {
849     try
850     {
851       RoleInfo roleInfoB1 = createRoleInfo("roleB1", Trivial.class, true, false, 1, 2);
852       RoleInfo roleInfoB2 = createRoleInfo("roleB2", Trivial.class, false, true, 3, 4);
853       RoleInfo[] roleInfos = new RoleInfo[] { roleInfoB1, roleInfoB2 };
854       createRelationType(relationService, "relationTypeB", roleInfos );
855       for (int i=0; i < roleInfos.length; i++)
856         roleInfosB.put(roleInfos[i].getName(), roleInfos[i]);
857     }
858     catch(Exception e)
859     {
860       fail(e.toString());
861     }
862   }
863
864   private void createRolesC(MBeanServer server)
865   {
866     try
867     {
868       ArrayList roleC1Values = new ArrayList();
869       roleC1Values.add(createRoleValueBean("x:relation=c,role=1,bean=1",
870                                             Trivial.class, server));
871       roleC1Values.add(createRoleValueBean("x:relation=c,role=1,bean=1",
872                                             Trivial.class, null));
873       roleC1 = new Role("roleC1", roleC1Values);
874
875       ArrayList roleC2Values = new ArrayList();
876       roleC2Values.add(createRoleValueBean("x:relation=c,role=1,bean=1",
877                                             Trivial.class, null));
878       roleC2Values.add(createRoleValueBean("x:relation=c,role=2,bean=1",
879                                             Trivial.class, server));
880       roleC2Values.add(createRoleValueBean("x:relation=c,role=2,bean=2",
881                                             Trivial.class, server));
882       roleC2 = new Role("roleC2", roleC2Values);
883
884       rolesC = new RoleList();
885       rolesC.add(roleC1);
886       rolesC.add(roleC2);
887     }
888     catch(Exception e)
889     {
890       fail(e.toString());
891     }
892   }
893
894   private void createRelationTypeC(ObjectName relationService)
895   {
896     try
897     {
898       RoleInfo roleInfoC1 = createRoleInfo("roleC1", Trivial.class, true, false, 1, 2);
899       RoleInfo roleInfoC2 = createRoleInfo("roleC2", Trivial.class, false, true, 3, 4);
900       RoleInfo[] roleInfos = new RoleInfo[] { roleInfoC1, roleInfoC2 };
901       createRelationType(relationService, "relationTypeC", roleInfos );
902       for (int i=0; i < roleInfos.length; i++)
903         roleInfosC.put(roleInfos[i].getName(), roleInfos[i]);
904     }
905     catch(Exception e)
906     {
907       fail(e.toString());
908     }
909   }
910
911    private void createRolesCX(MBeanServer server)
912    {
913       try
914       {
915          ArrayList roleCX1Values = new ArrayList();
916          roleCX1Values.add(createRoleValueBean("x:relation=c,role=2,bean=1",
917                                               Trivial.class, null));
918          roleCX1 = new Role("roleC1", roleCX1Values);
919
920          ArrayList roleCX2Values = new ArrayList();
921          roleCX2Values.add(createRoleValueBean("x:relation=c,role=1,bean=1",
922                                               Trivial.class, null));
923          roleCX2Values.add(createRoleValueBean("x:relation=c,role=1,bean=2",
924                                               Trivial.class, server));
925          roleCX2Values.add(createRoleValueBean("x:relation=c,role=1,bean=3",
926                                               Trivial.class, server));
927          roleCX2Values.add(createRoleValueBean("x:relation=c,role=1,bean=4",
928                                               Trivial.class, server));
929          roleCX2 = new Role("roleC2", roleCX2Values);
930
931          rolesCX = new RoleList();
932          rolesCX.add(roleCX1);
933          rolesCX.add(roleCX2);
934       }
935       catch(Exception e)
936       {
937          fail(e.toString());
938       }
939    }
940
941    private class Listener
942       implements NotificationListener
943    {
944       String type;
945       HashSet notifications = new HashSet();
946       public Listener(String type)
947       {
948          this.type = type;
949       }
950       public void handleNotification(Notification n, Object h)
951       {
952          notifications.add(n);
953       }
954       public RelationNotification check(int size)
955       {
956          RelationNotification result = null;
957          assertEquals(size, notifications.size());
958          Iterator iterator = notifications.iterator();
959          while (iterator.hasNext())
960          {
961             RelationNotification rn = (RelationNotification) iterator.next();
962             assertEquals(type, rn.getType());
963             result = rn;
964          }
965          return result;
966       }
967    }
968 }
969
Popular Tags