KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > rift > coad > util > transaction > TransactionManagerTest


1 /*
2  * CoadunationUtil: The coaduntion utility 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  * TransactionManagerTest.java
20  */

21
22 package com.rift.coad.util.transaction;
23
24 import junit.framework.*;
25 import java.util.Iterator JavaDoc;
26 import java.util.HashSet JavaDoc;
27 import java.util.Map JavaDoc;
28 import java.util.LinkedHashMap JavaDoc;
29 import java.util.Set JavaDoc;
30 import java.util.concurrent.ConcurrentHashMap JavaDoc;
31 import javax.naming.Context JavaDoc;
32 import javax.naming.InitialContext JavaDoc;
33 import javax.transaction.xa.XAException JavaDoc;
34 import javax.transaction.xa.XAResource JavaDoc;
35 import javax.transaction.xa.Xid JavaDoc;
36 import org.apache.log4j.Logger;
37 import org.apache.log4j.BasicConfigurator;
38 import javax.transaction.UserTransaction JavaDoc;
39
40 // object web imports
41
import org.objectweb.jotm.Jotm;
42
43 // coadunation imports
44
import com.rift.coad.lib.naming.NamingDirector;
45 import com.rift.coad.lib.naming.ContextManager;
46 import com.rift.coad.lib.db.DBSourceManager;
47
48 import com.rift.coad.lib.interceptor.InterceptorFactory;
49 import com.rift.coad.lib.security.RoleManager;
50 import com.rift.coad.lib.security.ThreadsPermissionContainer;
51 import com.rift.coad.lib.security.ThreadPermissionSession;
52 import com.rift.coad.lib.security.UserSession;
53 import com.rift.coad.lib.security.user.UserSessionManager;
54 import com.rift.coad.lib.security.user.UserStoreManager;
55 import com.rift.coad.lib.security.SessionManager;
56 import com.rift.coad.lib.security.login.LoginManager;
57 import com.rift.coad.lib.thread.CoadunationThreadGroup;
58
59 import com.rift.coad.lib.configuration.Configuration;
60 import com.rift.coad.lib.configuration.ConfigurationFactory;
61 import com.rift.coad.util.lock.LockRef;
62 import com.rift.coad.util.lock.ObjectLockFactory;
63 import com.rift.coad.lib.transaction.TransactionDirector;
64
65 /**
66  * The test of the transaction manager object.
67  *
68  * @author Brett Chaldecott
69  */

70 public class TransactionManagerTest extends TestCase {
71     
72     public class TransactionTestResource implements XAResource JavaDoc {
73         
74         // private member variables
75
private String JavaDoc name = null;
76         
77         /**
78          * The transaction test resource contructor
79          */

80         public TransactionTestResource(String JavaDoc name) {
81             this.name = name;
82         }
83         
84         public void commit(Xid JavaDoc xid, boolean b) throws XAException JavaDoc {
85             System.out.println("Commit on : " + name);
86         }
87         
88         public void end(Xid JavaDoc xid, int i) throws XAException JavaDoc {
89         }
90         
91         public void forget(Xid JavaDoc xid) throws XAException JavaDoc {
92         }
93         
94         public int getTransactionTimeout() throws XAException JavaDoc {
95             return -1;
96         }
97         
98         public boolean isSameRM(XAResource JavaDoc xAResource) throws XAException JavaDoc {
99             return this == xAResource;
100         }
101         
102         public int prepare(Xid JavaDoc xid) throws XAException JavaDoc {
103             return -1;
104         }
105         
106         public Xid JavaDoc[] recover(int i) throws XAException JavaDoc {
107             return null;
108         }
109         
110         public void rollback(Xid JavaDoc xid) throws XAException JavaDoc {
111             System.out.println("Rollback on : " + name);
112         }
113         
114         public boolean setTransactionTimeout(int i) throws XAException JavaDoc {
115             return true;
116         }
117         
118         public void start(Xid JavaDoc xid, int i) throws XAException JavaDoc {
119             System.out.println("Start on : " + name + " id [" + xid + "]");
120         }
121         
122     }
123     
124     public TransactionManagerTest(String JavaDoc testName) {
125         super(testName);
126         BasicConfigurator.configure();
127     }
128     
129     protected void setUp() throws Exception JavaDoc {
130     }
131     
132     protected void tearDown() throws Exception JavaDoc {
133     }
134     
135     /**
136      * Test of of class com.rift.coad.util.transaction.TransactionManager.
137      */

138     public void testTransactionManager() throws Exception JavaDoc {
139         System.out.println("TransactionManager");
140         // init the session information
141
ThreadsPermissionContainer permissions = new ThreadsPermissionContainer();
142         SessionManager.init(permissions);
143         UserStoreManager userStoreManager = new UserStoreManager();
144         UserSessionManager sessionManager = new UserSessionManager(permissions,
145                 userStoreManager);
146         LoginManager.init(sessionManager,userStoreManager);
147         // instanciate the thread manager
148
CoadunationThreadGroup threadGroup = new CoadunationThreadGroup(sessionManager,
149                 userStoreManager);
150         
151         // add a user to the session for the current thread
152
RoleManager.getInstance();
153         
154         InterceptorFactory.init(permissions,sessionManager,userStoreManager);
155         
156         // add a new user object and add to the permission
157
Set JavaDoc set = new HashSet JavaDoc();
158         set.add("test");
159         UserSession user = new UserSession("test1", set);
160         permissions.putSession(new Long JavaDoc(Thread.currentThread().getId()),
161                 new ThreadPermissionSession(
162                 new Long JavaDoc(Thread.currentThread().getId()),user));
163         
164         // init the naming director
165
NamingDirector.init(threadGroup);
166         
167         // instanciate the transaction director
168
TransactionDirector transactionDirector = TransactionDirector.init();
169         
170         try {
171             TransactionManager.getInstance();
172             fail("Could retrieve a tranaction manager reference before init");
173         } catch (TransactionException ex) {
174             System.out.println(ex.getMessage());
175         }
176         
177         // init the transaction manager
178
ObjectLockFactory.init();
179         TransactionManager.init();
180         
181         TransactionManager expResult = TransactionManager.getInstance();
182         TransactionManager result = TransactionManager.getInstance();
183         assertEquals(expResult, result);
184         
185         // retrieve the user transaction
186
Context JavaDoc context = new InitialContext JavaDoc();
187         UserTransaction JavaDoc ut =
188                 (UserTransaction JavaDoc)context.lookup("java:comp/UserTransaction");
189         
190         ut.begin();
191         
192         TransactionTestResource testResource = new
193                 TransactionTestResource("test");
194         
195         result.bindResource(testResource,true);
196         result.bindResource(testResource,true);
197         
198         ut.commit();
199         
200         
201         TransactionTestResource testResource2 = new
202                 TransactionTestResource("test2");
203         
204         
205         ut.begin();
206         
207         result.bindResource(testResource,false);
208         result.bindResource(testResource2,true);
209         
210         ut.commit();
211         
212         try {
213             ut.begin();
214             
215             result.bindResource(testResource2,true);
216             result.bindResource(testResource2,false);
217             
218             
219             ut.commit();
220             fail("Was able to bind transaction lock incorrectly");
221         } catch (TransactionException ex) {
222             try {
223                 ut.rollback();
224             } catch (Exception JavaDoc ex2) {
225                 //
226
}
227         }
228         
229         
230         try {
231             ut.begin();
232             
233             result.bindResource(testResource2,false);
234             result.bindResource(testResource2,true);
235             
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 JavaDoc 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 }
257
Popular Tags