KickJava   Java API By Example, From Geeks To Geeks.

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


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  * ProxyCacheTest.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.HashMap JavaDoc;
29 import java.util.Iterator JavaDoc;
30 import java.util.Map JavaDoc;
31 import java.util.Vector 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.common.RandomGuid;
36 import com.rift.coad.lib.configuration.ConfigurationFactory;
37 import com.rift.coad.lib.configuration.Configuration;
38 import com.rift.coad.lib.thread.ThreadStateMonitor;
39
40 /**
41  *
42  * @author Brett Chaldecott
43  */

44 public class ProxyCacheTest extends TestCase {
45     
46     /**
47      * The test cache class
48      */

49     public static class ProxyCacheTestClass implements java.rmi.Remote JavaDoc,
50             CacheEntry {
51         // private member variables
52
private String JavaDoc id = null;
53         private Date JavaDoc lastTouchTime = new Date JavaDoc();
54         public static int count = 0;
55         
56         /**
57          * The constructor of the cache entry
58          */

59         public ProxyCacheTestClass() throws Exception JavaDoc {
60             id = RandomGuid.getInstance().getGuid();
61         }
62         
63         
64         /**
65          * This method will return true if the date is older than the given expiry
66          * date.
67          *
68          * @return TRUE if expired FALSE if not.
69          * @param expiryDate The expiry date to perform the check with.
70          */

71         public boolean isExpired(Date JavaDoc expiryDate) {
72             System.out.println("Current time : " + lastTouchTime.getTime());
73             System.out.println("Expiry time : " + expiryDate.getTime());
74             return (lastTouchTime.getTime() < expiryDate.getTime());
75         }
76         
77         /**
78          * Release the count
79          */

80         public void cacheRelease() {
81             count++;
82         }
83         
84         /**
85          * The touch method
86          */

87         public void touch() {
88             lastTouchTime = new Date JavaDoc();
89         }
90         
91     }
92     
93     public ProxyCacheTest(String JavaDoc testName) {
94         super(testName);
95     }
96     
97     protected void setUp() throws Exception JavaDoc {
98     }
99     
100     protected void tearDown() throws Exception JavaDoc {
101     }
102     
103     public static Test suite() {
104         TestSuite suite = new TestSuite(ProxyCacheTest.class);
105         
106         return suite;
107     }
108     
109     /**
110      * Test of ProxyCache, of class com.rift.coad.lib.bean.ProxyCache.
111      */

112     public void testProxyCache() throws Exception JavaDoc {
113         System.out.println("ProxyCache");
114         
115         ProxyCache instance = new ProxyCache();
116         
117         
118         ProxyCacheTestClass cacheObject1 = new ProxyCacheTestClass();
119         ProxyCacheTestClass cacheObject2 = new ProxyCacheTestClass();
120         
121         instance.addCacheEntry(500,cacheObject1,cacheObject1);
122         instance.addCacheEntry(500,cacheObject2,cacheObject2);
123         
124         System.out.println("Start time is : " + new Date JavaDoc().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 JavaDoc().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,cacheObject1);
147             fail("The cache has not been invalidated");
148         } catch (BeanException ex) {
149             // ignore
150
}
151         
152         if (ProxyCacheTestClass.count != 2) {
153             fail("Release was not called on both classes : " +
154                     ProxyCacheTestClass.count);
155         }
156         
157     }
158     
159 }
160
Popular Tags