001: /*
002: * MessageService: The message service daemon
003: * Copyright (C) 2007 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: * IDLockTest.java
020: */
021:
022: package com.rift.coad.daemon.messageservice;
023:
024: // java imports
025: import java.util.Iterator;
026: import java.util.Set;
027: import java.util.HashSet;
028: import java.util.Map;
029: import java.util.LinkedHashMap;
030: import java.util.HashMap;
031: import java.util.concurrent.ConcurrentHashMap;
032: import javax.naming.Context;
033: import javax.naming.InitialContext;
034: import javax.transaction.xa.XAException;
035: import javax.transaction.xa.XAResource;
036: import javax.transaction.xa.Xid;
037: import org.apache.log4j.Logger;
038: import com.rift.coad.util.transaction.TransactionManager;
039:
040: // junit imports
041: import junit.framework.*;
042:
043: // object web imports
044: import org.objectweb.jotm.Jotm;
045:
046: // coadunation imports
047: import com.rift.coad.lib.naming.NamingDirector;
048: import com.rift.coad.lib.naming.ContextManager;
049: import com.rift.coad.lib.db.DBSourceManager;
050: import com.rift.coad.lib.common.ObjectSerializer;
051: import com.rift.coad.lib.interceptor.InterceptorFactory;
052: import com.rift.coad.lib.security.RoleManager;
053: import com.rift.coad.lib.security.ThreadsPermissionContainer;
054: import com.rift.coad.lib.security.ThreadPermissionSession;
055: import com.rift.coad.lib.security.UserSession;
056: import com.rift.coad.lib.security.user.UserSessionManager;
057: import com.rift.coad.lib.security.user.UserStoreManager;
058: import com.rift.coad.lib.security.SessionManager;
059: import com.rift.coad.lib.security.login.LoginManager;
060: import com.rift.coad.lib.thread.CoadunationThreadGroup;
061: import com.rift.coad.lib.transaction.TransactionDirector;
062: import com.rift.coad.daemon.messageservice.Message;
063: import com.rift.coad.daemon.messageservice.MessageError;
064: import com.rift.coad.daemon.messageservice.RPCMessage;
065: import com.rift.coad.daemon.messageservice.TextMessage;
066: import com.rift.coad.daemon.messageservice.MessageManager;
067: import com.rift.coad.daemon.messageservice.MessageServiceException;
068: import com.rift.coad.daemon.messageservice.MessageServiceManager;
069: import com.rift.coad.daemon.messageservice.db.*;
070: import com.rift.coad.daemon.messageservice.message.MessageImpl;
071: import com.rift.coad.daemon.messageservice.message.RPCMessageImpl;
072: import com.rift.coad.daemon.messageservice.message.TextMessageImpl;
073: import com.rift.coad.hibernate.util.HibernateUtil;
074: import com.rift.coad.util.lock.ObjectLockFactory;
075: import com.rift.coad.util.transaction.TransactionManager;
076: import com.rift.coad.util.transaction.UserTransactionWrapper;
077: import com.rift.coad.util.change.ChangeLog;
078: import com.rift.coad.lib.thread.ThreadGroupManager;
079:
080: /**
081: * This is the test of the id lock object.
082: *
083: * @author brett
084: */
085: public class IDLockTest extends TestCase {
086:
087: /**
088: * This object is responsible for processing the threads.
089: */
090: public class IDLockThread extends Thread {
091: private UserTransactionWrapper utw = null;
092:
093: /**
094: * The constructor of the id lock thread
095: */
096: public IDLockThread() throws Exception {
097: utw = new UserTransactionWrapper();
098: }
099:
100: /**
101: * This method is used to test the id lock
102: */
103: public void run() {
104: try {
105: utw.begin();
106: IDLock.getInstance().lock("test");
107: lockCount++;
108: utw.commit();
109: } catch (Exception ex) {
110: System.out.println("Failed to lock.");
111: } finally {
112: utw.release();
113: }
114: }
115: }
116:
117: // private member variables
118: private int lockCount = 0;
119:
120: public IDLockTest(String testName) {
121: super (testName);
122: }
123:
124: protected void setUp() throws Exception {
125: }
126:
127: protected void tearDown() throws Exception {
128: }
129:
130: /**
131: * Test of class com.rift.coad.daemon.messageservice.IDLock.
132: */
133: public void testIDLock() throws Exception {
134: System.out.println("testIDLock");
135:
136: Thread.currentThread().setContextClassLoader(
137: this .getClass().getClassLoader());
138:
139: // init the session information
140: ThreadsPermissionContainer permissions = new ThreadsPermissionContainer();
141: SessionManager.init(permissions);
142: UserStoreManager userStoreManager = new UserStoreManager();
143: UserSessionManager sessionManager = new UserSessionManager(
144: permissions, userStoreManager);
145: LoginManager.init(sessionManager, userStoreManager);
146: // instanciate the thread manager
147: CoadunationThreadGroup threadGroup = new CoadunationThreadGroup(
148: sessionManager, userStoreManager);
149:
150: // add a user to the session for the current thread
151: RoleManager.getInstance();
152:
153: InterceptorFactory.init(permissions, sessionManager,
154: userStoreManager);
155:
156: // add a new user object and add to the permission
157: Set set = new HashSet();
158: set.add("test");
159: UserSession user = new UserSession("test1", set);
160: permissions.putSession(
161: new Long(Thread.currentThread().getId()),
162: new ThreadPermissionSession(new Long(Thread
163: .currentThread().getId()), user));
164:
165: // instanciate the thread manager
166: ThreadGroupManager.getInstance().initThreadGroup(threadGroup);
167:
168: // init the naming director
169: NamingDirector.init(threadGroup);
170:
171: // instanciate the transaction director
172: TransactionDirector transactionDirector = TransactionDirector
173: .init();
174:
175: // init the database source
176: DBSourceManager.init();
177: ObjectLockFactory.init();
178: TransactionManager.init();
179:
180: IDLock result = IDLock.getInstance();
181: assertEquals(IDLock.getInstance(), result);
182:
183: UserTransactionWrapper utw = new UserTransactionWrapper();
184:
185: utw.begin();
186: IDLock.getInstance().lock("test");
187: IDLockThread thread1 = new IDLockThread();
188: thread1.start();
189: IDLockThread thread2 = new IDLockThread();
190: thread2.start();
191:
192: Thread.sleep(500);
193:
194: if (lockCount == 2) {
195: fail("Threads aquired the lock when it has not been released.");
196: }
197:
198: utw.commit();
199: utw.release();
200:
201: Thread.sleep(500);
202:
203: if (lockCount != 2) {
204: fail("Threads could not aquire the lock.");
205: }
206:
207: utw.begin();
208: IDLock.getInstance().lock("test");
209: IDLockThread thread3 = new IDLockThread();
210: thread3.start();
211: IDLockThread thread4 = new IDLockThread();
212: thread4.start();
213:
214: Thread.sleep(500);
215:
216: if (lockCount == 4) {
217: fail("Threads aquired the lock when it has not been released.");
218: }
219:
220: utw.release();
221:
222: Thread.sleep(500);
223:
224: if (lockCount != 4) {
225: fail("Threads could not aquire the lock.");
226: }
227:
228: }
229: }
|