1 23 24 package com.rift.coad.lib.bean; 25 26 import junit.framework.*; 27 import java.util.Date ; 28 import java.util.HashMap ; 29 import java.util.Iterator ; 30 import java.util.Map ; 31 import java.util.Vector ; 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.common.RandomGuid; 36 import com.rift.coad.lib.configuration.ConfigurationFactory; 37 import com.rift.coad.lib.configuration.Configuration; 38 import com.rift.coad.lib.thread.ThreadStateMonitor; 39 40 44 public class ProxyCacheTest extends TestCase { 45 46 49 public static class ProxyCacheTestClass implements java.rmi.Remote , 50 CacheEntry { 51 private String id = null; 53 private Date lastTouchTime = new Date (); 54 public static int count = 0; 55 56 59 public ProxyCacheTestClass() throws Exception { 60 id = RandomGuid.getInstance().getGuid(); 61 } 62 63 64 71 public boolean isExpired(Date expiryDate) { 72 System.out.println("Current time : " + lastTouchTime.getTime()); 73 System.out.println("Expiry time : " + expiryDate.getTime()); 74 return (lastTouchTime.getTime() < expiryDate.getTime()); 75 } 76 77 80 public void cacheRelease() { 81 count++; 82 } 83 84 87 public void touch() { 88 lastTouchTime = new Date (); 89 } 90 91 } 92 93 public ProxyCacheTest(String testName) { 94 super(testName); 95 } 96 97 protected void setUp() throws Exception { 98 } 99 100 protected void tearDown() throws Exception { 101 } 102 103 public static Test suite() { 104 TestSuite suite = new TestSuite(ProxyCacheTest.class); 105 106 return suite; 107 } 108 109 112 public void testProxyCache() throws Exception { 113 System.out.println("ProxyCache"); 114 115 ProxyCache instance = new ProxyCache(); 116 117 118 ProxyCacheTestClass cacheObject1 = new ProxyCacheTestClass(); 119 ProxyCacheTestClass cacheObject2 = new ProxyCacheTestClass(); 120 121 instance.addCacheEntry(500,cacheObject1,cacheObject1); 122 instance.addCacheEntry(500,cacheObject2,cacheObject2); 123 124 System.out.println("Start time is : " + new Date ().getTime()); 125 for (int count = 0; count < 4; count++) { 126 Thread.sleep(500); 127 cacheObject1.touch(); 128 } 129 System.out.println("End time is : " + new Date ().getTime()); 130 131 instance.garbageCollect(); 132 133 if (!instance.contains(cacheObject1)) { 134 fail("Cache does not contain cache object1"); 135 } else if (instance.contains(cacheObject2)) { 136 fail("Cache contains cache object2"); 137 } 138 139 instance.clear(); 140 141 if (instance.contains(cacheObject1)) { 142 fail("Cache contains cache object1"); 143 } 144 145 try { 146 instance.addCacheEntry(500,cacheObject1,cacheObject1); 147 fail("The cache has not been invalidated"); 148 } catch (BeanException ex) { 149 } 151 152 if (ProxyCacheTestClass.count != 2) { 153 fail("Release was not called on both classes : " + 154 ProxyCacheTestClass.count); 155 } 156 157 } 158 159 } 160 | Popular Tags |