KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > rift > coad > lib > bean > BeanCacheTest


1 /*
2  * CoadunationLib: The coaduntion implementation library.
3  * Copyright (C) 2006 Rift IT Contracting
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18  *
19  * BeanCacheTest.java
20  *
21  * JUnit based test
22  */

23
24 package com.rift.coad.lib.bean;
25
26 import junit.framework.*;
27 import java.util.Date JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.Map JavaDoc;
30 import java.util.HashMap JavaDoc;
31 import javax.rmi.PortableRemoteObject JavaDoc;
32 import org.apache.log4j.Logger;
33 import com.rift.coad.lib.cache.Cache;
34 import com.rift.coad.lib.cache.CacheEntry;
35 import com.rift.coad.lib.configuration.ConfigurationFactory;
36 import com.rift.coad.lib.configuration.Configuration;
37
38 /**
39  *
40  * @author Brett Chaldecott
41  */

42 public class BeanCacheTest extends TestCase {
43     
44     /**
45      * The cache entry to add
46      */

47     public static class TestCacheEntry implements CacheEntry, java.rmi.Remote JavaDoc {
48         
49         // touch date
50
private Date JavaDoc touchTime = new Date JavaDoc();
51         public static int count = 0;
52         
53         /**
54          * The constructor of the test cache entry object.
55          */

56         public TestCacheEntry() {
57             
58         }
59         
60         
61         /**
62          * The touch method of the test cache entry object.
63          */

64         public void touch() {
65             touchTime = new Date JavaDoc();
66         }
67         
68         
69         /**
70          * This method returns true if this object has expired.
71          *
72          * @return TRUE if expired FALSE if not.
73          * @param expiryDate The date of the expiry.
74          */

75         public boolean isExpired(Date JavaDoc expiryDate) {
76             System.out.println("Touch time : " + touchTime.getTime() +
77                     " expiry date : " + expiryDate.getTime());
78             return touchTime.getTime() < expiryDate.getTime();
79         }
80         
81         
82         /**
83          * Release the cache
84          */

85         public void cacheRelease() {
86             count++;
87         }
88     }
89     
90     public BeanCacheTest(String JavaDoc testName) {
91         super(testName);
92     }
93
94     protected void setUp() throws Exception JavaDoc {
95     }
96
97     protected void tearDown() throws Exception JavaDoc {
98     }
99
100
101     public static Test suite() {
102         TestSuite suite = new TestSuite(BeanCacheTest.class);
103         
104         return suite;
105     }
106
107     /**
108      * Test of testCache method, of class com.rift.coad.lib.bean.BeanCache.
109      */

110     public void testCache() throws Exception JavaDoc {
111         System.out.println("garbageCollect");
112         
113         BeanCache instance = new BeanCache();
114         
115         TestCacheEntry bob = new TestCacheEntry();
116         instance.addCacheEntry(500,"bob","bob",bob);
117         instance.addCacheEntry(500,"bob","bob",bob);
118         TestCacheEntry fred = new TestCacheEntry();
119         instance.addCacheEntry(500,"fred","fred",fred);
120         instance.addCacheEntry(500,"fred","fred",fred);
121         TestCacheEntry mary = new TestCacheEntry();
122         instance.addCacheEntry(500,"mary","mary","mary",mary);
123         
124         if (bob != instance.getEntry("bob").getCacheEntry()) {
125             fail("bob could not be found");
126         }
127         instance.getEntry("bob").setCacheEntry(mary);
128         if (mary != instance.getEntry("bob").getCacheEntry()) {
129             fail("mary could not be found");
130         }
131         if (mary != instance.getEntry("mary").getBeanHandler()) {
132             fail("mary could not be found");
133         }
134         instance.getEntry("mary").setBeanHandler(bob);
135         if (bob != instance.getEntry("mary").getBeanHandler()) {
136             fail("bob could not be found");
137         }
138         if (!instance.getEntry("mary").getProxy().equals("mary")) {
139             fail("mary could not be found");
140         }
141         instance.getEntry("mary").setProxy("mary1");
142         if (!instance.getEntry("mary").getProxy().equals("mary1")) {
143             fail("mary1 could not be found");
144         }
145         
146         for (int count = 0; count < 4; count++) {
147             Thread.sleep(500);
148             bob.touch();
149             mary.touch();
150         }
151         
152         instance.garbageCollect();
153         
154         if (!instance.contains("bob")) {
155             fail("bob could not be found");
156         }
157         if (!instance.contains("mary")) {
158             fail("mary could not be found");
159         }
160         if (instance.contains("fred")) {
161             fail("fred was found");
162         }
163         
164         instance.clear();
165         
166         if (instance.contains("bob")) {
167             fail("bob could still be found");
168         }
169         if (instance.contains("mary")) {
170             fail("bob could still be found");
171         }
172         
173         try {
174             instance.addCacheEntry(500,"mary","mary","mary",mary);
175             fail("Could add an entry should not be allowed");
176         } catch (BeanException ex) {
177             // ingore
178
}
179         try {
180             instance.addCacheEntry(500,"bob","bob",bob);
181             fail("Could add an entry should not be allowed");
182         } catch (BeanException ex) {
183             // ingore
184
}
185         
186         if (TestCacheEntry.count != 3) {
187             fail("Release not called on all classes");
188         }
189     }
190
191     
192 }
193
Popular Tags