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