KickJava   Java API By Example, From Geeks To Geeks.

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


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  * BeanCacheManagerTest.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.Map JavaDoc;
29 import java.util.HashMap JavaDoc;
30 import java.util.Iterator JavaDoc;
31 import com.rift.coad.lib.cache.Cache;
32 import com.rift.coad.lib.cache.CacheEntry;
33
34 /**
35  *
36  * @author mincemeat
37  */

38 public class BeanCacheManagerTest extends TestCase {
39     
40     /**
41      * The cache entry to add
42      */

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

52         public TestCacheEntry() {
53             
54         }
55         
56         
57         /**
58          * The touch method of the test cache entry object.
59          */

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

71         public boolean isExpired(Date JavaDoc expiryDate) {
72             System.out.println("Touch time : " + touchTime.getTime() +
73                     " expiry date : " + expiryDate.getTime());
74             return touchTime.getTime() < expiryDate.getTime();
75         }
76         
77         
78         /**
79          * cache release
80          */

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

105     public void testBeanCacheManager() throws Exception JavaDoc {
106         System.out.println("BeanCacheManager");
107         
108         Object JavaDoc ref = null;
109         BeanCacheManager instance = new BeanCacheManager();
110         
111         BeanCache beanCache1 = instance.getBeanCache("beanCache1");
112         BeanCache beanCache2 = instance.getBeanCache("beanCache2");
113         if (instance.getBeanCache("beanCache1") != beanCache1) {
114             fail("Failed to retrieve the bean cache result object");
115         }
116         if (!instance.contains("beanCache1")) {
117             fail("The instance does not contain beanCache1");
118         }
119         if (instance.getBeanCache("beanCache2") != beanCache2) {
120             fail("Failed to retrieve the bean cache 2 result object");
121         }
122         if (!instance.contains("beanCache2")) {
123             fail("The instance does not contain beanCache2");
124         }
125         
126         TestCacheEntry bob = new TestCacheEntry();
127         beanCache1.addCacheEntry(500,"bob","bob",bob);
128         TestCacheEntry fred = new TestCacheEntry();
129         beanCache1.addCacheEntry(500,"fred","fred",fred);
130         TestCacheEntry mary = new TestCacheEntry();
131         beanCache2.addCacheEntry(500,"mary","mary","mary",mary);
132         TestCacheEntry jill = new TestCacheEntry();
133         beanCache2.addCacheEntry(500,"jill","jill","jill",jill);
134         
135         for (int count = 0; count < 4; count++) {
136             Thread.sleep(500);
137             bob.touch();
138             mary.touch();
139         }
140         
141         instance.garbageCollect();
142         
143         if (!beanCache1.contains("bob")) {
144             fail("bob could not be found");
145         }
146         if (!beanCache2.contains("mary")) {
147             fail("mary could not be found");
148         }
149         if (beanCache1.contains("fred")) {
150             fail("fred was found");
151         }
152         if (beanCache2.contains("jill")) {
153             fail("jill was found");
154         }
155         
156         
157         instance.clear();
158         
159         if (beanCache1.contains("bob")) {
160             fail("bob could still be found");
161         }
162         if (beanCache2.contains("mary")) {
163             fail("mary could still be found");
164         }
165         
166         try {
167             beanCache1.addCacheEntry(500,"mary","mary","mary",mary);
168             fail("Could add an entry should not be allowed");
169         } catch (BeanException ex) {
170             // ingore
171
}
172         try {
173             beanCache2.addCacheEntry(500,"bob","bob",bob);
174             fail("Could add an entry should not be allowed");
175         } catch (BeanException ex) {
176             // ingore
177
}
178         
179         try {
180             beanCache2 = instance.getBeanCache("beanCache2");
181             fail("Could add an entry to the bean cache manager.");
182         } catch (BeanException ex) {
183             // ignore
184
}
185         
186         if (TestCacheEntry.count != 4) {
187             fail ("Release not called on all classes");
188         }
189     }
190
191     
192 }
193
Popular Tags