📄 entitycachetester.java
字号:
package org.jasig.portal.concurrency.caching;import java.util.Date;import junit.framework.TestCase;import junit.framework.TestSuite;import org.jasig.portal.EntityIdentifier;import org.jasig.portal.IBasicEntity;import org.jasig.portal.concurrency.CachingException;import org.jasig.portal.concurrency.IEntityCache;import org.jasig.portal.services.EntityCachingService;/** * Tests the entity caching framework. * @author: Dan Ellentuck * * TESTS: * (1) testEntityServiceAddsAndDeletes() -- Add, retrieve and remove entities * via the service facade. * (2) testEntityCacheAddsAndDeletes() -- Add, retrieve and remove entries * from an IEntityCache. * (3) testEntityCacheSweep() -- Sweep must remove correct number of entries * from cache, based on time of last use. * (4) testInvalidatingCacheAddsAndDeletes() -- Add, retrieve and remove * entries from an invalidating IEntityCache. * (5) testInvalidatingCacheInvalidation() -- Invalidating IEntityCaches must * invalidate each others' entries when their own entries are either * updated or deleted. * (6) testStoreAddsAndDeletes() -- Add and delete invalidations via the * invalidation store. * (7) testStoreBeforeAndAfter() -- Retrieve invalidations from the store * that were added after some point in time. * (8) testStoreUpdates() -- Retrieve invalidations from the store that * were updated after some point in time. * (9) testFudgeFactor() -- An earlier invalidation from one invalidating cache * should invalidate its corresponding entity in another cache IF the * interval is no greater than the fudge factor. But a cache must not * clobber its own entries. * */public class EntityCacheTester extends TestCase { private static Class GROUP_CLASS; private static Class IPERSON_CLASS; private static Class MINIMAL_ENTITY_CLASS; private IBasicEntity[] testEntities; private String[] testEntityKeys; private int numTestEntities = 0; // cache defaults: private int cacheSize = 1000; private int cacheIdleTimeSecs = 5*60; private int cacheSweepIntervalSecs = 30; private int clockTolerance = 5000; private class MinimalEntity implements IBasicEntity { private String key; private MinimalEntity(String entityKey) { super(); key = entityKey; } public EntityIdentifier getEntityIdentifier() { return new EntityIdentifier(getKey(), getType()); } public Class getType() { return this.getClass(); } public String getKey() { return key; } public boolean equals(Object o) { if ( o == null ) return false; if ( ! (o instanceof IBasicEntity) ) return false; IBasicEntity ent = (IBasicEntity) o; return ent.getEntityIdentifier().getType() == getType() && ent.getEntityIdentifier().getKey().equals(getKey()); } public String toString() { return "MinimalEntity(" + key + ")"; } }/** * EntityLockTester constructor comment. */public EntityCacheTester(String name) { super(name);}/** */protected void addTestEntityType(){ try { org.jasig.portal.EntityTypes.singleton(). addEntityTypeIfNecessary(MINIMAL_ENTITY_CLASS, "Test Entity Type"); } catch (Exception ex) { print("EntityCacheTester.addTestEntityType(): " + ex.getMessage());} }/** */protected void deleteTestEntityType(){ try { org.jasig.portal.EntityTypes.singleton().deleteEntityType(MINIMAL_ENTITY_CLASS); } catch (Exception ex) { print("EntityCacheTester.deleteTestEntityType(): " + ex.getMessage());} }/** * @return org.jasig.portal.concurrency.caching.IEntityCache */private IEntityCache getEntityCache() throws CachingException{ return getEntityCache(cacheSize, cacheIdleTimeSecs*1000, cacheSweepIntervalSecs*1000);}/** * @return org.jasig.portal.concurrency.caching.IEntityCache */private IEntityCache getEntityCache(int size, int idleTime, int sweepInterval)throws CachingException{ return new ReferenceEntityCache(MINIMAL_ENTITY_CLASS, size, idleTime, sweepInterval);}/** * @return org.jasig.portal.concurrency.IEntityCache */private IEntityCache getInvalidatingEntityCache() throws CachingException{ return getInvalidatingEntityCache(cacheSize, cacheIdleTimeSecs*1000, cacheSweepIntervalSecs*1000, clockTolerance);}/** * @return org.jasig.portal.concurrency.IEntityCache */private IEntityCache getInvalidatingEntityCache(int size, int idleTime, int sweepInterval, int tolerance)throws CachingException{ return new ReferenceInvalidatingEntityCache(MINIMAL_ENTITY_CLASS, size, idleTime, sweepInterval, tolerance);}/*** @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 RDBMCachedEntityInvalidationStore */private RDBMCachedEntityInvalidationStore getStore() throws CachingException{ return RDBMCachedEntityInvalidationStore.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.caching.EntityCacheTester"}; print("START TESTING CACHE"); printBlankLine(); junit.swingui.TestRunner.main(mainArgs); printBlankLine(); print("END TESTING CACHE");}/** */private static void print (IBasicEntity[] 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 ( MINIMAL_ENTITY_CLASS == null ) { MINIMAL_ENTITY_CLASS = MinimalEntity.class; } addTestEntityType(); getStore().deleteAll(); numTestEntities = 1000; // Entity keys: testEntityKeys = new String[numTestEntities]; java.util.Random random = new java.util.Random(); for (int i=0; i<numTestEntities; i++) { testEntityKeys[i] = (getRandomString(random, 3) + i); } // Entities testEntities = new IBasicEntity[numTestEntities]; for (int i=0; i<numTestEntities; i++) { testEntities[i] = new MinimalEntity(testEntityKeys[i]); } } catch (Exception ex) { print("EntityCacheTester.setUp(): " + ex.getMessage());} }/** * @return junit.framework.Test */public static junit.framework.Test suite() { TestSuite suite = new TestSuite(); suite.addTest(new EntityCacheTester("testIEntityCacheAddsAndDeletes")); suite.addTest(new EntityCacheTester("testStoreAddsAndDeletes")); suite.addTest(new EntityCacheTester("testStoreBeforeAndAfter")); suite.addTest(new EntityCacheTester("testStoreUpdates")); suite.addTest(new EntityCacheTester("testInvalidatingCacheAddsAndDeletes")); suite.addTest(new EntityCacheTester("testInvalidatingCacheInvalidation")); suite.addTest(new EntityCacheTester("testIEntityCacheSweep")); suite.addTest(new EntityCacheTester("testEntityCachingServiceAddsAndDeletes")); suite.addTest(new EntityCacheTester("testFudgeFactor"));// suite.addTest(new EntityCacheTester("testStoreDeleteBefore"));// Add more tests here.// NB: Order of tests is not guaranteed. return suite;}/** */protected void tearDown(){ try { testEntityKeys = null; testEntities = null; deleteTestEntityType(); getStore().deleteAll(); } catch (Exception ex) { print("EntityCacheTester.tearDown(): " + ex.getMessage());}}/** * Adds, retrieves and removes entities via the service facade, * org.jasig.portal.services.EntityCachingServices. * - Adds must be found. * - Removes must not be found. */public void testEntityCachingServiceAddsAndDeletes() throws Exception{ print("***** ENTERING EntityCacheTester.testEntityCachingAddsAndDeletes() *****"); String msg = null; int idx = 0; IBasicEntity ent = null; int adds = 100; Class type = MINIMAL_ENTITY_CLASS; EntityCachingService service = EntityCachingService.instance(); msg = "Adding " + adds + " entities to the cache."; print(msg); for(idx=0; idx<adds; idx++) { service.add(testEntities[idx]); } msg = "Retrieving entities from the cache."; print(msg); for(idx=0; idx<adds; idx++) { ent = service.get(type, testEntityKeys[idx] ); assertEquals(msg, ent, testEntities[idx]); } msg = "Removing entities from the cache."; print(msg); for(idx=0; idx<numTestEntities; idx++) { service.remove( type, testEntityKeys[idx] ); ent = service.get( type, testEntityKeys[idx] ); assertNull(msg, ent); } print("***** LEAVING EntityCacheTester.testEntityCachingServiceAddsAndDeletes() *****");}/** * Gets an instance of IEntityCache and adds, retrieves and removes * entities directly from the cache. * * - After adds, cache size must equal number of adds. * - Adds must be correctly retrieved. * - Removes must not be found. * - After removes, cache size must be 0. */public void testIEntityCacheAddsAndDeletes() throws Exception{ print("***** ENTERING EntityCacheTester.testIEntityCacheAddsAndDeletes() *****"); String msg = null; int idx = 0; IBasicEntity ent = null; IEntityCache c = getEntityCache(); ReferenceEntityCache rec = (ReferenceEntityCache) c; msg = "Adding " + numTestEntities + " entities to the cache."; print(msg); for(idx=0; idx<numTestEntities; idx++) { c.add(testEntities[idx]); } assertEquals(msg, rec.size(), numTestEntities); msg = "Retrieving entities from the cache."; print(msg); for(idx=0; idx<numTestEntities; idx++) { ent = c.get( testEntityKeys[idx] ); assertEquals(msg, ent, testEntities[idx]); } msg = "Removing entities from the cache."; print(msg); for(idx=0; idx<numTestEntities; idx++) { c.remove( testEntityKeys[idx] ); ent = c.get( testEntityKeys[idx] ); assertNull(msg, ent); } // We should have removed all entries. assertEquals(msg, rec.size(), 0); print("***** LEAVING EntityCacheTester.testIEntityCacheAddsAndDeletes() *****");}/** * Creates an IEntityCache and adds [firstAdded] number of entities.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -