001: /*
002: * CoadunationLib: The coaduntion implementation library.
003: * Copyright (C) 2006 Rift IT Contracting
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation; either
008: * version 2.1 of the License, or (at your option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, write to the Free Software
017: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
018: *
019: * BeanCacheManagerTest.java
020: *
021: * JUnit based test
022: */
023:
024: package com.rift.coad.lib.bean;
025:
026: // java imports
027: import javax.naming.InitialContext;
028: import javax.naming.Context;
029: import java.sql.PreparedStatement;
030: import java.sql.ResultSet;
031: import java.sql.Statement;
032: import javax.sql.DataSource;
033: import java.util.Set;
034: import java.util.HashSet;
035: import javax.transaction.UserTransaction;
036: import java.sql.Timestamp;
037: import java.util.ArrayList;
038: import java.util.Date;
039: import java.util.Enumeration;
040: import java.util.HashSet;
041: import java.util.Iterator;
042: import java.util.List;
043: import java.util.Set;
044: import javax.transaction.xa.XAException;
045: import javax.transaction.xa.XAResource;
046: import javax.transaction.xa.Xid;
047: import org.apache.log4j.Logger;
048: import org.apache.log4j.BasicConfigurator;
049:
050: // junit imports
051: import junit.framework.*;
052:
053: // object web imports
054: import org.objectweb.jotm.Jotm;
055:
056: // coadunation imports
057: import com.rift.coad.lib.naming.NamingDirector;
058: import com.rift.coad.lib.naming.ContextManager;
059: import com.rift.coad.lib.db.DBSourceManager;
060: import com.rift.coad.lib.common.ObjectSerializer;
061: import com.rift.coad.lib.cache.CacheEntry;
062: import com.rift.coad.lib.interceptor.InterceptorFactory;
063: import com.rift.coad.lib.security.RoleManager;
064: import com.rift.coad.lib.security.ThreadsPermissionContainer;
065: import com.rift.coad.lib.security.ThreadPermissionSession;
066: import com.rift.coad.lib.security.UserSession;
067: import com.rift.coad.lib.security.user.UserSessionManager;
068: import com.rift.coad.lib.security.user.UserStoreManager;
069: import com.rift.coad.lib.security.SessionManager;
070: import com.rift.coad.lib.security.login.LoginManager;
071: import com.rift.coad.lib.thread.CoadunationThreadGroup;
072: import com.rift.coad.lib.transaction.TransactionDirector;
073: import com.rift.coad.util.lock.ObjectLockFactory;
074: import com.rift.coad.util.transaction.TransactionManager;
075:
076: /**
077: *
078: * @author mincemeat
079: */
080: public class TransactionBeanCacheManagerTest extends TestCase {
081:
082: /**
083: * The cache entry to add
084: */
085: public static class TestCacheEntry implements CacheEntry,
086: java.rmi.Remote {
087:
088: // touch date
089: private Date touchTime = new Date();
090: public static int count = 0;
091:
092: /**
093: * The constructor of the test cache entry object.
094: */
095: public TestCacheEntry() {
096:
097: }
098:
099: /**
100: * The touch method of the test cache entry object.
101: */
102: public void touch() {
103: touchTime = new Date();
104: }
105:
106: /**
107: * This method returns true if this object has expired.
108: *
109: * @return TRUE if expired FALSE if not.
110: * @param expiryDate The date of the expiry.
111: */
112: public boolean isExpired(Date expiryDate) {
113: System.out.println("Touch time : " + touchTime.getTime()
114: + " expiry date : " + expiryDate.getTime());
115: return touchTime.getTime() < expiryDate.getTime();
116: }
117:
118: /**
119: * cache release
120: */
121: public void cacheRelease() {
122: count++;
123: }
124: }
125:
126: // private member variables
127: private UserTransaction ut = null;
128:
129: public TransactionBeanCacheManagerTest(String testName) {
130: super (testName);
131: }
132:
133: protected void setUp() throws Exception {
134: }
135:
136: protected void tearDown() throws Exception {
137: }
138:
139: public static Test suite() {
140: TestSuite suite = new TestSuite(
141: TransactionBeanCacheManagerTest.class);
142:
143: return suite;
144: }
145:
146: /**
147: * Test of BeanCacheManager method, of class com.rift.coad.lib.bean.BeanCacheManager.
148: */
149: public void testBeanCacheManager() throws Exception {
150: System.out.println("TransactionBeanCacheManager");
151:
152: // init the session information
153: ThreadsPermissionContainer permissions = new ThreadsPermissionContainer();
154: SessionManager.init(permissions);
155: UserStoreManager userStoreManager = new UserStoreManager();
156: UserSessionManager sessionManager = new UserSessionManager(
157: permissions, userStoreManager);
158: LoginManager.init(sessionManager, userStoreManager);
159: // instanciate the thread manager
160: CoadunationThreadGroup threadGroup = new CoadunationThreadGroup(
161: sessionManager, userStoreManager);
162:
163: // add a user to the session for the current thread
164: RoleManager.getInstance();
165:
166: InterceptorFactory.init(permissions, sessionManager,
167: userStoreManager);
168:
169: // add a new user object and add to the permission
170: Set set = new HashSet();
171: set.add("test");
172: UserSession user = new UserSession("test1", set);
173: permissions.putSession(
174: new Long(Thread.currentThread().getId()),
175: new ThreadPermissionSession(new Long(Thread
176: .currentThread().getId()), user));
177:
178: // init the naming director
179: NamingDirector.init(threadGroup);
180:
181: // instanciate the transaction director
182: TransactionDirector transactionDirector = TransactionDirector
183: .init();
184:
185: // init the database source
186: DBSourceManager.init();
187: Context context = new InitialContext();
188: ObjectLockFactory.init();
189: TransactionManager.init();
190:
191: ut = (UserTransaction) context
192: .lookup("java:comp/UserTransaction");
193:
194: Object ref = null;
195: TransactionBeanCacheManager instance = new TransactionBeanCacheManager();
196:
197: TransactionBeanCache beanCache1 = instance
198: .getBeanCache("beanCache1");
199: TransactionBeanCache beanCache2 = instance
200: .getBeanCache("beanCache2");
201: if (instance.getBeanCache("beanCache1") != beanCache1) {
202: fail("Failed to retrieve the bean cache result object");
203: }
204: if (!instance.contains("beanCache1")) {
205: fail("The instance does not contain beanCache1");
206: }
207: if (instance.getBeanCache("beanCache2") != beanCache2) {
208: fail("Failed to retrieve the bean cache 2 result object");
209: }
210: if (!instance.contains("beanCache2")) {
211: fail("The instance does not contain beanCache2");
212: }
213:
214: ut.begin();
215: TestCacheEntry bob = new TestCacheEntry();
216: beanCache1.addCacheEntry(500, "bob", "bob", bob);
217: System.out
218: .println("Class : " + beanCache1.getClass().getName());
219: ut.commit();
220: ut.begin();
221: TestCacheEntry fred = new TestCacheEntry();
222: beanCache1.addCacheEntry(500, "fred", "fred", fred);
223: ut.commit();
224: ut.begin();
225: TestCacheEntry mary = new TestCacheEntry();
226: beanCache2.addCacheEntry(500, "mary", "mary", "mary", mary);
227: ut.commit();
228: ut.begin();
229: TestCacheEntry jill = new TestCacheEntry();
230: beanCache2.addCacheEntry(500, "jill", "jill", "jill", jill);
231: ut.commit();
232:
233: ut.begin();
234: if (!beanCache1.contains("bob")) {
235: fail("bob could not be found");
236: }
237:
238: if (!beanCache2.contains("mary")) {
239: fail("mary could not be found");
240: }
241: if (!beanCache1.contains("fred")) {
242: fail("fred was found");
243: }
244: if (!beanCache2.contains("jill")) {
245: fail("jill was found");
246: }
247: ut.commit();
248: for (int count = 0; count < 4; count++) {
249: Thread.sleep(450);
250: bob.touch();
251: mary.touch();
252: }
253:
254: instance.garbageCollect();
255: ut.begin();
256: if (!beanCache1.contains("bob")) {
257: fail("bob could not be found");
258: }
259: if (!beanCache2.contains("mary")) {
260: fail("mary could not be found");
261: }
262: if (beanCache1.contains("fred")) {
263: fail("fred was found");
264: }
265: if (beanCache2.contains("jill")) {
266: fail("jill was found");
267: }
268: ut.commit();
269:
270: instance.clear();
271:
272: ut.begin();
273: if (beanCache1.contains("bob")) {
274: fail("bob could still be found");
275: }
276: if (beanCache2.contains("mary")) {
277: fail("mary could still be found");
278: }
279: ut.commit();
280: try {
281: ut.begin();
282: beanCache1.addCacheEntry(500, "mary", "mary", "mary", mary);
283: ut.commit();
284: fail("Could add an entry should not be allowed");
285:
286: } catch (BeanException ex) {
287: ut.rollback();
288: }
289: try {
290: ut.begin();
291:
292: beanCache2.addCacheEntry(500, "bob", "bob", bob);
293: fail("Could add an entry should not be allowed");
294: } catch (BeanException ex) {
295: ut.rollback();
296: }
297:
298: try {
299: ut.begin();
300: beanCache2 = instance.getBeanCache("beanCache2");
301: ut.commit();
302: fail("Could add an entry to the bean cache manager.");
303: } catch (BeanException ex) {
304: ut.rollback();
305: }
306:
307: if (TestCacheEntry.count != 4) {
308: fail("Release not called on all classes");
309: }
310: }
311:
312: }
|