001: /*
002: * CoadunationUtil: The coaduntion utility 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: * TransactionManagerTest.java
020: */
021:
022: package com.rift.coad.util.transaction;
023:
024: import junit.framework.*;
025: import java.util.Iterator;
026: import java.util.HashSet;
027: import java.util.Map;
028: import java.util.LinkedHashMap;
029: import java.util.Set;
030: import java.util.concurrent.ConcurrentHashMap;
031: import javax.naming.Context;
032: import javax.naming.InitialContext;
033: import javax.transaction.xa.XAException;
034: import javax.transaction.xa.XAResource;
035: import javax.transaction.xa.Xid;
036: import org.apache.log4j.Logger;
037: import org.apache.log4j.BasicConfigurator;
038: import javax.transaction.UserTransaction;
039:
040: // object web imports
041: import org.objectweb.jotm.Jotm;
042:
043: // coadunation imports
044: import com.rift.coad.lib.naming.NamingDirector;
045: import com.rift.coad.lib.naming.ContextManager;
046: import com.rift.coad.lib.db.DBSourceManager;
047:
048: import com.rift.coad.lib.interceptor.InterceptorFactory;
049: import com.rift.coad.lib.security.RoleManager;
050: import com.rift.coad.lib.security.ThreadsPermissionContainer;
051: import com.rift.coad.lib.security.ThreadPermissionSession;
052: import com.rift.coad.lib.security.UserSession;
053: import com.rift.coad.lib.security.user.UserSessionManager;
054: import com.rift.coad.lib.security.user.UserStoreManager;
055: import com.rift.coad.lib.security.SessionManager;
056: import com.rift.coad.lib.security.login.LoginManager;
057: import com.rift.coad.lib.thread.CoadunationThreadGroup;
058:
059: import com.rift.coad.lib.configuration.Configuration;
060: import com.rift.coad.lib.configuration.ConfigurationFactory;
061: import com.rift.coad.util.lock.LockRef;
062: import com.rift.coad.util.lock.ObjectLockFactory;
063: import com.rift.coad.lib.transaction.TransactionDirector;
064:
065: /**
066: * The test of the transaction manager object.
067: *
068: * @author Brett Chaldecott
069: */
070: public class TransactionManagerTest extends TestCase {
071:
072: public class TransactionTestResource implements XAResource {
073:
074: // private member variables
075: private String name = null;
076:
077: /**
078: * The transaction test resource contructor
079: */
080: public TransactionTestResource(String name) {
081: this .name = name;
082: }
083:
084: public void commit(Xid xid, boolean b) throws XAException {
085: System.out.println("Commit on : " + name);
086: }
087:
088: public void end(Xid xid, int i) throws XAException {
089: }
090:
091: public void forget(Xid xid) throws XAException {
092: }
093:
094: public int getTransactionTimeout() throws XAException {
095: return -1;
096: }
097:
098: public boolean isSameRM(XAResource xAResource)
099: throws XAException {
100: return this == xAResource;
101: }
102:
103: public int prepare(Xid xid) throws XAException {
104: return -1;
105: }
106:
107: public Xid[] recover(int i) throws XAException {
108: return null;
109: }
110:
111: public void rollback(Xid xid) throws XAException {
112: System.out.println("Rollback on : " + name);
113: }
114:
115: public boolean setTransactionTimeout(int i) throws XAException {
116: return true;
117: }
118:
119: public void start(Xid xid, int i) throws XAException {
120: System.out.println("Start on : " + name + " id [" + xid
121: + "]");
122: }
123:
124: }
125:
126: public TransactionManagerTest(String testName) {
127: super (testName);
128: BasicConfigurator.configure();
129: }
130:
131: protected void setUp() throws Exception {
132: }
133:
134: protected void tearDown() throws Exception {
135: }
136:
137: /**
138: * Test of of class com.rift.coad.util.transaction.TransactionManager.
139: */
140: public void testTransactionManager() throws Exception {
141: System.out.println("TransactionManager");
142: // init the session information
143: ThreadsPermissionContainer permissions = new ThreadsPermissionContainer();
144: SessionManager.init(permissions);
145: UserStoreManager userStoreManager = new UserStoreManager();
146: UserSessionManager sessionManager = new UserSessionManager(
147: permissions, userStoreManager);
148: LoginManager.init(sessionManager, userStoreManager);
149: // instanciate the thread manager
150: CoadunationThreadGroup threadGroup = new CoadunationThreadGroup(
151: sessionManager, userStoreManager);
152:
153: // add a user to the session for the current thread
154: RoleManager.getInstance();
155:
156: InterceptorFactory.init(permissions, sessionManager,
157: userStoreManager);
158:
159: // add a new user object and add to the permission
160: Set set = new HashSet();
161: set.add("test");
162: UserSession user = new UserSession("test1", set);
163: permissions.putSession(
164: new Long(Thread.currentThread().getId()),
165: new ThreadPermissionSession(new Long(Thread
166: .currentThread().getId()), user));
167:
168: // init the naming director
169: NamingDirector.init(threadGroup);
170:
171: // instanciate the transaction director
172: TransactionDirector transactionDirector = TransactionDirector
173: .init();
174:
175: try {
176: TransactionManager.getInstance();
177: fail("Could retrieve a tranaction manager reference before init");
178: } catch (TransactionException ex) {
179: System.out.println(ex.getMessage());
180: }
181:
182: // init the transaction manager
183: ObjectLockFactory.init();
184: TransactionManager.init();
185:
186: TransactionManager expResult = TransactionManager.getInstance();
187: TransactionManager result = TransactionManager.getInstance();
188: assertEquals(expResult, result);
189:
190: // retrieve the user transaction
191: Context context = new InitialContext();
192: UserTransaction ut = (UserTransaction) context
193: .lookup("java:comp/UserTransaction");
194:
195: ut.begin();
196:
197: TransactionTestResource testResource = new TransactionTestResource(
198: "test");
199:
200: result.bindResource(testResource, true);
201: result.bindResource(testResource, true);
202:
203: ut.commit();
204:
205: TransactionTestResource testResource2 = new TransactionTestResource(
206: "test2");
207:
208: ut.begin();
209:
210: result.bindResource(testResource, false);
211: result.bindResource(testResource2, true);
212:
213: ut.commit();
214:
215: try {
216: ut.begin();
217:
218: result.bindResource(testResource2, true);
219: result.bindResource(testResource2, false);
220:
221: ut.commit();
222: fail("Was able to bind transaction lock incorrectly");
223: } catch (TransactionException ex) {
224: try {
225: ut.rollback();
226: } catch (Exception ex2) {
227: //
228: }
229: }
230:
231: try {
232: ut.begin();
233:
234: result.bindResource(testResource2, false);
235: result.bindResource(testResource2, true);
236:
237: ut.commit();
238: fail("Was able to bind transaction lock incorrectly");
239: } catch (TransactionException ex) {
240: try {
241: ut.rollback();
242: } catch (Exception ex2) {
243: //
244: }
245: }
246:
247: TransactionManager.fin();
248: try {
249: TransactionManager.getInstance();
250: fail("Could retrieve a tranaction manager reference after fin");
251: } catch (TransactionException ex) {
252: System.out.println(ex.getMessage());
253: }
254: }
255:
256: }
|