| 1 23 24 package com.rift.coad.lib.bean; 25 26 import junit.framework.*; 27 import java.util.Date ; 28 import java.util.Map ; 29 import java.util.HashMap ; 30 import java.util.Iterator ; 31 import com.rift.coad.lib.cache.Cache; 32 import com.rift.coad.lib.cache.CacheEntry; 33 34 38 public class BeanCacheManagerTest extends TestCase { 39 40 43 public static class TestCacheEntry implements CacheEntry, java.rmi.Remote { 44 45 private Date touchTime = new Date (); 47 public static int count = 0; 48 49 52 public TestCacheEntry() { 53 54 } 55 56 57 60 public void touch() { 61 touchTime = new Date (); 62 } 63 64 65 71 public boolean isExpired(Date expiryDate) { 72 System.out.println("Touch time : " + touchTime.getTime() + 73 " expiry date : " + expiryDate.getTime()); 74 return touchTime.getTime() < expiryDate.getTime(); 75 } 76 77 78 81 public void cacheRelease() { 82 count++; 83 } 84 } 85 86 public BeanCacheManagerTest(String testName) { 87 super(testName); 88 } 89 90 protected void setUp() throws Exception { 91 } 92 93 protected void tearDown() throws Exception { 94 } 95 96 public static Test suite() { 97 TestSuite suite = new TestSuite(BeanCacheManagerTest.class); 98 99 return suite; 100 } 101 102 105 public void testBeanCacheManager() throws Exception { 106 System.out.println("BeanCacheManager"); 107 108 Object ref = null; 109 BeanCacheManager instance = new BeanCacheManager(); 110 111 BeanCache beanCache1 = instance.getBeanCache("beanCache1"); 112 BeanCache beanCache2 = instance.getBeanCache("beanCache2"); 113 if (instance.getBeanCache("beanCache1") != beanCache1) { 114 fail("Failed to retrieve the bean cache result object"); 115 } 116 if (!instance.contains("beanCache1")) { 117 fail("The instance does not contain beanCache1"); 118 } 119 if (instance.getBeanCache("beanCache2") != beanCache2) { 120 fail("Failed to retrieve the bean cache 2 result object"); 121 } 122 if (!instance.contains("beanCache2")) { 123 fail("The instance does not contain beanCache2"); 124 } 125 126 TestCacheEntry bob = new TestCacheEntry(); 127 beanCache1.addCacheEntry(500,"bob","bob",bob); 128 TestCacheEntry fred = new TestCacheEntry(); 129 beanCache1.addCacheEntry(500,"fred","fred",fred); 130 TestCacheEntry mary = new TestCacheEntry(); 131 beanCache2.addCacheEntry(500,"mary","mary","mary",mary); 132 TestCacheEntry jill = new TestCacheEntry(); 133 beanCache2.addCacheEntry(500,"jill","jill","jill",jill); 134 135 for (int count = 0; count < 4; count++) { 136 Thread.sleep(500); 137 bob.touch(); 138 mary.touch(); 139 } 140 141 instance.garbageCollect(); 142 143 if (!beanCache1.contains("bob")) { 144 fail("bob could not be found"); 145 } 146 if (!beanCache2.contains("mary")) { 147 fail("mary could not be found"); 148 } 149 if (beanCache1.contains("fred")) { 150 fail("fred was found"); 151 } 152 if (beanCache2.contains("jill")) { 153 fail("jill was found"); 154 } 155 156 157 instance.clear(); 158 159 if (beanCache1.contains("bob")) { 160 fail("bob could still be found"); 161 } 162 if (beanCache2.contains("mary")) { 163 fail("mary could still be found"); 164 } 165 166 try { 167 beanCache1.addCacheEntry(500,"mary","mary","mary",mary); 168 fail("Could add an entry should not be allowed"); 169 } catch (BeanException ex) { 170 } 172 try { 173 beanCache2.addCacheEntry(500,"bob","bob",bob); 174 fail("Could add an entry should not be allowed"); 175 } catch (BeanException ex) { 176 } 178 179 try { 180 beanCache2 = instance.getBeanCache("beanCache2"); 181 fail("Could add an entry to the bean cache manager."); 182 } catch (BeanException ex) { 183 } 185 186 if (TestCacheEntry.count != 4) { 187 fail ("Release not called on all classes"); 188 } 189 } 190 191 192 } 193 | Popular Tags |