KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > rift > coad > lib > deployment > rmi > RMICacheTest


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  * RMICacheTest.java
20  *
21  * JUnit based test
22  */

23
24 package com.rift.coad.lib.deployment.rmi;
25
26 import junit.framework.*;
27 import java.util.Date JavaDoc;
28 import java.util.HashSet JavaDoc;
29 import java.util.Iterator JavaDoc;
30 import java.util.Vector JavaDoc;
31 import java.util.Set JavaDoc;
32 import javax.rmi.PortableRemoteObject JavaDoc;
33 import org.apache.log4j.Logger;
34 import com.rift.coad.lib.cache.Cache;
35 import com.rift.coad.lib.cache.CacheEntry;
36 import com.rift.coad.lib.common.RandomGuid;
37 import com.rift.coad.lib.configuration.ConfigurationFactory;
38 import com.rift.coad.lib.configuration.Configuration;
39 import com.rift.coad.lib.thread.ThreadStateMonitor;
40
41 /**
42  *
43  * @author Brett Chaldecott
44  */

45 public class RMICacheTest extends TestCase {
46     
47     /**
48      * The test cache class
49      */

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

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

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

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

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

114     public void testRMICache() throws Exception JavaDoc {
115         System.out.println("testRMICache");
116         
117         RMICache instance = new RMICache();
118         
119         
120         RMICacheTestClass cacheObject1 = new RMICacheTestClass();
121         RMICacheTestClass cacheObject2 = new RMICacheTestClass();
122         
123         instance.addCacheEntry(500,cacheObject1);
124         instance.addCacheEntry(500,cacheObject2);
125         
126         System.out.println("Start time is : " + new Date JavaDoc().getTime());
127         for (int count = 0; count < 4; count++) {
128             Thread.sleep(500);
129             cacheObject1.touch();
130         }
131         System.out.println("End time is : " + new Date JavaDoc().getTime());
132         
133         instance.garbageCollect();
134         
135         if (!instance.contains(cacheObject1)) {
136             fail("Cache does not contain cache object1");
137         } else if (instance.contains(cacheObject2)) {
138             fail("Cache contains cache object2");
139         }
140         
141         instance.clear();
142         
143         if (instance.contains(cacheObject1)) {
144             fail("Cache contains cache object1");
145         }
146         
147         try {
148             instance.addCacheEntry(500,cacheObject1);
149             fail("The cache has not been invalidated");
150         } catch (RMIException ex) {
151             // ignore
152
}
153         
154         if (RMICacheTestClass.count != 2) {
155             fail("Release was not called on both classes");
156         }
157     }
158
159     
160     
161 }
162
Popular Tags