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: * TransactionDirectorTest.java
020: *
021: * JUnit based test
022: */
023:
024: package com.rift.coad.lib.transaction;
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 javax.transaction.TransactionManager;
037:
038: // junit imports
039: import junit.framework.*;
040:
041: // object web imports
042: import org.objectweb.jotm.Jotm;
043:
044: // coadunation imports
045: import com.rift.coad.lib.naming.NamingDirector;
046: import com.rift.coad.lib.naming.ContextManager;
047: import com.rift.coad.lib.db.DBSourceManager;
048:
049: import com.rift.coad.lib.interceptor.InterceptorFactory;
050: import com.rift.coad.lib.security.RoleManager;
051: import com.rift.coad.lib.security.ThreadsPermissionContainer;
052: import com.rift.coad.lib.security.ThreadPermissionSession;
053: import com.rift.coad.lib.security.UserSession;
054: import com.rift.coad.lib.security.user.UserSessionManager;
055: import com.rift.coad.lib.security.user.UserStoreManager;
056: import com.rift.coad.lib.security.SessionManager;
057: import com.rift.coad.lib.security.login.LoginManager;
058: import com.rift.coad.lib.thread.CoadunationThreadGroup;
059:
060: /**
061: *
062: * @author mincemeat
063: */
064: public class TransactionDirectorTest extends TestCase {
065:
066: public TransactionDirectorTest(String testName) {
067: super (testName);
068: }
069:
070: protected void setUp() throws Exception {
071: }
072:
073: protected void tearDown() throws Exception {
074: }
075:
076: public static Test suite() {
077: TestSuite suite = new TestSuite(TransactionDirectorTest.class);
078:
079: return suite;
080: }
081:
082: /**
083: * Test of TransactionDirector, of class com.rift.coad.lib.transaction.TransactionDirector.
084: */
085: public void testTransactionDirector() throws Exception {
086: System.out.println("TransactionDirector");
087:
088: // init the session information
089: ThreadsPermissionContainer permissions = new ThreadsPermissionContainer();
090: SessionManager.init(permissions);
091: UserStoreManager userStoreManager = new UserStoreManager();
092: UserSessionManager sessionManager = new UserSessionManager(
093: permissions, userStoreManager);
094: LoginManager.init(sessionManager, userStoreManager);
095: // instanciate the thread manager
096: CoadunationThreadGroup threadGroup = new CoadunationThreadGroup(
097: sessionManager, userStoreManager);
098:
099: // add a user to the session for the current thread
100: RoleManager.getInstance();
101:
102: InterceptorFactory.init(permissions, sessionManager,
103: userStoreManager);
104:
105: // add a new user object and add to the permission
106: Set set = new HashSet();
107: set.add("test");
108: UserSession user = new UserSession("test1", set);
109: permissions.putSession(
110: new Long(Thread.currentThread().getId()),
111: new ThreadPermissionSession(new Long(Thread
112: .currentThread().getId()), user));
113:
114: // init the naming director
115: NamingDirector.init(threadGroup);
116:
117: // instanciate the transaction director
118: TransactionDirector result = TransactionDirector.init();
119:
120: if (result != TransactionDirector.getInstance()) {
121: fail("Singleton does not operate correctly");
122: }
123:
124: // init the database source
125: DBSourceManager.init();
126:
127: Context context = new InitialContext();
128: DataSource ds = (DataSource) context
129: .lookup("java:comp/env/jdbc/test");
130: Object ref = context.lookup("java:comp/UserTransaction");
131: System.out.println("User transaction is ["
132: + ref.getClass().getName() + "]");
133: UserTransaction ut = (UserTransaction) context
134: .lookup("java:comp/UserTransaction");
135: if (ut == null) {
136: fail("User transaction is equal to null");
137: }
138: if (context.lookup("java:comp/TransactionManager") == null) {
139: fail("Cannot retrieve valid transaction manager");
140: }
141:
142: java.sql.Connection conn = ds.getConnection();
143: ut.begin();
144:
145: Statement stmt = conn.createStatement();
146: stmt.execute("INSERT INTO test (id) VALUES (1)");
147: ResultSet rs = stmt.executeQuery("SELECT * FROM test");
148: if (!rs.next()) {
149: fail("Failed to retrieve result from database");
150: }
151:
152: ut.rollback();
153:
154: stmt = conn.createStatement();
155: rs = stmt.executeQuery("SELECT * FROM test");
156: if (rs.next()) {
157: fail("Failed: retrieved result from database");
158: }
159:
160: conn.close();
161:
162: result.stop();
163: NamingDirector.getInstance().shutdown();
164: }
165:
166: }
|