001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package test.compliance.relation;
023:
024: import java.util.ArrayList;
025: import java.util.HashMap;
026: import java.util.HashSet;
027: import java.util.Iterator;
028: import java.util.List;
029: import java.util.Map;
030:
031: import javax.management.MBeanServer;
032: import javax.management.MBeanServerFactory;
033: import javax.management.Notification;
034: import javax.management.NotificationListener;
035: import javax.management.ObjectName;
036: import javax.management.relation.RelationNotification;
037: import javax.management.relation.RelationService;
038: import javax.management.relation.RelationSupport;
039: import javax.management.relation.Role;
040: import javax.management.relation.RoleInfo;
041: import javax.management.relation.RoleList;
042: import javax.management.relation.RoleResult;
043: import javax.management.relation.RoleUnresolved;
044: import javax.management.relation.RoleUnresolvedList;
045:
046: import junit.framework.TestCase;
047: import test.compliance.relation.support.Trivial;
048:
049: /**
050: * Relation Support tests
051: *
052: * @author <a href="mailto:Adrian.Brock@HappeningTimes.com">Adrian Brock</a>.
053: */
054: public class RelationSupportTestCase extends TestCase {
055:
056: // Constants -----------------------------------------------------------------
057:
058: // Attributes ----------------------------------------------------------------
059:
060: HashMap services = new HashMap();
061:
062: RoleList rolesA;
063: HashMap roleInfosA = new HashMap();
064: RoleList rolesB;
065: HashMap roleInfosB = new HashMap();
066: Role roleC1;
067: Role roleC2;
068: RoleList rolesC;
069: HashMap roleInfosC = new HashMap();
070: Role roleCX1;
071: Role roleCX2;
072: RoleList rolesCX;
073: HashMap roleInfosCX = new HashMap();
074:
075: // Constructor ---------------------------------------------------------------
076:
077: /**
078: * Construct the test
079: */
080: public RelationSupportTestCase(String s) {
081: super (s);
082: }
083:
084: // Tests ---------------------------------------------------------------------
085:
086: /**
087: * Test the delegate constructor
088: */
089: public void testDelegateConstructor() {
090: MBeanServer server = createMBeanServer();
091: ObjectName service = createRelationService("test:type=service",
092: null);
093: createRolesB(null);
094: RelationSupport support = null;
095: try {
096: support = new RelationSupport("id", service, server,
097: "relationTypeB", rolesB);
098: } catch (Exception e) {
099: fail(e.toString());
100: }
101: assertEquals("id", support.getRelationId());
102: assertEquals("relationTypeB", support.getRelationTypeName());
103: assertEquals("test:type=service", support
104: .getRelationServiceName().toString());
105: RoleList roleList = support.retrieveAllRoles();
106: compare(rolesB, roleList);
107: }
108:
109: /**
110: * Test get all roles
111: */
112: public void testGetAllRoles() {
113: MBeanServer server = createMBeanServer();
114: ObjectName service = createRelationService("test:type=service",
115: server);
116: createRelationTypeB(service);
117: createRolesB(server);
118: RoleResult result = null;
119: try {
120: RelationSupport support = new RelationSupport("id",
121: service, server, "relationTypeB", rolesB);
122: addRelation(server, service, support, "test:type=support");
123: result = support.getAllRoles();
124: } catch (Exception e) {
125: fail(e.toString());
126: }
127: checkResult(result, roleInfosB, rolesB);
128: }
129:
130: /**
131: * Test get referenced mbeans when not registered in service
132: */
133: public void testGetReferencedMBeansNotRegistered() {
134: MBeanServer server = createMBeanServer();
135: ObjectName service = createRelationService("test:type=service",
136: null);
137: createRelationTypeC(service);
138: createRolesC(server);
139: Map result = null;
140: try {
141: RelationSupport support = new RelationSupport("id",
142: service, server, "relationTypeC", rolesC);
143: result = support.getReferencedMBeans();
144: } catch (Exception e) {
145: fail(e.toString());
146: }
147: checkMBeans(result, rolesC);
148: }
149:
150: /**
151: * Test get referenced mbeans when registered in service
152: */
153: public void testGetReferencedMBeansWhenRegistered() {
154: MBeanServer server = createMBeanServer();
155: ObjectName service = createRelationService("test:type=service",
156: server);
157: createRelationTypeC(service);
158: createRolesC(server);
159: Map result = null;
160: try {
161: RelationSupport support = new RelationSupport("id",
162: service, server, "relationTypeC", rolesC);
163: addRelation(server, service, support, "test:type=support");
164: result = support.getReferencedMBeans();
165: } catch (Exception e) {
166: fail(e.toString());
167: }
168: checkMBeans(result, rolesC);
169: }
170:
171: /**
172: * Test get Role
173: */
174: public void testGetRole() {
175: MBeanServer server = createMBeanServer();
176: ObjectName service = createRelationService("test:type=service",
177: server);
178: createRelationTypeC(service);
179: createRolesC(server);
180: List result = null;
181: try {
182: RelationSupport support = new RelationSupport("id",
183: service, server, "relationTypeC", rolesC);
184: addRelation(server, service, support, "test:type=support");
185: result = support.getRole("roleC1");
186: } catch (Exception e) {
187: fail(e.toString());
188: }
189: compareListOfObjectNames(getRole(rolesC, "roleC1")
190: .getRoleValue(), result);
191: }
192:
193: /**
194: * Test get Role cardinality when not registered
195: */
196: public void testGetRoleCardinalityUnregistered() {
197: MBeanServer server = createMBeanServer();
198: ObjectName service = createRelationService("test:type=service",
199: null);
200: createRolesC(null);
201: Integer result = null;
202: RelationSupport support = null;
203: try {
204: support = new RelationSupport("id", service, server,
205: "relationTypeC", rolesC);
206: result = support.getRoleCardinality("roleC1");
207: } catch (Exception e) {
208: fail(e.toString());
209: }
210: assertEquals(2, result.intValue());
211:
212: try {
213: result = support.getRoleCardinality("roleC2");
214: } catch (Exception e) {
215: fail(e.toString());
216: }
217: assertEquals(3, result.intValue());
218: }
219:
220: /**
221: * Test get Role cardinality registered
222: */
223: public void testGetRoleCardinalityRegistered() {
224: MBeanServer server = createMBeanServer();
225: ObjectName service = createRelationService("test:type=service",
226: server);
227: createRelationTypeC(service);
228: createRolesC(server);
229: Integer result = null;
230: RelationSupport support = null;
231: try {
232: support = new RelationSupport("id", service, server,
233: "relationTypeC", rolesC);
234: addRelation(server, service, support, "test:type=support");
235: result = support.getRoleCardinality("roleC1");
236: } catch (Exception e) {
237: fail(e.toString());
238: }
239: assertEquals(2, result.intValue());
240:
241: try {
242: result = support.getRoleCardinality("roleC2");
243: } catch (Exception e) {
244: fail(e.toString());
245: }
246: assertEquals(3, result.intValue());
247: }
248:
249: /**
250: * Test get Roles
251: */
252: public void testGetRoles() {
253: MBeanServer server = createMBeanServer();
254: ObjectName service = createRelationService("test:type=service",
255: server);
256: createRelationTypeC(service);
257: createRolesC(server);
258: RoleResult result = null;
259: RelationSupport support = null;
260: try {
261: support = new RelationSupport("id", service, server,
262: "relationTypeC", rolesC);
263: addRelation(server, service, support, "test:type=support");
264: result = support
265: .getRoles(new String[] { "roleC1", "roleC2" });
266: } catch (Exception e) {
267: fail(e.toString());
268: }
269: checkResult(result, roleInfosC, rolesC);
270:
271: try {
272: result = support.getRoles(new String[] { "roleC1" });
273: } catch (Exception e) {
274: fail(e.toString());
275: }
276: RoleList resolved = result.getRoles();
277: assertEquals(1, resolved.size());
278: assertEquals(0, result.getRolesUnresolved().size());
279: compare(getRole(rolesC, "roleC1"), (Role) resolved.get(0));
280:
281: try {
282: result = support.getRoles(new String[] { "roleC2" });
283: } catch (Exception e) {
284: fail(e.toString());
285: }
286: RoleUnresolvedList unresolved = result.getRolesUnresolved();
287: assertEquals(0, result.getRoles().size());
288: assertEquals(1, unresolved.size());
289: assertEquals("roleC2", ((RoleUnresolved) unresolved.get(0))
290: .getRoleName());
291: }
292:
293: /**
294: * Test relation service flag
295: */
296: public void testRelationServiceFlag() {
297: MBeanServer server = createMBeanServer();
298: ObjectName service = createRelationService("test:type=service",
299: null);
300: createRolesB(null);
301: RelationSupport support = null;
302: try {
303: support = new RelationSupport("id", service, server,
304: "relationTypeB", rolesB);
305: } catch (Exception e) {
306: fail(e.toString());
307: }
308: assertEquals(false, support.isInRelationService()
309: .booleanValue());
310: support.setRelationServiceManagementFlag(new Boolean(true));
311: assertEquals(true, support.isInRelationService().booleanValue());
312: support.setRelationServiceManagementFlag(new Boolean(false));
313: assertEquals(false, support.isInRelationService()
314: .booleanValue());
315: }
316:
317: /**
318: * Test retrieve all roles when unregistered
319: */
320: public void testRetrieveAllRolesUnRegistered() {
321: MBeanServer server = createMBeanServer();
322: ObjectName service = createRelationService("test:type=service",
323: null);
324: createRolesB(null);
325: RoleList result = null;
326: try {
327: RelationSupport support = new RelationSupport("id",
328: service, server, "relationTypeB", rolesB);
329: result = support.retrieveAllRoles();
330: } catch (Exception e) {
331: fail(e.toString());
332: }
333: compare(rolesB, result);
334: }
335:
336: /**
337: * Test retrieve all roles registered
338: */
339: public void testRetrieveAllRolesRegistered() {
340: MBeanServer server = createMBeanServer();
341: ObjectName service = createRelationService("test:type=service",
342: server);
343: createRelationTypeB(service);
344: createRolesB(server);
345: RoleList result = null;
346: try {
347: RelationSupport support = new RelationSupport("id",
348: service, server, "relationTypeB", rolesB);
349: addRelation(server, service, support, "test:type=support");
350: result = support.retrieveAllRoles();
351: } catch (Exception e) {
352: fail(e.toString());
353: }
354: compare(rolesB, result);
355: }
356:
357: /**
358: * Test set a role
359: */
360: public void testSetRole() {
361: MBeanServer server = MBeanServerFactory.createMBeanServer();
362: try {
363: ObjectName service = createRelationService(
364: "test:type=service", server);
365: createRelationTypeC(service);
366: createRolesC(server);
367: createRolesCX(server);
368: RelationSupport support = null;
369: ObjectName on = null;
370: Listener listener = new Listener(
371: RelationNotification.RELATION_MBEAN_UPDATE);
372: try {
373: support = new RelationSupport("id1", service, server,
374: "relationTypeC", rolesC);
375: addRelation(server, service, support,
376: "test:type=support1");
377: server.addNotificationListener(service, listener, null,
378: null);
379: support.setRole(roleCX2);
380: on = new ObjectName("test:type=support1");
381: } catch (Exception e) {
382: fail(e.toString());
383: }
384: RoleList shouldBe = new RoleList();
385: shouldBe.add(roleC1);
386: shouldBe.add(roleCX2);
387: compare(shouldBe, support.retrieveAllRoles());
388: RelationNotification rn = listener.check(1);
389: assertEquals(new ArrayList(), rn.getMBeansToUnregister());
390: assertEquals(roleCX2.getRoleValue(), rn.getNewRoleValue());
391: assertEquals(on, rn.getObjectName());
392: assertEquals(roleC2.getRoleValue(), rn.getOldRoleValue());
393: assertEquals("id1", rn.getRelationId());
394: assertEquals("relationTypeC", rn.getRelationTypeName());
395: assertEquals("roleC2", rn.getRoleName());
396: } finally {
397: MBeanServerFactory.releaseMBeanServer(server);
398: }
399: }
400:
401: /**
402: * Test set roles
403: * @info.todo different permutations
404: */
405: public void testSetRoles() {
406: MBeanServer server = MBeanServerFactory.createMBeanServer();
407: try {
408: ObjectName service = createRelationService(
409: "test:type=service", server);
410: createRelationTypeC(service);
411: createRolesC(server);
412: createRolesCX(server);
413: RelationSupport support = null;
414: ObjectName on = null;
415: Listener listener = new Listener(
416: RelationNotification.RELATION_MBEAN_UPDATE);
417: RoleList shouldBe = new RoleList();
418: shouldBe.add(roleC1);
419: shouldBe.add(roleCX2);
420: try {
421: support = new RelationSupport("id1", service, server,
422: "relationTypeC", rolesC);
423: addRelation(server, service, support,
424: "test:type=support1");
425: server.addNotificationListener(service, listener, null,
426: null);
427:
428: support.setRoles(shouldBe);
429: on = new ObjectName("test:type=support1");
430: } catch (Exception e) {
431: fail(e.toString());
432: }
433: compare(shouldBe, support.retrieveAllRoles());
434: RelationNotification rn = listener.check(1);
435: } finally {
436: MBeanServerFactory.releaseMBeanServer(server);
437: }
438: }
439:
440: /**
441: * Test error handling
442: */
443: public void testErrors() {
444: // TODO !!!!!!!!
445: }
446:
447: // Support -------------------------------------------------------------------
448:
449: private MBeanServer createMBeanServer() {
450: return MBeanServerFactory.createMBeanServer();
451: }
452:
453: private ObjectName createRelationService(String name,
454: MBeanServer server) {
455: ObjectName result = null;
456: RelationService relationService = new RelationService(true);
457: try {
458: result = new ObjectName(name);
459: services.put(result, relationService);
460: if (server != null)
461: server.registerMBean(relationService, result);
462: } catch (Exception e) {
463: fail(e.toString());
464: }
465: return result;
466: }
467:
468: private ObjectName addRelation(MBeanServer server,
469: ObjectName service, RelationSupport support, String name) {
470: ObjectName result = null;
471: try {
472: result = new ObjectName(name);
473: server.registerMBean(support, result);
474: if (service != null) {
475: RelationService relationService = (RelationService) services
476: .get(service);
477: relationService.addRelation(result);
478: }
479: } catch (Exception e) {
480: fail(e.toString());
481: }
482: return result;
483: }
484:
485: private RoleInfo createRoleInfo(String name, Class mbean,
486: boolean read, boolean write, int min, int max) {
487: RoleInfo result = null;
488: try {
489: result = new RoleInfo(name, mbean.getName(), read, write,
490: min, max, "");
491: } catch (Exception e) {
492: fail(e.toString());
493: }
494: return result;
495: }
496:
497: private void createRelationType(ObjectName relationService,
498: String name, RoleInfo[] roleInfos) {
499: try {
500: RelationService service = (RelationService) services
501: .get(relationService);
502: service.createRelationType(name, roleInfos);
503: } catch (Exception e) {
504: fail(e.toString());
505: }
506: }
507:
508: private void compare(RoleList original, RoleList result) {
509: assertEquals(original.size(), result.size());
510: Iterator iterator = original.iterator();
511: while (iterator.hasNext()) {
512: Role originalRole = (Role) iterator.next();
513: Iterator iterator2 = result.iterator();
514: while (iterator2.hasNext()) {
515: Role resultRole = (Role) iterator2.next();
516: if (originalRole.getRoleName().equals(
517: resultRole.getRoleName())) {
518: compare(originalRole, resultRole);
519: iterator2.remove();
520: }
521: }
522: }
523: assertEquals(0, result.size());
524: }
525:
526: private void compare(Role original, Role result) {
527: assertEquals(original.getRoleName(), result.getRoleName());
528: compareListOfObjectNames(original.getRoleValue(), result
529: .getRoleValue());
530: }
531:
532: private void compareListOfObjectNames(List original, List result) {
533: assertEquals(original.size(), result.size());
534: Iterator iterator = original.iterator();
535: while (iterator.hasNext()) {
536: ObjectName originalBean = (ObjectName) iterator.next();
537: Iterator iterator2 = result.iterator();
538: while (iterator2.hasNext()) {
539: ObjectName resultBean = (ObjectName) iterator2.next();
540: if (originalBean.equals(resultBean)) {
541: iterator2.remove();
542: }
543: }
544: }
545: assertEquals(0, result.size());
546: }
547:
548: private void compareListOfStrings(List original, List result) {
549: assertEquals(original.size(), result.size());
550: Iterator iterator = original.iterator();
551: while (iterator.hasNext()) {
552: String originalString = (String) iterator.next();
553: Iterator iterator2 = result.iterator();
554: while (iterator2.hasNext()) {
555: String resultString = (String) iterator2.next();
556: if (originalString.equals(resultString)) {
557: iterator2.remove();
558: }
559: }
560: }
561: assertEquals(0, result.size());
562: }
563:
564: private ObjectName createRoleValueBean(String name, Class mbean,
565: MBeanServer server) {
566: ObjectName result = null;
567: try {
568: result = new ObjectName(name);
569: if (server != null) {
570: server.registerMBean(mbean.newInstance(), result);
571: }
572: } catch (Exception e) {
573: fail(e.toString());
574: }
575: return result;
576: }
577:
578: private void checkResult(RoleResult result, HashMap infos,
579: RoleList roles) {
580: checkResolved(result.getRoles(), infos, roles);
581: checkUnresolved(result.getRolesUnresolved(), infos, roles);
582: }
583:
584: private void checkResolved(RoleList resolved, HashMap infos,
585: RoleList roles) {
586: RoleList copy = (RoleList) roles.clone();
587: Iterator iterator = resolved.iterator();
588: while (iterator.hasNext()) {
589: Role role = (Role) iterator.next();
590: String roleName = role.getRoleName();
591: RoleInfo info = (RoleInfo) infos.get(roleName);
592: if (info == null)
593: fail("unknown role " + roleName);
594: if (info.isReadable() == false)
595: fail("role should not be readable " + roleName);
596: Role original = removeRole(copy, roleName);
597: compareListOfObjectNames(original.getRoleValue(), role
598: .getRoleValue());
599: }
600:
601: iterator = copy.iterator();
602: while (iterator.hasNext()) {
603: Role role = (Role) iterator.next();
604: String roleName = role.getRoleName();
605: RoleInfo info = (RoleInfo) infos.get(roleName);
606: if (info.isReadable() == true)
607: fail("missing role " + roleName);
608: }
609: }
610:
611: private void checkUnresolved(RoleUnresolvedList unresolved,
612: HashMap infos, RoleList roles) {
613: RoleList copy = (RoleList) roles.clone();
614: Iterator iterator = unresolved.iterator();
615: while (iterator.hasNext()) {
616: RoleUnresolved roleUnresolved = (RoleUnresolved) iterator
617: .next();
618: String roleName = roleUnresolved.getRoleName();
619: RoleInfo info = (RoleInfo) infos.get(roleName);
620: if (info == null)
621: fail("unknown role " + roleName);
622: if (info.isReadable() == true)
623: fail("role should be readable " + roleName);
624: removeRole(copy, roleName);
625: }
626:
627: iterator = copy.iterator();
628: while (iterator.hasNext()) {
629: Role role = (Role) iterator.next();
630: String roleName = role.getRoleName();
631: RoleInfo info = (RoleInfo) infos.get(roleName);
632: if (info.isReadable() == false)
633: fail("missing unresolved role " + roleName);
634: }
635: }
636:
637: private Role removeRole(RoleList roles, String roleName) {
638: Iterator iterator = roles.iterator();
639: while (iterator.hasNext()) {
640: Role role = (Role) iterator.next();
641: if (role.getRoleName().equals(roleName)) {
642: iterator.remove();
643: return role;
644: }
645: }
646: fail("role was not in the original " + roleName);
647: return null;
648: }
649:
650: private Role getRole(RoleList roles, String roleName) {
651: Iterator iterator = roles.iterator();
652: while (iterator.hasNext()) {
653: Role role = (Role) iterator.next();
654: if (role.getRoleName().equals(roleName)) {
655: return role;
656: }
657: }
658: fail("role was not in the original " + roleName);
659: return null;
660: }
661:
662: private void checkMBeans(Map result, RoleList roles) {
663: // Construct what we think the value should be
664: Map expected = calcMBeanRoleMap(roles);
665:
666: // Check the actual result
667: Iterator iterator = result.entrySet().iterator();
668: while (iterator.hasNext()) {
669: Map.Entry entry = (Map.Entry) iterator.next();
670: ObjectName key = (ObjectName) entry.getKey();
671: ArrayList roleNames = (ArrayList) entry.getValue();
672: ArrayList expectedNames = (ArrayList) expected.get(key);
673: if (expectedNames == null)
674: fail("Unexpected object name " + key);
675: compareListOfStrings(expectedNames, roleNames);
676: expected.remove(key);
677: }
678: assertEquals(0, expected.size());
679: }
680:
681: private Map calcMBeanRoleMap(RoleList roles) {
682: HashMap result = new HashMap();
683: Iterator iterator = roles.iterator();
684: while (iterator.hasNext()) {
685: Role role = (Role) iterator.next();
686: String roleName = role.getRoleName();
687: ArrayList mbeans = (ArrayList) role.getRoleValue();
688: Iterator iterator2 = mbeans.iterator();
689: while (iterator2.hasNext()) {
690: ObjectName objectName = (ObjectName) iterator2.next();
691: ArrayList names = (ArrayList) result.get(objectName);
692: if (names == null) {
693: names = new ArrayList();
694: result.put(objectName, names);
695: }
696: // It seems the role name should be duplicated?
697: // Include the following test if this is a bug in RI.
698: // if (names.contains(roleName) == false)
699:
700: names.add(roleName);
701: }
702: }
703: return result;
704: }
705:
706: private void createRolesA(MBeanServer server) {
707: try {
708: ArrayList roleA1Values = new ArrayList();
709: roleA1Values
710: .add(createRoleValueBean(
711: "x:relation=a,role=1,bean=1",
712: Trivial.class, server));
713: Role roleA1 = new Role("roleA1", roleA1Values);
714: rolesA = new RoleList();
715: rolesA.add(roleA1);
716: } catch (Exception e) {
717: fail(e.toString());
718: }
719: }
720:
721: private void createRelationTypeA(ObjectName relationService) {
722: try {
723: RoleInfo roleInfoA1 = createRoleInfo("roleA1",
724: Trivial.class, true, true, 1, 1);
725: RoleInfo[] roleInfos = new RoleInfo[] { roleInfoA1 };
726: createRelationType(relationService, "relationTypeA",
727: roleInfos);
728: for (int i = 0; i < roleInfos.length; i++)
729: roleInfosA.put(roleInfos[i].getName(), roleInfos[i]);
730: } catch (Exception e) {
731: fail(e.toString());
732: }
733: }
734:
735: private void createRolesB(MBeanServer server) {
736: try {
737: ArrayList roleB1Values = new ArrayList();
738: roleB1Values
739: .add(createRoleValueBean(
740: "x:relation=b,role=1,bean=1",
741: Trivial.class, server));
742: roleB1Values
743: .add(createRoleValueBean(
744: "x:relation=b,role=1,bean=2",
745: Trivial.class, server));
746: Role roleB1 = new Role("roleB1", roleB1Values);
747:
748: ArrayList roleB2Values = new ArrayList();
749: roleB2Values
750: .add(createRoleValueBean(
751: "x:relation=b,role=2,bean=1",
752: Trivial.class, server));
753: roleB2Values
754: .add(createRoleValueBean(
755: "x:relation=b,role=2,bean=2",
756: Trivial.class, server));
757: roleB2Values
758: .add(createRoleValueBean(
759: "x:relation=b,role=2,bean=3",
760: Trivial.class, server));
761: Role roleB2 = new Role("roleB2", roleB2Values);
762:
763: rolesB = new RoleList();
764: rolesB.add(roleB1);
765: rolesB.add(roleB2);
766: } catch (Exception e) {
767: fail(e.toString());
768: }
769: }
770:
771: private void createRelationTypeB(ObjectName relationService) {
772: try {
773: RoleInfo roleInfoB1 = createRoleInfo("roleB1",
774: Trivial.class, true, false, 1, 2);
775: RoleInfo roleInfoB2 = createRoleInfo("roleB2",
776: Trivial.class, false, true, 3, 4);
777: RoleInfo[] roleInfos = new RoleInfo[] { roleInfoB1,
778: roleInfoB2 };
779: createRelationType(relationService, "relationTypeB",
780: roleInfos);
781: for (int i = 0; i < roleInfos.length; i++)
782: roleInfosB.put(roleInfos[i].getName(), roleInfos[i]);
783: } catch (Exception e) {
784: fail(e.toString());
785: }
786: }
787:
788: private void createRolesC(MBeanServer server) {
789: try {
790: ArrayList roleC1Values = new ArrayList();
791: roleC1Values
792: .add(createRoleValueBean(
793: "x:relation=c,role=1,bean=1",
794: Trivial.class, server));
795: roleC1Values.add(createRoleValueBean(
796: "x:relation=c,role=1,bean=1", Trivial.class, null));
797: roleC1 = new Role("roleC1", roleC1Values);
798:
799: ArrayList roleC2Values = new ArrayList();
800: roleC2Values.add(createRoleValueBean(
801: "x:relation=c,role=1,bean=1", Trivial.class, null));
802: roleC2Values
803: .add(createRoleValueBean(
804: "x:relation=c,role=2,bean=1",
805: Trivial.class, server));
806: roleC2Values
807: .add(createRoleValueBean(
808: "x:relation=c,role=2,bean=2",
809: Trivial.class, server));
810: roleC2 = new Role("roleC2", roleC2Values);
811:
812: rolesC = new RoleList();
813: rolesC.add(roleC1);
814: rolesC.add(roleC2);
815: } catch (Exception e) {
816: fail(e.toString());
817: }
818: }
819:
820: private void createRelationTypeC(ObjectName relationService) {
821: try {
822: RoleInfo roleInfoC1 = createRoleInfo("roleC1",
823: Trivial.class, true, false, 1, 2);
824: RoleInfo roleInfoC2 = createRoleInfo("roleC2",
825: Trivial.class, false, true, 3, 4);
826: RoleInfo[] roleInfos = new RoleInfo[] { roleInfoC1,
827: roleInfoC2 };
828: createRelationType(relationService, "relationTypeC",
829: roleInfos);
830: for (int i = 0; i < roleInfos.length; i++)
831: roleInfosC.put(roleInfos[i].getName(), roleInfos[i]);
832: } catch (Exception e) {
833: fail(e.toString());
834: }
835: }
836:
837: private void createRolesCX(MBeanServer server) {
838: try {
839: ArrayList roleCX1Values = new ArrayList();
840: roleCX1Values.add(createRoleValueBean(
841: "x:relation=c,role=2,bean=1", Trivial.class, null));
842: roleCX1 = new Role("roleC1", roleCX1Values);
843:
844: ArrayList roleCX2Values = new ArrayList();
845: roleCX2Values.add(createRoleValueBean(
846: "x:relation=c,role=1,bean=1", Trivial.class, null));
847: roleCX2Values
848: .add(createRoleValueBean(
849: "x:relation=c,role=1,bean=2",
850: Trivial.class, server));
851: roleCX2Values
852: .add(createRoleValueBean(
853: "x:relation=c,role=1,bean=3",
854: Trivial.class, server));
855: roleCX2Values
856: .add(createRoleValueBean(
857: "x:relation=c,role=1,bean=4",
858: Trivial.class, server));
859: roleCX2 = new Role("roleC2", roleCX2Values);
860:
861: rolesCX = new RoleList();
862: rolesCX.add(roleCX1);
863: rolesCX.add(roleCX2);
864: } catch (Exception e) {
865: fail(e.toString());
866: }
867: }
868:
869: private class Listener implements NotificationListener {
870: String type;
871: HashSet notifications = new HashSet();
872:
873: public Listener(String type) {
874: this .type = type;
875: }
876:
877: public void handleNotification(Notification n, Object h) {
878: notifications.add(n);
879: }
880:
881: public RelationNotification check(int size) {
882: RelationNotification result = null;
883: assertEquals(size, notifications.size());
884: Iterator iterator = notifications.iterator();
885: while (iterator.hasNext()) {
886: RelationNotification rn = (RelationNotification) iterator
887: .next();
888: assertEquals(type, rn.getType());
889: result = rn;
890: }
891: return result;
892: }
893: }
894: }
|