KickJava   Java API By Example, From Geeks To Geeks.

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


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

23
24 // package path
25
package com.rift.coad.lib.deployment.rmi;
26
27 // java imports
28
import javax.naming.InitialContext JavaDoc;
29 import javax.naming.Context JavaDoc;
30 import java.sql.PreparedStatement JavaDoc;
31 import java.sql.ResultSet JavaDoc;
32 import java.sql.Statement JavaDoc;
33 import javax.sql.DataSource JavaDoc;
34 import java.util.Set JavaDoc;
35 import java.util.HashSet JavaDoc;
36 import javax.transaction.UserTransaction JavaDoc;
37 import java.sql.Timestamp JavaDoc;
38 import java.util.ArrayList JavaDoc;
39 import java.util.Date JavaDoc;
40 import java.util.Enumeration JavaDoc;
41 import java.util.HashSet JavaDoc;
42 import java.util.Iterator JavaDoc;
43 import java.util.List JavaDoc;
44 import java.util.Set JavaDoc;
45 import javax.transaction.xa.XAException JavaDoc;
46 import javax.transaction.xa.XAResource JavaDoc;
47 import javax.transaction.xa.Xid JavaDoc;
48 import org.apache.log4j.Logger;
49 import org.apache.log4j.BasicConfigurator;
50
51 // junit imports
52
import junit.framework.*;
53
54 // object web imports
55
import org.objectweb.jotm.Jotm;
56
57 // coadunation imports
58
import com.rift.coad.lib.naming.NamingDirector;
59 import com.rift.coad.lib.naming.ContextManager;
60 import com.rift.coad.lib.db.DBSourceManager;
61 import com.rift.coad.lib.common.RandomGuid;
62 import com.rift.coad.lib.common.ObjectSerializer;
63 import com.rift.coad.lib.cache.CacheEntry;
64 import com.rift.coad.lib.interceptor.InterceptorFactory;
65 import com.rift.coad.lib.security.RoleManager;
66 import com.rift.coad.lib.security.ThreadsPermissionContainer;
67 import com.rift.coad.lib.security.ThreadPermissionSession;
68 import com.rift.coad.lib.security.UserSession;
69 import com.rift.coad.lib.security.user.UserSessionManager;
70 import com.rift.coad.lib.security.user.UserStoreManager;
71 import com.rift.coad.lib.security.SessionManager;
72 import com.rift.coad.lib.security.login.LoginManager;
73 import com.rift.coad.lib.thread.CoadunationThreadGroup;
74 import com.rift.coad.lib.transaction.TransactionDirector;
75 import com.rift.coad.util.lock.ObjectLockFactory;
76 import com.rift.coad.util.transaction.TransactionManager;
77
78
79 /**
80  * This class is responsible for testing the transaction RMI cache.
81  * @author Brett Chaldecott
82  */

83 public class TransactionRMICacheTest extends TestCase {
84     
85     /**
86      * The test cache class
87      */

88     public static class RMICacheTestClass implements java.rmi.Remote JavaDoc,
89             CacheEntry {
90         // private member variables
91
private String JavaDoc id = null;
92         private Date JavaDoc lastTouchTime = new Date JavaDoc();
93         public static int count = 0;
94         
95         /**
96          * The constructor of the cache entry
97          */

98         public RMICacheTestClass () throws Exception JavaDoc {
99             id = RandomGuid.getInstance().getGuid();
100         }
101         
102         
103         /**
104          * This method will return true if the date is older than the given expiry
105          * date.
106          *
107          * @return TRUE if expired FALSE if not.
108          * @param expiryDate The expiry date to perform the check with.
109          */

110         public boolean isExpired(Date JavaDoc expiryDate) {
111             System.out.println("Current time : " + lastTouchTime.getTime());
112             System.out.println("Expiry time : " + expiryDate.getTime());
113             return (lastTouchTime.getTime() < expiryDate.getTime());
114         }
115         
116         /**
117          * Release the count
118          */

119         public void cacheRelease() {
120             count++;
121         }
122         
123         /**
124          * The touch method
125          */

126         public void touch() {
127             lastTouchTime = new Date JavaDoc();
128         }
129         
130     }
131     
132     
133     public TransactionRMICacheTest(String JavaDoc testName) {
134         super(testName);
135     }
136
137     protected void setUp() throws Exception JavaDoc {
138     }
139
140     protected void tearDown() throws Exception JavaDoc {
141     }
142
143     public static Test suite() {
144         TestSuite suite = new TestSuite(TransactionRMICacheTest.class);
145         
146         return suite;
147     }
148     
149     // user transaction
150
private UserTransaction JavaDoc ut = null;
151     private boolean addedEntry = false;
152     private boolean caughtException = false;
153     private boolean gotCacheEntry= false;
154     
155     
156     /**
157      * Test of garbageCollect method, of class com.rift.coad.lib.deployment.rmi.RMICache.
158      */

159     public void testRMICache() throws Exception JavaDoc {
160         System.out.println("testRMICache");
161         
162         // init the session information
163
ThreadsPermissionContainer permissions = new ThreadsPermissionContainer();
164         SessionManager.init(permissions);
165         UserStoreManager userStoreManager = new UserStoreManager();
166         UserSessionManager sessionManager = new UserSessionManager(permissions,
167                 userStoreManager);
168         LoginManager.init(sessionManager,userStoreManager);
169         // instanciate the thread manager
170
CoadunationThreadGroup threadGroup = new CoadunationThreadGroup(sessionManager,
171             userStoreManager);
172         
173         // add a user to the session for the current thread
174
RoleManager.getInstance();
175         
176         InterceptorFactory.init(permissions,sessionManager,userStoreManager);
177         
178         // add a new user object and add to the permission
179
Set JavaDoc set = new HashSet JavaDoc();
180         set.add("test");
181         UserSession user = new UserSession("test1", set);
182         permissions.putSession(new Long JavaDoc(Thread.currentThread().getId()),
183                 new ThreadPermissionSession(
184                 new Long JavaDoc(Thread.currentThread().getId()),user));
185         
186         // init the naming director
187
NamingDirector.init(threadGroup);
188         
189         // instanciate the transaction director
190
TransactionDirector transactionDirector = TransactionDirector.init();
191         
192         // init the database source
193
DBSourceManager.init();
194         Context JavaDoc context = new InitialContext JavaDoc();
195         ObjectLockFactory.init();
196         TransactionManager.init();
197         
198         ut = (UserTransaction JavaDoc)context.lookup("java:comp/UserTransaction");
199         
200         TransactionRMICache instance = new TransactionRMICache();
201         
202         
203         RMICacheTestClass cacheObject1 = new RMICacheTestClass();
204         RMICacheTestClass cacheObject2 = new RMICacheTestClass();
205         RMICacheTestClass cacheObject3 = new RMICacheTestClass();
206         RMICacheTestClass cacheObject4 = new RMICacheTestClass();
207         
208         ut.begin();
209         instance.addCacheEntry(500,cacheObject3);
210         ut.rollback();
211         
212         if (instance.contains(cacheObject3)) {
213             fail("Cache did not roll back");
214         }
215         
216         ut.begin();
217         instance.addCacheEntry(500,cacheObject1);
218         instance.addCacheEntry(500,cacheObject2);
219         ut.commit();
220         
221         ut.begin();
222         instance.addCacheEntry(500,cacheObject4);
223         ut.commit();
224         
225         if (!instance.contains(cacheObject1)) {
226             fail("Cache does not contain entry1");
227         }
228         if (!instance.contains(cacheObject2)) {
229             fail("Cache does not contain entry2");
230         }
231         
232         System.out.println("Start time is : " + new Date JavaDoc().getTime());
233         for (int count = 0; count < 4; count++) {
234             Thread.sleep(500);
235             cacheObject1.touch();
236         }
237         System.out.println("End time is : " + new Date JavaDoc().getTime());
238         
239         instance.garbageCollect();
240         
241         if (!instance.contains(cacheObject1)) {
242             fail("Cache does not contain cache object1");
243         } else if (instance.contains(cacheObject2)) {
244             fail("Cache contains cache object2");
245         }
246         
247         instance.clear();
248         
249         if (instance.contains(cacheObject1)) {
250             fail("Cache contains cache object1");
251         }
252         
253         try {
254             ut.begin();
255             instance.addCacheEntry(500,cacheObject1);
256             fail("The cache has not been invalidated");
257             ut.commit();
258         } catch (RMIException ex) {
259             System.out.println(ex.getMessage());
260         }
261         
262         if (RMICacheTestClass.count != 4) {
263             fail("Release was not called on both classes");
264         }
265     }
266
267     
268     
269 }
270
Popular Tags