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: * BeanCacheTest.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.Iterator;
029: import java.util.Map;
030: import java.util.HashMap;
031: import javax.rmi.PortableRemoteObject;
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.configuration.ConfigurationFactory;
036: import com.rift.coad.lib.configuration.Configuration;
037:
038: /**
039: *
040: * @author Brett Chaldecott
041: */
042: public class BeanCacheTest extends TestCase {
043:
044: /**
045: * The cache entry to add
046: */
047: public static class TestCacheEntry implements CacheEntry,
048: java.rmi.Remote {
049:
050: // touch date
051: private Date touchTime = new Date();
052: public static int count = 0;
053:
054: /**
055: * The constructor of the test cache entry object.
056: */
057: public TestCacheEntry() {
058:
059: }
060:
061: /**
062: * The touch method of the test cache entry object.
063: */
064: public void touch() {
065: touchTime = new Date();
066: }
067:
068: /**
069: * This method returns true if this object has expired.
070: *
071: * @return TRUE if expired FALSE if not.
072: * @param expiryDate The date of the expiry.
073: */
074: public boolean isExpired(Date expiryDate) {
075: System.out.println("Touch time : " + touchTime.getTime()
076: + " expiry date : " + expiryDate.getTime());
077: return touchTime.getTime() < expiryDate.getTime();
078: }
079:
080: /**
081: * Release the cache
082: */
083: public void cacheRelease() {
084: count++;
085: }
086: }
087:
088: public BeanCacheTest(String testName) {
089: super (testName);
090: }
091:
092: protected void setUp() throws Exception {
093: }
094:
095: protected void tearDown() throws Exception {
096: }
097:
098: public static Test suite() {
099: TestSuite suite = new TestSuite(BeanCacheTest.class);
100:
101: return suite;
102: }
103:
104: /**
105: * Test of testCache method, of class com.rift.coad.lib.bean.BeanCache.
106: */
107: public void testCache() throws Exception {
108: System.out.println("garbageCollect");
109:
110: BeanCache instance = new BeanCache();
111:
112: TestCacheEntry bob = new TestCacheEntry();
113: instance.addCacheEntry(600, "bob", "bob", bob);
114: instance.addCacheEntry(600, "bob", "bob", bob);
115: TestCacheEntry fred = new TestCacheEntry();
116: instance.addCacheEntry(500, "fred", "fred", fred);
117: instance.addCacheEntry(500, "fred", "fred", fred);
118: TestCacheEntry mary = new TestCacheEntry();
119: instance.addCacheEntry(600, "mary", "mary", "mary", mary);
120:
121: if (bob != instance.getEntry("bob").getCacheEntry()) {
122: fail("bob could not be found");
123: }
124: instance.getEntry("bob").setCacheEntry(mary);
125: if (mary != instance.getEntry("bob").getCacheEntry()) {
126: fail("mary could not be found");
127: }
128: if (mary != instance.getEntry("mary").getBeanHandler()) {
129: fail("mary could not be found");
130: }
131: instance.getEntry("mary").setBeanHandler(bob);
132: if (bob != instance.getEntry("mary").getBeanHandler()) {
133: fail("bob could not be found");
134: }
135: if (!instance.getEntry("mary").getProxy().equals("mary")) {
136: fail("mary could not be found");
137: }
138: instance.getEntry("mary").setProxy("mary1");
139: if (!instance.getEntry("mary").getProxy().equals("mary1")) {
140: fail("mary1 could not be found");
141: }
142:
143: for (int count = 0; count < 4; count++) {
144: Thread.sleep(500);
145: bob.touch();
146: mary.touch();
147: }
148:
149: instance.garbageCollect();
150:
151: if (!instance.contains("bob")) {
152: fail("bob could not be found");
153: }
154: if (!instance.contains("mary")) {
155: fail("mary could not be found");
156: }
157: if (instance.contains("fred")) {
158: fail("fred was found");
159: }
160:
161: instance.clear();
162:
163: if (instance.contains("bob")) {
164: fail("bob could still be found");
165: }
166: if (instance.contains("mary")) {
167: fail("bob could still be found");
168: }
169:
170: try {
171: instance.addCacheEntry(500, "mary", "mary", "mary", mary);
172: fail("Could add an entry should not be allowed");
173: } catch (BeanException ex) {
174: // ingore
175: }
176: try {
177: instance.addCacheEntry(500, "bob", "bob", bob);
178: fail("Could add an entry should not be allowed");
179: } catch (BeanException ex) {
180: // ingore
181: }
182:
183: if (TestCacheEntry.count != 3) {
184: fail("Release not called on all classes");
185: }
186: }
187:
188: }
|