KickJava   Java API By Example, From Geeks To Geeks.

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


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

80 public class TransactionBeanCacheManagerTest extends TestCase {
81     
82     /**
83      * The cache entry to add
84      */

85     public static class TestCacheEntry implements CacheEntry, java.rmi.Remote JavaDoc {
86         
87         // touch date
88
private Date JavaDoc touchTime = new Date JavaDoc();
89         public static int count = 0;
90         
91         /**
92          * The constructor of the test cache entry object.
93          */

94         public TestCacheEntry() {
95             
96         }
97         
98         
99         /**
100          * The touch method of the test cache entry object.
101          */

102         public void touch() {
103             touchTime = new Date JavaDoc();
104         }
105         
106         
107         /**
108          * This method returns true if this object has expired.
109          *
110          * @return TRUE if expired FALSE if not.
111          * @param expiryDate The date of the expiry.
112          */

113         public boolean isExpired(Date JavaDoc expiryDate) {
114             System.out.println("Touch time : " + touchTime.getTime() +
115                     " expiry date : " + expiryDate.getTime());
116             return touchTime.getTime() < expiryDate.getTime();
117         }
118         
119         
120         /**
121          * cache release
122          */

123         public void cacheRelease() {
124             count++;
125         }
126     }
127     
128     // private member variables
129
private UserTransaction JavaDoc ut = null;
130     
131     public TransactionBeanCacheManagerTest(String JavaDoc testName) {
132         super(testName);
133     }
134
135     protected void setUp() throws Exception JavaDoc {
136     }
137
138     protected void tearDown() throws Exception JavaDoc {
139     }
140
141     public static Test suite() {
142         TestSuite suite = new TestSuite(TransactionBeanCacheManagerTest.class);
143         
144         return suite;
145     }
146
147     /**
148      * Test of BeanCacheManager method, of class com.rift.coad.lib.bean.BeanCacheManager.
149      */

150     public void testBeanCacheManager() throws Exception JavaDoc {
151         System.out.println("TransactionBeanCacheManager");
152         
153         // init the session information
154
ThreadsPermissionContainer permissions = new ThreadsPermissionContainer();
155         SessionManager.init(permissions);
156         UserStoreManager userStoreManager = new UserStoreManager();
157         UserSessionManager sessionManager = new UserSessionManager(permissions,
158                 userStoreManager);
159         LoginManager.init(sessionManager,userStoreManager);
160         // instanciate the thread manager
161
CoadunationThreadGroup threadGroup = new CoadunationThreadGroup(sessionManager,
162             userStoreManager);
163         
164         // add a user to the session for the current thread
165
RoleManager.getInstance();
166         
167         InterceptorFactory.init(permissions,sessionManager,userStoreManager);
168         
169         // add a new user object and add to the permission
170
Set JavaDoc set = new HashSet JavaDoc();
171         set.add("test");
172         UserSession user = new UserSession("test1", set);
173         permissions.putSession(new Long JavaDoc(Thread.currentThread().getId()),
174                 new ThreadPermissionSession(
175                 new Long JavaDoc(Thread.currentThread().getId()),user));
176         
177         // init the naming director
178
NamingDirector.init(threadGroup);
179         
180         // instanciate the transaction director
181
TransactionDirector transactionDirector = TransactionDirector.init();
182         
183         // init the database source
184
DBSourceManager.init();
185         Context JavaDoc context = new InitialContext JavaDoc();
186         ObjectLockFactory.init();
187         TransactionManager.init();
188         
189         ut = (UserTransaction JavaDoc)context.lookup("java:comp/UserTransaction");
190         
191         Object JavaDoc ref = null;
192         TransactionBeanCacheManager instance = new TransactionBeanCacheManager();
193         
194         TransactionBeanCache beanCache1 = instance.getBeanCache("beanCache1");
195         TransactionBeanCache beanCache2 = instance.getBeanCache("beanCache2");
196         if (instance.getBeanCache("beanCache1") != beanCache1) {
197             fail("Failed to retrieve the bean cache result object");
198         }
199         if (!instance.contains("beanCache1")) {
200             fail("The instance does not contain beanCache1");
201         }
202         if (instance.getBeanCache("beanCache2") != beanCache2) {
203             fail("Failed to retrieve the bean cache 2 result object");
204         }
205         if (!instance.contains("beanCache2")) {
206             fail("The instance does not contain beanCache2");
207         }
208         
209         ut.begin();
210         TestCacheEntry bob = new TestCacheEntry();
211         beanCache1.addCacheEntry(500,"bob","bob",bob);
212         System.out.println("Class : " + beanCache1.getClass().getName());
213         ut.commit();
214         ut.begin();
215         TestCacheEntry fred = new TestCacheEntry();
216         beanCache1.addCacheEntry(500,"fred","fred",fred);
217         ut.commit();
218         ut.begin();
219         TestCacheEntry mary = new TestCacheEntry();
220         beanCache2.addCacheEntry(500,"mary","mary","mary",mary);
221         ut.commit();
222         ut.begin();
223         TestCacheEntry jill = new TestCacheEntry();
224         beanCache2.addCacheEntry(500,"jill","jill","jill",jill);
225         ut.commit();
226         
227         ut.begin();
228         if (!beanCache1.contains("bob")) {
229             fail("bob could not be found");
230         }
231         
232         if (!beanCache2.contains("mary")) {
233             fail("mary could not be found");
234         }
235         if (!beanCache1.contains("fred")) {
236             fail("fred was found");
237         }
238         if (!beanCache2.contains("jill")) {
239             fail("jill was found");
240         }
241         ut.commit();
242         for (int count = 0; count < 4; count++) {
243             Thread.sleep(450);
244             bob.touch();
245             mary.touch();
246         }
247         
248         instance.garbageCollect();
249         ut.begin();
250         if (!beanCache1.contains("bob")) {
251             fail("bob could not be found");
252         }
253         if (!beanCache2.contains("mary")) {
254             fail("mary could not be found");
255         }
256         if (beanCache1.contains("fred")) {
257             fail("fred was found");
258         }
259         if (beanCache2.contains("jill")) {
260             fail("jill was found");
261         }
262         ut.commit();
263         
264         instance.clear();
265         
266         ut.begin();
267         if (beanCache1.contains("bob")) {
268             fail("bob could still be found");
269         }
270         if (beanCache2.contains("mary")) {
271             fail("mary could still be found");
272         }
273         ut.commit();
274         try {
275             ut.begin();
276             beanCache1.addCacheEntry(500,"mary","mary","mary",mary);
277             ut.commit();
278             fail("Could add an entry should not be allowed");
279             
280         } catch (BeanException ex) {
281             ut.rollback();
282         }
283         try {
284             ut.begin();
285             
286             beanCache2.addCacheEntry(500,"bob","bob",bob);
287             fail("Could add an entry should not be allowed");
288         } catch (BeanException ex) {
289             ut.rollback();
290         }
291         
292         try {
293             ut.begin();
294             beanCache2 = instance.getBeanCache("beanCache2");
295             ut.commit();
296             fail("Could add an entry to the bean cache manager.");
297         } catch (BeanException ex) {
298             ut.rollback();
299         }
300         
301         if (TestCacheEntry.count != 4) {
302             fail ("Release not called on all classes");
303         }
304     }
305
306     
307 }
308
Popular Tags