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: * TransactionRMICacheTest.java
020: *
021: * JUnit based test
022: */
023:
024: // package path
025: package com.rift.coad.lib.deployment.rmi;
026:
027: // java imports
028: import javax.naming.InitialContext;
029: import javax.naming.Context;
030: import java.sql.PreparedStatement;
031: import java.sql.ResultSet;
032: import java.sql.Statement;
033: import javax.sql.DataSource;
034: import java.util.Set;
035: import java.util.HashSet;
036: import javax.transaction.UserTransaction;
037: import java.sql.Timestamp;
038: import java.util.ArrayList;
039: import java.util.Date;
040: import java.util.Enumeration;
041: import java.util.HashSet;
042: import java.util.Iterator;
043: import java.util.List;
044: import java.util.Set;
045: import javax.transaction.xa.XAException;
046: import javax.transaction.xa.XAResource;
047: import javax.transaction.xa.Xid;
048: import org.apache.log4j.Logger;
049: import org.apache.log4j.BasicConfigurator;
050:
051: // junit imports
052: import junit.framework.*;
053:
054: // object web imports
055: import org.objectweb.jotm.Jotm;
056:
057: // coadunation imports
058: import com.rift.coad.lib.naming.NamingDirector;
059: import com.rift.coad.lib.naming.ContextManager;
060: import com.rift.coad.lib.db.DBSourceManager;
061: import com.rift.coad.lib.common.RandomGuid;
062: import com.rift.coad.lib.common.ObjectSerializer;
063: import com.rift.coad.lib.cache.CacheEntry;
064: import com.rift.coad.lib.interceptor.InterceptorFactory;
065: import com.rift.coad.lib.security.RoleManager;
066: import com.rift.coad.lib.security.ThreadsPermissionContainer;
067: import com.rift.coad.lib.security.ThreadPermissionSession;
068: import com.rift.coad.lib.security.UserSession;
069: import com.rift.coad.lib.security.user.UserSessionManager;
070: import com.rift.coad.lib.security.user.UserStoreManager;
071: import com.rift.coad.lib.security.SessionManager;
072: import com.rift.coad.lib.security.login.LoginManager;
073: import com.rift.coad.lib.thread.CoadunationThreadGroup;
074: import com.rift.coad.lib.transaction.TransactionDirector;
075: import com.rift.coad.util.lock.ObjectLockFactory;
076: import com.rift.coad.util.transaction.TransactionManager;
077:
078: /**
079: * This class is responsible for testing the transaction RMI cache.
080: * @author Brett Chaldecott
081: */
082: public class TransactionRMICacheTest extends TestCase {
083:
084: /**
085: * The test cache class
086: */
087: public static class RMICacheTestClass implements java.rmi.Remote,
088: CacheEntry {
089: // private member variables
090: private String id = null;
091: private Date lastTouchTime = new Date();
092: public static int count = 0;
093:
094: /**
095: * The constructor of the cache entry
096: */
097: public RMICacheTestClass() throws Exception {
098: id = RandomGuid.getInstance().getGuid();
099: }
100:
101: /**
102: * This method will return true if the date is older than the given expiry
103: * date.
104: *
105: * @return TRUE if expired FALSE if not.
106: * @param expiryDate The expiry date to perform the check with.
107: */
108: public boolean isExpired(Date expiryDate) {
109: System.out.println("Current time : "
110: + 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();
127: }
128:
129: }
130:
131: public TransactionRMICacheTest(String testName) {
132: super (testName);
133: }
134:
135: protected void setUp() throws Exception {
136: }
137:
138: protected void tearDown() throws Exception {
139: }
140:
141: public static Test suite() {
142: TestSuite suite = new TestSuite(TransactionRMICacheTest.class);
143:
144: return suite;
145: }
146:
147: // user transaction
148: private UserTransaction ut = null;
149: private boolean addedEntry = false;
150: private boolean caughtException = false;
151: private boolean gotCacheEntry = false;
152:
153: /**
154: * Test of garbageCollect method, of class com.rift.coad.lib.deployment.rmi.RMICache.
155: */
156: public void testRMICache() throws Exception {
157: System.out.println("testRMICache");
158:
159: // init the session information
160: ThreadsPermissionContainer permissions = new ThreadsPermissionContainer();
161: SessionManager.init(permissions);
162: UserStoreManager userStoreManager = new UserStoreManager();
163: UserSessionManager sessionManager = new UserSessionManager(
164: permissions, userStoreManager);
165: LoginManager.init(sessionManager, userStoreManager);
166: // instanciate the thread manager
167: CoadunationThreadGroup threadGroup = new CoadunationThreadGroup(
168: sessionManager, userStoreManager);
169:
170: // add a user to the session for the current thread
171: RoleManager.getInstance();
172:
173: InterceptorFactory.init(permissions, sessionManager,
174: userStoreManager);
175:
176: // add a new user object and add to the permission
177: Set set = new HashSet();
178: set.add("test");
179: UserSession user = new UserSession("test1", set);
180: permissions.putSession(
181: new Long(Thread.currentThread().getId()),
182: new ThreadPermissionSession(new Long(Thread
183: .currentThread().getId()), user));
184:
185: // init the naming director
186: NamingDirector.init(threadGroup);
187:
188: // instanciate the transaction director
189: TransactionDirector transactionDirector = TransactionDirector
190: .init();
191:
192: // init the database source
193: DBSourceManager.init();
194: Context context = new InitialContext();
195: ObjectLockFactory.init();
196: TransactionManager.init();
197:
198: ut = (UserTransaction) context
199: .lookup("java:comp/UserTransaction");
200:
201: TransactionRMICache instance = new TransactionRMICache();
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().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().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: }
|