📄 groupstester.java
字号:
package org.jasig.portal.groups;import java.sql.Connection;import java.sql.ResultSet;import java.sql.Statement;import java.util.ArrayList;import java.util.Collection;import java.util.Iterator;import java.util.Random;import junit.framework.TestCase;import junit.framework.TestSuite;import org.jasig.portal.EntityIdentifier;import org.jasig.portal.EntityTypes;import org.jasig.portal.IBasicEntity;import org.jasig.portal.concurrency.CachingException;import org.jasig.portal.concurrency.caching.ReferenceEntityCachingService;import org.jasig.portal.security.IPerson;import org.jasig.portal.services.GroupService;/** * Tests the groups framework (a start). * @author: Dan Ellentuck */public class GroupsTester extends TestCase { private static Class GROUP_CLASS; private static Class IPERSON_CLASS; private static Class TEST_ENTITY_CLASS; private static String CR = "\n"; private IEntity[] testEntities; private String[] testEntityKeys; private int numTestEntities = 0; private Random random = new Random(); private class TestEntity implements IBasicEntity { private EntityIdentifier entityIdentifier; private TestEntity(String entityKey) { super(); entityIdentifier = new EntityIdentifier(entityKey, this.getClass()); } public EntityIdentifier getEntityIdentifier() { return entityIdentifier; } public boolean equals(Object o) { if ( o == null ) return false; if ( ! (o instanceof IBasicEntity) ) return false; IBasicEntity ent = (IBasicEntity) o; return ent.getEntityIdentifier().equals(getEntityIdentifier()); } public String toString() { return "TestEntity(" + getEntityIdentifier().getKey() + ")"; } } private class GroupsReadTester implements Runnable { protected IEntityGroup group = null; protected int numTests = 0; protected String testerID = null; protected String printID = null; protected GroupsReadTester(String id, IEntityGroup g, int tests) { super(); group = g; numTests = tests; testerID = id; } public void run() { printID = "Tester " + testerID; print(printID + " starting."); for (int i=0; i<numTests; i++) { // print(printID + " running test # " + (i+1)); try { runTest(); } catch (GroupsException ge) {} int sleepMillis = random.nextInt(20); // print(printID + " will now sleep for " + sleepMillis + " ms."); try { Thread.sleep(sleepMillis); } catch (Exception ex) {} } } private void runTest() throws GroupsException { int numMembers = 0, numEntities = 0, numContainingGroups = 0; Iterator itr = null; for (itr = group.getMembers(); itr.hasNext(); itr.next() ) { numMembers++; } for (itr = group.getEntities(); itr.hasNext(); itr.next() ) { numEntities++; } for (itr = group.getContainingGroups(); itr.hasNext(); itr.next() ) { numContainingGroups++; }// print (printID + " members: " + numMembers + " entities: " + numEntities + " containing groups: " + numContainingGroups); } } /** * EntityLockTester constructor comment. */public GroupsTester(String name) { super(name);}/** */protected void addTestEntityType(){ try { org.jasig.portal.EntityTypes.singleton(). addEntityTypeIfNecessary(TEST_ENTITY_CLASS, "Test Entity Type"); } catch (Exception ex) { print("GroupsTester.addTestEntityType(): " + ex.getMessage());} }/** * */private void clearGroupCache() throws CachingException{ ((ReferenceEntityCachingService) ReferenceEntityCachingService.singleton()) .getCache(GROUP_CLASS).clearCache();}/** */protected void deleteTestEntityType(){ try { org.jasig.portal.EntityTypes.singleton().deleteEntityType(TEST_ENTITY_CLASS); } catch (Exception ex) { print("EntityCacheTester.deleteTestEntityType(): " + ex.getMessage());} }/** */protected void deleteTestGroups(){ String sql = " FROM UP_GROUP WHERE ENTITY_TYPE_ID = " + EntityTypes.getEntityTypeID(TEST_ENTITY_CLASS); String selectSql = "SELECT GROUP_ID" + sql; String deleteSql = "DELETE" + sql; String deleteMemberSql = "DELETE FROM UP_GROUP_MEMBERSHIP WHERE GROUP_ID = "; Connection conn = null; try { conn = org.jasig.portal.RDBMServices.getConnection(); Statement selectStmnt = conn.createStatement(); ResultSet rs = selectStmnt.executeQuery( selectSql ); while ( rs.next() ) { String key = rs.getString(1); Statement deleteMemberStmnt = conn.createStatement(); int memberRC = deleteMemberStmnt.executeUpdate( deleteMemberSql + "'" + key + "'" ); print("Test member rows deleted: " + memberRC); } Statement deleteGroupStmnt = conn.createStatement(); int rc = deleteGroupStmnt.executeUpdate( deleteSql ); print("Test group rows deleted: " + rc); } catch (Exception ex) { print("GroupsTester.deleteTestGroups(): " + ex.getMessage());} finally { org.jasig.portal.RDBMServices.releaseConnection(conn); } }/** * @return org.jasig.portal.groups.IEntityGroup */private IEntityGroup findGroup(String key) throws GroupsException{ IEntityGroup group = GroupService.findGroup(key); return group;}/** * @return org.jasig.portal.groups.ILockableEntityGroup */private ILockableEntityGroup findLockableGroup(String key) throws GroupsException{ String owner = "de3"; ILockableEntityGroup group = GroupService.findLockableGroup(key, owner); return group;}/** * @return org.jasig.portal.services.GroupService */private Collection getAllGroupMembers(IGroupMember gm) throws GroupsException{ Collection list = new ArrayList(); for( Iterator itr=gm.getAllMembers(); itr.hasNext(); ) { list.add(itr.next()); } return list;}/** * @return RDBMEntityStore */private IEntityStore getEntityStore() throws GroupsException{ return RDBMEntityStore.singleton();}/** * @return org.jasig.portal.services.GroupService */private Collection getGroupMembers(IGroupMember gm) throws GroupsException{ Collection list = new ArrayList(); for( Iterator itr=gm.getMembers(); itr.hasNext(); ) { list.add(itr.next()); } return list;}/** * @return RDBMEntityGroupStore */private RDBMEntityGroupStore getGroupStore() throws GroupsException{ return RDBMEntityGroupStore.singleton();}/** * @return org.jasig.portal.groups.IEntity */private IEntity getNewEntity(String key) throws GroupsException{ return GroupService.getEntity(key, TEST_ENTITY_CLASS);}/** * @return org.jasig.portal.groups.IEntityGroup */private IEntityGroup getNewGroup() throws GroupsException{ IEntityGroup group = GroupService.newGroup(TEST_ENTITY_CLASS); group.setName("name_" + group.getKey()); group.setCreatorID("de3"); return group;}/*** @return java.lang.String * @param length int */private String getRandomString(java.util.Random r, int length) { char[] chars = new char[length]; for(int i=0; i<length; i++) { int diff = ( r.nextInt(25) ); int charValue = (int)'A' + diff; chars[i] = (char) charValue; } return new String(chars);}/** * @return org.jasig.portal.services.GroupService */private GroupService getService() throws GroupsException{ return GroupService.instance();}/** * Starts the application. * @param args an array of command-line arguments */public static void main(java.lang.String[] args) throws Exception{ String[] mainArgs = {"org.jasig.portal.concurrency.caching.EntityCacheTester"}; print("START TESTING CACHE"); printBlankLine(); junit.swingui.TestRunner.main(mainArgs); printBlankLine(); print("END TESTING CACHE");}/** */private static void print (IEntity[] entities){ for ( int i=0; i<entities.length; i++ ) { print("(" + (i+1) + ") " + entities[i]); } print(" Total: " + entities.length);}/** * @param msg java.lang.String */private static void print(String msg){ java.sql.Timestamp ts = new java.sql.Timestamp(System.currentTimeMillis()); System.out.println(ts + " : " + msg);}private static void printBlankLine(){ System.out.println("");}/** */protected void setUp(){ try { if ( GROUP_CLASS == null ) { GROUP_CLASS = Class.forName("org.jasig.portal.groups.IEntityGroup"); } if ( IPERSON_CLASS == null ) { IPERSON_CLASS = Class.forName("org.jasig.portal.security.IPerson"); } if ( TEST_ENTITY_CLASS == null ) { TEST_ENTITY_CLASS = TestEntity.class; } addTestEntityType(); numTestEntities = 100; // Entities and their keys: testEntityKeys = new String[numTestEntities]; testEntities = new IEntity[numTestEntities]; for (int i=0; i<numTestEntities; i++) { testEntityKeys[i] = (getRandomString(random, 3) + i); testEntities[i] = getNewEntity(testEntityKeys[i]); } } catch (Exception ex) { print("GroupsTester.setUp(): " + ex.getMessage());} }/** * @return junit.framework.Test */public static junit.framework.Test suite() { TestSuite suite = new TestSuite(); suite.addTest(new GroupsTester("testAddAndDeleteGroups")); suite.addTest(new GroupsTester("testAddAndDeleteMembers")); suite.addTest(new GroupsTester("testGroupMemberValidation")); suite.addTest(new GroupsTester("testGroupMemberUpdate")); suite.addTest(new GroupsTester("testRetrieveParentGroups")); suite.addTest(new GroupsTester("testUpdateMembersVisibility")); suite.addTest(new GroupsTester("testUpdateLockableGroups")); suite.addTest(new GroupsTester("testUpdateLockableGroupsWithRenewableLock")); suite.addTest(new GroupsTester("testContains")); suite.addTest(new GroupsTester("testDeleteChildGroup")); suite.addTest(new GroupsTester("testMixLockableAndNonLockableGroups")); suite.addTest(new GroupsTester("testConcurrentAccess")); suite.addTest(new GroupsTester("testParseCompoundKeys")); suite.addTest(new GroupsTester("testPagsContains")); suite.addTest(new GroupsTester("testAddToALargeGroup"));// Add more tests here.// NB: Order of tests is not guaranteed. return suite;}/** */protected void tearDown(){ try { testEntityKeys = null; testEntities = null; deleteTestGroups(); deleteTestEntityType(); clearGroupCache(); } catch (Exception ex) { print("GroupTester.tearDown(): " + ex.getMessage());}}/** */public void testAddAndDeleteGroups() throws Exception{ print(CR + "***** ENTERING GroupsTester.testAddAndDeleteGroups() *****" + CR); String msg = null; msg = "Creating a new IEntityGroup."; print(msg); IEntityGroup newGroup = getNewGroup(); assertNotNull(msg, newGroup); print("Now updating " + newGroup); newGroup.setName("Test"); newGroup.setCreatorID("de3"); newGroup.update(); print("Now retrieving group just created from the store."); String key = newGroup.getKey(); IEntityGroup retrievedGroup = GroupService.findGroup(key); msg = "Testing retrieved group."; print(msg); assertEquals(msg, newGroup, retrievedGroup); print("Now deleting group just created from the store."); retrievedGroup.delete(); print("Attempting to retrieve deleted group from the store."); retrievedGroup = GroupService.findGroup(key); assertNull(msg, retrievedGroup); print(CR + "***** LEAVING GroupsTester.testAddAndDeleteGroups() *****" + CR);}/** */public void testAddToALargeGroup() throws Exception{ print(CR + "***** ENTERING GroupsTester.testAddToALargeGroup() *****" + CR); int numEntities = 1000; String msg = null; int idx = 0; String[] entityKeys = new String[numEntities]; IEntity[] entities = new IEntity[numEntities]; print("About to create test entities."); for (int i=0; i<numEntities; i++)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -