1 23 24 package com.rift.coad.lib.cache; 25 26 import junit.framework.*; 27 import java.util.Iterator ; 28 import java.util.Map ; 29 import java.util.HashMap ; 30 import com.rift.coad.lib.thread.ThreadStateMonitor; 31 32 36 public class KeySyncCacheManagerTest extends TestCase { 37 38 public KeySyncCacheManagerTest(String testName) { 39 super(testName); 40 } 41 42 protected void setUp() throws Exception { 43 } 44 45 protected void tearDown() throws Exception { 46 } 47 48 public static Test suite() { 49 TestSuite suite = new TestSuite(KeySyncCacheManagerTest.class); 50 51 return suite; 52 } 53 54 55 58 public void testKeySyncCacheManager() throws Exception { 59 System.out.println("KeySyncCacheManager"); 60 61 Object identifier = null; 62 KeySyncCacheManager instance = new KeySyncCacheManager(); 63 64 KeySyncCache keySyncCache1 = instance.getKeySyncCache("key1"); 65 KeySyncCache keySyncCache2 = instance.getKeySyncCache("key2"); 66 assertEquals(keySyncCache1, instance.getKeySyncCache("key1")); 67 assertEquals(keySyncCache2, instance.getKeySyncCache("key2")); 68 69 KeySyncCache.KeySync fred = keySyncCache1.getKeySync("fred"); 70 KeySyncCache.KeySync bob = keySyncCache1.getKeySync("bob"); 71 KeySyncCache.KeySync mary = keySyncCache2.getKeySync("mary"); 72 KeySyncCache.KeySync jill = keySyncCache2.getKeySync("jill"); 73 assertEquals(fred, keySyncCache1.getKeySync("fred")); 74 assertEquals(bob, keySyncCache1.getKeySync("bob")); 75 assertEquals(mary, keySyncCache2.getKeySync("mary")); 76 assertEquals(jill, keySyncCache2.getKeySync("jill")); 77 78 for (int count = 0; count < 4; count++) { 79 Thread.sleep(500); 80 fred.touch(); 81 mary = keySyncCache2.getKeySync("mary"); 82 } 83 84 instance.garbageCollect(); 85 86 assertEquals(fred, keySyncCache1.getKeySync("fred")); 87 assertEquals(mary, keySyncCache2.getKeySync("mary")); 88 if (bob == keySyncCache1.getKeySync("bob")) { 89 fail("The entry should not have been found"); 90 } 91 if (jill == keySyncCache2.getKeySync("jill")) { 92 fail("The entry should not have been found"); 93 } 94 95 instance.clear(); 96 97 if (keySyncCache1.contains("fred")) { 98 fail("The instance still contains fred"); 99 } 100 if (keySyncCache2.contains("mary")) { 101 fail("The instance still contains mary"); 102 } 103 104 try { 105 instance.getKeySyncCache("key1"); 106 fail("Should not have been able to retrieve a sync instance"); 107 } catch (CacheException ex) { 108 } 110 } 111 112 } 113 | Popular Tags |