KickJava   Java API By Example, From Geeks To Geeks.

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


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 path
25
package com.rift.coad.lib.bean;
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  * @author Brett Chaldecott
81  */

82 public class TransactionProxyCacheTest extends TestCase {
83     
84     /**
85      * The test cache class
86      */

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

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

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

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

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

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