KickJava   Java API By Example, From Geeks To Geeks.

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


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  * TransactionBeanCacheTest.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.ObjectSerializer;
62 import com.rift.coad.lib.cache.CacheEntry;
63 import com.rift.coad.lib.interceptor.InterceptorFactory;
64 import com.rift.coad.lib.security.RoleManager;
65 import com.rift.coad.lib.security.ThreadsPermissionContainer;
66 import com.rift.coad.lib.security.ThreadPermissionSession;
67 import com.rift.coad.lib.security.UserSession;
68 import com.rift.coad.lib.security.user.UserSessionManager;
69 import com.rift.coad.lib.security.user.UserStoreManager;
70 import com.rift.coad.lib.security.SessionManager;
71 import com.rift.coad.lib.security.login.LoginManager;
72 import com.rift.coad.lib.thread.CoadunationThreadGroup;
73 import com.rift.coad.lib.transaction.TransactionDirector;
74 import com.rift.coad.util.lock.ObjectLockFactory;
75 import com.rift.coad.util.transaction.TransactionManager;
76
77 /**
78  *
79  * @author Brett Chaldecott
80  */

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

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

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

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

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

124         public void cacheRelease() {
125             count++;
126         }
127     }
128     
129     
130     // private member variables
131
private TransactionBeanCache instance = null;
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 TransactionBeanCacheTest(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
149     public static Test suite() {
150         TestSuite suite = new TestSuite(TransactionBeanCacheTest.class);
151         
152         return suite;
153     }
154
155     /**
156      * Test of class com.rift.coad.lib.bean.BeanCache.
157      */

158     public void testCache() throws Exception JavaDoc {
159         System.out.println("TransactionBeanCacheTest");
160         
161         // init the session information
162
ThreadsPermissionContainer permissions = new ThreadsPermissionContainer();
163         SessionManager.init(permissions);
164         UserStoreManager userStoreManager = new UserStoreManager();
165         UserSessionManager sessionManager = new UserSessionManager(permissions,
166                 userStoreManager);
167         LoginManager.init(sessionManager,userStoreManager);
168         // instanciate the thread manager
169
CoadunationThreadGroup threadGroup = new CoadunationThreadGroup(sessionManager,
170             userStoreManager);
171         
172         // add a user to the session for the current thread
173
RoleManager.getInstance();
174         
175         InterceptorFactory.init(permissions,sessionManager,userStoreManager);
176         
177         // add a new user object and add to the permission
178
Set JavaDoc set = new HashSet JavaDoc();
179         set.add("test");
180         UserSession user = new UserSession("test1", set);
181         permissions.putSession(new Long JavaDoc(Thread.currentThread().getId()),
182                 new ThreadPermissionSession(
183                 new Long JavaDoc(Thread.currentThread().getId()),user));
184         
185         // init the naming director
186
NamingDirector.init(threadGroup);
187         
188         // instanciate the transaction director
189
TransactionDirector transactionDirector = TransactionDirector.init();
190         
191         // init the database source
192
DBSourceManager.init();
193         Context JavaDoc context = new InitialContext JavaDoc();
194         ObjectLockFactory.init();
195         TransactionManager.init();
196         
197         ut = (UserTransaction JavaDoc)context.lookup("java:comp/UserTransaction");
198         
199         
200         // the transaction bean cache
201
instance = new TransactionBeanCache();
202         
203         ut.begin();
204         TestCacheEntry bob = new TestCacheEntry();
205         instance.addCacheEntry(500,"bob","bob",bob);
206         Thread JavaDoc testThread = new Thread JavaDoc(new Runnable JavaDoc() {
207             public void run() {
208                 try {
209                     ut.begin();
210                     instance.addCacheEntry(500,"bob","bob",new TestCacheEntry());
211                     addedEntry = true;
212                     ut.commit();
213                 } catch (Exception JavaDoc ex) {
214                     System.out.println("Failed to get the queue reference : " +
215                             ex.getMessage());
216                     ex.printStackTrace(System.out);
217                     caughtException = true;
218                     try {
219                         ut.rollback();
220                     } catch (Exception JavaDoc ex2) {
221                         System.out.println(
222                                 "Failed to roll the transaction back : " +
223                                 ex2.getMessage());
224                     }
225                 }
226             }
227         });
228         testThread.start();
229         
230         Thread.sleep(500);
231         if (addedEntry) {
232             fail("An entry was added");
233         } else if (caughtException) {
234             fail("Caught an exception");
235         }
236         ut.commit();
237         Thread.sleep(500);
238         if (addedEntry) {
239             fail("An entry was added");
240         } else if (caughtException == false) {
241             fail("Failed to catch an exception");
242         }
243         
244         
245         ut.begin();
246         try {
247             instance.addCacheEntry(500,"bob","bob",bob);
248             fail("The second add must fail");
249         } catch (BeanException ex) {
250             System.out.println(ex.getMessage());
251         }
252         
253         
254         
255         ut.commit();
256         TestCacheEntry fred = new TestCacheEntry();
257         ut.begin();
258         instance.addCacheEntry(500,"fred","fred",fred);
259         try {
260             instance.addCacheEntry(500,"fred","fred",fred);
261             fail("The second add must fail");
262         } catch (BeanException ex) {
263             System.out.println(ex.getMessage());
264         }
265         ut.commit();
266         TestCacheEntry mary = new TestCacheEntry();
267         ut.begin();
268         instance.addCacheEntry(500,"mary","mary","mary",mary);
269         ut.commit();
270         
271         ut.begin();
272         if (bob != instance.getCacheEntry("bob").getCacheEntry()) {
273             fail("bob could not be found");
274         }
275         instance.getCacheEntry("bob").setCacheEntry(mary);
276         caughtException = false;
277         testThread = new Thread JavaDoc(new Runnable JavaDoc() {
278             public void run() {
279                 try {
280                     ut.begin();
281                     instance.getCacheEntry("bob");
282                     gotCacheEntry = true;
283                     ut.commit();
284                 } catch (Exception JavaDoc ex) {
285                     System.out.println("Failed to get the queue reference : " +
286                             ex.getMessage());
287                     ex.printStackTrace(System.out);
288                     caughtException = true;
289                     try {
290                         ut.rollback();
291                     } catch (Exception JavaDoc ex2) {
292                         System.out.println(
293                                 "Failed to roll the transaction back : " +
294                                 ex2.getMessage());
295                     }
296                 }
297             }
298         });
299         testThread.start();
300         
301         Thread.sleep(500);
302         if (gotCacheEntry) {
303             fail("Managed to retrieve the cache entry");
304         } else if (caughtException) {
305             fail("Caught an exception");
306         }
307         ut.commit();
308         Thread.sleep(500);
309         if (!gotCacheEntry) {
310             fail("Failed to retrieve the cache entry");
311         } else if (caughtException) {
312             fail("Caught an exception");
313         }
314         
315         ut.begin();
316         if (mary != instance.getCacheEntry("bob").getCacheEntry()) {
317             fail("mary could not be found");
318         }
319         ut.commit();
320         ut.begin();
321         if (mary != instance.getCacheEntry("mary").getBeanHandler()) {
322             fail("mary could not be found");
323         }
324         instance.getCacheEntry("mary").setBeanHandler(bob);
325         ut.commit();
326         ut.begin();
327         if (bob != instance.getCacheEntry("mary").getBeanHandler()) {
328             fail("bob could not be found");
329         }
330         if (!instance.getCacheEntry("mary").getProxy().equals("mary")) {
331             fail("mary could not be found");
332         }
333         instance.getCacheEntry("mary").setProxy("mary1");
334         if (!instance.getCacheEntry("mary").getProxy().equals("mary1")) {
335             fail("mary1 could not be found");
336         }
337         ut.commit();
338         
339         for (int count = 0; count < 4; count++) {
340             Thread.sleep(500);
341             bob.touch();
342             mary.touch();
343         }
344         
345         instance.garbageCollect();
346         
347         ut.begin();
348         if (!instance.contains("bob")) {
349             fail("bob could not be found");
350         }
351         if (!instance.contains("mary")) {
352             fail("mary could not be found");
353         }
354         if (instance.contains("fred")) {
355             fail("fred was found");
356         }
357         ut.commit();
358         
359         ut.begin();
360         instance.removeCacheEntry("bob");
361         instance.addCacheEntry(500,"fred","fred",new TestCacheEntry());
362         if (!instance.contains("fred")) {
363             fail("fred was found");
364         }
365         if (instance.contains("bob")) {
366             fail("bob was found");
367         }
368         ut.rollback();
369         ut.begin();
370         if (instance.contains("fred")) {
371             fail("fred was found");
372         }
373         if (!instance.contains("bob")) {
374             fail("bob was not found");
375         }
376         ut.commit();
377         instance.clear();
378         ut.begin();
379         if (instance.contains("bob")) {
380             fail("bob could still be found");
381         }
382         
383         if (instance.contains("mary")) {
384             fail("bob could still be found");
385         }
386         try {
387             instance.addCacheEntry(500,"mary","mary","mary",mary);
388             fail("Could add an entry should not be allowed");
389         } catch (BeanException ex) {
390             // ingore
391
}
392         try {
393             instance.addCacheEntry(500,"bob","bob",bob);
394             fail("Could add an entry should not be allowed");
395         } catch (BeanException ex) {
396             // ingore
397
}
398         
399         if (TestCacheEntry.count != 4) {
400             fail("Release not called on all classes [" + TestCacheEntry.count
401                     + "]");
402         }
403         ut.rollback();
404     }
405
406     
407 }
408
Popular Tags