0001: /*
0002: * JBoss, Home of Professional Open Source.
0003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
0004: * as indicated by the @author tags. See the copyright.txt file in the
0005: * distribution for a full listing of individual contributors.
0006: *
0007: * This is free software; you can redistribute it and/or modify it
0008: * under the terms of the GNU Lesser General Public License as
0009: * published by the Free Software Foundation; either version 2.1 of
0010: * the License, or (at your option) any later version.
0011: *
0012: * This software is distributed in the hope that it will be useful,
0013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
0014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
0015: * Lesser General Public License for more details.
0016: *
0017: * You should have received a copy of the GNU Lesser General Public
0018: * License along with this software; if not, write to the Free
0019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
0020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
0021: */
0022: package test.compliance.relation;
0023:
0024: import test.compliance.relation.support.Trivial;
0025:
0026: import junit.framework.TestCase;
0027:
0028: import java.util.ArrayList;
0029: import java.util.HashMap;
0030: import java.util.HashSet;
0031: import java.util.Iterator;
0032: import java.util.List;
0033: import java.util.Map;
0034:
0035: import javax.management.InstanceNotFoundException;
0036: import javax.management.MBeanNotificationInfo;
0037: import javax.management.MBeanServer;
0038: import javax.management.Notification;
0039: import javax.management.NotificationListener;
0040: import javax.management.MBeanServerFactory;
0041: import javax.management.ObjectName;
0042:
0043: import javax.management.relation.InvalidRelationIdException;
0044: import javax.management.relation.InvalidRelationServiceException;
0045: import javax.management.relation.InvalidRelationTypeException;
0046: import javax.management.relation.InvalidRoleValueException;
0047: import javax.management.relation.RelationNotFoundException;
0048: import javax.management.relation.RelationNotification;
0049: import javax.management.relation.RelationService;
0050: import javax.management.relation.RelationServiceNotRegisteredException;
0051: import javax.management.relation.RelationSupport;
0052: import javax.management.relation.RelationTypeNotFoundException;
0053: import javax.management.relation.RelationTypeSupport;
0054: import javax.management.relation.Role;
0055: import javax.management.relation.RoleInfo;
0056: import javax.management.relation.RoleInfoNotFoundException;
0057: import javax.management.relation.RoleList;
0058: import javax.management.relation.RoleNotFoundException;
0059: import javax.management.relation.RoleResult;
0060: import javax.management.relation.RoleStatus;
0061: import javax.management.relation.RoleUnresolved;
0062: import javax.management.relation.RoleUnresolvedList;
0063:
0064: /**
0065: * Relation Service tests
0066: *
0067: * //TODO test internal relations
0068: * //TODO test relations as mbeans in roles
0069: * //TODO test multiple relation services (avoid future mods adding static data)
0070: *
0071: * @author <a href="mailto:Adrian.Brock@HappeningTimes.com">Adrian Brock</a>.
0072: */
0073: public class RelationServiceTestCase extends TestCase {
0074: // Constants -----------------------------------------------------------------
0075:
0076: // Attributes ----------------------------------------------------------------
0077:
0078: HashMap services = new HashMap();
0079:
0080: RoleList rolesA;
0081: HashMap roleInfosA = new HashMap();
0082: Role roleB1;
0083: Role roleB2;
0084: RoleList rolesB;
0085: HashMap roleInfosB = new HashMap();
0086: Role roleC1;
0087: Role roleC2;
0088: RoleList rolesC;
0089: HashMap roleInfosC = new HashMap();
0090: Role roleCX1;
0091: Role roleCX2;
0092: RoleList rolesCX;
0093: HashMap roleInfosCX = new HashMap();
0094: Role roleCZ2;
0095: RoleList rolesCZ;
0096: HashMap roleInfosCZ = new HashMap();
0097: Role roleCZZ;
0098: RoleList rolesCZZ;
0099: HashMap roleInfosCZZ = new HashMap();
0100: Role roleCZZZ;
0101: RoleList rolesCZZZ;
0102: HashMap roleInfosCZZZ = new HashMap();
0103:
0104: // Constructor ---------------------------------------------------------------
0105:
0106: /**
0107: * Construct the test
0108: */
0109: public RelationServiceTestCase(String s) {
0110: super (s);
0111: }
0112:
0113: // Tests ---------------------------------------------------------------------
0114:
0115: /**
0116: * Test the constructor
0117: */
0118: public void testConstructor() throws Exception {
0119: RelationService rs = null;
0120: rs = new RelationService(true);
0121: assertEquals(true, rs.getPurgeFlag());
0122:
0123: rs = new RelationService(false);
0124: assertEquals(false, rs.getPurgeFlag());
0125: }
0126:
0127: /**
0128: * Test add a relation
0129: */
0130: public void testAddRelation() throws Exception {
0131: MBeanServer server = MBeanServerFactory.createMBeanServer();
0132: try {
0133: ObjectName service = createRelationService(
0134: "test:type=service", server);
0135: createRelationTypeB(service);
0136: createRolesB(server);
0137: RelationSupport support = null;
0138: ObjectName rsupp = null;
0139: String result = null;
0140: Listener listener = new Listener(
0141: RelationNotification.RELATION_MBEAN_CREATION);
0142: RelationService rs = (RelationService) services
0143: .get(service);
0144: server.addNotificationListener(service, listener, null,
0145: null);
0146: support = new RelationSupport("id", service, server,
0147: "relationTypeB", rolesB);
0148: rsupp = new ObjectName("test:add=relation");
0149: server.registerMBean(support, rsupp);
0150: rs.addRelation(rsupp);
0151: result = rs.isRelation(rsupp);
0152: assertEquals("id", result);
0153: listener.check(1);
0154: } finally {
0155: MBeanServerFactory.releaseMBeanServer(server);
0156: }
0157: }
0158:
0159: /**
0160: * Test add a relation errors
0161: */
0162: public void testAddRelationErrors() throws Exception {
0163: MBeanServer server = MBeanServerFactory.createMBeanServer();
0164: try {
0165: ObjectName service = createRelationService(
0166: "test:type=service", null);
0167: RelationService rs = (RelationService) services
0168: .get(service);
0169: createRelationTypeB(service);
0170: createRolesB(server);
0171: RelationSupport support = null;
0172: ObjectName name = null;
0173: support = new RelationSupport("id", service, server,
0174: "relationTypeB", rolesB);
0175: name = new ObjectName("test:type=relation");
0176: server.registerMBean(support, name);
0177:
0178: boolean caught = false;
0179: try {
0180: rs.addRelation(null);
0181: } catch (IllegalArgumentException e) {
0182: caught = true;
0183: }
0184: if (caught == false)
0185: fail("addRelation allows null relation");
0186:
0187: caught = false;
0188: try {
0189: rs.addRelation(name);
0190: } catch (RelationServiceNotRegisteredException e) {
0191: caught = true;
0192: }
0193: if (caught == false)
0194: fail("addRelation allowed when not registered");
0195:
0196: ObjectName badRelation = null;
0197: server.registerMBean(rs, service);
0198: badRelation = new ObjectName("test:type=bad");
0199: server.registerMBean(new Trivial(), badRelation);
0200:
0201: caught = false;
0202: try {
0203: rs.addRelation(badRelation);
0204: } catch (NoSuchMethodException e) {
0205: caught = true;
0206: }
0207: if (caught == false)
0208: fail("addRelation allowed when not a relation");
0209:
0210: caught = false;
0211: try {
0212: rs.addRelation(name);
0213: rs.addRelation(name);
0214: } catch (InvalidRelationIdException e) {
0215: caught = true;
0216: }
0217: if (caught == false)
0218: fail("addRelation allows duplicate relation ids");
0219:
0220: rs.removeRelation("id");
0221: server.unregisterMBean(name);
0222:
0223: caught = false;
0224: try {
0225: rs.addRelation(name);
0226: } catch (InstanceNotFoundException e) {
0227: caught = true;
0228: }
0229: if (caught == false)
0230: fail("addRelation allows unregistered relation");
0231:
0232: ObjectName service2 = createRelationService(
0233: "test:type=service2", null);
0234: RelationService rs2 = (RelationService) services
0235: .get(service);
0236: createRelationTypeB(service2);
0237: RelationSupport support2 = null;
0238: ObjectName name2 = null;
0239: support = new RelationSupport("id", service2, server,
0240: "relationTypeB", rolesB);
0241: name2 = new ObjectName("test:type=relation2");
0242: server.registerMBean(support, name2);
0243:
0244: caught = false;
0245: try {
0246: rs.addRelation(name2);
0247: } catch (InvalidRelationServiceException e) {
0248: caught = true;
0249: }
0250: if (caught == false)
0251: fail("addRelation allows registration in the wrong relation service");
0252:
0253: support = new RelationSupport("id", service, server,
0254: "relationTypeX", rolesB);
0255: name = new ObjectName("test:type=relationX");
0256: server.registerMBean(support, name);
0257:
0258: caught = false;
0259: try {
0260: rs.addRelation(name);
0261: } catch (RelationTypeNotFoundException e) {
0262: caught = true;
0263: }
0264: if (caught == false)
0265: fail("addRelation allows registration with invalid relation type");
0266:
0267: createRelationTypeC(service);
0268: createRolesC(server);
0269: createRolesCZ(server);
0270: support = new RelationSupport("idC", service, server,
0271: "relationTypeB", rolesC);
0272: name = new ObjectName("test:type=relationC");
0273: server.registerMBean(support, name);
0274:
0275: caught = false;
0276: try {
0277: rs.addRelation(name);
0278: } catch (RoleNotFoundException e) {
0279: caught = true;
0280: }
0281: if (caught == false)
0282: fail("addRelation allows registration with invalid role name");
0283:
0284: support = new RelationSupport("idCZ", service, server,
0285: "relationTypeC", rolesCZ);
0286: name = new ObjectName("test:type=relationCZ");
0287: server.registerMBean(support, name);
0288:
0289: caught = false;
0290: try {
0291: rs.addRelation(name);
0292: } catch (InvalidRoleValueException e) {
0293: caught = true;
0294: }
0295: if (caught == false)
0296: fail("addRelation allows registration with invalid role value");
0297: } finally {
0298: MBeanServerFactory.releaseMBeanServer(server);
0299: }
0300: }
0301:
0302: /**
0303: * Test add a relation type
0304: */
0305: public void testAddRelationType() throws Exception {
0306: RoleInfo roleInfo1 = null;
0307: RoleInfo roleInfo2 = null;
0308: RoleInfo[] roleInfos = null;
0309: RelationService rs = null;
0310: ArrayList result = null;
0311: RoleInfo result1 = null;
0312: RoleInfo result2 = null;
0313: roleInfo1 = new RoleInfo("roleInfo1", Trivial.class.getName());
0314: roleInfo2 = new RoleInfo("roleInfo2", Trivial.class.getName());
0315: roleInfos = new RoleInfo[] { roleInfo1, roleInfo2 };
0316: RelationTypeSupport rtsupp = new RelationTypeSupport(
0317: "RelationTypeName", roleInfos);
0318: rs = new RelationService(true);
0319: rs.addRelationType(rtsupp);
0320: result = (ArrayList) rs.getRoleInfos("RelationTypeName");
0321: result1 = rs.getRoleInfo("RelationTypeName", "roleInfo1");
0322: result2 = rs.getRoleInfo("RelationTypeName", "roleInfo2");
0323:
0324: // Check the roleInfos
0325: assertEquals(2, result.size());
0326: assertEquals(roleInfo1.toString(), result1.toString());
0327: assertEquals(roleInfo2.toString(), result2.toString());
0328: }
0329:
0330: /**
0331: * Test create relation type errors
0332: */
0333: public void testAddRelationTypeErrors() throws Exception {
0334: RoleInfo roleInfo1 = null;
0335: RoleInfo roleInfo2 = null;
0336: RoleInfo[] roleInfos = null;
0337: RelationService rs = null;
0338: RelationTypeSupport rtsupp = null;
0339:
0340: // Null relation type
0341: boolean caught = false;
0342: try {
0343: roleInfo1 = new RoleInfo("roleInfo1", Trivial.class
0344: .getName());
0345: roleInfo2 = new RoleInfo("roleInfo2", Trivial.class
0346: .getName());
0347: roleInfos = new RoleInfo[] { roleInfo1, roleInfo2 };
0348: rs = new RelationService(true);
0349: rs.addRelationType(null);
0350: } catch (IllegalArgumentException e) {
0351: caught = true;
0352: }
0353: if (caught == false)
0354: fail("addRelationType allows null relation type");
0355:
0356: // Duplicate relation types
0357: caught = false;
0358: try {
0359: rtsupp = new RelationTypeSupport("RelationTypeName",
0360: roleInfos);
0361: rs.addRelationType(rtsupp);
0362: rs.addRelationType(rtsupp);
0363: } catch (InvalidRelationTypeException e) {
0364: caught = true;
0365: }
0366: if (caught == false)
0367: fail("addRelationType allows duplication relation types");
0368:
0369: // Duplicate role infos
0370: caught = false;
0371: try {
0372: roleInfos[1] = roleInfos[0];
0373: rtsupp = new RelationTypeSupport("RelationTypeName1",
0374: roleInfos);
0375: rs.addRelationType(rtsupp);
0376: } catch (InvalidRelationTypeException e) {
0377: caught = true;
0378: }
0379: if (caught == false)
0380: fail("addRelationType allows duplicate role names");
0381:
0382: // Null role info
0383: caught = false;
0384: try {
0385: roleInfos[1] = null;
0386: rtsupp = new RelationTypeSupport("RelationTypeName1",
0387: roleInfos);
0388: rs.addRelationType(rtsupp);
0389: } catch (InvalidRelationTypeException e) {
0390: caught = true;
0391: }
0392: if (caught == false)
0393: fail("addRelationType allows null role info");
0394:
0395: // No role info
0396: caught = false;
0397: try {
0398: rtsupp = new RelationTypeSupport("RelationTypeName1",
0399: new RoleInfo[0]);
0400: rs.addRelationType(rtsupp);
0401: } catch (InvalidRelationTypeException e) {
0402: caught = true;
0403: }
0404: if (caught == false)
0405: fail("addRelationType allows no role info");
0406: }
0407:
0408: /**
0409: * Test check role reading
0410: */
0411: public void testCheckRoleReadingExternal() throws Exception {
0412: MBeanServer server = MBeanServerFactory.createMBeanServer();
0413: try {
0414: ObjectName service = createRelationService(
0415: "test:type=service", server);
0416: createRelationTypeB(service);
0417: createRolesB(server);
0418: RelationSupport support = null;
0419: Integer readB1 = null;
0420: Integer readB2 = null;
0421: RelationService rs = (RelationService) services
0422: .get(service);
0423: support = new RelationSupport("id", service, server,
0424: "relationTypeB", rolesB);
0425: addRelation(server, service, support, "test:type=support");
0426: readB1 = rs.checkRoleReading("roleB1", "relationTypeB");
0427: readB2 = rs.checkRoleReading("roleB2", "relationTypeB");
0428:
0429: assertEquals(0, readB1.intValue());
0430: assertEquals(RoleStatus.ROLE_NOT_READABLE, readB2
0431: .intValue());
0432: } finally {
0433: MBeanServerFactory.releaseMBeanServer(server);
0434: }
0435: }
0436:
0437: /**
0438: * Test check role reading errors
0439: */
0440: public void testCheckRoleReadingErrors() throws Exception {
0441: ObjectName service = createRelationService("test:type=service",
0442: null);
0443: RelationService rs = (RelationService) services.get(service);
0444: createRelationTypeB(service);
0445: createRolesB(null);
0446:
0447: boolean caught = false;
0448: try {
0449: rs.checkRoleReading(null, "relationTypeB");
0450: } catch (IllegalArgumentException e) {
0451: caught = true;
0452: }
0453: if (caught == false)
0454: fail("checkRoleReading allows null role name");
0455:
0456: caught = false;
0457: try {
0458: rs.checkRoleReading("roleB1", null);
0459: } catch (IllegalArgumentException e) {
0460: caught = true;
0461: }
0462: if (caught == false)
0463: fail("checkRoleReading allows null relation name");
0464:
0465: caught = false;
0466: try {
0467: rs.checkRoleReading("roleB1", "rubbish");
0468: } catch (RelationTypeNotFoundException e) {
0469: caught = true;
0470: }
0471: if (caught == false)
0472: fail("checkRoleReading allows invalid relation type name");
0473: }
0474:
0475: /**
0476: * Test check role writing
0477: */
0478: public void testCheckRoleWritingExternal() throws Exception {
0479: MBeanServer server = MBeanServerFactory.createMBeanServer();
0480: try {
0481: ObjectName service = createRelationService(
0482: "test:type=service", server);
0483: createRelationTypeB(service);
0484: createRolesB(server);
0485: RelationSupport support = null;
0486: Integer writeB1normal = null;
0487: Integer writeB2normal = null;
0488: Integer writeB1init = null;
0489: Integer writeB2init = null;
0490: RelationService rs = (RelationService) services
0491: .get(service);
0492: support = new RelationSupport("id", service, server,
0493: "relationTypeB", rolesB);
0494: addRelation(server, service, support, "test:type=support");
0495: writeB1normal = rs.checkRoleWriting(roleB1,
0496: "relationTypeB", new Boolean(false));
0497: writeB2normal = rs.checkRoleWriting(roleB2,
0498: "relationTypeB", new Boolean(false));
0499: writeB1init = rs.checkRoleWriting(roleB1, "relationTypeB",
0500: new Boolean(true));
0501: writeB2init = rs.checkRoleWriting(roleB2, "relationTypeB",
0502: new Boolean(true));
0503: assertEquals(RoleStatus.ROLE_NOT_WRITABLE, writeB1normal
0504: .intValue());
0505: assertEquals(0, writeB2normal.intValue());
0506: assertEquals(0, writeB1init.intValue());
0507: assertEquals(0, writeB2init.intValue());
0508: } finally {
0509: MBeanServerFactory.releaseMBeanServer(server);
0510: }
0511: }
0512:
0513: /**
0514: * Test check role writing errors
0515: */
0516: public void testCheckRoleWritingErrors() throws Exception {
0517: ObjectName service = createRelationService("test:type=service",
0518: null);
0519: RelationService rs = (RelationService) services.get(service);
0520: createRelationTypeB(service);
0521: createRolesB(null);
0522:
0523: boolean caught = false;
0524: try {
0525: rs.checkRoleWriting(null, "relationTypeB",
0526: new Boolean(true));
0527: } catch (IllegalArgumentException e) {
0528: caught = true;
0529: }
0530: if (caught == false)
0531: fail("checkRoleWriting allows null role name");
0532:
0533: caught = false;
0534: try {
0535: rs.checkRoleWriting(roleB1, null, new Boolean(true));
0536: } catch (IllegalArgumentException e) {
0537: caught = true;
0538: }
0539: if (caught == false)
0540: fail("checkRoleWriting allows null relation name");
0541:
0542: caught = false;
0543: try {
0544: rs.checkRoleWriting(roleB1, "relationTypeB", null);
0545: } catch (IllegalArgumentException e) {
0546: caught = true;
0547: }
0548: if (caught == false)
0549: fail("checkRoleWriting allows null init flag");
0550:
0551: caught = false;
0552: try {
0553: rs.checkRoleWriting(roleB1, "rubbish", new Boolean(true));
0554: } catch (RelationTypeNotFoundException e) {
0555: caught = true;
0556: }
0557: if (caught == false)
0558: fail("checkRoleWriting allows invalid relation type name");
0559: }
0560:
0561: /**
0562: * Test create relation
0563: */
0564: public void testCreateRelation() throws Exception {
0565: MBeanServer server = MBeanServerFactory.createMBeanServer();
0566: try {
0567: ObjectName service = createRelationService(
0568: "test:type=service", server);
0569: createRelationTypeB(service);
0570: createRolesB(server);
0571: RelationSupport support = null;
0572: Listener listener = new Listener(
0573: RelationNotification.RELATION_BASIC_CREATION);
0574: RelationService rs = (RelationService) services
0575: .get(service);
0576: server.addNotificationListener(service, listener, null,
0577: null);
0578: rs.createRelation("id", "relationTypeB", rolesB);
0579: boolean result = rs.hasRelation("id").booleanValue();
0580: assertEquals(true, result);
0581: listener.check(1);
0582: } finally {
0583: MBeanServerFactory.releaseMBeanServer(server);
0584: }
0585: }
0586:
0587: /**
0588: * Test create relation errors
0589: */
0590: public void testCreateRelationErrors() throws Exception {
0591: MBeanServer server = MBeanServerFactory.createMBeanServer();
0592: try {
0593: ObjectName service = createRelationService(
0594: "test:type=service", server);
0595: RelationService rs = (RelationService) services
0596: .get(service);
0597: createRelationTypeB(service);
0598: createRolesB(server);
0599: createRelationTypeC(service);
0600: createRolesC(server);
0601: createRolesCZ(server);
0602: createRolesCZZ(server);
0603:
0604: boolean caught = false;
0605: try {
0606: rs.createRelation(null, "relationTypeC", rolesC);
0607: } catch (IllegalArgumentException e) {
0608: caught = true;
0609: }
0610: if (caught == false)
0611: fail("createRelation allows null relation id");
0612:
0613: caught = false;
0614: try {
0615: rs.createRelation("relationId", null, rolesC);
0616: } catch (IllegalArgumentException e) {
0617: caught = true;
0618: }
0619: if (caught == false)
0620: fail("createRelation allows null relation type name");
0621:
0622: caught = false;
0623: try {
0624: rs.createRelation("relationId", "rubbish", rolesC);
0625: } catch (RelationTypeNotFoundException e) {
0626: caught = true;
0627: }
0628: if (caught == false)
0629: fail("createRelation allows invalid relation type name");
0630:
0631: caught = false;
0632: try {
0633: RoleList roleList = new RoleList();
0634: roleList.add(roleC1);
0635: roleList.add(roleB1);
0636: rs.createRelation("relationId", "relationTypeC",
0637: roleList);
0638: } catch (RoleNotFoundException e) {
0639: caught = true;
0640: }
0641: if (caught == false)
0642: fail("createRelation allows invalid role name");
0643:
0644: caught = false;
0645: try {
0646: RoleList roleList = new RoleList();
0647: roleList.add(roleC1);
0648: roleList.add(roleCZ2);
0649: rs.createRelation("relationId", "relationTypeC",
0650: roleList);
0651: } catch (InvalidRoleValueException e) {
0652: caught = true;
0653: }
0654: if (caught == false)
0655: fail("createRelation allows a role below the minimum");
0656:
0657: caught = false;
0658: try {
0659: RoleList roleList = new RoleList();
0660: roleList.add(roleC1);
0661: roleList.add(roleCZZ);
0662: rs.createRelation("relationId", "relationTypeC",
0663: roleList);
0664: } catch (InvalidRoleValueException e) {
0665: caught = true;
0666: }
0667: if (caught == false)
0668: fail("createRelation allows a role above the maximum");
0669:
0670: caught = false;
0671: try {
0672: RoleList roleList = new RoleList();
0673: roleList.add(roleC1);
0674: roleList.add(roleCZZZ);
0675: rs.createRelation("relationId", "relationTypeC",
0676: roleList);
0677: } catch (InvalidRoleValueException e) {
0678: caught = true;
0679: }
0680: if (caught == false)
0681: fail("createRelation allows a role with unregistered beans");
0682:
0683: caught = false;
0684: try {
0685: rs
0686: .createRelation("relationId", "relationTypeC",
0687: rolesC);
0688: rs
0689: .createRelation("relationId", "relationTypeC",
0690: rolesC);
0691: } catch (InvalidRelationIdException e) {
0692: caught = true;
0693: }
0694: if (caught == false)
0695: fail("createRelation allows duplicate relation id");
0696:
0697: server.unregisterMBean(service);
0698: caught = false;
0699: try {
0700: rs.createRelation("relationId2", "relationTypeC",
0701: rolesC);
0702: } catch (RelationServiceNotRegisteredException e) {
0703: caught = true;
0704: }
0705: if (caught == false)
0706: fail("FAILS IN RI: createRelation allowed when not registered");
0707: } finally {
0708: MBeanServerFactory.releaseMBeanServer(server);
0709: }
0710: }
0711:
0712: /**
0713: * Test create relation type, getRoleInfo and getRoleInfos
0714: */
0715: public void testCreationRelationType() throws Exception {
0716: RoleInfo roleInfo1 = null;
0717: RoleInfo roleInfo2 = null;
0718: RoleInfo[] roleInfos = null;
0719: RelationService rs = null;
0720: ArrayList result = null;
0721: RoleInfo result1 = null;
0722: RoleInfo result2 = null;
0723: roleInfo1 = new RoleInfo("roleInfo1", Trivial.class.getName());
0724: roleInfo2 = new RoleInfo("roleInfo2", Trivial.class.getName());
0725: roleInfos = new RoleInfo[] { roleInfo1, roleInfo2 };
0726: rs = new RelationService(true);
0727: rs.createRelationType("RelationTypeName", roleInfos);
0728: result = (ArrayList) rs.getRoleInfos("RelationTypeName");
0729: result1 = rs.getRoleInfo("RelationTypeName", "roleInfo1");
0730: result2 = rs.getRoleInfo("RelationTypeName", "roleInfo2");
0731:
0732: // Check the roleInfos
0733: assertEquals(2, result.size());
0734: assertEquals(roleInfo1.toString(), result1.toString());
0735: assertEquals(roleInfo2.toString(), result2.toString());
0736: }
0737:
0738: /**
0739: * Test create relation type errors
0740: */
0741: public void testCreateRelationTypeErrors() throws Exception {
0742: RoleInfo roleInfo1 = null;
0743: RoleInfo roleInfo2 = null;
0744: RoleInfo[] roleInfos = null;
0745: RelationService rs = null;
0746:
0747: // Null relation type name
0748: boolean caught = false;
0749: try {
0750: roleInfo1 = new RoleInfo("roleInfo1", Trivial.class
0751: .getName());
0752: roleInfo2 = new RoleInfo("roleInfo2", Trivial.class
0753: .getName());
0754: roleInfos = new RoleInfo[] { roleInfo1, roleInfo2 };
0755: rs = new RelationService(true);
0756: rs.createRelationType(null, roleInfos);
0757: } catch (IllegalArgumentException e) {
0758: caught = true;
0759: }
0760: if (caught == false)
0761: fail("createRelationType allows null relation type name");
0762:
0763: // Null role infos
0764: caught = false;
0765: try {
0766: rs.createRelationType("relationTypeName", null);
0767: } catch (IllegalArgumentException e) {
0768: caught = true;
0769: }
0770: if (caught == false)
0771: fail("createRelationType allows null role infos");
0772:
0773: // Duplicate relation types
0774: caught = false;
0775: try {
0776: rs.createRelationType("relationTypeName", roleInfos);
0777: rs.createRelationType("relationTypeName", roleInfos);
0778: } catch (InvalidRelationTypeException e) {
0779: caught = true;
0780: }
0781: if (caught == false)
0782: fail("createRelationType allows duplicate relation type names");
0783:
0784: // Duplicate role infos
0785: caught = false;
0786: try {
0787: roleInfos[1] = roleInfos[0];
0788: rs.createRelationType("relationTypeName1", roleInfos);
0789: } catch (InvalidRelationTypeException e) {
0790: caught = true;
0791: }
0792: if (caught == false)
0793: fail("createRelationType allows duplicate role names");
0794:
0795: // Null role info
0796: caught = false;
0797: try {
0798: roleInfos[1] = null;
0799: rs.createRelationType("relationTypeName1", roleInfos);
0800: } catch (InvalidRelationTypeException e) {
0801: caught = true;
0802: }
0803: if (caught == false)
0804: fail("createRelationType allows null role info");
0805:
0806: // No role info
0807: caught = false;
0808: try {
0809: rs.createRelationType("relationTypeName1", new RoleInfo[0]);
0810: } catch (InvalidRelationTypeException e) {
0811: caught = true;
0812: }
0813: if (caught == false)
0814: fail("createRelationType allows no role info");
0815: }
0816:
0817: /**
0818: * Test getRoleInfo errors
0819: */
0820: public void testGetRoleInfoErrors() throws Exception {
0821: RoleInfo roleInfo1 = null;
0822: RoleInfo roleInfo2 = null;
0823: RoleInfo[] roleInfos = null;
0824: RelationService rs = null;
0825: ArrayList result = null;
0826: RoleInfo result1 = null;
0827: roleInfo1 = new RoleInfo("roleInfo1", Trivial.class.getName());
0828: roleInfo2 = new RoleInfo("roleInfo2", Trivial.class.getName());
0829: roleInfos = new RoleInfo[] { roleInfo1, roleInfo2 };
0830: rs = new RelationService(true);
0831: rs.createRelationType("RelationTypeName", roleInfos);
0832:
0833: boolean caught = false;
0834: try {
0835: rs.getRoleInfo(null, "roleInfo1");
0836: } catch (IllegalArgumentException e) {
0837: caught = true;
0838: }
0839: if (caught == false)
0840: fail("getRoleInfo allows null relation type name");
0841:
0842: caught = false;
0843: try {
0844: rs.getRoleInfo("RelationTypeName", null);
0845: } catch (IllegalArgumentException e) {
0846: caught = true;
0847: }
0848: if (caught == false)
0849: fail("getRoleInfo allows null role info name");
0850:
0851: caught = false;
0852: try {
0853: rs.getRoleInfo("RelationTypeNameX", "roleInfo1");
0854: } catch (RelationTypeNotFoundException e) {
0855: caught = true;
0856: }
0857: if (caught == false)
0858: fail("getRoleInfo allows non-existent relation type name");
0859:
0860: caught = false;
0861: try {
0862: rs.getRoleInfo("RelationTypeName", "roleInfoX");
0863: } catch (RoleInfoNotFoundException e) {
0864: caught = true;
0865: }
0866: if (caught == false)
0867: fail("getRoleInfo allows non-existent role info name");
0868: }
0869:
0870: /**
0871: * Test getRoleInfos errors
0872: */
0873: public void testGetRoleInfosErrors() throws Exception {
0874: RoleInfo roleInfo1 = null;
0875: RoleInfo roleInfo2 = null;
0876: RoleInfo[] roleInfos = null;
0877: RelationService rs = null;
0878: ArrayList result = null;
0879: RoleInfo result1 = null;
0880: roleInfo1 = new RoleInfo("roleInfo1", Trivial.class.getName());
0881: roleInfo2 = new RoleInfo("roleInfo2", Trivial.class.getName());
0882: roleInfos = new RoleInfo[] { roleInfo1, roleInfo2 };
0883: rs = new RelationService(true);
0884: rs.createRelationType("RelationTypeName", roleInfos);
0885:
0886: boolean caught = false;
0887: try {
0888: rs.getRoleInfos(null);
0889: } catch (IllegalArgumentException e) {
0890: caught = true;
0891: }
0892: if (caught == false)
0893: fail("getRoleInfos allows null relation type name");
0894:
0895: caught = false;
0896: try {
0897: rs.getRoleInfos("RelationTypeNameX");
0898: } catch (RelationTypeNotFoundException e) {
0899: caught = true;
0900: }
0901: if (caught == false)
0902: fail("getRoleInfos allows non-existent relation type name");
0903: }
0904:
0905: /**
0906: * Test find associated mbeans
0907: *
0908: * REVIEW: This test needs more thought
0909: */
0910: public void testFindAssociatedMBeansExternal() throws Exception {
0911: MBeanServer server = MBeanServerFactory.createMBeanServer();
0912: try {
0913: ObjectName service = createRelationService(
0914: "test:type=service", server);
0915: createRelationTypeC(service);
0916: createRolesC(server);
0917: createRelationTypeCX(service);
0918: createRolesCX(server);
0919: Map result1 = null;
0920: Map result2 = null;
0921: Map result3 = null;
0922: Map result4 = null;
0923: Map result5 = null;
0924: Map result6 = null;
0925: Map result7 = null;
0926: Map result8 = null;
0927: Map result9 = null;
0928: Map result10 = null;
0929: Map result11 = null;
0930: Map result12 = null;
0931: Map result13 = null;
0932: Map result14 = null;
0933: Map result15 = null;
0934: Map result16 = null;
0935: RelationSupport supportCX = new RelationSupport("idcx",
0936: service, server, "relationTypeCX", rolesCX);
0937: RelationSupport supportC = new RelationSupport("idc",
0938: service, server, "relationTypeC", rolesC);
0939: addRelation(server, service, supportCX,
0940: "test:type=supportCX");
0941: addRelation(server, service, supportC, "test:type=supportC");
0942: RelationService rs = (RelationService) services
0943: .get(service);
0944: result1 = rs.findAssociatedMBeans(new ObjectName(
0945: "x:relation=c,role=1,bean=1"), null, null);
0946: result2 = rs.findAssociatedMBeans(new ObjectName(
0947: "x:relation=c,role=1,bean=1"), null, null);
0948: result3 = rs.findAssociatedMBeans(new ObjectName(
0949: "x:relation=c,role=1,bean=1"), "relationTypeCX",
0950: null);
0951: result4 = rs.findAssociatedMBeans(new ObjectName(
0952: "x:relation=c,role=1,bean=1"), "relationTypeC",
0953: null);
0954: result5 = rs.findAssociatedMBeans(new ObjectName(
0955: "x:relation=c,role=1,bean=1"), null, "roleC1");
0956: result6 = rs.findAssociatedMBeans(new ObjectName(
0957: "x:relation=c,role=1,bean=1"), null, "roleC2");
0958: result7 = rs.findAssociatedMBeans(new ObjectName(
0959: "x:relation=c,role=1,bean=1"), null, "roleC1");
0960: result8 = rs.findAssociatedMBeans(new ObjectName(
0961: "x:relation=c,role=1,bean=1"), null, "roleC2");
0962: result9 = rs.findAssociatedMBeans(new ObjectName(
0963: "x:relation=c,role=1,bean=1"), "relationTypeCX",
0964: "roleC1");
0965: result10 = rs.findAssociatedMBeans(new ObjectName(
0966: "x:relation=c,role=1,bean=1"), "relationTypeC",
0967: "roleC1");
0968: result11 = rs.findAssociatedMBeans(new ObjectName(
0969: "x:relation=c,role=1,bean=1"), "relationTypeCX",
0970: "roleC2");
0971: result12 = rs.findAssociatedMBeans(new ObjectName(
0972: "x:relation=c,role=1,bean=1"), "relationTypeC",
0973: "roleC2");
0974: result13 = rs.findAssociatedMBeans(new ObjectName(
0975: "x:relation=c,role=1,bean=1"), "relationTypeCX",
0976: "roleC1");
0977: result14 = rs.findAssociatedMBeans(new ObjectName(
0978: "x:relation=c,role=1,bean=1"), "relationTypeC",
0979: "roleC1");
0980: result15 = rs.findAssociatedMBeans(new ObjectName(
0981: "x:relation=c,role=1,bean=1"), "relationTypeCX",
0982: "roleC2");
0983: result16 = rs.findAssociatedMBeans(new ObjectName(
0984: "x:relation=c,role=1,bean=1"), "relationTypeC",
0985: "roleC2");
0986: assertEquals(5, result1.size());
0987: assertEquals(5, result2.size());
0988: assertEquals(4, result3.size());
0989: assertEquals(2, result4.size());
0990: assertEquals(2, result5.size());
0991: assertEquals(5, result6.size());
0992: assertEquals(2, result7.size());
0993: assertEquals(5, result8.size());
0994: assertEquals(0, result9.size());
0995: assertEquals(2, result10.size());
0996: assertEquals(4, result11.size());
0997: assertEquals(2, result12.size());
0998: assertEquals(0, result13.size());
0999: assertEquals(2, result14.size());
1000: assertEquals(4, result15.size());
1001: assertEquals(2, result16.size());
1002: } finally {
1003: MBeanServerFactory.releaseMBeanServer(server);
1004: }
1005: }
1006:
1007: /**
1008: * Test find associated mbeans errors
1009: */
1010: public void testFindAssociatedMBeansErrors() throws Exception {
1011: RelationService rs = new RelationService(true);
1012:
1013: boolean caught = false;
1014: try {
1015: rs.findAssociatedMBeans(null, null, null);
1016: } catch (IllegalArgumentException e) {
1017: caught = true;
1018: }
1019: if (caught == false)
1020: fail("findAssociatedMBeans allows a null mbean name");
1021: }
1022:
1023: /**
1024: * Test find referencing relations
1025: *
1026: * REVIEW: This test needs more thought
1027: */
1028: public void testFindReferencingRelationsExternal() throws Exception {
1029: MBeanServer server = MBeanServerFactory.createMBeanServer();
1030: try {
1031: ObjectName service = createRelationService(
1032: "test:type=service", server);
1033: createRelationTypeC(service);
1034: createRolesC(server);
1035: createRelationTypeCX(service);
1036: createRolesCX(server);
1037: Map result1 = null;
1038: Map result2 = null;
1039: Map result3 = null;
1040: Map result4 = null;
1041: Map result5 = null;
1042: Map result6 = null;
1043: Map result7 = null;
1044: Map result8 = null;
1045: Map result9 = null;
1046: Map result10 = null;
1047: Map result11 = null;
1048: Map result12 = null;
1049: Map result13 = null;
1050: Map result14 = null;
1051: Map result15 = null;
1052: Map result16 = null;
1053: RelationSupport supportCX = new RelationSupport("idcx",
1054: service, server, "relationTypeCX", rolesCX);
1055: RelationSupport supportC = new RelationSupport("idc",
1056: service, server, "relationTypeC", rolesC);
1057: addRelation(server, service, supportCX,
1058: "test:type=supportCX");
1059: addRelation(server, service, supportC, "test:type=supportC");
1060: RelationService rs = (RelationService) services
1061: .get(service);
1062: result1 = rs.findReferencingRelations(new ObjectName(
1063: "x:relation=c,role=1,bean=1"), null, null);
1064: result2 = rs.findReferencingRelations(new ObjectName(
1065: "x:relation=c,role=1,bean=1"), null, null);
1066: result3 = rs.findReferencingRelations(new ObjectName(
1067: "x:relation=c,role=1,bean=1"), "relationTypeCX",
1068: null);
1069: result4 = rs.findReferencingRelations(new ObjectName(
1070: "x:relation=c,role=1,bean=1"), "relationTypeC",
1071: null);
1072: result5 = rs.findReferencingRelations(new ObjectName(
1073: "x:relation=c,role=1,bean=1"), null, "roleC1");
1074: result6 = rs.findReferencingRelations(new ObjectName(
1075: "x:relation=c,role=1,bean=1"), null, "roleC2");
1076: result7 = rs.findReferencingRelations(new ObjectName(
1077: "x:relation=c,role=1,bean=1"), null, "roleC1");
1078: result8 = rs.findReferencingRelations(new ObjectName(
1079: "x:relation=c,role=1,bean=1"), null, "roleC2");
1080: result9 = rs.findReferencingRelations(new ObjectName(
1081: "x:relation=c,role=1,bean=1"), "relationTypeCX",
1082: "roleC1");
1083: result10 = rs.findReferencingRelations(new ObjectName(
1084: "x:relation=c,role=1,bean=1"), "relationTypeC",
1085: "roleC1");
1086: result11 = rs.findReferencingRelations(new ObjectName(
1087: "x:relation=c,role=1,bean=1"), "relationTypeCX",
1088: "roleC2");
1089: result12 = rs.findReferencingRelations(new ObjectName(
1090: "x:relation=c,role=1,bean=1"), "relationTypeC",
1091: "roleC2");
1092: result13 = rs.findReferencingRelations(new ObjectName(
1093: "x:relation=c,role=1,bean=1"), "relationTypeCX",
1094: "roleC1");
1095: result14 = rs.findReferencingRelations(new ObjectName(
1096: "x:relation=c,role=1,bean=1"), "relationTypeC",
1097: "roleC1");
1098: result15 = rs.findReferencingRelations(new ObjectName(
1099: "x:relation=c,role=1,bean=1"), "relationTypeCX",
1100: "roleC2");
1101: result16 = rs.findReferencingRelations(new ObjectName(
1102: "x:relation=c,role=1,bean=1"), "relationTypeC",
1103: "roleC2");
1104: assertEquals(2, result1.size());
1105: assertEquals(2, result2.size());
1106: assertEquals(1, result3.size());
1107: assertEquals(1, result4.size());
1108: assertEquals(1, result5.size());
1109: assertEquals(2, result6.size());
1110: assertEquals(1, result7.size());
1111: assertEquals(2, result8.size());
1112: assertEquals(0, result9.size());
1113: assertEquals(1, result10.size());
1114: assertEquals(1, result11.size());
1115: assertEquals(1, result12.size());
1116: assertEquals(0, result13.size());
1117: assertEquals(1, result14.size());
1118: assertEquals(1, result15.size());
1119: assertEquals(1, result16.size());
1120: } finally {
1121: MBeanServerFactory.releaseMBeanServer(server);
1122: }
1123: }
1124:
1125: /**
1126: * Test find referencing relations errors
1127: */
1128: public void testFindReferencingRelationsErrors() throws Exception {
1129: RelationService rs = new RelationService(true);
1130:
1131: boolean caught = false;
1132: try {
1133: rs.findReferencingRelations(null, null, null);
1134: } catch (IllegalArgumentException e) {
1135: caught = true;
1136: }
1137: if (caught == false)
1138: fail("findReferencingRelations allows a null mbean name");
1139: }
1140:
1141: /**
1142: * Test find relations of types
1143: *
1144: * REVIEW: This test needs more thought
1145: */
1146: public void testFindRelationsOfTypeExternal() throws Exception {
1147: MBeanServer server = MBeanServerFactory.createMBeanServer();
1148: try {
1149: ObjectName service = createRelationService(
1150: "test:type=service", server);
1151: createRelationTypeA(service);
1152: createRolesA(server);
1153: createRelationTypeB(service);
1154: createRolesB(server);
1155: createRelationTypeC(service);
1156: createRolesC(server);
1157: createRelationTypeCX(service);
1158: createRolesCX(server);
1159: List result1 = null;
1160: List result2 = null;
1161: List result3 = null;
1162: List result4 = null;
1163: RelationSupport supportA1 = new RelationSupport("ida1",
1164: service, server, "relationTypeA", rolesA);
1165: RelationSupport supportA2 = new RelationSupport("ida2",
1166: service, server, "relationTypeA", rolesA);
1167: RelationSupport supportCX = new RelationSupport("idcx",
1168: service, server, "relationTypeCX", rolesCX);
1169: RelationSupport supportC = new RelationSupport("idc",
1170: service, server, "relationTypeC", rolesC);
1171: addRelation(server, service, supportA1,
1172: "test:type=supportA1");
1173: addRelation(server, service, supportA2,
1174: "test:type=supportA2");
1175: addRelation(server, service, supportCX,
1176: "test:type=supportCX");
1177: addRelation(server, service, supportC, "test:type=supportC");
1178: RelationService rs = (RelationService) services
1179: .get(service);
1180: result1 = rs.findRelationsOfType("relationTypeA");
1181: result2 = rs.findRelationsOfType("relationTypeB");
1182: result3 = rs.findRelationsOfType("relationTypeC");
1183: result4 = rs.findRelationsOfType("relationTypeCX");
1184: assertEquals(2, result1.size());
1185: assertEquals(0, result2.size());
1186: assertEquals(1, result3.size());
1187: assertEquals(1, result4.size());
1188: } finally {
1189: MBeanServerFactory.releaseMBeanServer(server);
1190: }
1191: }
1192:
1193: /**
1194: * Test find relations of type errors
1195: */
1196: public void testFindRelationsOfTypeErrors() throws Exception {
1197: RelationService rs = new RelationService(true);
1198:
1199: boolean caught = false;
1200: try {
1201: rs.findRelationsOfType(null);
1202: } catch (IllegalArgumentException e) {
1203: caught = true;
1204: }
1205: if (caught == false)
1206: fail("findRelationsOfType allows a null relation type name");
1207:
1208: caught = false;
1209: try {
1210: rs.findRelationsOfType("rubbish");
1211: } catch (RelationTypeNotFoundException e) {
1212: caught = true;
1213: }
1214: if (caught == false)
1215: fail("findRelationsOfType allows an invalid relation type name");
1216: }
1217:
1218: /**
1219: * Test get all relations ids
1220: *
1221: * REVIEW: This test needs more thought
1222: */
1223: public void testGetAllRelationsIdsExternal() throws Exception {
1224: MBeanServer server = MBeanServerFactory.createMBeanServer();
1225: try {
1226: ObjectName service = createRelationService(
1227: "test:type=service", server);
1228: createRelationTypeA(service);
1229: createRolesA(server);
1230: createRelationTypeB(service);
1231: createRolesB(server);
1232: createRelationTypeC(service);
1233: createRolesC(server);
1234: createRelationTypeCX(service);
1235: createRolesCX(server);
1236: List result = null;
1237: RelationSupport supportA1 = new RelationSupport("ida1",
1238: service, server, "relationTypeA", rolesA);
1239: RelationSupport supportA2 = new RelationSupport("ida2",
1240: service, server, "relationTypeA", rolesA);
1241: RelationSupport supportCX = new RelationSupport("idcx",
1242: service, server, "relationTypeCX", rolesCX);
1243: RelationSupport supportC = new RelationSupport("idc",
1244: service, server, "relationTypeC", rolesC);
1245: addRelation(server, service, supportA1,
1246: "test:type=supportA1");
1247: addRelation(server, service, supportA2,
1248: "test:type=supportA2");
1249: addRelation(server, service, supportCX,
1250: "test:type=supportCX");
1251: addRelation(server, service, supportC, "test:type=supportC");
1252: RelationService rs = (RelationService) services
1253: .get(service);
1254: result = rs.getAllRelationIds();
1255: assertEquals(4, result.size());
1256: assertEquals(true, result.contains("ida1"));
1257: assertEquals(true, result.contains("ida2"));
1258: assertEquals(true, result.contains("idcx"));
1259: assertEquals(true, result.contains("idc"));
1260: } finally {
1261: MBeanServerFactory.releaseMBeanServer(server);
1262: }
1263: }
1264:
1265: /**
1266: * Test get all relation type names
1267: */
1268: public void testGetAllRelationTypeNames() throws Exception {
1269: RelationService rs = new RelationService(true);
1270: assertEquals(0, rs.getAllRelationTypeNames().size());
1271:
1272: RoleInfo roleInfo1 = null;
1273: RoleInfo roleInfo2 = null;
1274: RoleInfo[] roleInfos = null;
1275: roleInfo1 = new RoleInfo("roleInfo1", Trivial.class.getName());
1276: roleInfo2 = new RoleInfo("roleInfo2", Trivial.class.getName());
1277: roleInfos = new RoleInfo[] { roleInfo1, roleInfo2 };
1278: rs.createRelationType("name1", roleInfos);
1279: rs.createRelationType("name2", roleInfos);
1280: rs.createRelationType("name3", roleInfos);
1281:
1282: ArrayList result = (ArrayList) rs.getAllRelationTypeNames();
1283: assertEquals(3, result.size());
1284: assertEquals(true, result.contains("name1"));
1285: assertEquals(true, result.contains("name2"));
1286: assertEquals(true, result.contains("name3"));
1287: }
1288:
1289: /**
1290: * Test get all roles
1291: */
1292: public void testGetAllRolesExternal() throws Exception {
1293: MBeanServer server = MBeanServerFactory.createMBeanServer();
1294: try {
1295: ObjectName service = createRelationService(
1296: "test:type=service", server);
1297: createRelationTypeB(service);
1298: createRolesB(server);
1299: RoleResult result = null;
1300: RelationSupport support = new RelationSupport("id",
1301: service, server, "relationTypeB", rolesB);
1302: addRelation(server, service, support, "test:type=support");
1303: result = support.getAllRoles();
1304: checkResult(result, roleInfosB, rolesB);
1305: } finally {
1306: MBeanServerFactory.releaseMBeanServer(server);
1307: }
1308: }
1309:
1310: /**
1311: * Test get all roles errors
1312: */
1313: public void testGetAllRolesErrors() throws Exception {
1314: MBeanServer server = MBeanServerFactory.createMBeanServer();
1315: try {
1316: ObjectName service = createRelationService(
1317: "test:type=service", server);
1318: RelationService rs = (RelationService) services
1319: .get(service);
1320: createRelationTypeC(service);
1321: createRolesC(server);
1322: RoleList roles = new RoleList();
1323: roles.add(roleC1);
1324: roles.add(roleC2);
1325: rs.createRelation("relationId", "relationTypeC", roles);
1326: String[] roleNames = new String[] { "roleC1" };
1327:
1328: boolean caught = false;
1329: try {
1330: rs.getAllRoles(null);
1331: } catch (IllegalArgumentException e) {
1332: caught = true;
1333: }
1334: if (caught == false)
1335: fail("getAllRoles allows null relation id");
1336:
1337: caught = false;
1338: try {
1339: rs.getAllRoles("rubbish");
1340: } catch (RelationNotFoundException e) {
1341: caught = true;
1342: }
1343: if (caught == false)
1344: fail("getAllRoles allows invalid relation id");
1345:
1346: server.unregisterMBean(service);
1347: caught = false;
1348: try {
1349: rs.getAllRoles("relationId");
1350: } catch (RelationServiceNotRegisteredException e) {
1351: caught = true;
1352: }
1353: if (caught == false)
1354: fail("FAILS IN RI: getAllRoles allowed when not registered");
1355: } finally {
1356: MBeanServerFactory.releaseMBeanServer(server);
1357: }
1358: }
1359:
1360: /**
1361: * Test get Notification info
1362: */
1363: public void testGetNotificationInfo() {
1364: RelationService rs = new RelationService(true);
1365: MBeanNotificationInfo[] mbni = rs.getNotificationInfo();
1366: assertEquals(1, mbni.length);
1367: HashSet types = new HashSet();
1368: types.add(RelationNotification.RELATION_BASIC_CREATION);
1369: types.add(RelationNotification.RELATION_BASIC_REMOVAL);
1370: types.add(RelationNotification.RELATION_BASIC_UPDATE);
1371: types.add(RelationNotification.RELATION_MBEAN_CREATION);
1372: types.add(RelationNotification.RELATION_MBEAN_REMOVAL);
1373: types.add(RelationNotification.RELATION_MBEAN_UPDATE);
1374: String[] mbniTypes = mbni[0].getNotifTypes();
1375: assertEquals(types.size(), mbniTypes.length);
1376: for (int i = 0; i < mbniTypes.length; i++) {
1377: if (types.contains(mbniTypes[i]) == false)
1378: fail("Unexpected relation notification type: "
1379: + mbniTypes[i]);
1380: }
1381: }
1382:
1383: /**
1384: * Test get/set Purge Flag
1385: */
1386: public void testGetSetPurgeFlag() throws Exception {
1387: MBeanServer server = MBeanServerFactory.createMBeanServer();
1388: try {
1389: RelationService rs = null;
1390: ObjectName name = null;
1391: rs = new RelationService(true);
1392: name = new ObjectName("test:type = rs");
1393: server.registerMBean(rs, name);
1394: assertEquals(true, rs.getPurgeFlag());
1395: rs.setPurgeFlag(false);
1396: assertEquals(false, rs.getPurgeFlag());
1397: rs.setPurgeFlag(true);
1398: assertEquals(true, rs.getPurgeFlag());
1399: } finally {
1400: MBeanServerFactory.releaseMBeanServer(server);
1401: }
1402: }
1403:
1404: /**
1405: * Test get Referenced MBeans
1406: */
1407: public void testGetReferencedMBeansExternal() throws Exception {
1408: MBeanServer server = MBeanServerFactory.createMBeanServer();
1409: try {
1410: ObjectName service = createRelationService(
1411: "test:type=service", server);
1412: createRelationTypeC(service);
1413: createRolesC(server);
1414: Map result = null;
1415: RelationSupport support = new RelationSupport("id",
1416: service, server, "relationTypeC", rolesC);
1417: addRelation(server, service, support, "test:type=support");
1418: result = support.getReferencedMBeans();
1419: checkMBeans(result, rolesC);
1420: } finally {
1421: MBeanServerFactory.releaseMBeanServer(server);
1422: }
1423: }
1424:
1425: /**
1426: * Test get referenced mbeans errors
1427: */
1428: public void testGetReferencedMBeansErrors() throws Exception {
1429: MBeanServer server = MBeanServerFactory.createMBeanServer();
1430: try {
1431: ObjectName service = createRelationService(
1432: "test:type=service", server);
1433: RelationService rs = (RelationService) services
1434: .get(service);
1435: createRelationTypeC(service);
1436: createRolesC(server);
1437: RoleList roles = new RoleList();
1438: roles.add(roleC1);
1439: roles.add(roleC2);
1440: rs.createRelation("relationId", "relationTypeC", roles);
1441: String[] roleNames = new String[] { "roleC1" };
1442:
1443: boolean caught = false;
1444: try {
1445: rs.getReferencedMBeans(null);
1446: } catch (IllegalArgumentException e) {
1447: caught = true;
1448: }
1449: if (caught == false)
1450: fail("getReferencedMBeans allows null relation id");
1451:
1452: caught = false;
1453: try {
1454: rs.getReferencedMBeans("rubbish");
1455: } catch (RelationNotFoundException e) {
1456: caught = true;
1457: }
1458: if (caught == false)
1459: fail("getReferencedMBeans allows invalid relation id");
1460: } finally {
1461: MBeanServerFactory.releaseMBeanServer(server);
1462: }
1463: }
1464:
1465: /**
1466: * Test get Relation Type Names
1467: */
1468: public void testGetRelationTypeNameExternal() throws Exception {
1469: MBeanServer server = MBeanServerFactory.createMBeanServer();
1470: try {
1471: ObjectName service = createRelationService(
1472: "test:type=service", server);
1473: RelationService rs = (RelationService) services
1474: .get(service);
1475: createRelationTypeB(service);
1476: createRolesB(server);
1477: createRelationTypeC(service);
1478: createRolesC(server);
1479: RelationSupport relB = null;
1480: RelationSupport relC = null;
1481: String resultB = null;
1482: String resultC = null;
1483: relB = new RelationSupport("idB", service, server,
1484: "relationTypeB", rolesB);
1485: addRelation(server, service, relB, "test:type=supportB");
1486: relC = new RelationSupport("idC", service, server,
1487: "relationTypeC", rolesC);
1488: addRelation(server, service, relC, "test:type=supportC");
1489: resultB = rs.getRelationTypeName("idB");
1490: resultC = rs.getRelationTypeName("idC");
1491: assertEquals("relationTypeB", resultB);
1492: assertEquals("relationTypeC", resultC);
1493: } finally {
1494: MBeanServerFactory.releaseMBeanServer(server);
1495: }
1496: }
1497:
1498: /**
1499: * Test get relation type name errors
1500: */
1501: public void testGetRelationTypeNameErrors() throws Exception {
1502: MBeanServer server = MBeanServerFactory.createMBeanServer();
1503: try {
1504: ObjectName service = createRelationService(
1505: "test:type=service", server);
1506: RelationService rs = (RelationService) services
1507: .get(service);
1508: createRelationTypeC(service);
1509: createRolesC(server);
1510: RoleList roles = new RoleList();
1511: roles.add(roleC1);
1512: roles.add(roleC2);
1513: rs.createRelation("relationId", "relationTypeC", roles);
1514: String[] roleNames = new String[] { "roleC1" };
1515:
1516: boolean caught = false;
1517: try {
1518: rs.getRelationTypeName(null);
1519: } catch (IllegalArgumentException e) {
1520: caught = true;
1521: }
1522: if (caught == false)
1523: fail("getRelationTypeName allows null relation id");
1524:
1525: caught = false;
1526: try {
1527: rs.getRelationTypeName("rubbish");
1528: } catch (RelationNotFoundException e) {
1529: caught = true;
1530: }
1531: if (caught == false)
1532: fail("getRelationTypeName allows invalid relation id");
1533: } finally {
1534: MBeanServerFactory.releaseMBeanServer(server);
1535: }
1536: }
1537:
1538: /**
1539: * Test get Role
1540: */
1541: public void testGetRoleExternal() throws Exception {
1542: MBeanServer server = MBeanServerFactory.createMBeanServer();
1543: try {
1544: ObjectName service = createRelationService(
1545: "test:type=service", server);
1546: createRelationTypeC(service);
1547: createRolesC(server);
1548: List result = null;
1549: RelationSupport support = new RelationSupport("id",
1550: service, server, "relationTypeC", rolesC);
1551: addRelation(server, service, support, "test:type=support");
1552: result = support.getRole("roleC1");
1553: compareListOfObjectNames(getRole(rolesC, "roleC1")
1554: .getRoleValue(), result);
1555: } finally {
1556: MBeanServerFactory.releaseMBeanServer(server);
1557: }
1558: }
1559:
1560: /**
1561: * Test get role errors
1562: */
1563: public void testGetRoleErrors() throws Exception {
1564: MBeanServer server = MBeanServerFactory.createMBeanServer();
1565: try {
1566: ObjectName service = createRelationService(
1567: "test:type=service", server);
1568: RelationService rs = (RelationService) services
1569: .get(service);
1570: createRelationTypeC(service);
1571: createRolesC(server);
1572: RoleList roles = new RoleList();
1573: roles.add(roleC1);
1574: roles.add(roleC2);
1575: rs.createRelation("relationId", "relationTypeC", roles);
1576:
1577: boolean caught = false;
1578: try {
1579: rs.getRole(null, "roleC1");
1580: } catch (IllegalArgumentException e) {
1581: caught = true;
1582: }
1583: if (caught == false)
1584: fail("getRole allows null relation id");
1585:
1586: caught = false;
1587: try {
1588: rs.getRole("relationId", null);
1589: } catch (IllegalArgumentException e) {
1590: caught = true;
1591: }
1592: if (caught == false)
1593: fail("getRole allows null role");
1594:
1595: caught = false;
1596: try {
1597: rs.getRole("rubbish", "roleC1");
1598: } catch (RelationNotFoundException e) {
1599: caught = true;
1600: }
1601: if (caught == false)
1602: fail("getRole allows invalid relation id");
1603:
1604: caught = false;
1605: try {
1606: rs.getRole("relationId", "rubbish");
1607: } catch (RoleNotFoundException e) {
1608: caught = true;
1609: }
1610: if (caught == false)
1611: fail("getRole allows invalid role name");
1612:
1613: caught = false;
1614: try {
1615: rs.getRole("relationId", "roleC2");
1616: } catch (RoleNotFoundException e) {
1617: caught = true;
1618: }
1619: if (caught == false)
1620: fail("getRole allows unreadable role");
1621:
1622: server.unregisterMBean(service);
1623: caught = false;
1624: try {
1625: rs.getRole("relationId", "roleC1");
1626: } catch (RelationServiceNotRegisteredException e) {
1627: caught = true;
1628: }
1629: if (caught == false)
1630: fail("FAILS IN RI: getRole allowed when not registered");
1631: } finally {
1632: MBeanServerFactory.releaseMBeanServer(server);
1633: }
1634: }
1635:
1636: /**
1637: * Test get Role Cardinality
1638: */
1639: public void testGetRoleCardinalityExternal() throws Exception {
1640: MBeanServer server = MBeanServerFactory.createMBeanServer();
1641: try {
1642: ObjectName service = createRelationService(
1643: "test:type=service", server);
1644: createRelationTypeC(service);
1645: createRolesC(server);
1646: Integer result = null;
1647: RelationSupport support = null;
1648: support = new RelationSupport("id", service, server,
1649: "relationTypeC", rolesC);
1650: addRelation(server, service, support, "test:type=support");
1651: result = support.getRoleCardinality("roleC1");
1652: assertEquals(2, result.intValue());
1653:
1654: result = support.getRoleCardinality("roleC2");
1655: assertEquals(3, result.intValue());
1656: } finally {
1657: MBeanServerFactory.releaseMBeanServer(server);
1658: }
1659: }
1660:
1661: /**
1662: * Test get role cardinality errors
1663: */
1664: public void testGetRoleCardinalityErrors() throws Exception {
1665: MBeanServer server = MBeanServerFactory.createMBeanServer();
1666: try {
1667: ObjectName service = createRelationService(
1668: "test:type=service", server);
1669: RelationService rs = (RelationService) services
1670: .get(service);
1671: createRelationTypeC(service);
1672: createRolesC(server);
1673: RoleList roles = new RoleList();
1674: roles.add(roleC1);
1675: roles.add(roleC2);
1676: rs.createRelation("relationId", "relationTypeC", roles);
1677:
1678: boolean caught = false;
1679: try {
1680: rs.getRoleCardinality(null, "roleC1");
1681: } catch (IllegalArgumentException e) {
1682: caught = true;
1683: }
1684: if (caught == false)
1685: fail("getRoleCardinality allows null relation id");
1686:
1687: caught = false;
1688: try {
1689: rs.getRoleCardinality("relationId", null);
1690: } catch (IllegalArgumentException e) {
1691: caught = true;
1692: }
1693: if (caught == false)
1694: fail("getRoleCardinality allows null role");
1695:
1696: caught = false;
1697: try {
1698: rs.getRoleCardinality("rubbish", "roleC1");
1699: } catch (RelationNotFoundException e) {
1700: caught = true;
1701: }
1702: if (caught == false)
1703: fail("getRoleCardinality allows invalid relation id");
1704:
1705: caught = false;
1706: try {
1707: rs.getRoleCardinality("relationId", "rubbish");
1708: } catch (RoleNotFoundException e) {
1709: caught = true;
1710: }
1711: if (caught == false)
1712: fail("getRoleCardinality allows invalid role name");
1713: } finally {
1714: MBeanServerFactory.releaseMBeanServer(server);
1715: }
1716: }
1717:
1718: /**
1719: * Test get Roles
1720: */
1721: public void testGetRolesExternal() throws Exception {
1722: MBeanServer server = MBeanServerFactory.createMBeanServer();
1723: try {
1724: ObjectName service = createRelationService(
1725: "test:type=service", server);
1726: RelationService rs = (RelationService) services
1727: .get(service);
1728: createRelationTypeC(service);
1729: createRolesC(server);
1730: RoleResult result = null;
1731: RelationSupport support = null;
1732: support = new RelationSupport("id", service, server,
1733: "relationTypeC", rolesC);
1734: addRelation(server, service, support, "test:type=support");
1735: result = rs.getRoles("id", new String[] { "roleC1",
1736: "roleC2" });
1737: checkResult(result, roleInfosC, rolesC);
1738:
1739: result = rs.getRoles("id", new String[] { "roleC1" });
1740: RoleList resolved = result.getRoles();
1741: assertEquals(1, resolved.size());
1742: assertEquals(0, result.getRolesUnresolved().size());
1743: compare(getRole(rolesC, "roleC1"), (Role) resolved.get(0));
1744:
1745: result = rs.getRoles("id", new String[] { "roleC2" });
1746: RoleUnresolvedList unresolved = result.getRolesUnresolved();
1747: assertEquals(0, result.getRoles().size());
1748: assertEquals(1, unresolved.size());
1749: assertEquals("roleC2", ((RoleUnresolved) unresolved.get(0))
1750: .getRoleName());
1751: } finally {
1752: MBeanServerFactory.releaseMBeanServer(server);
1753: }
1754: }
1755:
1756: /**
1757: * Test get roles errors
1758: */
1759: public void testGetRolesErrors() throws Exception {
1760: MBeanServer server = MBeanServerFactory.createMBeanServer();
1761: try {
1762: ObjectName service = createRelationService(
1763: "test:type=service", server);
1764: RelationService rs = (RelationService) services
1765: .get(service);
1766: createRelationTypeC(service);
1767: createRolesC(server);
1768: RoleList roles = new RoleList();
1769: roles.add(roleC1);
1770: roles.add(roleC2);
1771: rs.createRelation("relationId", "relationTypeC", roles);
1772: String[] roleNames = new String[] { "roleC1" };
1773:
1774: boolean caught = false;
1775: try {
1776: rs.getRoles(null, roleNames);
1777: } catch (IllegalArgumentException e) {
1778: caught = true;
1779: }
1780: if (caught == false)
1781: fail("getRoles allows null relation id");
1782:
1783: caught = false;
1784: try {
1785: rs.getRoles("relationId", null);
1786: } catch (IllegalArgumentException e) {
1787: caught = true;
1788: }
1789: if (caught == false)
1790: fail("getRoles allows null role name array");
1791:
1792: caught = false;
1793: try {
1794: rs.getRoles("rubbish", roleNames);
1795: } catch (RelationNotFoundException e) {
1796: caught = true;
1797: }
1798: if (caught == false)
1799: fail("getRoles allows invalid relation id");
1800:
1801: server.unregisterMBean(service);
1802: caught = false;
1803: try {
1804: rs.getRoles("relationId", roleNames);
1805: } catch (RelationServiceNotRegisteredException e) {
1806: caught = true;
1807: }
1808: if (caught == false)
1809: fail("FAILS IN RI: getRoles allowed when not registered");
1810: } finally {
1811: MBeanServerFactory.releaseMBeanServer(server);
1812: }
1813: }
1814:
1815: /**
1816: * Test Has Relation
1817: */
1818: public void testHasRelationExternal() throws Exception {
1819: MBeanServer server = MBeanServerFactory.createMBeanServer();
1820: try {
1821: ObjectName service = createRelationService(
1822: "test:type=service", server);
1823: createRelationTypeC(service);
1824: createRolesC(server);
1825: RelationSupport support = null;
1826: Boolean result1 = null;
1827: Boolean result2 = null;
1828: Boolean result3 = null;
1829: Boolean result4 = null;
1830: Boolean result5 = null;
1831: Boolean result6 = null;
1832: support = new RelationSupport("id1", service, server,
1833: "relationTypeC", rolesC);
1834: addRelation(server, service, support, "test:type=support1");
1835: support = new RelationSupport("id2", service, server,
1836: "relationTypeC", rolesC);
1837: addRelation(server, service, support, "test:type=support2");
1838: RelationService rs = (RelationService) services
1839: .get(service);
1840: result1 = rs.hasRelation("id1");
1841: result2 = rs.hasRelation("id2");
1842: result3 = rs.hasRelation("id3");
1843: rs.removeRelation("id2");
1844: result4 = rs.hasRelation("id1");
1845: result5 = rs.hasRelation("id2");
1846: result6 = rs.hasRelation("id3");
1847: assertEquals(true, result1.booleanValue());
1848: assertEquals(true, result2.booleanValue());
1849: assertEquals(false, result3.booleanValue());
1850: assertEquals(true, result4.booleanValue());
1851: assertEquals(false, result5.booleanValue());
1852: assertEquals(false, result6.booleanValue());
1853: } finally {
1854: MBeanServerFactory.releaseMBeanServer(server);
1855: }
1856: }
1857:
1858: /**
1859: * Test Has Relation Errors
1860: */
1861: public void testHasRelationErrors() throws Exception {
1862: RelationService rs = new RelationService(true);
1863:
1864: boolean caught = false;
1865: try {
1866: rs.hasRelation(null);
1867: } catch (IllegalArgumentException e) {
1868: caught = true;
1869: }
1870: if (caught == false)
1871: fail("hasRelation allows null relation id");
1872: }
1873:
1874: /**
1875: * Test Is Active
1876: */
1877: public void testIsActive() throws Exception {
1878: MBeanServer server = MBeanServerFactory.createMBeanServer();
1879: try {
1880: RelationService rs = null;
1881: boolean caught = false;
1882: try {
1883: rs = new RelationService(true);
1884: rs.isActive();
1885: } catch (RelationServiceNotRegisteredException e) {
1886: caught = true;
1887: } catch (Exception e) {
1888: fail(e.toString());
1889: }
1890: assertEquals(true, caught);
1891:
1892: ObjectName name = null;
1893: name = new ObjectName("test:type = rs");
1894: server.registerMBean(rs, name);
1895: rs.isActive();
1896:
1897: caught = false;
1898: try {
1899: server.unregisterMBean(name);
1900: rs.isActive();
1901: } catch (RelationServiceNotRegisteredException e) {
1902: caught = true;
1903: }
1904: if (caught == false)
1905: fail("FAILS IN RI: Relation Service still reports itself active.");
1906: } finally {
1907: MBeanServerFactory.releaseMBeanServer(server);
1908: }
1909: }
1910:
1911: /**
1912: * Test Is Relation
1913: */
1914: public void testIsRelationExternal() throws Exception {
1915: MBeanServer server = MBeanServerFactory.createMBeanServer();
1916: try {
1917: ObjectName service = createRelationService(
1918: "test:type=service", server);
1919: createRelationTypeC(service);
1920: createRolesC(server);
1921: RelationSupport support = null;
1922: String result1 = null;
1923: String result2 = null;
1924: String result3 = null;
1925: String result4 = null;
1926: String result5 = null;
1927: String result6 = null;
1928: support = new RelationSupport("id1", service, server,
1929: "relationTypeC", rolesC);
1930: addRelation(server, service, support, "test:type=support1");
1931: support = new RelationSupport("id2", service, server,
1932: "relationTypeC", rolesC);
1933: addRelation(server, service, support, "test:type=support2");
1934: RelationService rs = (RelationService) services
1935: .get(service);
1936: result1 = rs
1937: .isRelation(new ObjectName("test:type=support1"));
1938: result2 = rs
1939: .isRelation(new ObjectName("test:type=support2"));
1940: result3 = rs
1941: .isRelation(new ObjectName("test:type=support3"));
1942: rs.removeRelation("id2");
1943: result4 = rs
1944: .isRelation(new ObjectName("test:type=support1"));
1945: result5 = rs
1946: .isRelation(new ObjectName("test:type=support2"));
1947: result6 = rs
1948: .isRelation(new ObjectName("test:type=support3"));
1949: assertEquals("id1", result1);
1950: assertEquals("id2", result2);
1951: assertEquals(null, result3);
1952: assertEquals("id1", result4);
1953: assertEquals(null, result5);
1954: assertEquals(null, result6);
1955: } finally {
1956: MBeanServerFactory.releaseMBeanServer(server);
1957: }
1958: }
1959:
1960: /**
1961: * Test Is Relation Errors
1962: */
1963: public void testIsRelationErrors() throws Exception {
1964: RelationService rs = new RelationService(true);
1965:
1966: boolean caught = false;
1967: try {
1968: rs.isRelation(null);
1969: } catch (IllegalArgumentException e) {
1970: caught = true;
1971: }
1972: if (caught == false)
1973: fail("isRelation allows null relation id");
1974: }
1975:
1976: /**
1977: * Test Is Relation MBean
1978: */
1979: public void testIsRelationMBeanExternal() throws Exception {
1980: MBeanServer server = MBeanServerFactory.createMBeanServer();
1981: try {
1982: ObjectName service = createRelationService(
1983: "test:type=service", server);
1984: createRelationTypeC(service);
1985: createRolesC(server);
1986: RelationSupport support = null;
1987: ObjectName result1 = null;
1988: ObjectName result2 = null;
1989: ObjectName on1 = null;
1990: ObjectName on2 = null;
1991: support = new RelationSupport("id1", service, server,
1992: "relationTypeC", rolesC);
1993: addRelation(server, service, support, "test:type=support1");
1994: support = new RelationSupport("id2", service, server,
1995: "relationTypeC", rolesC);
1996: addRelation(server, service, support, "test:type=support2");
1997: RelationService rs = (RelationService) services
1998: .get(service);
1999: result1 = rs.isRelationMBean("id1");
2000: result2 = rs.isRelationMBean("id2");
2001: on1 = new ObjectName("test:type=support1");
2002: on2 = new ObjectName("test:type=support2");
2003: assertEquals(on1, result1);
2004: assertEquals(on2, result2);
2005: } finally {
2006: MBeanServerFactory.releaseMBeanServer(server);
2007: }
2008: }
2009:
2010: /**
2011: * Test Is Relation MBean Errors
2012: */
2013: public void testIsRelationMBeanErrors() throws Exception {
2014: RelationService rs = new RelationService(true);
2015:
2016: boolean caught = false;
2017: try {
2018: rs.isRelationMBean(null);
2019: } catch (IllegalArgumentException e) {
2020: caught = true;
2021: }
2022: if (caught == false)
2023: fail("isRelationMBean allows null relation id");
2024:
2025: caught = false;
2026: try {
2027: rs.isRelationMBean("rubbish");
2028: } catch (RelationNotFoundException e) {
2029: caught = true;
2030: }
2031: if (caught == false)
2032: fail("isRelationMBean allows non-existent relation");
2033: }
2034:
2035: /**
2036: * Test purge Relations Automatically
2037: */
2038: public void testPurgeRelationsAutomaticExternal() throws Exception {
2039: MBeanServer server = MBeanServerFactory.createMBeanServer();
2040: try {
2041: ObjectName service = createRelationService(
2042: "test:type=service", server);
2043: createRelationTypeC(service);
2044: createRolesC(server);
2045: RelationSupport support = null;
2046: ObjectName on = null;
2047: Listener listener = new Listener(
2048: RelationNotification.RELATION_MBEAN_REMOVAL);
2049: boolean result = false;
2050: support = new RelationSupport("id1", service, server,
2051: "relationTypeC", rolesC);
2052: addRelation(server, service, support, "test:type=support1");
2053: server.addNotificationListener(service, listener, null,
2054: null);
2055: RelationService rs = (RelationService) services
2056: .get(service);
2057: server.unregisterMBean(new ObjectName(
2058: "x:relation=c,role=2,bean=1"));
2059: on = new ObjectName("test:type=support1");
2060: result = rs.hasRelation("id1").booleanValue();
2061: assertEquals(false, result);
2062: RelationNotification rn = listener.check(1);
2063: assertEquals(new ArrayList(), rn.getMBeansToUnregister());
2064: assertEquals(on, rn.getObjectName());
2065: assertEquals("id1", rn.getRelationId());
2066: assertEquals("relationTypeC", rn.getRelationTypeName());
2067: } finally {
2068: MBeanServerFactory.releaseMBeanServer(server);
2069: }
2070: }
2071:
2072: /**
2073: * Test purge Relations Manually
2074: */
2075: public void testPurgeRelationsManuallyExternal() throws Exception {
2076: MBeanServer server = MBeanServerFactory.createMBeanServer();
2077: try {
2078: ObjectName service = createRelationService(
2079: "test:type=service", server);
2080: createRelationTypeC(service);
2081: createRolesC(server);
2082: RelationSupport support = null;
2083: ObjectName on = null;
2084: Listener listener = new Listener(
2085: RelationNotification.RELATION_MBEAN_REMOVAL);
2086: boolean result = false;
2087: RelationService rs = null;
2088: support = new RelationSupport("id1", service, server,
2089: "relationTypeC", rolesC);
2090: addRelation(server, service, support, "test:type=support1");
2091: server.addNotificationListener(service, listener, null,
2092: null);
2093: rs = (RelationService) services.get(service);
2094: rs.setPurgeFlag(false);
2095: server.unregisterMBean(new ObjectName(
2096: "x:relation=c,role=2,bean=1"));
2097: on = new ObjectName("test:type=support1");
2098: result = rs.hasRelation("id1").booleanValue();
2099:
2100: assertEquals(true, result);
2101: RelationNotification rn = listener.check(0);
2102:
2103: rs.purgeRelations();
2104: result = rs.hasRelation("id1").booleanValue();
2105: assertEquals(false, result);
2106: rn = listener.check(1);
2107: assertEquals(new ArrayList(), rn.getMBeansToUnregister());
2108: assertEquals(on, rn.getObjectName());
2109: assertEquals("id1", rn.getRelationId());
2110: assertEquals("relationTypeC", rn.getRelationTypeName());
2111: } finally {
2112: MBeanServerFactory.releaseMBeanServer(server);
2113: }
2114: }
2115:
2116: /**
2117: * Test purge relations errors
2118: */
2119: public void testPurgeRelationsErrors() throws Exception {
2120: RelationService rs = new RelationService(true);
2121:
2122: boolean caught = false;
2123: try {
2124: rs.purgeRelations();
2125: } catch (RelationServiceNotRegisteredException e) {
2126: caught = true;
2127: }
2128: if (caught == false)
2129: fail("purgeRelations allowed when not registered");
2130: }
2131:
2132: /**
2133: * Test remove relation
2134: */
2135: public void testRemoveRelationExternal() throws Exception {
2136: MBeanServer server = MBeanServerFactory.createMBeanServer();
2137: try {
2138: ObjectName service = createRelationService(
2139: "test:type=service", server);
2140: createRelationTypeA(service);
2141: createRolesA(server);
2142: createRelationTypeB(service);
2143: createRolesB(server);
2144: createRelationTypeC(service);
2145: createRolesC(server);
2146: createRelationTypeCX(service);
2147: createRolesCX(server);
2148: List result = null;
2149: RelationSupport supportA1 = new RelationSupport("ida1",
2150: service, server, "relationTypeA", rolesA);
2151: RelationSupport supportA2 = new RelationSupport("ida2",
2152: service, server, "relationTypeA", rolesA);
2153: RelationSupport supportCX = new RelationSupport("idcx",
2154: service, server, "relationTypeCX", rolesCX);
2155: RelationSupport supportC = new RelationSupport("idc",
2156: service, server, "relationTypeC", rolesC);
2157: addRelation(server, service, supportA1,
2158: "test:type=supportA1");
2159: addRelation(server, service, supportA2,
2160: "test:type=supportA2");
2161: addRelation(server, service, supportCX,
2162: "test:type=supportCX");
2163: addRelation(server, service, supportC, "test:type=supportC");
2164: RelationService rs = (RelationService) services
2165: .get(service);
2166: rs.removeRelation("idcx");
2167: result = rs.getAllRelationIds();
2168: assertEquals(3, result.size());
2169: assertEquals(true, result.contains("ida1"));
2170: assertEquals(true, result.contains("ida2"));
2171: assertEquals(true, result.contains("idc"));
2172: assertEquals(false, result.contains("idcx"));
2173: } finally {
2174: MBeanServerFactory.releaseMBeanServer(server);
2175: }
2176: }
2177:
2178: /**
2179: * Test remove relation errors
2180: */
2181: public void testRemoveRelationErrors() throws Exception {
2182: MBeanServer server = MBeanServerFactory.createMBeanServer();
2183: try {
2184: ObjectName service = createRelationService(
2185: "test:type=serviceA", null);
2186: RelationService rs = null;
2187: rs = (RelationService) services.get(service);
2188:
2189: boolean caught = false;
2190: try {
2191: rs.removeRelation("RelationId");
2192: } catch (RelationServiceNotRegisteredException e) {
2193: caught = true;
2194: }
2195: if (caught == false)
2196: fail("removeRelation allowed when not registered");
2197:
2198: service = createRelationService("test:type=service", server);
2199: createRelationTypeA(service);
2200: createRolesA(server);
2201: RelationSupport supportA1 = new RelationSupport("ida1",
2202: service, server, "relationTypeA", rolesA);
2203: addRelation(server, service, supportA1,
2204: "test:type=supportA1");
2205: rs = (RelationService) services.get(service);
2206:
2207: caught = false;
2208: try {
2209: rs.removeRelation(null);
2210: } catch (IllegalArgumentException e) {
2211: caught = true;
2212: }
2213: if (caught == false)
2214: fail("removeRelation accepts a null relation");
2215:
2216: caught = false;
2217: try {
2218: rs.removeRelation("rubbish");
2219: } catch (RelationNotFoundException e) {
2220: caught = true;
2221: }
2222: if (caught == false)
2223: fail("removeRelation accepts a non existent relation");
2224: } finally {
2225: MBeanServerFactory.releaseMBeanServer(server);
2226: }
2227: }
2228:
2229: /**
2230: * Test remove relation type
2231: */
2232: public void testRemoveRelationType() throws Exception {
2233: MBeanServer server = MBeanServerFactory.createMBeanServer();
2234: try {
2235: RelationService rs = new RelationService(true);
2236: assertEquals(0, rs.getAllRelationTypeNames().size());
2237:
2238: RoleInfo roleInfo1 = null;
2239: RoleInfo roleInfo2 = null;
2240: RoleInfo[] roleInfos = null;
2241: ObjectName name = new ObjectName("test:type = rs");
2242: server.registerMBean(rs, name);
2243: roleInfo1 = new RoleInfo("roleInfo1", Trivial.class
2244: .getName());
2245: roleInfo2 = new RoleInfo("roleInfo2", Trivial.class
2246: .getName());
2247: roleInfos = new RoleInfo[] { roleInfo1, roleInfo2 };
2248: rs.createRelationType("name1", roleInfos);
2249: rs.createRelationType("name2", roleInfos);
2250: rs.createRelationType("name3", roleInfos);
2251: rs.removeRelationType("name3");
2252:
2253: ArrayList result = (ArrayList) rs.getAllRelationTypeNames();
2254: assertEquals(2, result.size());
2255: assertEquals(true, result.contains("name1"));
2256: assertEquals(true, result.contains("name2"));
2257: assertEquals(false, result.contains("name3"));
2258: } finally {
2259: MBeanServerFactory.releaseMBeanServer(server);
2260: }
2261: }
2262:
2263: /**
2264: * Test remove relation type errors
2265: */
2266: public void testRemoveRelationTypeErrors() throws Exception {
2267: MBeanServer server = MBeanServerFactory.createMBeanServer();
2268: try {
2269: RelationService rs = null;
2270: RoleInfo roleInfo1 = null;
2271: RoleInfo roleInfo2 = null;
2272: RoleInfo[] roleInfos = null;
2273: rs = new RelationService(true);
2274: roleInfo1 = new RoleInfo("roleInfo1", Trivial.class
2275: .getName());
2276: roleInfo2 = new RoleInfo("roleInfo2", Trivial.class
2277: .getName());
2278: roleInfos = new RoleInfo[] { roleInfo1, roleInfo2 };
2279: rs.createRelationType("name1", roleInfos);
2280:
2281: boolean caught = false;
2282: try {
2283: rs.removeRelationType("name1");
2284: } catch (RelationServiceNotRegisteredException e) {
2285: caught = true;
2286: }
2287: if (caught == false)
2288: fail("Remove relation type allowed when not registered, why not?");
2289:
2290: server.registerMBean(rs, new ObjectName(
2291: "test:type=RelationService"));
2292:
2293: caught = false;
2294: try {
2295: rs.removeRelationType(null);
2296: } catch (IllegalArgumentException e) {
2297: caught = true;
2298: }
2299: if (caught == false)
2300: fail("Remove relation type allows null relation type name");
2301:
2302: caught = false;
2303: try {
2304: rs.removeRelationType("rubbish");
2305: } catch (RelationTypeNotFoundException e) {
2306: caught = true;
2307: }
2308: if (caught == false)
2309: fail("Remove relation type allows non-existent relation type name");
2310: } finally {
2311: MBeanServerFactory.releaseMBeanServer(server);
2312: }
2313: }
2314:
2315: /**
2316: * Test send relation creation notification
2317: */
2318: public void testSendRelationCreationNotificationExternal()
2319: throws Exception {
2320: MBeanServer server = MBeanServerFactory.createMBeanServer();
2321: try {
2322: ObjectName service = createRelationService(
2323: "test:type=service", server);
2324: createRelationTypeC(service);
2325: createRolesC(server);
2326: RelationSupport support = null;
2327: ObjectName on = null;
2328: Listener listener = new Listener(
2329: RelationNotification.RELATION_MBEAN_CREATION);
2330: support = new RelationSupport("id1", service, server,
2331: "relationTypeC", rolesC);
2332: addRelation(server, service, support, "test:type=support1");
2333: server.addNotificationListener(service, listener, null,
2334: null);
2335: RelationService rs = (RelationService) services
2336: .get(service);
2337: rs.sendRelationCreationNotification("id1");
2338: on = new ObjectName("test:type=support1");
2339: RelationNotification rn = listener.check(1);
2340: assertEquals(new ArrayList(), rn.getMBeansToUnregister());
2341: assertEquals(new ArrayList(), rn.getNewRoleValue());
2342: assertEquals(on, rn.getObjectName());
2343: assertEquals(new ArrayList(), rn.getOldRoleValue());
2344: assertEquals("id1", rn.getRelationId());
2345: assertEquals("relationTypeC", rn.getRelationTypeName());
2346: assertEquals(null, rn.getRoleName());
2347: } finally {
2348: MBeanServerFactory.releaseMBeanServer(server);
2349: }
2350: }
2351:
2352: /**
2353: * Test send relation removal notification errors
2354: */
2355: public void testSendRelationCreationNotificationErrors()
2356: throws Exception {
2357: RelationService rs = new RelationService(true);
2358:
2359: boolean caught = false;
2360: try {
2361: rs.sendRelationCreationNotification(null);
2362: } catch (IllegalArgumentException e) {
2363: caught = true;
2364: }
2365: if (caught == false)
2366: fail("sendRelationCreationNotification allows null relation id");
2367:
2368: caught = false;
2369: try {
2370: rs.sendRelationCreationNotification("rubbish");
2371: } catch (RelationNotFoundException e) {
2372: caught = true;
2373: }
2374: if (caught == false)
2375: fail("sendRelationCreationNotification allows invalid relation id");
2376: }
2377:
2378: /**
2379: * Test send relation removal notification
2380: */
2381: public void testSendRelationRemovalNotificationExternal()
2382: throws Exception {
2383: MBeanServer server = MBeanServerFactory.createMBeanServer();
2384: try {
2385: ObjectName service = createRelationService(
2386: "test:type=service", server);
2387: createRelationTypeC(service);
2388: createRolesC(server);
2389: RelationSupport support = null;
2390: ObjectName on = null;
2391: ArrayList test = new ArrayList();
2392: Listener listener = new Listener(
2393: RelationNotification.RELATION_MBEAN_REMOVAL);
2394: support = new RelationSupport("id1", service, server,
2395: "relationTypeC", rolesC);
2396: addRelation(server, service, support, "test:type=support1");
2397: server.addNotificationListener(service, listener, null,
2398: null);
2399: RelationService rs = (RelationService) services
2400: .get(service);
2401: test.add(new ObjectName("test:type=test"));
2402: rs.sendRelationRemovalNotification("id1", test);
2403: on = new ObjectName("test:type=support1");
2404: RelationNotification rn = listener.check(1);
2405: assertEquals(test, rn.getMBeansToUnregister());
2406: assertEquals(new ArrayList(), rn.getNewRoleValue());
2407: assertEquals(on, rn.getObjectName());
2408: assertEquals(new ArrayList(), rn.getOldRoleValue());
2409: assertEquals("id1", rn.getRelationId());
2410: assertEquals("relationTypeC", rn.getRelationTypeName());
2411: assertEquals(null, rn.getRoleName());
2412: } finally {
2413: MBeanServerFactory.releaseMBeanServer(server);
2414: }
2415: }
2416:
2417: /**
2418: * Test send relation removal notification errors
2419: */
2420: public void testSendRelationRemovalNotificationErrors()
2421: throws Exception {
2422: RelationService rs = new RelationService(true);
2423:
2424: boolean caught = false;
2425: try {
2426: rs.sendRelationRemovalNotification(null, new ArrayList());
2427: } catch (IllegalArgumentException e) {
2428: caught = true;
2429: }
2430: if (caught == false)
2431: fail("sendRelationRemovalNotification allows null relation id");
2432:
2433: caught = false;
2434: try {
2435: rs.sendRelationRemovalNotification("rubbish",
2436: new ArrayList());
2437: } catch (RelationNotFoundException e) {
2438: caught = true;
2439: }
2440: if (caught == false)
2441: fail("sendRelationRemovalNotification allows invalid relation id");
2442: }
2443:
2444: /**
2445: * Test send role update notification
2446: */
2447: public void testSendRoleUpdateNotificationExternal()
2448: throws Exception {
2449: MBeanServer server = MBeanServerFactory.createMBeanServer();
2450: try {
2451: ObjectName service = createRelationService(
2452: "test:type=service", server);
2453: createRelationTypeC(service);
2454: createRolesB(server);
2455: createRolesC(server);
2456: RelationSupport support = null;
2457: ObjectName on = null;
2458: ArrayList test = new ArrayList();
2459: Listener listener = new Listener(
2460: RelationNotification.RELATION_MBEAN_UPDATE);
2461: support = new RelationSupport("id1", service, server,
2462: "relationTypeC", rolesC);
2463: addRelation(server, service, support, "test:type=support1");
2464: server.addNotificationListener(service, listener, null,
2465: null);
2466: RelationService rs = (RelationService) services
2467: .get(service);
2468: test.add(new ObjectName("test:type=test"));
2469: rs.sendRoleUpdateNotification("id1", roleB1, test);
2470: on = new ObjectName("test:type=support1");
2471: RelationNotification rn = listener.check(1);
2472: assertEquals(new ArrayList(), rn.getMBeansToUnregister());
2473: assertEquals(roleB1.getRoleValue(), rn.getNewRoleValue());
2474: assertEquals(on, rn.getObjectName());
2475: assertEquals(test, rn.getOldRoleValue());
2476: assertEquals("id1", rn.getRelationId());
2477: assertEquals("relationTypeC", rn.getRelationTypeName());
2478: assertEquals("roleB1", rn.getRoleName());
2479: } finally {
2480: MBeanServerFactory.releaseMBeanServer(server);
2481: }
2482: }
2483:
2484: /**
2485: * Test send role update notification errors
2486: */
2487: public void testSendRoleUpdateNotificationErrors() throws Exception {
2488: MBeanServer server = MBeanServerFactory.createMBeanServer();
2489: try {
2490: ObjectName service = createRelationService(
2491: "test:type=service", server);
2492: RelationService rs = (RelationService) services
2493: .get(service);
2494: createRelationTypeB(service);
2495: createRolesB(server);
2496:
2497: boolean caught = false;
2498: try {
2499: rs.sendRoleUpdateNotification(null, roleB1,
2500: new ArrayList());
2501: } catch (IllegalArgumentException e) {
2502: caught = true;
2503: }
2504: if (caught == false)
2505: fail("sendRoleUpdateNotification allows null relation id");
2506:
2507: RoleList roleList = new RoleList();
2508: roleList.add(roleB1);
2509: roleList.add(roleB2);
2510: rs.createRelation("relationId", "relationTypeB", roleList);
2511:
2512: caught = false;
2513: try {
2514: rs.sendRoleUpdateNotification("relationId", null,
2515: new ArrayList());
2516: } catch (IllegalArgumentException e) {
2517: caught = true;
2518: }
2519: if (caught == false)
2520: fail("sendRoleUpdateNotification allows null role");
2521:
2522: caught = false;
2523: try {
2524: rs.sendRoleUpdateNotification("rubbish", roleB1,
2525: new ArrayList());
2526: } catch (RelationNotFoundException e) {
2527: caught = true;
2528: }
2529: if (caught == false)
2530: fail("sendRoleUpdateNotification allows invalid relation id");
2531: } finally {
2532: MBeanServerFactory.releaseMBeanServer(server);
2533: }
2534: }
2535:
2536: /**
2537: * Test set a role
2538: */
2539: public void testSetRoleExternal() throws Exception {
2540: MBeanServer server = MBeanServerFactory.createMBeanServer();
2541: try {
2542: ObjectName service = createRelationService(
2543: "test:type=service", server);
2544: createRelationTypeC(service);
2545: createRolesC(server);
2546: createRolesCX(server);
2547: RelationSupport support = null;
2548: ObjectName on = null;
2549: Listener listener = new Listener(
2550: RelationNotification.RELATION_MBEAN_UPDATE);
2551: support = new RelationSupport("id1", service, server,
2552: "relationTypeC", rolesC);
2553: addRelation(server, service, support, "test:type=support1");
2554: server.addNotificationListener(service, listener, null,
2555: null);
2556: RelationService rs = (RelationService) services
2557: .get(service);
2558: rs.setRole("id1", roleCX2);
2559: on = new ObjectName("test:type=support1");
2560: RoleList shouldBe = new RoleList();
2561: shouldBe.add(roleC1);
2562: shouldBe.add(roleCX2);
2563: compare(shouldBe, support.retrieveAllRoles());
2564: RelationNotification rn = listener.check(1);
2565: assertEquals(new ArrayList(), rn.getMBeansToUnregister());
2566: assertEquals(roleCX2.getRoleValue(), rn.getNewRoleValue());
2567: assertEquals(on, rn.getObjectName());
2568: assertEquals(roleC2.getRoleValue(), rn.getOldRoleValue());
2569: assertEquals("id1", rn.getRelationId());
2570: assertEquals("relationTypeC", rn.getRelationTypeName());
2571: assertEquals("roleC2", rn.getRoleName());
2572: } finally {
2573: MBeanServerFactory.releaseMBeanServer(server);
2574: }
2575: }
2576:
2577: /**
2578: * Test set role errors
2579: */
2580: public void testSetRoleErrors() throws Exception {
2581: MBeanServer server = MBeanServerFactory.createMBeanServer();
2582: try {
2583: ObjectName service = createRelationService(
2584: "test:type=service", server);
2585: RelationService rs = (RelationService) services
2586: .get(service);
2587: createRelationTypeB(service);
2588: createRolesB(server);
2589: createRelationTypeC(service);
2590: createRolesC(server);
2591: createRolesCZ(server);
2592: createRolesCZZ(server);
2593: RoleList roles = new RoleList();
2594: roles.add(roleC1);
2595: roles.add(roleC2);
2596: rs.createRelation("relationId", "relationTypeC", roles);
2597:
2598: boolean caught = false;
2599: try {
2600: rs.setRole(null, roleC2);
2601: } catch (IllegalArgumentException e) {
2602: caught = true;
2603: }
2604: if (caught == false)
2605: fail("setRole allows null relation id");
2606:
2607: caught = false;
2608: try {
2609: rs.setRole("relationId", null);
2610: } catch (IllegalArgumentException e) {
2611: caught = true;
2612: }
2613: if (caught == false)
2614: fail("setRole allows null role");
2615:
2616: caught = false;
2617: try {
2618: rs.setRole("rubbish", roleC2);
2619: } catch (RelationNotFoundException e) {
2620: caught = true;
2621: }
2622: if (caught == false)
2623: fail("setRole allows invalid relation id");
2624:
2625: caught = false;
2626: try {
2627: rs.setRole("relationId", roleB1);
2628: } catch (RoleNotFoundException e) {
2629: caught = true;
2630: }
2631: if (caught == false)
2632: fail("setRole allows invalid role name");
2633:
2634: caught = false;
2635: try {
2636: rs.setRole("relationId", roleC1);
2637: } catch (RoleNotFoundException e) {
2638: caught = true;
2639: }
2640: if (caught == false)
2641: fail("setRole allows non-writable role");
2642:
2643: caught = false;
2644: try {
2645: rs.setRole("relationId", roleCZ2);
2646: } catch (InvalidRoleValueException e) {
2647: caught = true;
2648: }
2649: if (caught == false)
2650: fail("setRole allows a role below the minimum");
2651:
2652: caught = false;
2653: try {
2654: rs.setRole("relationId", roleCZZ);
2655: } catch (InvalidRoleValueException e) {
2656: caught = true;
2657: }
2658: if (caught == false)
2659: fail("setRole allows a role above the maximum");
2660:
2661: caught = false;
2662: try {
2663: rs.setRole("relationId", roleCZZZ);
2664: } catch (InvalidRoleValueException e) {
2665: caught = true;
2666: }
2667: if (caught == false)
2668: fail("setRole allows a role with unregistered beans");
2669:
2670: server.unregisterMBean(service);
2671: caught = false;
2672: try {
2673: rs.setRole("relationId", roleC2);
2674: } catch (RelationServiceNotRegisteredException e) {
2675: caught = true;
2676: }
2677: if (caught == false)
2678: fail("FAILS IN RI: setRole allowed when not registered");
2679: } finally {
2680: MBeanServerFactory.releaseMBeanServer(server);
2681: }
2682: }
2683:
2684: /**
2685: * Test set roles
2686: * @info.todo different permutations
2687: */
2688: public void testSetRolesExternal() throws Exception {
2689: MBeanServer server = MBeanServerFactory.createMBeanServer();
2690: try {
2691: ObjectName service = createRelationService(
2692: "test:type=service", server);
2693: createRelationTypeC(service);
2694: createRolesC(server);
2695: createRolesCX(server);
2696: RelationSupport support = null;
2697: ObjectName on = null;
2698: Listener listener = new Listener(
2699: RelationNotification.RELATION_MBEAN_UPDATE);
2700: RoleList shouldBe = new RoleList();
2701: shouldBe.add(roleC1);
2702: shouldBe.add(roleCX2);
2703: support = new RelationSupport("id1", service, server,
2704: "relationTypeC", rolesC);
2705: addRelation(server, service, support, "test:type=support1");
2706: server.addNotificationListener(service, listener, null,
2707: null);
2708: RelationService rs = (RelationService) services
2709: .get(service);
2710:
2711: rs.setRoles("id1", shouldBe);
2712: on = new ObjectName("test:type=support1");
2713: compare(shouldBe, support.retrieveAllRoles());
2714: RelationNotification rn = listener.check(1);
2715: } finally {
2716: MBeanServerFactory.releaseMBeanServer(server);
2717: }
2718: }
2719:
2720: /**
2721: * Test set roles errors
2722: */
2723: public void testSetRolesErrors() throws Exception {
2724: MBeanServer server = MBeanServerFactory.createMBeanServer();
2725: try {
2726: ObjectName service = createRelationService(
2727: "test:type=service", server);
2728: RelationService rs = (RelationService) services
2729: .get(service);
2730: createRelationTypeC(service);
2731: createRolesC(server);
2732: RoleList roles = new RoleList();
2733: roles.add(roleC1);
2734: roles.add(roleC2);
2735: rs.createRelation("relationId", "relationTypeC", roles);
2736: RoleList newRoles = new RoleList();
2737: newRoles.add(roleC2);
2738:
2739: boolean caught = false;
2740: try {
2741: rs.setRoles(null, newRoles);
2742: } catch (IllegalArgumentException e) {
2743: caught = true;
2744: }
2745: if (caught == false)
2746: fail("setRoles allows null relation id");
2747:
2748: caught = false;
2749: try {
2750: rs.setRole("relationId", null);
2751: } catch (IllegalArgumentException e) {
2752: caught = true;
2753: }
2754: if (caught == false)
2755: fail("setRoles allows null role list");
2756:
2757: caught = false;
2758: try {
2759: rs.setRoles("rubbish", newRoles);
2760: } catch (RelationNotFoundException e) {
2761: caught = true;
2762: }
2763: if (caught == false)
2764: fail("setRoles allows invalid relation id");
2765:
2766: server.unregisterMBean(service);
2767: caught = false;
2768: try {
2769: rs.setRoles("relationId", newRoles);
2770: } catch (RelationServiceNotRegisteredException e) {
2771: caught = true;
2772: }
2773: if (caught == false)
2774: fail("FAILS IN RI: setRoles allowed when not registered");
2775: } finally {
2776: MBeanServerFactory.releaseMBeanServer(server);
2777: }
2778: }
2779:
2780: /**
2781: * Test update role map errors
2782: */
2783: public void testUpdateRoleMapErrors() throws Exception {
2784: MBeanServer server = MBeanServerFactory.createMBeanServer();
2785: try {
2786: ObjectName service = createRelationService(
2787: "test:type=service", server);
2788: RelationService rs = (RelationService) services
2789: .get(service);
2790: createRelationTypeB(service);
2791: createRolesB(server);
2792: RoleList roleList = new RoleList();
2793: roleList.add(roleB1);
2794: roleList.add(roleB2);
2795: rs.createRelation("relationId", "relationTypeB", roleList);
2796:
2797: boolean caught = false;
2798: try {
2799: rs.updateRoleMap(null, roleB1, new ArrayList());
2800: } catch (IllegalArgumentException e) {
2801: caught = true;
2802: }
2803: if (caught == false)
2804: fail("updateRoleMap allows null relation id");
2805:
2806: caught = false;
2807: try {
2808: rs.updateRoleMap("relationId", null, new ArrayList());
2809: } catch (IllegalArgumentException e) {
2810: caught = true;
2811: }
2812: if (caught == false)
2813: fail("updateRoleMap allows null role");
2814:
2815: caught = false;
2816: try {
2817: rs.updateRoleMap("rubbish", roleB1, new ArrayList());
2818: } catch (RelationNotFoundException e) {
2819: caught = true;
2820: }
2821: if (caught == false)
2822: fail("updateRoleMap allows invalid relation id");
2823:
2824: server.unregisterMBean(service);
2825:
2826: caught = false;
2827: try {
2828: rs.updateRoleMap("relationId", roleB1, new ArrayList());
2829: } catch (RelationServiceNotRegisteredException e) {
2830: caught = true;
2831: }
2832: if (caught == false)
2833: fail("FAILS IN RI: updateRoleMap allowed when not registered");
2834: } finally {
2835: MBeanServerFactory.releaseMBeanServer(server);
2836: }
2837: }
2838:
2839: // Support -----------------------------------------------------------------
2840:
2841: private ObjectName createRelationService(String name,
2842: MBeanServer server) {
2843: ObjectName result = null;
2844: RelationService relationService = new RelationService(true);
2845: try {
2846: result = new ObjectName(name);
2847: services.put(result, relationService);
2848: if (server != null)
2849: server.registerMBean(relationService, result);
2850: } catch (Exception e) {
2851: fail(e.toString());
2852: }
2853: return result;
2854: }
2855:
2856: private ObjectName addRelation(MBeanServer server,
2857: ObjectName service, RelationSupport support, String name) {
2858: ObjectName result = null;
2859: try {
2860: result = new ObjectName(name);
2861: server.registerMBean(support, result);
2862: if (service != null) {
2863: RelationService relationService = (RelationService) services
2864: .get(service);
2865: relationService.addRelation(result);
2866: }
2867: } catch (Exception e) {
2868: fail(e.toString());
2869: }
2870: return result;
2871: }
2872:
2873: private RoleInfo createRoleInfo(String name, Class mbean,
2874: boolean read, boolean write, int min, int max) {
2875: RoleInfo result = null;
2876: try {
2877: result = new RoleInfo(name, mbean.getName(), read, write,
2878: min, max, "");
2879: } catch (Exception e) {
2880: fail(e.toString());
2881: }
2882: return result;
2883: }
2884:
2885: private void createRelationType(ObjectName relationService,
2886: String name, RoleInfo[] roleInfos) {
2887: try {
2888: RelationService service = (RelationService) services
2889: .get(relationService);
2890: service.createRelationType(name, roleInfos);
2891: } catch (Exception e) {
2892: fail(e.toString());
2893: }
2894: }
2895:
2896: private void compare(RoleList original, RoleList result) {
2897: assertEquals(original.size(), result.size());
2898: Iterator iterator = original.iterator();
2899: while (iterator.hasNext()) {
2900: Role originalRole = (Role) iterator.next();
2901: Iterator iterator2 = result.iterator();
2902: while (iterator2.hasNext()) {
2903: Role resultRole = (Role) iterator2.next();
2904: if (originalRole.getRoleName().equals(
2905: resultRole.getRoleName())) {
2906: compare(originalRole, resultRole);
2907: iterator2.remove();
2908: }
2909: }
2910: }
2911: assertEquals(0, result.size());
2912: }
2913:
2914: private void compare(Role original, Role result) {
2915: assertEquals(original.getRoleName(), result.getRoleName());
2916: compareListOfObjectNames(original.getRoleValue(), result
2917: .getRoleValue());
2918: }
2919:
2920: private void compareListOfObjectNames(List original, List result) {
2921: assertEquals(original.size(), result.size());
2922: Iterator iterator = original.iterator();
2923: while (iterator.hasNext()) {
2924: ObjectName originalBean = (ObjectName) iterator.next();
2925: Iterator iterator2 = result.iterator();
2926: while (iterator2.hasNext()) {
2927: ObjectName resultBean = (ObjectName) iterator2.next();
2928: if (originalBean.equals(resultBean)) {
2929: iterator2.remove();
2930: }
2931: }
2932: }
2933: assertEquals(0, result.size());
2934: }
2935:
2936: private void compareListOfStrings(List original, List result) {
2937: assertEquals(original.size(), result.size());
2938: Iterator iterator = original.iterator();
2939: while (iterator.hasNext()) {
2940: String originalString = (String) iterator.next();
2941: Iterator iterator2 = result.iterator();
2942: while (iterator2.hasNext()) {
2943: String resultString = (String) iterator2.next();
2944: if (originalString.equals(resultString)) {
2945: iterator2.remove();
2946: }
2947: }
2948: }
2949: assertEquals(0, result.size());
2950: }
2951:
2952: private ObjectName createRoleValueBean(String name, Class mbean,
2953: MBeanServer server) {
2954: ObjectName result = null;
2955: try {
2956: result = new ObjectName(name);
2957: if (server != null) {
2958: server.registerMBean(mbean.newInstance(), result);
2959: }
2960: } catch (Exception e) {
2961: fail(e.toString());
2962: }
2963: return result;
2964: }
2965:
2966: private void checkResult(RoleResult result, HashMap infos,
2967: RoleList roles) {
2968: checkResolved(result.getRoles(), infos, roles);
2969: checkUnresolved(result.getRolesUnresolved(), infos, roles);
2970: }
2971:
2972: private void checkResolved(RoleList resolved, HashMap infos,
2973: RoleList roles) {
2974: RoleList copy = (RoleList) roles.clone();
2975: Iterator iterator = resolved.iterator();
2976: while (iterator.hasNext()) {
2977: Role role = (Role) iterator.next();
2978: String roleName = role.getRoleName();
2979: RoleInfo info = (RoleInfo) infos.get(roleName);
2980: if (info == null)
2981: fail("unknown role " + roleName);
2982: if (info.isReadable() == false)
2983: fail("role should not be readable " + roleName);
2984: Role original = removeRole(copy, roleName);
2985: compareListOfObjectNames(original.getRoleValue(), role
2986: .getRoleValue());
2987: }
2988:
2989: iterator = copy.iterator();
2990: while (iterator.hasNext()) {
2991: Role role = (Role) iterator.next();
2992: String roleName = role.getRoleName();
2993: RoleInfo info = (RoleInfo) infos.get(roleName);
2994: if (info.isReadable() == true)
2995: fail("missing role " + roleName);
2996: }
2997: }
2998:
2999: private void checkUnresolved(RoleUnresolvedList unresolved,
3000: HashMap infos, RoleList roles) {
3001: RoleList copy = (RoleList) roles.clone();
3002: Iterator iterator = unresolved.iterator();
3003: while (iterator.hasNext()) {
3004: RoleUnresolved roleUnresolved = (RoleUnresolved) iterator
3005: .next();
3006: String roleName = roleUnresolved.getRoleName();
3007: RoleInfo info = (RoleInfo) infos.get(roleName);
3008: if (info == null)
3009: fail("unknown role " + roleName);
3010: if (info.isReadable() == true)
3011: fail("role should be readable " + roleName);
3012: removeRole(copy, roleName);
3013: }
3014:
3015: iterator = copy.iterator();
3016: while (iterator.hasNext()) {
3017: Role role = (Role) iterator.next();
3018: String roleName = role.getRoleName();
3019: RoleInfo info = (RoleInfo) infos.get(roleName);
3020: if (info.isReadable() == false)
3021: fail("missing unresolved role " + roleName);
3022: }
3023: }
3024:
3025: private Role removeRole(RoleList roles, String roleName) {
3026: Iterator iterator = roles.iterator();
3027: while (iterator.hasNext()) {
3028: Role role = (Role) iterator.next();
3029: if (role.getRoleName().equals(roleName)) {
3030: iterator.remove();
3031: return role;
3032: }
3033: }
3034: fail("role was not in the original " + roleName);
3035: return null;
3036: }
3037:
3038: private Role getRole(RoleList roles, String roleName) {
3039: Iterator iterator = roles.iterator();
3040: while (iterator.hasNext()) {
3041: Role role = (Role) iterator.next();
3042: if (role.getRoleName().equals(roleName)) {
3043: return role;
3044: }
3045: }
3046: fail("role was not in the original " + roleName);
3047: return null;
3048: }
3049:
3050: private void checkMBeans(Map result, RoleList roles) {
3051: // Construct what we think the value should be
3052: Map expected = calcMBeanRoleMap(roles);
3053:
3054: // Check the actual result
3055: Iterator iterator = result.entrySet().iterator();
3056: while (iterator.hasNext()) {
3057: Map.Entry entry = (Map.Entry) iterator.next();
3058: ObjectName key = (ObjectName) entry.getKey();
3059: ArrayList roleNames = (ArrayList) entry.getValue();
3060: ArrayList expectedNames = (ArrayList) expected.get(key);
3061: if (expectedNames == null)
3062: fail("Unexpected object name " + key);
3063: compareListOfStrings(expectedNames, roleNames);
3064: expected.remove(key);
3065: }
3066: assertEquals(0, expected.size());
3067: }
3068:
3069: private Map calcMBeanRoleMap(RoleList roles) {
3070: HashMap result = new HashMap();
3071: Iterator iterator = roles.iterator();
3072: while (iterator.hasNext()) {
3073: Role role = (Role) iterator.next();
3074: String roleName = role.getRoleName();
3075: ArrayList mbeans = (ArrayList) role.getRoleValue();
3076: Iterator iterator2 = mbeans.iterator();
3077: while (iterator2.hasNext()) {
3078: ObjectName objectName = (ObjectName) iterator2.next();
3079: ArrayList names = (ArrayList) result.get(objectName);
3080: if (names == null) {
3081: names = new ArrayList();
3082: result.put(objectName, names);
3083: }
3084: // It seems the role name should be duplicated?
3085: // Include the following test if this is a bug in RI.
3086: // if (names.contains(roleName) == false)
3087:
3088: names.add(roleName);
3089: }
3090: }
3091: return result;
3092: }
3093:
3094: private void createRolesA(MBeanServer server) {
3095: try {
3096: ArrayList roleA1Values = new ArrayList();
3097: roleA1Values
3098: .add(createRoleValueBean(
3099: "x:relation=a,role=1,bean=1",
3100: Trivial.class, server));
3101: Role roleA1 = new Role("roleA1", roleA1Values);
3102: rolesA = new RoleList();
3103: rolesA.add(roleA1);
3104: } catch (Exception e) {
3105: fail(e.toString());
3106: }
3107: }
3108:
3109: private void createRelationTypeA(ObjectName relationService) {
3110: try {
3111: RoleInfo roleInfoA1 = createRoleInfo("roleA1",
3112: Trivial.class, true, true, 1, 1);
3113: RoleInfo[] roleInfos = new RoleInfo[] { roleInfoA1 };
3114: createRelationType(relationService, "relationTypeA",
3115: roleInfos);
3116: for (int i = 0; i < roleInfos.length; i++)
3117: roleInfosA.put(roleInfos[i].getName(), roleInfos[i]);
3118: } catch (Exception e) {
3119: fail(e.toString());
3120: }
3121: }
3122:
3123: private void createRolesB(MBeanServer server) {
3124: try {
3125: ArrayList roleB1Values = new ArrayList();
3126: roleB1Values
3127: .add(createRoleValueBean(
3128: "x:relation=b,role=1,bean=1",
3129: Trivial.class, server));
3130: roleB1Values
3131: .add(createRoleValueBean(
3132: "x:relation=b,role=1,bean=2",
3133: Trivial.class, server));
3134: roleB1 = new Role("roleB1", roleB1Values);
3135:
3136: ArrayList roleB2Values = new ArrayList();
3137: roleB2Values
3138: .add(createRoleValueBean(
3139: "x:relation=b,role=2,bean=1",
3140: Trivial.class, server));
3141: roleB2Values
3142: .add(createRoleValueBean(
3143: "x:relation=b,role=2,bean=2",
3144: Trivial.class, server));
3145: roleB2Values
3146: .add(createRoleValueBean(
3147: "x:relation=b,role=2,bean=3",
3148: Trivial.class, server));
3149: roleB2 = new Role("roleB2", roleB2Values);
3150:
3151: rolesB = new RoleList();
3152: rolesB.add(roleB1);
3153: rolesB.add(roleB2);
3154: } catch (Exception e) {
3155: fail(e.toString());
3156: }
3157: }
3158:
3159: private void createRelationTypeB(ObjectName relationService) {
3160: try {
3161: RoleInfo roleInfoB1 = createRoleInfo("roleB1",
3162: Trivial.class, true, false, 1, 2);
3163: RoleInfo roleInfoB2 = createRoleInfo("roleB2",
3164: Trivial.class, false, true, 3, 4);
3165: RoleInfo[] roleInfos = new RoleInfo[] { roleInfoB1,
3166: roleInfoB2 };
3167: createRelationType(relationService, "relationTypeB",
3168: roleInfos);
3169: for (int i = 0; i < roleInfos.length; i++)
3170: roleInfosB.put(roleInfos[i].getName(), roleInfos[i]);
3171: } catch (Exception e) {
3172: fail(e.toString());
3173: }
3174: }
3175:
3176: private void createRolesC(MBeanServer server) {
3177: try {
3178: ArrayList roleC1Values = new ArrayList();
3179: roleC1Values
3180: .add(createRoleValueBean(
3181: "x:relation=c,role=1,bean=1",
3182: Trivial.class, server));
3183: roleC1Values.add(createRoleValueBean(
3184: "x:relation=c,role=1,bean=1", Trivial.class, null));
3185: roleC1 = new Role("roleC1", roleC1Values);
3186:
3187: ArrayList roleC2Values = new ArrayList();
3188: roleC2Values.add(createRoleValueBean(
3189: "x:relation=c,role=1,bean=1", Trivial.class, null));
3190: roleC2Values
3191: .add(createRoleValueBean(
3192: "x:relation=c,role=2,bean=1",
3193: Trivial.class, server));
3194: roleC2Values
3195: .add(createRoleValueBean(
3196: "x:relation=c,role=2,bean=2",
3197: Trivial.class, server));
3198: roleC2 = new Role("roleC2", roleC2Values);
3199:
3200: rolesC = new RoleList();
3201: rolesC.add(roleC1);
3202: rolesC.add(roleC2);
3203: } catch (Exception e) {
3204: fail(e.toString());
3205: }
3206: }
3207:
3208: private void createRelationTypeC(ObjectName relationService) {
3209: try {
3210: RoleInfo roleInfoC1 = createRoleInfo("roleC1",
3211: Trivial.class, true, false, 1, 2);
3212: RoleInfo roleInfoC2 = createRoleInfo("roleC2",
3213: Trivial.class, false, true, 3, 4);
3214: RoleInfo[] roleInfos = new RoleInfo[] { roleInfoC1,
3215: roleInfoC2 };
3216: createRelationType(relationService, "relationTypeC",
3217: roleInfos);
3218: for (int i = 0; i < roleInfos.length; i++)
3219: roleInfosC.put(roleInfos[i].getName(), roleInfos[i]);
3220: } catch (Exception e) {
3221: fail(e.toString());
3222: }
3223: }
3224:
3225: private void createRolesCX(MBeanServer server) {
3226: try {
3227: ArrayList roleCX1Values = new ArrayList();
3228: roleCX1Values.add(createRoleValueBean(
3229: "x:relation=c,role=2,bean=1", Trivial.class, null));
3230: roleCX1 = new Role("roleC1", roleCX1Values);
3231:
3232: ArrayList roleCX2Values = new ArrayList();
3233: roleCX2Values.add(createRoleValueBean(
3234: "x:relation=c,role=1,bean=1", Trivial.class, null));
3235: roleCX2Values
3236: .add(createRoleValueBean(
3237: "x:relation=c,role=1,bean=2",
3238: Trivial.class, server));
3239: roleCX2Values
3240: .add(createRoleValueBean(
3241: "x:relation=c,role=1,bean=3",
3242: Trivial.class, server));
3243: roleCX2Values
3244: .add(createRoleValueBean(
3245: "x:relation=c,role=1,bean=4",
3246: Trivial.class, server));
3247: roleCX2 = new Role("roleC2", roleCX2Values);
3248:
3249: rolesCX = new RoleList();
3250: rolesCX.add(roleCX1);
3251: rolesCX.add(roleCX2);
3252: } catch (Exception e) {
3253: fail(e.toString());
3254: }
3255: }
3256:
3257: private void createRelationTypeCX(ObjectName relationService) {
3258: try {
3259: RoleInfo roleInfoCX1 = createRoleInfo("roleC1",
3260: Trivial.class, true, false, 1, 2);
3261: RoleInfo roleInfoCX2 = createRoleInfo("roleC2",
3262: Trivial.class, false, true, 3, 4);
3263: RoleInfo[] roleInfos = new RoleInfo[] { roleInfoCX1,
3264: roleInfoCX2 };
3265: createRelationType(relationService, "relationTypeCX",
3266: roleInfos);
3267: for (int i = 0; i < roleInfos.length; i++)
3268: roleInfosCX.put(roleInfos[i].getName(), roleInfos[i]);
3269: } catch (Exception e) {
3270: fail(e.toString());
3271: }
3272: }
3273:
3274: private void createRolesCZ(MBeanServer server) {
3275: try {
3276: ArrayList roleCZ2Values = new ArrayList();
3277: roleCZ2Values.add(createRoleValueBean(
3278: "x:relation=c,role=1,bean=1", Trivial.class, null));
3279: roleCZ2 = new Role("roleC2", roleCZ2Values);
3280:
3281: rolesCZ = new RoleList();
3282: rolesCZ.add(roleCZ2);
3283: } catch (Exception e) {
3284: fail(e.toString());
3285: }
3286: }
3287:
3288: private void createRolesCZZ(MBeanServer server) {
3289: try {
3290: ArrayList roleCZZValues = new ArrayList();
3291: roleCZZValues.add(createRoleValueBean(
3292: "x:relation=c,role=1,bean=1", Trivial.class, null));
3293: roleCZZValues.add(createRoleValueBean(
3294: "x:relation=c,role=1,bean=2", Trivial.class, null));
3295: roleCZZValues.add(createRoleValueBean(
3296: "x:relation=c,role=1,bean=3", Trivial.class, null));
3297: roleCZZValues.add(createRoleValueBean(
3298: "x:relation=c,role=1,bean=4", Trivial.class, null));
3299: roleCZZValues
3300: .add(createRoleValueBean(
3301: "x:relation=c,role=1,bean=5",
3302: Trivial.class, server));
3303: roleCZZ = new Role("roleC2", roleCZZValues);
3304:
3305: rolesCZZ = new RoleList();
3306: rolesCZZ.add(roleCZZ);
3307:
3308: ArrayList roleCZZZValues = new ArrayList();
3309: roleCZZZValues
3310: .add(createRoleValueBean(
3311: "x:relation=c,role=1x,bean=1",
3312: Trivial.class, null));
3313: roleCZZZValues
3314: .add(createRoleValueBean(
3315: "x:relation=c,role=1x,bean=2",
3316: Trivial.class, null));
3317: roleCZZZValues
3318: .add(createRoleValueBean(
3319: "x:relation=c,role=1x,bean=3",
3320: Trivial.class, null));
3321: roleCZZZ = new Role("roleC2", roleCZZZValues);
3322:
3323: rolesCZZZ = new RoleList();
3324: rolesCZZZ.add(roleCZZZ);
3325: } catch (Exception e) {
3326: fail(e.toString());
3327: }
3328: }
3329:
3330: private class Listener implements NotificationListener {
3331: String type;
3332: HashSet notifications = new HashSet();
3333:
3334: public Listener(String type) {
3335: this .type = type;
3336: }
3337:
3338: public void handleNotification(Notification n, Object h) {
3339: notifications.add(n);
3340: }
3341:
3342: public RelationNotification check(int size) {
3343: RelationNotification result = null;
3344: assertEquals(size, notifications.size());
3345: Iterator iterator = notifications.iterator();
3346: while (iterator.hasNext()) {
3347: RelationNotification rn = (RelationNotification) iterator
3348: .next();
3349: assertEquals(type, rn.getType());
3350: result = rn;
3351: }
3352: return result;
3353: }
3354: }
3355: }
|