KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > rift > coad > daemon > messageservice > IDLockTest


1 /*
2  * MessageService: The message service daemon
3  * Copyright (C) 2007 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  * IDLockTest.java
20  */

21
22 package com.rift.coad.daemon.messageservice;
23
24 // java imports
25
import java.util.Iterator JavaDoc;
26 import java.util.Set JavaDoc;
27 import java.util.HashSet JavaDoc;
28 import java.util.Map JavaDoc;
29 import java.util.LinkedHashMap JavaDoc;
30 import java.util.HashMap JavaDoc;
31 import java.util.concurrent.ConcurrentHashMap JavaDoc;
32 import javax.naming.Context JavaDoc;
33 import javax.naming.InitialContext JavaDoc;
34 import javax.transaction.xa.XAException JavaDoc;
35 import javax.transaction.xa.XAResource JavaDoc;
36 import javax.transaction.xa.Xid JavaDoc;
37 import org.apache.log4j.Logger;
38 import com.rift.coad.util.transaction.TransactionManager;
39
40 // junit imports
41
import junit.framework.*;
42
43 // object web imports
44
import org.objectweb.jotm.Jotm;
45
46 // coadunation imports
47
import com.rift.coad.lib.naming.NamingDirector;
48 import com.rift.coad.lib.naming.ContextManager;
49 import com.rift.coad.lib.db.DBSourceManager;
50 import com.rift.coad.lib.common.ObjectSerializer;
51 import com.rift.coad.lib.interceptor.InterceptorFactory;
52 import com.rift.coad.lib.security.RoleManager;
53 import com.rift.coad.lib.security.ThreadsPermissionContainer;
54 import com.rift.coad.lib.security.ThreadPermissionSession;
55 import com.rift.coad.lib.security.UserSession;
56 import com.rift.coad.lib.security.user.UserSessionManager;
57 import com.rift.coad.lib.security.user.UserStoreManager;
58 import com.rift.coad.lib.security.SessionManager;
59 import com.rift.coad.lib.security.login.LoginManager;
60 import com.rift.coad.lib.thread.CoadunationThreadGroup;
61 import com.rift.coad.lib.transaction.TransactionDirector;
62 import com.rift.coad.daemon.messageservice.Message;
63 import com.rift.coad.daemon.messageservice.MessageError;
64 import com.rift.coad.daemon.messageservice.RPCMessage;
65 import com.rift.coad.daemon.messageservice.TextMessage;
66 import com.rift.coad.daemon.messageservice.MessageManager;
67 import com.rift.coad.daemon.messageservice.MessageServiceException;
68 import com.rift.coad.daemon.messageservice.MessageServiceImpl;
69 import com.rift.coad.daemon.messageservice.db.*;
70 import com.rift.coad.daemon.messageservice.message.MessageImpl;
71 import com.rift.coad.daemon.messageservice.message.RPCMessageImpl;
72 import com.rift.coad.daemon.messageservice.message.TextMessageImpl;
73 import com.rift.coad.hibernate.util.HibernateUtil;
74 import com.rift.coad.util.lock.ObjectLockFactory;
75 import com.rift.coad.util.transaction.TransactionManager;
76 import com.rift.coad.util.transaction.UserTransactionWrapper;
77 import com.rift.coad.util.change.ChangeLog;
78 import com.rift.coad.lib.thread.ThreadGroupManager;
79
80
81 /**
82  * This is the test of the id lock object.
83  *
84  * @author brett
85  */

86 public class IDLockTest extends TestCase {
87     
88     /**
89      * This object is responsible for processing the threads.
90      */

91     public class IDLockThread extends Thread JavaDoc {
92         private UserTransactionWrapper utw = null;
93         /**
94          * The constructor of the id lock thread
95          */

96         public IDLockThread() throws Exception JavaDoc {
97             utw = new UserTransactionWrapper();
98         }
99         
100         
101         /**
102          * This method is used to test the id lock
103          */

104         public void run() {
105             try {
106                 utw.begin();
107                 IDLock.getInstance().lock("test");
108                 lockCount++;
109                 utw.commit();
110             } catch (Exception JavaDoc ex) {
111                 System.out.println("Failed to lock.");
112             } finally {
113                 utw.release();
114             }
115         }
116     }
117     
118     // private member variables
119
private int lockCount = 0;
120     
121     public IDLockTest(String JavaDoc testName) {
122         super(testName);
123     }
124
125     protected void setUp() throws Exception JavaDoc {
126     }
127
128     protected void tearDown() throws Exception JavaDoc {
129     }
130
131     /**
132      * Test of class com.rift.coad.daemon.messageservice.IDLock.
133      */

134     public void testIDLock() throws Exception JavaDoc {
135         System.out.println("testIDLock");
136         
137         Thread.currentThread().setContextClassLoader(this.getClass().
138                 getClassLoader());
139         
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(
149                 sessionManager,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         // instanciate the thread manager
165
ThreadGroupManager.getInstance().initThreadGroup(threadGroup);
166         
167         
168         // init the naming director
169
NamingDirector.init(threadGroup);
170         
171         // instanciate the transaction director
172
TransactionDirector transactionDirector = TransactionDirector.init();
173         
174         // init the database source
175
DBSourceManager.init();
176         ObjectLockFactory.init();
177         TransactionManager.init();
178         
179         IDLock result = IDLock.getInstance();
180         assertEquals(IDLock.getInstance(), result);
181         
182         UserTransactionWrapper utw = new UserTransactionWrapper();
183         
184         utw.begin();
185         IDLock.getInstance().lock("test");
186         IDLockThread thread1 = new IDLockThread();
187         thread1.start();
188         IDLockThread thread2 = new IDLockThread();
189         thread2.start();
190         
191         Thread.sleep(500);
192         
193         if (lockCount == 2) {
194             fail("Threads aquired the lock when it has not been released.");
195         }
196         
197         utw.commit();
198         utw.release();
199         
200         Thread.sleep(500);
201         
202         if (lockCount != 2) {
203             fail("Threads could not aquire the lock.");
204         }
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 }
230
Popular Tags