001: /*
002: * CoadunationLib: The coaduntion implementation library.
003: * Copyright (C) 2006 Rift IT Contracting
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation; either
008: * version 2.1 of the License, or (at your option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, write to the Free Software
017: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
018: *
019: * ProxyCacheTest.java
020: *
021: * JUnit based test
022: */
023:
024: package com.rift.coad.lib.bean;
025:
026: import junit.framework.*;
027: import java.util.Date;
028: import java.util.HashMap;
029: import java.util.Iterator;
030: import java.util.Map;
031: import java.util.Vector;
032: import org.apache.log4j.Logger;
033: import com.rift.coad.lib.cache.Cache;
034: import com.rift.coad.lib.cache.CacheEntry;
035: import com.rift.coad.lib.common.RandomGuid;
036: import com.rift.coad.lib.configuration.ConfigurationFactory;
037: import com.rift.coad.lib.configuration.Configuration;
038: import com.rift.coad.lib.thread.ThreadStateMonitor;
039:
040: /**
041: *
042: * @author Brett Chaldecott
043: */
044: public class ProxyCacheTest extends TestCase {
045:
046: /**
047: * The test cache class
048: */
049: public static class ProxyCacheTestClass implements java.rmi.Remote,
050: CacheEntry {
051: // private member variables
052: private String id = null;
053: private Date lastTouchTime = new Date();
054: public static int count = 0;
055:
056: /**
057: * The constructor of the cache entry
058: */
059: public ProxyCacheTestClass() throws Exception {
060: id = RandomGuid.getInstance().getGuid();
061: }
062:
063: /**
064: * This method will return true if the date is older than the given expiry
065: * date.
066: *
067: * @return TRUE if expired FALSE if not.
068: * @param expiryDate The expiry date to perform the check with.
069: */
070: public boolean isExpired(Date expiryDate) {
071: System.out.println("Current time : "
072: + lastTouchTime.getTime());
073: System.out.println("Expiry time : " + expiryDate.getTime());
074: return (lastTouchTime.getTime() < expiryDate.getTime());
075: }
076:
077: /**
078: * Release the count
079: */
080: public void cacheRelease() {
081: count++;
082: }
083:
084: /**
085: * The touch method
086: */
087: public void touch() {
088: lastTouchTime = new Date();
089: }
090:
091: }
092:
093: public ProxyCacheTest(String testName) {
094: super (testName);
095: }
096:
097: protected void setUp() throws Exception {
098: }
099:
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: /**
110: * Test of ProxyCache, of class com.rift.coad.lib.bean.ProxyCache.
111: */
112: public void testProxyCache() throws Exception {
113: System.out.println("ProxyCache");
114:
115: ProxyCache instance = new ProxyCache();
116:
117: ProxyCacheTestClass cacheObject1 = new ProxyCacheTestClass();
118: ProxyCacheTestClass cacheObject2 = new ProxyCacheTestClass();
119:
120: instance.addCacheEntry(500, cacheObject1, cacheObject1);
121: instance.addCacheEntry(500, cacheObject2, cacheObject2);
122:
123: System.out.println("Start time is : " + new Date().getTime());
124: for (int count = 0; count < 4; count++) {
125: Thread.sleep(500);
126: cacheObject1.touch();
127: }
128: System.out.println("End time is : " + new Date().getTime());
129:
130: instance.garbageCollect();
131:
132: if (!instance.contains(cacheObject1)) {
133: fail("Cache does not contain cache object1");
134: } else if (instance.contains(cacheObject2)) {
135: fail("Cache contains cache object2");
136: }
137:
138: instance.clear();
139:
140: if (instance.contains(cacheObject1)) {
141: fail("Cache contains cache object1");
142: }
143:
144: try {
145: instance.addCacheEntry(500, cacheObject1, cacheObject1);
146: fail("The cache has not been invalidated");
147: } catch (BeanException ex) {
148: // ignore
149: }
150:
151: if (ProxyCacheTestClass.count != 2) {
152: fail("Release was not called on both classes : "
153: + ProxyCacheTestClass.count);
154: }
155:
156: }
157:
158: }
|