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