0001: /* Copyright 2002 The JA-SIG Collaborative. All rights reserved.
0002: * See license distributed with this file and
0003: * available online at http://www.uportal.org/license.html
0004: */
0005:
0006: package org.jasig.portal.groups;
0007:
0008: import java.sql.Connection;
0009: import java.sql.ResultSet;
0010: import java.sql.Statement;
0011: import java.util.ArrayList;
0012: import java.util.Collection;
0013: import java.util.Iterator;
0014: import java.util.Random;
0015:
0016: import junit.framework.TestCase;
0017: import junit.framework.TestSuite;
0018:
0019: import org.jasig.portal.EntityIdentifier;
0020: import org.jasig.portal.EntityTypes;
0021: import org.jasig.portal.IBasicEntity;
0022: import org.jasig.portal.concurrency.CachingException;
0023: import org.jasig.portal.concurrency.caching.ReferenceEntityCachingService;
0024: import org.jasig.portal.security.IPerson;
0025: import org.jasig.portal.services.GroupService;
0026:
0027: /**
0028: * Tests the groups framework (a start).
0029: * @author: Dan Ellentuck
0030: */
0031: public class GroupsTester extends TestCase {
0032: private static Class GROUP_CLASS;
0033: private static Class IPERSON_CLASS;
0034: private static Class TEST_ENTITY_CLASS;
0035: private static String CR = "\n";
0036: private IEntity[] testEntities;
0037: private String[] testEntityKeys;
0038: private int numTestEntities = 0;
0039: private Random random = new Random();
0040:
0041: private class TestEntity implements IBasicEntity {
0042: private EntityIdentifier entityIdentifier;
0043:
0044: private TestEntity(String entityKey) {
0045: super ();
0046: entityIdentifier = new EntityIdentifier(entityKey, this
0047: .getClass());
0048: }
0049:
0050: public EntityIdentifier getEntityIdentifier() {
0051: return entityIdentifier;
0052: }
0053:
0054: public boolean equals(Object o) {
0055: if (o == null)
0056: return false;
0057: if (!(o instanceof IBasicEntity))
0058: return false;
0059: IBasicEntity ent = (IBasicEntity) o;
0060: return ent.getEntityIdentifier().equals(
0061: getEntityIdentifier());
0062: }
0063:
0064: public String toString() {
0065: return "TestEntity(" + getEntityIdentifier().getKey() + ")";
0066: }
0067: }
0068:
0069: private class GroupsReadTester implements Runnable {
0070: protected IEntityGroup group = null;
0071: protected int numTests = 0;
0072: protected String testerID = null;
0073: protected String printID = null;
0074:
0075: protected GroupsReadTester(String id, IEntityGroup g, int tests) {
0076: super ();
0077: group = g;
0078: numTests = tests;
0079: testerID = id;
0080: }
0081:
0082: public void run() {
0083: printID = "Tester " + testerID;
0084: print(printID + " starting.");
0085:
0086: for (int i = 0; i < numTests; i++) {
0087: // print(printID + " running test # " + (i+1));
0088: try {
0089: runTest();
0090: } catch (GroupsException ge) {
0091: }
0092: int sleepMillis = random.nextInt(20);
0093: // print(printID + " will now sleep for " + sleepMillis + " ms.");
0094: try {
0095: Thread.sleep(sleepMillis);
0096: } catch (Exception ex) {
0097: }
0098: }
0099: }
0100:
0101: private void runTest() throws GroupsException {
0102: int numMembers = 0, numEntities = 0, numContainingGroups = 0;
0103: Iterator itr = null;
0104: for (itr = group.getMembers(); itr.hasNext(); itr.next()) {
0105: numMembers++;
0106: }
0107: for (itr = group.getEntities(); itr.hasNext(); itr.next()) {
0108: numEntities++;
0109: }
0110: for (itr = group.getContainingGroups(); itr.hasNext(); itr
0111: .next()) {
0112: numContainingGroups++;
0113: }
0114: // print (printID + " members: " + numMembers + " entities: " + numEntities + " containing groups: " + numContainingGroups);
0115: }
0116: }
0117:
0118: /**
0119: * EntityLockTester constructor comment.
0120: */
0121: public GroupsTester(String name) {
0122: super (name);
0123: }
0124:
0125: /**
0126: */
0127: protected void addTestEntityType() {
0128: try {
0129: org.jasig.portal.EntityTypes.singleton()
0130: .addEntityTypeIfNecessary(TEST_ENTITY_CLASS,
0131: "Test Entity Type");
0132: } catch (Exception ex) {
0133: print("GroupsTester.addTestEntityType(): "
0134: + ex.getMessage());
0135: }
0136: }
0137:
0138: /**
0139: *
0140: */
0141: private void clearGroupCache() throws CachingException {
0142: ((ReferenceEntityCachingService) ReferenceEntityCachingService
0143: .singleton()).getCache(GROUP_CLASS).clearCache();
0144: }
0145:
0146: /**
0147: */
0148: protected void deleteTestEntityType() {
0149: try {
0150: org.jasig.portal.EntityTypes.singleton().deleteEntityType(
0151: TEST_ENTITY_CLASS);
0152: } catch (Exception ex) {
0153: print("EntityCacheTester.deleteTestEntityType(): "
0154: + ex.getMessage());
0155: }
0156: }
0157:
0158: /**
0159: */
0160: protected void deleteTestGroups() {
0161: String sql = " FROM UP_GROUP WHERE ENTITY_TYPE_ID = "
0162: + EntityTypes.getEntityTypeID(TEST_ENTITY_CLASS);
0163: String selectSql = "SELECT GROUP_ID" + sql;
0164: String deleteSql = "DELETE" + sql;
0165: String deleteMemberSql = "DELETE FROM UP_GROUP_MEMBERSHIP WHERE GROUP_ID = ";
0166: Connection conn = null;
0167: try {
0168: conn = org.jasig.portal.RDBMServices.getConnection();
0169: Statement selectStmnt = conn.createStatement();
0170: ResultSet rs = selectStmnt.executeQuery(selectSql);
0171: while (rs.next()) {
0172: String key = rs.getString(1);
0173: Statement deleteMemberStmnt = conn.createStatement();
0174: int memberRC = deleteMemberStmnt
0175: .executeUpdate(deleteMemberSql + "'" + key
0176: + "'");
0177: print("Test member rows deleted: " + memberRC);
0178: }
0179:
0180: Statement deleteGroupStmnt = conn.createStatement();
0181: int rc = deleteGroupStmnt.executeUpdate(deleteSql);
0182: print("Test group rows deleted: " + rc);
0183:
0184: } catch (Exception ex) {
0185: print("GroupsTester.deleteTestGroups(): " + ex.getMessage());
0186: } finally {
0187: org.jasig.portal.RDBMServices.releaseConnection(conn);
0188: }
0189: }
0190:
0191: /**
0192: * @return org.jasig.portal.groups.IEntityGroup
0193: */
0194: private IEntityGroup findGroup(String key) throws GroupsException {
0195: IEntityGroup group = GroupService.findGroup(key);
0196: return group;
0197: }
0198:
0199: /**
0200: * @return org.jasig.portal.groups.ILockableEntityGroup
0201: */
0202: private ILockableEntityGroup findLockableGroup(String key)
0203: throws GroupsException {
0204: String owner = "de3";
0205: ILockableEntityGroup group = GroupService.findLockableGroup(
0206: key, owner);
0207: return group;
0208: }
0209:
0210: /**
0211: * @return org.jasig.portal.services.GroupService
0212: */
0213: private Collection getAllGroupMembers(IGroupMember gm)
0214: throws GroupsException {
0215: Collection list = new ArrayList();
0216: for (Iterator itr = gm.getAllMembers(); itr.hasNext();) {
0217: list.add(itr.next());
0218: }
0219: return list;
0220: }
0221:
0222: /**
0223: * @return RDBMEntityStore
0224: */
0225: private IEntityStore getEntityStore() throws GroupsException {
0226: return RDBMEntityStore.singleton();
0227: }
0228:
0229: /**
0230: * @return org.jasig.portal.services.GroupService
0231: */
0232: private Collection getGroupMembers(IGroupMember gm)
0233: throws GroupsException {
0234: Collection list = new ArrayList();
0235: for (Iterator itr = gm.getMembers(); itr.hasNext();) {
0236: list.add(itr.next());
0237: }
0238: return list;
0239: }
0240:
0241: /**
0242: * @return RDBMEntityGroupStore
0243: */
0244: private RDBMEntityGroupStore getGroupStore() throws GroupsException {
0245: return RDBMEntityGroupStore.singleton();
0246: }
0247:
0248: /**
0249: * @return org.jasig.portal.groups.IEntity
0250: */
0251: private IEntity getNewEntity(String key) throws GroupsException {
0252: return GroupService.getEntity(key, TEST_ENTITY_CLASS);
0253: }
0254:
0255: /**
0256: * @return org.jasig.portal.groups.IEntityGroup
0257: */
0258: private IEntityGroup getNewGroup() throws GroupsException {
0259: IEntityGroup group = GroupService.newGroup(TEST_ENTITY_CLASS);
0260: group.setName("name_" + group.getKey());
0261: group.setCreatorID("de3");
0262: return group;
0263: }
0264:
0265: /**
0266: * @return java.lang.String
0267: * @param length int
0268: */
0269: private String getRandomString(java.util.Random r, int length) {
0270:
0271: char[] chars = new char[length];
0272:
0273: for (int i = 0; i < length; i++) {
0274: int diff = (r.nextInt(25));
0275: int charValue = (int) 'A' + diff;
0276: chars[i] = (char) charValue;
0277: }
0278: return new String(chars);
0279: }
0280:
0281: /**
0282: * @return org.jasig.portal.services.GroupService
0283: */
0284: private GroupService getService() throws GroupsException {
0285: return GroupService.instance();
0286: }
0287:
0288: /**
0289: * Starts the application.
0290: * @param args an array of command-line arguments
0291: */
0292: public static void main(java.lang.String[] args) throws Exception {
0293: String[] mainArgs = { "org.jasig.portal.concurrency.caching.EntityCacheTester" };
0294: print("START TESTING CACHE");
0295: printBlankLine();
0296: junit.swingui.TestRunner.main(mainArgs);
0297: printBlankLine();
0298: print("END TESTING CACHE");
0299:
0300: }
0301:
0302: /**
0303: */
0304: private static void print(IEntity[] entities) {
0305: for (int i = 0; i < entities.length; i++) {
0306: print("(" + (i + 1) + ") " + entities[i]);
0307: }
0308: print(" Total: " + entities.length);
0309: }
0310:
0311: /**
0312: * @param msg java.lang.String
0313: */
0314: private static void print(String msg) {
0315: java.sql.Timestamp ts = new java.sql.Timestamp(System
0316: .currentTimeMillis());
0317: System.out.println(ts + " : " + msg);
0318: }
0319:
0320: private static void printBlankLine() {
0321: System.out.println("");
0322: }
0323:
0324: /**
0325: */
0326: protected void setUp() {
0327: try {
0328: if (GROUP_CLASS == null) {
0329: GROUP_CLASS = Class
0330: .forName("org.jasig.portal.groups.IEntityGroup");
0331: }
0332: if (IPERSON_CLASS == null) {
0333: IPERSON_CLASS = Class
0334: .forName("org.jasig.portal.security.IPerson");
0335: }
0336: if (TEST_ENTITY_CLASS == null) {
0337: TEST_ENTITY_CLASS = TestEntity.class;
0338: }
0339:
0340: addTestEntityType();
0341: numTestEntities = 100;
0342:
0343: // Entities and their keys:
0344: testEntityKeys = new String[numTestEntities];
0345: testEntities = new IEntity[numTestEntities];
0346: for (int i = 0; i < numTestEntities; i++) {
0347: testEntityKeys[i] = (getRandomString(random, 3) + i);
0348: testEntities[i] = getNewEntity(testEntityKeys[i]);
0349: }
0350:
0351: } catch (Exception ex) {
0352: print("GroupsTester.setUp(): " + ex.getMessage());
0353: }
0354: }
0355:
0356: /**
0357: * @return junit.framework.Test
0358: */
0359: public static junit.framework.Test suite() {
0360: TestSuite suite = new TestSuite();
0361:
0362: suite.addTest(new GroupsTester("testAddAndDeleteGroups"));
0363: suite.addTest(new GroupsTester("testAddAndDeleteMembers"));
0364: suite.addTest(new GroupsTester("testGroupMemberValidation"));
0365: suite.addTest(new GroupsTester("testGroupMemberUpdate"));
0366: suite.addTest(new GroupsTester("testRetrieveParentGroups"));
0367: suite.addTest(new GroupsTester("testUpdateMembersVisibility"));
0368: suite.addTest(new GroupsTester("testUpdateLockableGroups"));
0369: suite.addTest(new GroupsTester(
0370: "testUpdateLockableGroupsWithRenewableLock"));
0371: suite.addTest(new GroupsTester("testContains"));
0372: suite.addTest(new GroupsTester("testDeleteChildGroup"));
0373: suite.addTest(new GroupsTester(
0374: "testMixLockableAndNonLockableGroups"));
0375: suite.addTest(new GroupsTester("testConcurrentAccess"));
0376: suite.addTest(new GroupsTester("testParseCompoundKeys"));
0377: suite.addTest(new GroupsTester("testPagsContains"));
0378: suite.addTest(new GroupsTester("testAddToALargeGroup"));
0379:
0380: // Add more tests here.
0381: // NB: Order of tests is not guaranteed.
0382:
0383: return suite;
0384: }
0385:
0386: /**
0387: */
0388: protected void tearDown() {
0389: try {
0390: testEntityKeys = null;
0391: testEntities = null;
0392: deleteTestGroups();
0393: deleteTestEntityType();
0394:
0395: clearGroupCache();
0396:
0397: } catch (Exception ex) {
0398: print("GroupTester.tearDown(): " + ex.getMessage());
0399: }
0400: }
0401:
0402: /**
0403: */
0404: public void testAddAndDeleteGroups() throws Exception {
0405: print(CR
0406: + "***** ENTERING GroupsTester.testAddAndDeleteGroups() *****"
0407: + CR);
0408: String msg = null;
0409:
0410: msg = "Creating a new IEntityGroup.";
0411: print(msg);
0412: IEntityGroup newGroup = getNewGroup();
0413: assertNotNull(msg, newGroup);
0414:
0415: print("Now updating " + newGroup);
0416: newGroup.setName("Test");
0417: newGroup.setCreatorID("de3");
0418: newGroup.update();
0419:
0420: print("Now retrieving group just created from the store.");
0421: String key = newGroup.getKey();
0422: IEntityGroup retrievedGroup = GroupService.findGroup(key);
0423:
0424: msg = "Testing retrieved group.";
0425: print(msg);
0426: assertEquals(msg, newGroup, retrievedGroup);
0427:
0428: print("Now deleting group just created from the store.");
0429: retrievedGroup.delete();
0430:
0431: print("Attempting to retrieve deleted group from the store.");
0432: retrievedGroup = GroupService.findGroup(key);
0433: assertNull(msg, retrievedGroup);
0434:
0435: print(CR
0436: + "***** LEAVING GroupsTester.testAddAndDeleteGroups() *****"
0437: + CR);
0438:
0439: }
0440:
0441: /**
0442: */
0443: public void testAddToALargeGroup() throws Exception {
0444: print(CR
0445: + "***** ENTERING GroupsTester.testAddToALargeGroup() *****"
0446: + CR);
0447:
0448: int numEntities = 1000;
0449: String msg = null;
0450: int idx = 0;
0451:
0452: String[] entityKeys = new String[numEntities];
0453: IEntity[] entities = new IEntity[numEntities];
0454:
0455: print("About to create test entities.");
0456: for (int i = 0; i < numEntities; i++) {
0457: entityKeys[i] = (getRandomString(random, 3) + i);
0458: entities[i] = getNewEntity(entityKeys[i]);
0459: }
0460: print("Created " + numEntities + " entities.");
0461:
0462: print("Creating new group.");
0463: IEntityGroup bigGroup = getNewGroup();
0464: bigGroup.setName("Big Group");
0465: bigGroup.getMembers();
0466: print("Created" + bigGroup + ". Will now add members...");
0467:
0468: for (idx = 0; idx < numEntities; idx++) {
0469: bigGroup.addMember(entities[idx]);
0470: }
0471:
0472: msg = "Added " + entities.length + "members. Will now update.";
0473: print(msg);
0474: bigGroup.update();
0475: print("Finished updating.");
0476:
0477: print("Will un-cache " + bigGroup);
0478: clearGroupCache();
0479:
0480: msg = "Finding duplicate group.";
0481: print(msg);
0482: String groupKey = bigGroup.getKey();
0483: IEntityGroup dupBigGroup = GroupService.findGroup(groupKey);
0484: assertNotNull(msg, dupBigGroup);
0485:
0486: int numAdditionalEntities = 10;
0487: String[] additionalEntityKeys = new String[numAdditionalEntities];
0488: IEntity[] additionalEntities = new IEntity[numAdditionalEntities];
0489:
0490: print("About to create additional entities.");
0491: for (int i = 0; i < numAdditionalEntities; i++) {
0492: additionalEntityKeys[i] = (getRandomString(random, 3) + i);
0493: additionalEntities[i] = getNewEntity(additionalEntityKeys[i]);
0494: }
0495: print("Created " + numAdditionalEntities
0496: + " additional entities.");
0497:
0498: print("Adding additional entities.");
0499: for (idx = 0; idx < numAdditionalEntities; idx++) {
0500: dupBigGroup.addMember(additionalEntities[idx]);
0501: print("Added " + additionalEntities[idx]);
0502: dupBigGroup.update();
0503: }
0504:
0505: print("Done adding additional entities");
0506:
0507: print("Will un-cache " + dupBigGroup);
0508: clearGroupCache();
0509:
0510: msg = "Retrieving duplicate group a 2nd time.";
0511: print(msg);
0512: dupBigGroup = GroupService.findGroup(groupKey);
0513: assertNotNull(msg, dupBigGroup);
0514: msg = "Getting members from duplicate group.";
0515: print(msg);
0516: Iterator itr = dupBigGroup.getMembers();
0517: msg = "Done retrieving members. Will now count members.";
0518: print(msg);
0519: int numMembers = 0;
0520: while (itr.hasNext()) {
0521: itr.next();
0522: numMembers++;
0523: }
0524: assertEquals(msg, numMembers,
0525: (numEntities + numAdditionalEntities));
0526:
0527: print(CR
0528: + "***** LEAVING GroupsTester.testAddToALargeGroup() *****"
0529: + CR);
0530:
0531: }
0532:
0533: public void testPagsContains() throws Exception {
0534: print(CR
0535: + "***** ENTERING GroupsTester.testPagsContains() *****"
0536: + CR);
0537: String message = null;
0538: boolean result = false;
0539:
0540: // We will rely on the groups defined in the sample configuration.
0541: print("Attempting to retrieve PAGS groups.");
0542: IEntityGroup pagsRoot = GroupService.findGroup("pags.0");
0543: IEntityGroup pagsUsers = GroupService.findGroup("pags.1");
0544: IEntityGroup pagsDs = GroupService.findGroup("pags.3");
0545: assertNotNull(pagsRoot);
0546: assertNotNull(pagsUsers);
0547: assertNotNull(pagsDs);
0548: print("Found PAGS groups.");
0549:
0550: message = "PAGS groups should contain no entity members";
0551: Iterator itr = null;
0552: Object o = null;
0553:
0554: for (itr = pagsRoot.getAllEntities(); itr.hasNext();) {
0555: o = itr.next();
0556: }
0557: assertTrue(message, o == null);
0558:
0559: for (itr = pagsUsers.getMembers(); itr.hasNext();) {
0560: o = itr.next();
0561: }
0562: assertTrue(message, o == null);
0563:
0564: for (itr = pagsDs.getMembers(); itr.hasNext();) {
0565: o = itr.next();
0566: }
0567: assertTrue(message, o == null);
0568:
0569: // Entities existing in UP_PERSON_DIR
0570: IEntity demo = GroupService.getEntity("demo", IPerson.class);
0571: IEntity admin = GroupService.getEntity("admin", IPerson.class);
0572: IEntity staff = GroupService.getEntity("staff", IPerson.class);
0573:
0574: // Entity not existing in UP_PERSON_DIR
0575: IEntity testUser = GroupService
0576: .getEntity("test", IPerson.class);
0577:
0578: message = "pags.1 (users) should contain all entities in UP_PERSON_DIR.";
0579: result = pagsUsers.contains(demo);
0580: assertTrue(message, result);
0581: result = pagsUsers.contains(admin);
0582: assertTrue(message, result);
0583: result = pagsUsers.contains(staff);
0584: assertTrue(message, result);
0585:
0586: message = "pags.1 (users) should not contain test entity.";
0587: result = pagsUsers.contains(testUser);
0588: assertTrue(message, !result);
0589:
0590: message = "pags.0 (root) should deepContain all entities in UP_PERSON_DIR.";
0591: result = pagsRoot.deepContains(demo);
0592: assertTrue(message, result);
0593: message = "pags.0 (users) should not deepContain test entity.";
0594: result = pagsRoot.deepContains(testUser);
0595: assertTrue(message, !result);
0596:
0597: print(CR
0598: + "***** LEAVING GroupsTester.testPagsContains() *****"
0599: + CR);
0600:
0601: }
0602:
0603: /**
0604: */
0605: public void testAddAndDeleteMembers() throws Exception {
0606: print(CR
0607: + "***** ENTERING GroupsTester.testAddAndDeleteMembers() *****"
0608: + CR);
0609: String msg = null;
0610: Class type = TEST_ENTITY_CLASS;
0611: int totNumGroups = 3;
0612: int totNumEntities = 5;
0613: IEntityGroup[] groups = new IEntityGroup[totNumGroups];
0614: IEntity[] entities = new IEntity[totNumEntities];
0615: IGroupMember[] groupMembers = null;
0616: Iterator itr = null;
0617: ArrayList list = null;
0618: int idx = 0;
0619:
0620: msg = "Creating " + totNumGroups + " new groups.";
0621: print(msg);
0622: for (idx = 0; idx < totNumGroups; idx++) {
0623: groups[idx] = getNewGroup();
0624: assertNotNull(msg, groups[idx]);
0625: }
0626: IEntityGroup rootGroup = groups[0];
0627: IEntityGroup childGroup = groups[1];
0628:
0629: msg = "Adding " + (totNumGroups - 1) + " to root group.";
0630: print(msg);
0631: for (idx = 1; idx < totNumGroups; idx++) {
0632: rootGroup.addMember(groups[idx]);
0633: }
0634:
0635: rootGroup.update(); // in case members not cached.
0636:
0637: msg = "Retrieving members from root group.";
0638: print(msg);
0639: list = new ArrayList();
0640: for (itr = rootGroup.getMembers(); itr.hasNext();) {
0641: list.add(itr.next());
0642: }
0643: assertEquals(msg, (totNumGroups - 1), list.size());
0644:
0645: msg = "Adding " + (totNumEntities - 2) + " to root group.";
0646: print(msg);
0647: for (idx = 0; idx < (totNumEntities - 2); idx++) {
0648: rootGroup.addMember(testEntities[idx]);
0649: }
0650:
0651: rootGroup.update(); // in case members not cached.
0652:
0653: msg = "Retrieving members from root group.";
0654: print(msg);
0655: list = new ArrayList();
0656: for (itr = rootGroup.getMembers(); itr.hasNext();) {
0657: list.add(itr.next());
0658: }
0659: assertEquals(msg, (totNumGroups - 1 + totNumEntities - 2), list
0660: .size());
0661:
0662: msg = "Adding 2 entities to child group.";
0663: print(msg);
0664: childGroup.addMember(testEntities[totNumEntities - 1]);
0665: childGroup.addMember(testEntities[totNumEntities]);
0666:
0667: childGroup.update(); // in case members not cached.
0668:
0669: msg = "Retrieving ALL members from root group.";
0670: print(msg);
0671: list = new ArrayList();
0672: for (itr = rootGroup.getAllMembers(); itr.hasNext();) {
0673: list.add(itr.next());
0674: }
0675: assertEquals(msg, (totNumGroups - 1 + totNumEntities), list
0676: .size());
0677:
0678: msg = "Deleting child group from root group.";
0679: print(msg);
0680: rootGroup.removeMember(childGroup);
0681:
0682: rootGroup.update(); // in case members not cached.
0683:
0684: msg = "Retrieving ALL members from root group.";
0685: print(msg);
0686: list = new ArrayList();
0687: for (itr = rootGroup.getAllMembers(); itr.hasNext();) {
0688: list.add(itr.next());
0689: }
0690: assertEquals(msg, (totNumGroups - 2 + totNumEntities - 2), list
0691: .size());
0692:
0693: print(CR
0694: + "***** LEAVING GroupsTester.testAddAndDeleteMembers() *****"
0695: + CR);
0696:
0697: }
0698:
0699: /**
0700: */
0701: public void testContains() throws Exception {
0702: print(CR + "***** ENTERING GroupsTester.testContains() *****"
0703: + CR);
0704: String msg = null;
0705: Class type = TEST_ENTITY_CLASS;
0706: int totNumEntities = 1;
0707: IEntityGroup containingGroup, childGroup, dupContainingGroup = null;
0708: IEntity[] entities = new IEntity[totNumEntities];
0709: IGroupMember[] groupMembers = null;
0710: Iterator itr = null;
0711: ArrayList list = null;
0712: int idx = 0;
0713: boolean testValue = false;
0714:
0715: msg = "Creating new parent group.";
0716: print(msg);
0717: containingGroup = getNewGroup();
0718: containingGroup.getMembers(); // cache members
0719: assertNotNull(msg, containingGroup);
0720:
0721: msg = "Creating new child group.";
0722: print(msg);
0723: childGroup = getNewGroup();
0724: childGroup.getMembers(); // cache members
0725: assertNotNull(msg, childGroup);
0726:
0727: msg = "Creating " + totNumEntities + " new entities.";
0728: print(msg);
0729: for (idx = 0; idx < totNumEntities; idx++) {
0730: entities[idx] = getNewEntity("E" + idx);
0731: }
0732:
0733: msg = "Adding " + (totNumEntities) + " to containing group.";
0734: print(msg);
0735: for (idx = 0; idx < totNumEntities; idx++) {
0736: containingGroup.addMember(entities[idx]);
0737: }
0738:
0739: msg = "Testing if containing group contains entities.";
0740: print(msg);
0741: for (idx = 0; idx < totNumEntities; idx++) {
0742: testValue = containingGroup.contains(entities[idx]);
0743: assertTrue(msg, testValue);
0744: }
0745:
0746: msg = "Adding child group to containing group.";
0747: print(msg);
0748: containingGroup.addMember(childGroup);
0749:
0750: msg = "Testing if containing group contains child group.";
0751: print(msg);
0752: testValue = containingGroup.contains(childGroup);
0753: assertTrue(msg, testValue);
0754:
0755: msg = "Updating containing group.";
0756: print(msg);
0757: containingGroup.update();
0758:
0759: msg = "Getting duplicate containing group.";
0760: print(msg);
0761: dupContainingGroup = findGroup(containingGroup.getKey());
0762: assertNotNull(msg, dupContainingGroup);
0763:
0764: msg = "Testing if RETRIEVED containing group contains entities.";
0765: print(msg);
0766: for (idx = 0; idx < totNumEntities; idx++) {
0767: testValue = dupContainingGroup.contains(entities[idx]);
0768: assertTrue(msg, testValue);
0769: }
0770:
0771: msg = "Testing if RETRIEVED containing group contains child group.";
0772: print(msg);
0773: testValue = dupContainingGroup.contains(childGroup);
0774: assertTrue(msg, testValue);
0775:
0776: msg = "Deleting containing group from db.";
0777: print(msg);
0778: containingGroup.delete();
0779:
0780: print(CR + "***** LEAVING GroupsTester.testContains() *****"
0781: + CR);
0782:
0783: }
0784:
0785: /**
0786: */
0787: public void testDeleteChildGroup() throws Exception {
0788: print(CR
0789: + "***** ENTERING GroupsTester.testDeleteChildGroup() *****"
0790: + CR);
0791: String msg = null;
0792: Class type = TEST_ENTITY_CLASS;
0793: int totNumGroups = 3;
0794: int totNumEntities = 5;
0795: IEntityGroup[] groups = new IEntityGroup[totNumGroups];
0796: IEntity[] entities = new IEntity[totNumEntities];
0797: IGroupMember[] groupMembers = null;
0798: Iterator itr = null;
0799: ArrayList list = null;
0800: int idx = 0;
0801: Exception e = null;
0802:
0803: msg = "Creating 3 new groups; 2 parents and 1 child...";
0804: print(msg);
0805:
0806: for (idx = 0; idx < totNumGroups; idx++) {
0807: groups[idx] = getNewGroup();
0808: assertNotNull(msg, groups[idx]);
0809: }
0810:
0811: IEntityGroup child = groups[0];
0812:
0813: msg = "Adding child to " + (totNumGroups - 1)
0814: + " parent groups.";
0815: print(msg);
0816: for (idx = 1; idx < totNumGroups; idx++) {
0817: groups[idx].addMember(child);
0818: groups[idx].update();
0819: }
0820:
0821: msg = "Retrieving containing groups from child.";
0822: print(msg);
0823: list = new ArrayList();
0824: for (itr = child.getContainingGroups(); itr.hasNext();) {
0825: list.add(itr.next());
0826: }
0827: assertEquals(msg, (totNumGroups - 1), list.size());
0828:
0829: msg = "Adding " + (totNumEntities) + " to child group.";
0830: print(msg);
0831: for (idx = 0; idx < (totNumEntities); idx++) {
0832: child.addMember(testEntities[idx]);
0833: }
0834:
0835: msg = "Retrieving members from child group.";
0836: print(msg);
0837: list = new ArrayList();
0838: for (itr = child.getMembers(); itr.hasNext();) {
0839: list.add(itr.next());
0840: }
0841: assertEquals(msg, (totNumEntities), list.size());
0842:
0843: msg = "Updating child.";
0844: print(msg);
0845: child.update();
0846:
0847: msg = "Will now lock one of the parent groups. (Delete of child should fail.)";
0848: print(msg);
0849: ILockableEntityGroup lockedParent = findLockableGroup(groups[1]
0850: .getKey());
0851: assertNotNull(msg, lockedParent);
0852: assertTrue(msg, lockedParent.getLock().isValid());
0853:
0854: msg = "Deleting child. (Should FAIL).";
0855: print(msg);
0856: ILockableEntityGroup legDelete = findLockableGroup(child
0857: .getKey());
0858: try {
0859: legDelete.delete();
0860: } catch (GroupsException ge) {
0861: e = ge;
0862: }
0863: assertNotNull(msg, e);
0864:
0865: msg = "Will now UN-lock the parent group.";
0866: print(msg);
0867: lockedParent.getLock().release();
0868: assertTrue(msg, !lockedParent.getLock().isValid());
0869:
0870: msg = "Deleting child. (Should SUCCEED).";
0871: print(msg);
0872: ILockableEntityGroup legDeleteDup = findLockableGroup(child
0873: .getKey());
0874: legDeleteDup.delete();
0875:
0876: msg = "Retrieving members from parent groups (should be EMPTY).";
0877: print(msg);
0878: for (idx = 1; idx < totNumGroups; idx++) {
0879: String groupKey = groups[idx].getKey();
0880: IEntityGroup g = findGroup(groupKey);
0881: list = new ArrayList();
0882: for (itr = g.getMembers(); itr.hasNext();) {
0883: list.add(itr.next());
0884: }
0885: assertEquals(msg, 0, list.size());
0886: }
0887:
0888: print(CR
0889: + "***** LEAVING GroupsTester.testDeleteChildGroup() *****"
0890: + CR);
0891:
0892: }
0893:
0894: /**
0895: */
0896: public void testGroupMemberUpdate() throws Exception {
0897: print(CR
0898: + "***** ENTERING GroupsTester.testGroupMemberUpdate() *****"
0899: + CR);
0900: String msg = null;
0901:
0902: Iterator itr;
0903: Collection list;
0904: int idx = 0;
0905: Exception e = null;
0906:
0907: int numAddedEntities = 10;
0908: int numDeletedEntities = 5;
0909:
0910: print("Creating 2 new groups.");
0911:
0912: IEntityGroup parent = getNewGroup();
0913: parent.setName("parent");
0914: parent.setCreatorID("de3");
0915: String parentKey = parent.getKey();
0916: IEntityGroup child = getNewGroup();
0917: child.setName("child");
0918: child.setCreatorID("de3");
0919: String childKey = child.getKey();
0920:
0921: print("Adding " + child + " to " + parent);
0922: parent.addMember(child);
0923:
0924: print("Adding " + numAddedEntities + " members to " + child);
0925: for (idx = 0; idx < numAddedEntities; idx++) {
0926: child.addMember(testEntities[idx]);
0927: }
0928:
0929: print("Now updating " + parent + " and " + child);
0930: child.update();
0931: parent.update();
0932:
0933: msg = "Retrieving members from " + child; // child should have numAddedEntities group members.
0934: print(msg);
0935: list = getGroupMembers(child);
0936: assertEquals(msg, (numAddedEntities), list.size());
0937:
0938: msg = "Retrieving members from " + parent; // parent should have numAddedEntities + 1 group members.
0939: print(msg);
0940: list = getAllGroupMembers(parent);
0941: assertEquals(msg, (numAddedEntities + 1), list.size());
0942:
0943: msg = "Retrieving " + parent + " and " + child + " from db.";
0944: print(msg);
0945: IEntityGroup retrievedParent = GroupService
0946: .findGroup(parentKey);
0947: IEntityGroup retrievedChild = GroupService.findGroup(childKey);
0948: assertEquals(msg, parent, retrievedParent);
0949: assertEquals(msg, child, retrievedChild);
0950:
0951: // retrievedChild should have numAddedEntities group members.
0952: msg = "Retrieving members from " + retrievedChild;
0953: print(msg);
0954: list = getAllGroupMembers(retrievedChild);
0955: assertEquals(msg, numAddedEntities, list.size());
0956:
0957: // retrievedParent should have numAddedEntities + 1 group members.
0958: msg = "Retrieving members from " + retrievedParent;
0959: print(msg);
0960: list = getAllGroupMembers(retrievedParent);
0961: assertEquals(msg, (numAddedEntities + 1), list.size());
0962:
0963: print("Deleting " + numDeletedEntities + " members from "
0964: + retrievedChild);
0965: for (idx = 0; idx < numDeletedEntities; idx++) {
0966: retrievedChild.removeMember(testEntities[idx]);
0967: }
0968:
0969: // retrievedChild should have (numAddedEntities - numDeletedEntities) members.
0970: msg = "Retrieving members from " + retrievedChild;
0971: print(msg);
0972: list = getAllGroupMembers(retrievedChild);
0973: assertEquals(msg, (numAddedEntities - numDeletedEntities), list
0974: .size());
0975:
0976: msg = "Adding back one member to " + retrievedChild;
0977: print(msg);
0978: retrievedChild.addMember(testEntities[0]);
0979:
0980: // retrievedChild should have (numAddedEntities - numDeletedEntities + 1) members.
0981: msg = "Retrieving members from " + retrievedChild;
0982: print(msg);
0983: list = getAllGroupMembers(retrievedChild);
0984: assertEquals(msg, (numAddedEntities - numDeletedEntities + 1),
0985: list.size());
0986:
0987: int numChildMembers = list.size();
0988: print("Now updating " + retrievedChild);
0989: retrievedChild.update();
0990:
0991: msg = "Re-Retrieving " + retrievedChild + " from db.";
0992: print(msg);
0993: IEntityGroup reRetrievedChild = GroupService
0994: .findGroup(childKey);
0995: assertEquals(msg, retrievedChild, reRetrievedChild);
0996:
0997: // re-RetrievedChild should have (numAddedEntities - numDeletedEntities + 1) members.
0998: msg = "Retrieving members from " + reRetrievedChild;
0999: print(msg);
1000: list = getAllGroupMembers(reRetrievedChild);
1001: assertEquals(msg, numChildMembers, list.size());
1002:
1003: // Remove parent and child groups from db.
1004: msg = "Deleting " + retrievedParent + " and "
1005: + reRetrievedChild + " from db.";
1006: print(msg);
1007: retrievedParent.delete();
1008: reRetrievedChild.delete();
1009:
1010: IEntityGroup deletedParent = GroupService.findGroup(parentKey);
1011: IEntityGroup deletedChild = GroupService.findGroup(childKey);
1012: assertNull(msg, deletedParent);
1013: assertNull(msg, deletedChild);
1014:
1015: print(CR
1016: + "***** LEAVING GroupsTester.testGroupMemberUpdate() *****"
1017: + CR);
1018:
1019: }
1020:
1021: /**
1022: */
1023: public void testGroupMemberValidation() throws Exception {
1024: print(CR
1025: + "***** ENTERING GroupsTester.testGroupMemberValidation() *****"
1026: + CR);
1027: String msg = null;
1028:
1029: Iterator itr;
1030: Collection list;
1031: int idx = 0;
1032: Exception e = null;
1033:
1034: IEntityGroup parent = getNewGroup();
1035: parent.setName("parent");
1036: parent.setCreatorID("de3");
1037: IEntityGroup child = getNewGroup();
1038: child.setName("child");
1039: child.setCreatorID("de3");
1040: IEntityGroup child2 = getNewGroup();
1041: child2.setName("child");
1042: child2.setCreatorID("de3");
1043:
1044: IEntity entity1 = getNewEntity("child");
1045: IEntity entity2 = getNewEntity("child");
1046: IEntity ipersonEntity = GroupService.getEntity("00000",
1047: IPERSON_CLASS);
1048:
1049: msg = "Adding " + child + " to " + parent;
1050: print(msg);
1051: parent.addMember(child);
1052: parent.update(); // members not cached.
1053:
1054: msg = "Retrieving members from " + parent; // parent should have 1 group member.
1055: print(msg);
1056: list = getGroupMembers(parent);
1057: assertEquals(msg, 1, list.size());
1058:
1059: // Test adding a group with a duplicate name.
1060: msg = "Adding " + child2 + " to " + parent
1061: + " (this is allowed).";
1062: print(msg);
1063: try {
1064: parent.addMember(child2);
1065: parent.update(); // members not cached.
1066: } catch (GroupsException ge) {
1067: e = ge;
1068: }
1069: assertNull(msg, e);
1070:
1071: msg = "Retrieving members from " + parent; // parent should now have 2 group members.
1072: print(msg);
1073: list = getGroupMembers(parent);
1074: assertEquals(msg, 2, list.size());
1075:
1076: // Test adding an ENTITY with the same name as a member GROUP.
1077: msg = "Adding entity w/same name as child group to " + parent;
1078: print(msg);
1079: parent.addMember(entity1);
1080: parent.update(); // members not cached.
1081:
1082: msg = "Retrieving members from " + parent; // parent should now have 3 group members.
1083: print(msg);
1084: list = getGroupMembers(parent);
1085: assertEquals(msg, 3, list.size());
1086:
1087: // Test adding a group member with a duplicate key.
1088: msg = "Adding another entity w/same key as child group to "
1089: + parent + " (noop).";
1090: print(msg);
1091: try {
1092: parent.addMember(entity2);
1093: parent.update(); // members not cached.
1094: e = null;
1095: } catch (GroupsException ge) {
1096: e = ge;
1097: }
1098: assertNull(msg, e);
1099:
1100: msg = "Retrieving members from " + parent; // parent should still have 3 group members.
1101: print(msg);
1102: list = getGroupMembers(parent);
1103: assertEquals(msg, 3, list.size());
1104:
1105: // Test adding a group member with a different type:
1106: msg = "Adding an entity of different type to " + parent;
1107: print(msg);
1108: try {
1109: parent.addMember(ipersonEntity);
1110: parent.update(); // members not cached.
1111: e = null;
1112: } catch (GroupsException ge) {
1113: e = ge;
1114: }
1115: assertNotNull(msg, e);
1116:
1117: msg = "Retrieving members from " + parent; // parent should still have 3 group members.
1118: print(msg);
1119: list = getGroupMembers(parent);
1120: assertEquals(msg, 3, list.size());
1121:
1122: // Test adding a circular reference.
1123: try {
1124: child.addMember(parent);
1125: child.update(); // members not cached.
1126: e = null;
1127: } catch (GroupsException ge) {
1128: e = ge;
1129: }
1130: assertNotNull(msg, e);
1131:
1132: msg = "Retrieving members from " + child; // child should have 0 members.
1133: print(msg);
1134: list = getGroupMembers(child);
1135: assertEquals(msg, 0, list.size());
1136:
1137: print(CR
1138: + "***** LEAVING GroupsTester.testGroupMemberValidation() *****"
1139: + CR);
1140:
1141: }
1142:
1143: /**
1144: */
1145: public void testRetrieveParentGroups() throws Exception {
1146: print(CR
1147: + "***** ENTERING GroupsTester.testRetrieveParentGroups() *****"
1148: + CR);
1149: String msg = null;
1150: int numAllGroups = 10;
1151: int numContainingGroups = 8;
1152:
1153: IEntityGroup[] allGroups = new IEntityGroup[numAllGroups];
1154: IEntity testEntity = testEntities[0];
1155: Iterator it = null;
1156: Collection list = null;
1157: int idx = 0;
1158:
1159: msg = "Creating " + numAllGroups + " new groups...";
1160: print(msg);
1161: for (idx = 0; idx < numAllGroups; idx++) {
1162: allGroups[idx] = getNewGroup();
1163: assertNotNull(msg, allGroups[idx]);
1164: allGroups[idx].setName("Parent Group " + idx);
1165: allGroups[idx].setCreatorID("de3");
1166: allGroups[idx].update();
1167: print("Group " + allGroups[idx].getName() + " created.");
1168: }
1169: msg = numAllGroups + " new groups created";
1170: print(msg);
1171:
1172: msg = "Adding " + testEntity + " to " + numContainingGroups
1173: + " containing groups.";
1174: print(msg);
1175: for (idx = 0; idx < numContainingGroups; idx++) {
1176: allGroups[idx].addMember(testEntity);
1177: allGroups[idx].update();
1178: }
1179:
1180: msg = "Getting containing groups for " + testEntity;
1181: print(msg);
1182: list = new ArrayList();
1183: for (it = testEntity.getContainingGroups(); it.hasNext();) {
1184: list.add(it.next());
1185: }
1186: assertEquals(msg, numContainingGroups, list.size());
1187:
1188: msg = "Adding parents to the immediate containing groups.";
1189: print(msg);
1190: for (idx = numContainingGroups; idx < numAllGroups; idx++) {
1191: IEntityGroup parent = allGroups[idx];
1192: IEntityGroup child = allGroups[idx - 1];
1193: msg = "Adding " + child + " to " + parent;
1194: print(msg);
1195: parent.addMember(child);
1196: parent.update();
1197: }
1198:
1199: msg = "Getting ALL containing groups for " + testEntity;
1200: print(msg);
1201: list = new ArrayList();
1202: for (it = testEntity.getAllContainingGroups(); it.hasNext();) {
1203: list.add(it.next());
1204: }
1205: assertEquals(msg, numAllGroups, list.size());
1206:
1207: IEntity duplicateTestEntity = GroupService.getEntity(testEntity
1208: .getKey(), testEntity.getType());
1209: msg = "Getting ALL containing groups for DUPLICATE entity:"
1210: + testEntity;
1211: print(msg);
1212: list = new ArrayList();
1213: for (it = duplicateTestEntity.getAllContainingGroups(); it
1214: .hasNext();) {
1215: list.add(it.next());
1216: }
1217: assertEquals(msg, numAllGroups, list.size());
1218:
1219: print(CR
1220: + "***** LEAVING GroupsTester.testRetrieveParentGroups() *****"
1221: + CR);
1222:
1223: }
1224:
1225: /**
1226: */
1227: public void testUpdateLockableGroups() throws Exception {
1228: print(CR
1229: + "***** ENTERING GroupsTester.testUpdateLockableGroups() *****"
1230: + CR);
1231: String msg = null;
1232: Class type = TEST_ENTITY_CLASS;
1233: int totNumGroups = 3;
1234: int totNumEntities = 5;
1235: IEntityGroup[] groups = new IEntityGroup[totNumGroups];
1236: IEntity[] entities = new IEntity[totNumEntities];
1237: IGroupMember[] groupMembers = null;
1238: Iterator itr = null;
1239: ArrayList list = null;
1240: int idx = 0;
1241: boolean testValue = false;
1242: Exception e = null;
1243:
1244: msg = "Creating " + totNumGroups + " new groups.";
1245: print(msg);
1246: for (idx = 0; idx < totNumGroups; idx++) {
1247: groups[idx] = getNewGroup();
1248: groups[idx].update();
1249: assertNotNull(msg, groups[idx]);
1250: groups[idx].update();
1251: }
1252:
1253: msg = "Getting group keys.";
1254: print(msg);
1255: String[] groupKeys = new String[totNumGroups];
1256: for (idx = 0; idx < totNumGroups; idx++) {
1257: groupKeys[idx] = groups[idx].getKey();
1258: }
1259:
1260: msg = "Retrieving lockable group for key " + groupKeys[0];
1261: print(msg);
1262: ILockableEntityGroup lockableGroup1 = findLockableGroup(groupKeys[0]);
1263: testValue = lockableGroup1.getLock().isValid();
1264: assertTrue(msg, testValue);
1265:
1266: msg = "Retrieving a duplicate lockable group for key "
1267: + groupKeys[0] + " (should FAIL)";
1268: print(msg);
1269: try {
1270: ILockableEntityGroup lockableGroup2 = findLockableGroup(groupKeys[0]);
1271: } catch (GroupsException ge) {
1272: e = ge;
1273: }
1274: assertNotNull(msg, e);
1275: e = null;
1276:
1277: msg = "Checking lock of first group";
1278: print(msg);
1279: testValue = lockableGroup1.getLock().isValid();
1280: assertTrue(msg, testValue);
1281:
1282: String oldName = lockableGroup1.getName();
1283: String newName = "NEW GROUP NAME";
1284: msg = "Update name of lockable group but do not commit.";
1285: print(msg);
1286: lockableGroup1.setName(newName);
1287: assertEquals(msg, newName, lockableGroup1.getName());
1288:
1289: msg = "Checking lock of first group";
1290: print(msg);
1291: testValue = lockableGroup1.getLock().isValid();
1292: assertTrue(msg, testValue);
1293:
1294: msg = "Retrieving duplicate group from service; change should NOT be visible.";
1295: print(msg);
1296: IEntityGroup nonLockableGroup = findGroup(groupKeys[0]);
1297: assertEquals(msg, oldName, nonLockableGroup.getName());
1298:
1299: msg = "Checking lock of first group";
1300: print(msg);
1301: testValue = lockableGroup1.getLock().isValid();
1302: assertTrue(msg, testValue);
1303:
1304: msg = "Committing change to lockable group";
1305: print(msg);
1306: lockableGroup1.update();
1307: testValue = lockableGroup1.getLock().isValid();
1308: assertTrue(msg, !testValue);
1309:
1310: msg = "Retrieving duplicate group from service; change should be visible now.";
1311: print(msg);
1312: nonLockableGroup = findGroup(groupKeys[0]);
1313: assertEquals(msg, newName, nonLockableGroup.getName());
1314:
1315: msg = "Attempting to delete old version of group "
1316: + groupKeys[0] + " (should FAIL.)";
1317: print(msg);
1318: try {
1319: lockableGroup1.delete();
1320: } catch (GroupsException ge) {
1321: e = ge;
1322: }
1323: assertNotNull(msg, e);
1324: e = null;
1325:
1326: msg = "Attempting to delete NEW version of group "
1327: + groupKeys[0];
1328: print(msg);
1329: ILockableEntityGroup lockableGroup3 = findLockableGroup(groupKeys[0]);
1330: lockableGroup3.delete();
1331: nonLockableGroup = findGroup(groupKeys[0]);
1332: assertNull(msg, nonLockableGroup);
1333:
1334: print(CR
1335: + "***** LEAVING GroupsTester.testUpdateLockableGroups() *****"
1336: + CR);
1337:
1338: }
1339:
1340: /**
1341: */
1342: public void testUpdateLockableGroupsWithRenewableLock()
1343: throws Exception {
1344: print(CR
1345: + "***** ENTERING GroupsTester.testUpdateLockableGroupsWithRenewableLock() *****"
1346: + CR);
1347: String msg = null;
1348: Class type = TEST_ENTITY_CLASS;
1349: IEntityGroup group = null;
1350: boolean testValue = false;
1351: Exception e = null;
1352: String groupKey = null;
1353:
1354: msg = "Creating new group.";
1355: print(msg);
1356: group = getNewGroup();
1357: group.update();
1358: assertNotNull(msg, group);
1359:
1360: msg = "Getting group key.";
1361: print(msg);
1362: groupKey = group.getKey();
1363:
1364: msg = "Retrieving lockable group for key " + groupKey;
1365: print(msg);
1366: ILockableEntityGroup lockableGroup = findLockableGroup(groupKey);
1367: assertNotNull(msg, lockableGroup);
1368:
1369: msg = "Checking lock of first group";
1370: print(msg);
1371: testValue = lockableGroup.getLock().isValid();
1372: assertTrue(msg, testValue);
1373:
1374: String oldName = lockableGroup.getName();
1375: String newName = "NEW GROUP NAME";
1376: msg = "Updating name of lockable group but not committing.";
1377: print(msg);
1378: lockableGroup.setName(newName);
1379: assertEquals(msg, newName, lockableGroup.getName());
1380:
1381: msg = "Checking lock of first group";
1382: print(msg);
1383: testValue = lockableGroup.getLock().isValid();
1384: assertTrue(msg, testValue);
1385:
1386: msg = "Committing change to lockable group and renewing lock.";
1387: print(msg);
1388: lockableGroup.updateAndRenewLock();
1389: testValue = lockableGroup.getLock().isValid();
1390: assertTrue(msg, testValue);
1391:
1392: msg = "Retrieving duplicate group from service; change should be visible now.";
1393: print(msg);
1394: IEntityGroup nonLockableGroup = findGroup(groupKey);
1395: assertEquals(msg, newName, nonLockableGroup.getName());
1396:
1397: msg = "Update name of lockable group again.";
1398: print(msg);
1399: lockableGroup.setName(oldName);
1400: assertEquals(msg, oldName, lockableGroup.getName());
1401:
1402: msg = "Committing change to lockable group and renewing lock.";
1403: print(msg);
1404: lockableGroup.updateAndRenewLock();
1405: testValue = lockableGroup.getLock().isValid();
1406: assertTrue(msg, testValue);
1407:
1408: msg = "Attempting to delete lockable group " + groupKey;
1409: print(msg);
1410: lockableGroup.delete();
1411: nonLockableGroup = findGroup(groupKey);
1412: assertNull(msg, nonLockableGroup);
1413:
1414: print(CR
1415: + "***** LEAVING GroupsTester.testUpdateLockableGroupsWithRenewableLock() *****"
1416: + CR);
1417:
1418: }
1419:
1420: /**
1421: */
1422: public void testUpdateMembersVisibility() throws Exception {
1423: print(CR
1424: + "***** ENTERING GroupsTester.testUpdateMembersVisibility() *****"
1425: + CR);
1426: String msg = null;
1427: Class type = TEST_ENTITY_CLASS;
1428: int totNumGroups = 3;
1429: int totNumEntities = 5;
1430: IEntityGroup[] groups = new IEntityGroup[totNumGroups];
1431: IEntity[] entities = new IEntity[totNumEntities];
1432: IGroupMember[] groupMembers = null;
1433: Iterator itr = null;
1434: ArrayList list = null;
1435: int idx = 0;
1436: boolean testValue = false;
1437:
1438: msg = "Creating " + totNumGroups + " new groups.";
1439: print(msg);
1440: for (idx = 0; idx < totNumGroups; idx++) {
1441: groups[idx] = getNewGroup();
1442: groups[idx].getMembers(); // cache members from now on.
1443: assertNotNull(msg, groups[idx]);
1444: }
1445: IEntityGroup rootGroup = groups[0];
1446: IEntityGroup childGroup = groups[1];
1447:
1448: msg = "Adding " + (totNumGroups - 1) + " to root group.";
1449: print(msg);
1450: for (idx = 1; idx < totNumGroups; idx++) {
1451: rootGroup.addMember(groups[idx]);
1452: }
1453:
1454: msg = "Retrieving members from root group.";
1455: print(msg);
1456: list = new ArrayList();
1457: for (itr = rootGroup.getMembers(); itr.hasNext();) {
1458: list.add(itr.next());
1459: }
1460: assertEquals(msg, (totNumGroups - 1), list.size());
1461:
1462: msg = "Adding " + (totNumEntities - 2) + " to root group.";
1463: print(msg);
1464: for (idx = 0; idx < (totNumEntities - 2); idx++) {
1465: rootGroup.addMember(testEntities[idx]);
1466: }
1467:
1468: msg = "Retrieving members from root group.";
1469: print(msg);
1470: list = new ArrayList();
1471: for (itr = rootGroup.getMembers(); itr.hasNext();) {
1472: list.add(itr.next());
1473: }
1474: assertEquals(msg, (totNumGroups - 1 + totNumEntities - 2), list
1475: .size());
1476:
1477: msg = "Adding 2 entities to child group.";
1478: print(msg);
1479: childGroup.addMember(testEntities[totNumEntities - 1]);
1480: childGroup.addMember(testEntities[totNumEntities]);
1481:
1482: msg = "Retrieving ALL members from root group.";
1483: print(msg);
1484: list = new ArrayList();
1485: for (itr = rootGroup.getAllMembers(); itr.hasNext();) {
1486: list.add(itr.next());
1487: }
1488: assertEquals(msg, (totNumGroups - 1 + totNumEntities), list
1489: .size());
1490:
1491: // At this point, the child group members should not yet be aware of their parents.
1492: msg = "Checking child groups for parents (should be none).";
1493: print(msg);
1494: list = new ArrayList();
1495: for (idx = 1; idx < totNumGroups; idx++) {
1496: for (itr = groups[idx].getContainingGroups(); itr.hasNext();) {
1497: list.add(itr.next());
1498: }
1499: assertEquals(msg, 0, list.size());
1500: }
1501:
1502: testValue = testEntities[0].isMemberOf(rootGroup);
1503: assertEquals(msg, false, testValue);
1504:
1505: // Update the parent group. Its children should now be aware of it.
1506: msg = "Updating parent group.";
1507: print(msg);
1508: rootGroup.update();
1509:
1510: msg = "Checking child entity for membership in parent.";
1511: print(msg);
1512: testValue = testEntities[0].isMemberOf(rootGroup);
1513: assertEquals(msg, true, testValue);
1514:
1515: // Child group not yet updated. Its child should still be unaware of it.
1516: msg = "Checking child entity for membership in child group.";
1517: print(msg);
1518: testValue = testEntities[totNumEntities].isMemberOf(childGroup);
1519: assertEquals(msg, false, testValue);
1520:
1521: // Update the child group. Its children should now be aware of it.
1522: msg = "Updating child group.";
1523: print(msg);
1524: childGroup.update();
1525:
1526: msg = "Checking child entity for membership in child group.";
1527: print(msg);
1528: testValue = testEntities[totNumEntities].isMemberOf(childGroup);
1529: assertEquals(msg, true, testValue);
1530:
1531: msg = "Getting child entity thru the service (should be cached copy).";
1532: print(msg);
1533: EntityIdentifier entID = testEntities[totNumEntities]
1534: .getUnderlyingEntityIdentifier();
1535: IGroupMember ent = GroupService.getGroupMember(entID);
1536: msg = "Checking child entity for membership in child group.";
1537: print(msg);
1538: testValue = ent.isMemberOf(childGroup);
1539: assertEquals(msg, true, testValue);
1540:
1541: // Child entity should now be aware of both of its parents.
1542: msg = "Checking child entity for ALL containing groups.";
1543: print(msg);
1544: list = new ArrayList();
1545: for (itr = ent.getAllContainingGroups(); itr.hasNext();) {
1546: list.add(itr.next());
1547: }
1548: assertEquals(msg, 2, list.size());
1549:
1550: print(CR
1551: + "***** LEAVING GroupsTester.testUpdateMembersVisibility() *****"
1552: + CR);
1553:
1554: }
1555:
1556: /**
1557: */
1558: public void testMixLockableAndNonLockableGroups() throws Exception {
1559: print(CR
1560: + "***** ENTERING GroupsTester.testMixLockableAndNonLockableGroups() *****"
1561: + CR);
1562: String msg = null;
1563: Class type = TEST_ENTITY_CLASS;
1564: int totNumGroups = 3;
1565: IEntityGroup[] groups = new IEntityGroup[totNumGroups];
1566: boolean testValue = false;
1567: Exception e = null;
1568: int idx = 0;
1569:
1570: msg = "Creating " + totNumGroups + " new groups.";
1571: print(msg);
1572: for (idx = 0; idx < totNumGroups; idx++) {
1573: groups[idx] = getNewGroup();
1574: groups[idx].update();
1575: assertNotNull(msg, groups[idx]);
1576: groups[idx].update();
1577: }
1578:
1579: msg = "Getting group keys.";
1580: print(msg);
1581: String[] groupKeys = new String[totNumGroups];
1582: for (idx = 0; idx < totNumGroups; idx++) {
1583: groupKeys[idx] = groups[idx].getKey();
1584: }
1585:
1586: msg = "Retrieving nonLockable group " + groupKeys[0];
1587: print(msg);
1588: IEntityGroup group1 = findGroup(groupKeys[0]);
1589: assertNotNull(msg, group1);
1590:
1591: msg = "Retrieving lockable group for key " + groupKeys[0];
1592: print(msg);
1593: ILockableEntityGroup lockableGroup = findLockableGroup(groupKeys[0]);
1594: testValue = lockableGroup.getLock().isValid();
1595: assertTrue(msg, testValue);
1596:
1597: msg = "Updating lockable group.";
1598: print(msg);
1599: String oldName = lockableGroup.getName();
1600: String newName = "NEW GROUP NAME";
1601: print(msg);
1602: lockableGroup.setName(newName);
1603: lockableGroup.update();
1604:
1605: msg = "Retrieving a second nonLockable group for "
1606: + groupKeys[0];
1607: print(msg);
1608: IEntityGroup group2 = findGroup(groupKeys[0]);
1609: assertNotNull(msg, group2);
1610: assertEquals(msg, newName, group2.getName());
1611:
1612: msg = "Updating second nonLockable group.";
1613: print(msg);
1614: group2.setName(oldName);
1615: group2.update();
1616:
1617: msg = "Retrieving a second lockable group for key "
1618: + groupKeys[0];
1619: print(msg);
1620: ILockableEntityGroup lockableGroup2 = findLockableGroup(groupKeys[0]);
1621: testValue = lockableGroup2.getLock().isValid();
1622: assertTrue(msg, testValue);
1623:
1624: msg = "Updating second lockable group.";
1625: print(msg);
1626: lockableGroup2.setName(newName);
1627: lockableGroup2.update();
1628:
1629: print(CR
1630: + "***** LEAVING GroupsTester.testMixLockableAndNonLockableGroups() *****"
1631: + CR);
1632:
1633: }
1634:
1635: /**
1636: */
1637: public void testConcurrentAccess() throws Exception {
1638: print(CR
1639: + "***** ENTERING GroupsTester.testConcurrentAccess() *****"
1640: + CR);
1641:
1642: String msg = null;
1643: Class type = TEST_ENTITY_CLASS;
1644: int totNumGroups = 3;
1645: int numContainingGroups = totNumGroups - 1;
1646: IEntityGroup[] groups = new IEntityGroup[totNumGroups];
1647: int idx = 0;
1648: int numReadTests = 50;
1649: int numThreads = 10;
1650:
1651: msg = "Creating " + totNumGroups + " new groups.";
1652: print(msg);
1653: for (idx = 0; idx < totNumGroups; idx++) {
1654: groups[idx] = getNewGroup();
1655: groups[idx].update();
1656: assertNotNull(msg, groups[idx]);
1657: groups[idx].update();
1658: }
1659:
1660: IEntityGroup child = groups[0];
1661: msg = "Adding parents to child group " + child.getName();
1662: print(msg);
1663: for (idx = 1; idx < totNumGroups; idx++) {
1664: IEntityGroup parent = groups[idx];
1665: parent.addMember(child);
1666: groups[idx].update();
1667: }
1668:
1669: print("Starting testing Threads.");
1670: Thread[] testers = new Thread[numThreads];
1671: for (idx = 0; idx < numThreads; idx++) {
1672: String id = "" + idx;
1673: GroupsReadTester grt = new GroupsReadTester(id, child,
1674: numReadTests);
1675: testers[idx] = new Thread(grt);
1676: testers[idx].start();
1677: }
1678:
1679: msg = "Adding members to " + child;
1680: print(msg);
1681: for (idx = 0; idx < numTestEntities; idx++) {
1682: IEntity childEntity = testEntities[idx];
1683: child.addMember(childEntity);
1684: if (idx % 10 == 0) // update once for every 10 adds
1685: {
1686: child.update();
1687: }
1688: assertTrue(msg, child.contains(childEntity));
1689: // print("added entity # " + (idx + 1) + " to " + child);
1690: }
1691:
1692: msg = "Updating " + child;
1693: print(msg);
1694: child.update();
1695:
1696: msg = "Removing members from " + child;
1697: print(msg);
1698: for (idx = 0; idx < numTestEntities; idx++) {
1699: IEntity childEntity = testEntities[idx];
1700: child.removeMember(childEntity);
1701: assertTrue(msg, !child.contains(childEntity));
1702: }
1703:
1704: msg = "Updating " + child;
1705: print(msg);
1706: child.update();
1707:
1708: Thread.sleep(numReadTests * 20); // let them die.
1709:
1710: print(CR
1711: + "***** LEAVING GroupsTester.testConcurrentAccess() *****"
1712: + CR);
1713: }
1714:
1715: /**
1716: */
1717: public void testParseCompoundKeys() throws Exception {
1718: print(CR
1719: + "***** ENTERING GroupsTester.testParseCompoundKeys() *****"
1720: + CR);
1721:
1722: String msg = null;
1723: int maxNodes = 5;
1724: int idx = 0;
1725: String[] keys = new String[maxNodes];
1726: String[] nodes = new String[maxNodes];
1727: String key = null;
1728: String sep = GroupServiceConfiguration.getConfiguration()
1729: .getNodeSeparator();
1730: print("GroupServiceConfiguration node separator: " + sep);
1731:
1732: print("Creating random node strings.");
1733: for (idx = 0; idx < maxNodes; idx++) {
1734: nodes[idx] = (getRandomString(random, 3) + idx);
1735: }
1736:
1737: print("Creating keys.");
1738: for (idx = 0; idx < maxNodes; idx++) {
1739: key = nodes[0];
1740: for (int i = 1; i <= idx; i++) {
1741: key = key + sep + nodes[i];
1742: }
1743: keys[idx] = key;
1744: print("key " + idx + " : " + key);
1745: }
1746:
1747: for (idx = 1; idx < maxNodes; idx++) {
1748: CompositeEntityIdentifier cei = null;
1749: msg = "Creating CompositeEntityIdentifier for " + keys[idx];
1750: print(msg);
1751: cei = new CompositeEntityIdentifier(keys[idx], GROUP_CLASS);
1752: assertNotNull(msg, cei);
1753: msg = "Testing COMPOUND key of " + cei;
1754: assertEquals(msg, keys[idx], cei.getKey());
1755: msg = "Testing LOCAL key of " + cei;
1756: assertEquals(msg, nodes[idx], cei.getLocalKey());
1757: msg = "Testing SERVICE NAME of " + cei;
1758: assertEquals(msg, idx, cei.getServiceName().size());
1759: }
1760:
1761: print(CR
1762: + "***** LEAVING GroupsTester.testParseCompoundKeys() *****"
1763: + CR);
1764:
1765: }
1766:
1767: }
|