📄 entitylocktester.java
字号:
package org.jasig.portal.concurrency.locking;import junit.framework.TestCase;import junit.framework.TestSuite;import org.jasig.portal.concurrency.IEntityLock;import org.jasig.portal.concurrency.IEntityLockService;import org.jasig.portal.concurrency.LockingException;import org.jasig.portal.properties.PropertiesManager;import org.jasig.portal.services.EntityLockService;/** * Tests the entity lock framework. * @author: Dan Ellentuck */public class EntityLockTester extends TestCase { private static Class GROUP_CLASS; private static Class IPERSON_CLASS; private IEntityLock[] testLocks; private IEntityLockStore lockStore; private int numUnexpiredIEntityGroupLocksInStore = 0; private int numExpiredLocks = 0; private int numUnexpiredIPersonLocksInStore = 0; private int numIPersonLocksInStoreForTestId = 0; private String[] testKeys = {"101", "102", "9999999", "12345"}; private String[] testIds = {"de3", "df7", "av317"};/** * EntityLockTester constructor comment. */public EntityLockTester(String name) { super(name);}/** * @return org.jasig.portal.concurrency.locking.IEntityLockStore */private IEntityLockStore getLockStore() { return lockStore;}/** * @return org.jasig.portal.groups.IEntityLockService */private IEntityLockService getService() throws LockingException{ return ReferenceEntityLockService.singleton();}/** * 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.locking.EntityLockTester"}; print("START TESTING LOCK STORE"); printBlankLine(); junit.swingui.TestRunner.main(mainArgs); printBlankLine(); print("END TESTING LOCK STORE");}/** */private static void print (IEntityLock[] locks){ for ( int i=0; i<locks.length; i++ ) { print("(" + (i+1) + ") " + locks[i]); } print(" Total: " + locks.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"); } try { boolean multiServer = PropertiesManager.getPropertyAsBoolean("org.jasig.portal.concurrency.multiServer"); lockStore = ( multiServer ) ? RDBMEntityLockStore.singleton() : MemoryEntityLockStore.singleton(); } catch ( Exception e ) { System.out.println("EntityLockTester:setUp(): Failed to instantiate entity lock store. " + e); } lockStore.deleteAll(); java.util.Date earlier = new java.util.Date(System.currentTimeMillis() - 1000); // - 1 second java.util.Date later = new java.util.Date(System.currentTimeMillis() + 300000); // + 5 minutes testLocks = new IEntityLock[5]; testLocks[0] = new EntityLockImpl(GROUP_CLASS, testKeys[0], 0, later, testIds[0]); testLocks[1] = new EntityLockImpl(GROUP_CLASS, testKeys[1], 0, later, testIds[0]); testLocks[2] = new EntityLockImpl(IPERSON_CLASS, testKeys[0], 0, earlier, testIds[0]); testLocks[3] = new EntityLockImpl(IPERSON_CLASS, testKeys[1], 1, later, testIds[0]); testLocks[4] = new EntityLockImpl(IPERSON_CLASS, testKeys[2], 1, later, testIds[1]); print("Adding test locks."); for (int i=0; i<testLocks.length; i++) { getLockStore().add(testLocks[i]); // print("Added " + testLocks[i]); } numUnexpiredIEntityGroupLocksInStore = 2; numExpiredLocks = 1; numUnexpiredIPersonLocksInStore = 2; numIPersonLocksInStoreForTestId = 1; } catch (Exception ex) { print("EntityLockTester.setUp(): " + ex.getMessage());} }/** * @return junit.framework.Test */public static junit.framework.Test suite() { TestSuite suite = new TestSuite(); suite.addTest(new EntityLockTester("testExistsInStore")); suite.addTest(new EntityLockTester("testSelectFromStore")); suite.addTest(new EntityLockTester("testExpirationInStore")); suite.addTest(new EntityLockTester("testStoreUpdate")); suite.addTest(new EntityLockTester("testServiceNewLock")); suite.addTest(new EntityLockTester("testServiceLockRenewal")); suite.addTest(new EntityLockTester("testServiceConvert")); suite.addTest(new EntityLockTester("testService"));// Add more tests here.// NB: Order of tests is not guaranteed. return suite;}/** */protected void tearDown(){ try { // delete any remaining test locks. print("Deleting test locks."); for (int i=0; i<testLocks.length; i++) { getLockStore().delete(testLocks[i]); } } catch (Exception ex) { print("EntityLockTester.tearDown(): " + ex.getMessage());}}/** */public void testExistsInStore() throws Exception{ String msg = null; IEntityLock lock = testLocks[4]; msg = "Checking if " + lock + " exists in database."; print(msg); boolean exists = getService().existsInStore(lock); assertTrue(msg, exists); // Delete the lock: print("Deleting lock from database."); getLockStore().delete(lock); // does lock exist? msg = "Checking if deleted lock exists in database."; print(msg); exists = getService().existsInStore(lock); assertTrue(msg, ! exists); // Add the lock back: print("Adding back the lock just deleted from database."); getLockStore().add(lock);}/** */public void testExpirationInStore() throws Exception{ int numLocks = 0; int ctr = 0; String msg = null; IEntityLock[] selectedLocks = null; java.util.Date now = new java.util.Date(System.currentTimeMillis()); // select unexpired locks by entity type msg = "Selecting unexpired locks by type."; print(msg); selectedLocks = getLockStore().findUnexpired(now, IPERSON_CLASS, null, null, null); numLocks = selectedLocks.length; assertEquals(numLocks, numUnexpiredIPersonLocksInStore); for (ctr=0; ctr<numLocks; ctr++) { assertTrue(msg, selectedLocks[ctr].getExpirationTime().after(now)); } // Get rid of the EXPIRED locks. print("Deleting expired locks."); getLockStore().deleteExpired(new java.util.Date(System.currentTimeMillis())); // Select the remaining UNEXPIRED locks. msg = "Selected all remaining locks."; print(msg); selectedLocks = getLockStore().find(null, null, null, null, null); numLocks = selectedLocks.length; msg = "Selected " + numLocks + " unexpired locks"; print(msg); assertEquals(msg, testLocks.length - numExpiredLocks, numLocks); for (ctr=0; ctr<numLocks; ctr++) { assertTrue(msg, selectedLocks[ctr].getExpirationTime().after(now)); }}/** */public void testSelectFromStore() throws Exception{ int numLocks = 0; int ctr = 0; IEntityLock[] selectedLocks = null; String msg = null; java.util.Date now = new java.util.Date(System.currentTimeMillis()); // select locks by entity type
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -