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