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: * RMICacheTest.java
020: *
021: * JUnit based test
022: */
023:
024: package com.rift.coad.lib.deployment.rmi;
025:
026: import junit.framework.*;
027: import java.util.Date;
028: import java.util.HashSet;
029: import java.util.Iterator;
030: import java.util.Vector;
031: import java.util.Set;
032: import javax.rmi.PortableRemoteObject;
033: import org.apache.log4j.Logger;
034: import com.rift.coad.lib.cache.Cache;
035: import com.rift.coad.lib.cache.CacheEntry;
036: import com.rift.coad.lib.common.RandomGuid;
037: import com.rift.coad.lib.configuration.ConfigurationFactory;
038: import com.rift.coad.lib.configuration.Configuration;
039: import com.rift.coad.lib.thread.ThreadStateMonitor;
040:
041: /**
042: *
043: * @author Brett Chaldecott
044: */
045: public class RMICacheTest extends TestCase {
046:
047: /**
048: * The test cache class
049: */
050: public static class RMICacheTestClass implements java.rmi.Remote,
051: CacheEntry {
052: // private member variables
053: private String id = null;
054: private Date lastTouchTime = new Date();
055: public static int count = 0;
056:
057: /**
058: * The constructor of the cache entry
059: */
060: public RMICacheTestClass() throws Exception {
061: id = RandomGuid.getInstance().getGuid();
062: }
063:
064: /**
065: * This method will return true if the date is older than the given expiry
066: * date.
067: *
068: * @return TRUE if expired FALSE if not.
069: * @param expiryDate The expiry date to perform the check with.
070: */
071: public boolean isExpired(Date expiryDate) {
072: System.out.println("Current time : "
073: + lastTouchTime.getTime());
074: System.out.println("Expiry time : " + expiryDate.getTime());
075: return (lastTouchTime.getTime() < expiryDate.getTime());
076: }
077:
078: /**
079: * Release the count
080: */
081: public void cacheRelease() {
082: count++;
083: }
084:
085: /**
086: * The touch method
087: */
088: public void touch() {
089: lastTouchTime = new Date();
090: }
091:
092: }
093:
094: public RMICacheTest(String testName) {
095: super (testName);
096: }
097:
098: protected void setUp() throws Exception {
099: }
100:
101: protected void tearDown() throws Exception {
102: }
103:
104: public static Test suite() {
105: TestSuite suite = new TestSuite(RMICacheTest.class);
106:
107: return suite;
108: }
109:
110: /**
111: * Test of garbageCollect method, of class com.rift.coad.lib.deployment.rmi.RMICache.
112: */
113: public void testRMICache() throws Exception {
114: System.out.println("testRMICache");
115:
116: RMICache instance = new RMICache();
117:
118: RMICacheTestClass cacheObject1 = new RMICacheTestClass();
119: RMICacheTestClass cacheObject2 = new RMICacheTestClass();
120:
121: instance.addCacheEntry(500, cacheObject1);
122: instance.addCacheEntry(500, 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);
147: fail("The cache has not been invalidated");
148: } catch (RMIException ex) {
149: // ignore
150: }
151:
152: if (RMICacheTestClass.count != 2) {
153: fail("Release was not called on both classes");
154: }
155: }
156:
157: }
|