1 23 24 package com.rift.coad.lib.bean; 25 26 import junit.framework.*; 27 import java.util.Date ; 28 import java.util.Iterator ; 29 import java.util.Map ; 30 import java.util.HashMap ; 31 import javax.rmi.PortableRemoteObject ; 32 import org.apache.log4j.Logger; 33 import com.rift.coad.lib.cache.Cache; 34 import com.rift.coad.lib.cache.CacheEntry; 35 import com.rift.coad.lib.configuration.ConfigurationFactory; 36 import com.rift.coad.lib.configuration.Configuration; 37 38 42 public class BeanCacheTest extends TestCase { 43 44 47 public static class TestCacheEntry implements CacheEntry, java.rmi.Remote { 48 49 private Date touchTime = new Date (); 51 public static int count = 0; 52 53 56 public TestCacheEntry() { 57 58 } 59 60 61 64 public void touch() { 65 touchTime = new Date (); 66 } 67 68 69 75 public boolean isExpired(Date expiryDate) { 76 System.out.println("Touch time : " + touchTime.getTime() + 77 " expiry date : " + expiryDate.getTime()); 78 return touchTime.getTime() < expiryDate.getTime(); 79 } 80 81 82 85 public void cacheRelease() { 86 count++; 87 } 88 } 89 90 public BeanCacheTest(String testName) { 91 super(testName); 92 } 93 94 protected void setUp() throws Exception { 95 } 96 97 protected void tearDown() throws Exception { 98 } 99 100 101 public static Test suite() { 102 TestSuite suite = new TestSuite(BeanCacheTest.class); 103 104 return suite; 105 } 106 107 110 public void testCache() throws Exception { 111 System.out.println("garbageCollect"); 112 113 BeanCache instance = new BeanCache(); 114 115 TestCacheEntry bob = new TestCacheEntry(); 116 instance.addCacheEntry(500,"bob","bob",bob); 117 instance.addCacheEntry(500,"bob","bob",bob); 118 TestCacheEntry fred = new TestCacheEntry(); 119 instance.addCacheEntry(500,"fred","fred",fred); 120 instance.addCacheEntry(500,"fred","fred",fred); 121 TestCacheEntry mary = new TestCacheEntry(); 122 instance.addCacheEntry(500,"mary","mary","mary",mary); 123 124 if (bob != instance.getEntry("bob").getCacheEntry()) { 125 fail("bob could not be found"); 126 } 127 instance.getEntry("bob").setCacheEntry(mary); 128 if (mary != instance.getEntry("bob").getCacheEntry()) { 129 fail("mary could not be found"); 130 } 131 if (mary != instance.getEntry("mary").getBeanHandler()) { 132 fail("mary could not be found"); 133 } 134 instance.getEntry("mary").setBeanHandler(bob); 135 if (bob != instance.getEntry("mary").getBeanHandler()) { 136 fail("bob could not be found"); 137 } 138 if (!instance.getEntry("mary").getProxy().equals("mary")) { 139 fail("mary could not be found"); 140 } 141 instance.getEntry("mary").setProxy("mary1"); 142 if (!instance.getEntry("mary").getProxy().equals("mary1")) { 143 fail("mary1 could not be found"); 144 } 145 146 for (int count = 0; count < 4; count++) { 147 Thread.sleep(500); 148 bob.touch(); 149 mary.touch(); 150 } 151 152 instance.garbageCollect(); 153 154 if (!instance.contains("bob")) { 155 fail("bob could not be found"); 156 } 157 if (!instance.contains("mary")) { 158 fail("mary could not be found"); 159 } 160 if (instance.contains("fred")) { 161 fail("fred was found"); 162 } 163 164 instance.clear(); 165 166 if (instance.contains("bob")) { 167 fail("bob could still be found"); 168 } 169 if (instance.contains("mary")) { 170 fail("bob could still be found"); 171 } 172 173 try { 174 instance.addCacheEntry(500,"mary","mary","mary",mary); 175 fail("Could add an entry should not be allowed"); 176 } catch (BeanException ex) { 177 } 179 try { 180 instance.addCacheEntry(500,"bob","bob",bob); 181 fail("Could add an entry should not be allowed"); 182 } catch (BeanException ex) { 183 } 185 186 if (TestCacheEntry.count != 3) { 187 fail("Release not called on all classes"); 188 } 189 } 190 191 192 } 193 | Popular Tags |