KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > rift > coad > lib > thread > CoadunationThreadGroupTest


1 /*
2  * CoadunationLib: The coaduntion implementation 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  * CoadunationThreadGroupTest.java
20  *
21  * JUnit based test
22  */

23
24 package com.rift.coad.lib.thread;
25
26 import java.net.URLClassLoader JavaDoc;
27 import java.net.URL JavaDoc;
28 import junit.framework.*;
29 import java.util.Vector JavaDoc;
30 import java.util.List JavaDoc;
31 import java.util.Date JavaDoc;
32 import java.util.ArrayList JavaDoc;
33 import org.apache.log4j.Logger;
34 import com.rift.coad.lib.security.UserSession;
35 import com.rift.coad.lib.security.user.UserSessionManager;
36 import com.rift.coad.lib.configuration.ConfigurationFactory;
37 import com.rift.coad.lib.configuration.Configuration;
38 import com.rift.coad.lib.security.user.UserStoreManager;
39 import com.rift.coad.lib.security.ThreadsPermissionContainer;
40 import com.rift.coad.lib.security.login.handlers.PasswordInfoHandler;
41 import com.rift.coad.lib.security.SessionManager;
42 import com.rift.coad.lib.security.RoleManager;
43 import com.rift.coad.lib.security.Validator;
44 import com.rift.coad.lib.security.login.LoginManager;
45 import com.rift.coad.lib.thread.CoadunationThread;
46
47
48 /**
49  *
50  * @author mincemeat
51  */

52 public class CoadunationThreadGroupTest extends TestCase {
53     
54     /**
55      * The class defintion
56      */

57     public static class TestMonitor {
58         // constants
59
private static final long TIMEOUT = 10000;
60         
61         // static variables
62
private static TestMonitor singleton = null;
63         
64         // private member variables
65
private long count = 0;
66         
67         
68         private TestMonitor() {
69         }
70         
71         public static synchronized TestMonitor getInstance() {
72             if (singleton == null) {
73                 singleton = new TestMonitor();
74             }
75             return singleton;
76         }
77         
78         
79         /**
80          * This method will notify any waiting threads that the call landed.
81          */

82         public synchronized void notifyTest() {
83             count++;
84             notify();
85         }
86         
87         
88         /**
89          * Wait for the test to complete.
90          *
91          * @return TRUE if the test succeeded, FALSE if not.
92          * @exception Exception
93          */

94         public synchronized boolean waitForTest() throws Exception JavaDoc {
95             try {
96                 long startTime = new Date JavaDoc().getTime();
97                 while (count < 6) {
98                     wait(TIMEOUT);
99                     long currentTime = new Date JavaDoc().getTime();
100                     if (count == 6) {
101                         return true;
102                     } else if ((currentTime - TIMEOUT) >= startTime) {
103                         return false;
104                     }
105                 }
106                 return true;
107             } catch (Exception JavaDoc ex) {
108                 throw new Exception JavaDoc ("Failed to wait for test : " +
109                         ex.getMessage(),ex);
110             }
111         }
112     }
113     
114     /**
115      *
116      */

117     public static class SubThread extends CoadunationThread {
118         // the class log variable
119
public static int subThreadTerminateCount = 0;
120         protected Logger log =
121             Logger.getLogger(TestThread.class.getName());
122         
123         // the flag
124
private boolean terminated = false;
125         
126         /**
127          * This object is used to test the threading funcationlity implemented
128          * by the threading group.
129          */

130         public SubThread() throws Exception JavaDoc {
131         }
132         
133         
134         /**
135          * This method replaces the run method in the BasicThread.
136          *
137          * @exception Exception
138          */

139         public void process() throws Exception JavaDoc {
140             boolean notify = false;
141             while(isTerminated() == false) {
142                 System.out.println("Message from sub thread : " + this.getId());
143                 if (isTerminated() == false) {
144                     delay(2000);
145                 }
146                 if (notify == false) {
147                     com.rift.coad.lib.thread.CoadunationThreadGroupTest.TestMonitor.
148                             getInstance().notifyTest();
149                     notify = true;
150                 }
151             }
152             System.out.println("Sub Thread id [" + this.getId() +
153                     "] is terminated");
154             subThreadTerminateCount++;
155         }
156
157
158         /**
159          * This method will be implemented by child objects to terminate the
160          * processing of this thread.
161          */

162         public synchronized void terminate() {
163             terminated = true;
164             notify();
165         }
166         
167         
168         /**
169          * This method will return true if the terminated flag has been set.
170          *
171          * @return TRUE if terminated flag set, FALSE otherwise
172          */

173         private synchronized boolean isTerminated() {
174             return terminated;
175         }
176         
177         
178         /**
179          * This method will wait for the required delay period.
180          *
181          * @param time The time to wait for.
182          */

183         private synchronized void delay(long time) {
184             try{
185                 wait(time);
186             } catch (Exception JavaDoc ex) {
187                 // do nothing
188
}
189         }
190     }
191     
192     
193     /**
194      * The test thread class.
195      */

196     public static class TestThread extends BasicThread {
197         // the class log variable
198
protected Logger log =
199             Logger.getLogger(TestThread.class.getName());
200         
201         // the flag
202
private boolean terminated = false;
203         
204         /**
205          * This object is used to test the threading funcationlity implemented
206          * by the threading group.
207          */

208         public TestThread() throws Exception JavaDoc {
209         }
210         
211         
212         /**
213          * This method replaces the run method in the BasicThread.
214          *
215          * @exception Exception
216          */

217         public void process() throws Exception JavaDoc {
218             boolean notify = false;
219             while(isTerminated() == false) {
220                 System.out.println("Message from thread : " + this.getId());
221                 if (isTerminated() == false) {
222                     delay(2000);
223                 }
224                 if (notify == false) {
225                     SubThread subThread = new SubThread();
226                     try {
227                         subThread.start("test");
228                     } catch (Exception JavaDoc ex) {
229                         System.out.println("Failed to start the sub thread : "
230                                 + ex.getMessage());
231                         ex.printStackTrace(System.out);
232                     }
233                     com.rift.coad.lib.thread.CoadunationThreadGroupTest.TestMonitor.
234                             getInstance().notifyTest();
235                     notify = true;
236                 }
237             }
238             System.out.println("Thread id [" + this.getId() + "] is terminated");
239         }
240
241
242         /**
243          * This method will be implemented by child objects to terminate the
244          * processing of this thread.
245          */

246         public synchronized void terminate() {
247             terminated = true;
248             notify();
249         }
250         
251         
252         /**
253          * This method will return true if the terminated flag has been set.
254          *
255          * @return TRUE if terminated flag set, FALSE otherwise
256          */

257         private synchronized boolean isTerminated() {
258             return terminated;
259         }
260         
261         
262         /**
263          * This method will wait for the required delay period.
264          *
265          * @param time The time to wait for.
266          */

267         private synchronized void delay(long time) {
268             try{
269                 wait(time);
270             } catch (Exception JavaDoc ex) {
271                 // do nothing
272
}
273         }
274     }
275     
276     
277     /**
278      * The test thread class.
279      */

280     public static class NoTerminateTestThread extends BasicThread {
281         // the class log variable
282
protected Logger log =
283             Logger.getLogger(NoTerminateTestThread.class.getName());
284         
285         /**
286          * This object is used to test the threading funcationlity implemented
287          * by the threading group.
288          */

289         public NoTerminateTestThread() throws Exception JavaDoc {
290         }
291         
292         
293         /**
294          * This method replaces the run method in the BasicThread.
295          *
296          * @exception Exception
297          */

298         public void process() throws Exception JavaDoc {
299             while(true) {
300                 System.out.println("Message from thread : " + this.getId());
301                 Thread.sleep(2000);
302             }
303         }
304
305
306         /**
307          * This method will be implemented by child objects to terminate the
308          * processing of this thread.
309          */

310         public synchronized void terminate() {
311             // do nothing
312
}
313     }
314     
315     
316     
317     public CoadunationThreadGroupTest(String JavaDoc testName) {
318         super(testName);
319     }
320
321     protected void setUp() throws Exception JavaDoc {
322     }
323
324     protected void tearDown() throws Exception JavaDoc {
325     }
326
327     public static Test suite() {
328         TestSuite suite = new TestSuite(CoadunationThreadGroupTest.class);
329         
330         return suite;
331     }
332
333     /**
334      * Test of the thread group class com.rift.coad.lib.thread.CoadunationThreadGroup.
335      */

336     public void testThreadGroup() throws Exception JavaDoc {
337         System.out.println("testThreadGroup");
338         
339         // initialize the thread permissions
340
ThreadsPermissionContainer permissions = new ThreadsPermissionContainer();
341         SessionManager.init(permissions);
342         UserStoreManager userStoreManager = new UserStoreManager();
343         UserSessionManager sessionManager = new UserSessionManager(permissions,
344                 userStoreManager);
345         LoginManager.init(sessionManager,userStoreManager);
346         
347         // add a user to the session for the current thread
348
RoleManager.getInstance();
349         
350         // the thread group manager
351

352         
353         // instanciate the thread manager
354
CoadunationThreadGroup threadGroup = new CoadunationThreadGroup(sessionManager,
355             userStoreManager);
356         ClassLoader JavaDoc loader = new URLClassLoader JavaDoc(new URL JavaDoc[0],
357                 this.getClass().getClassLoader());
358         Thread.currentThread().setContextClassLoader(loader);
359         ThreadGroupManager.getInstance().initThreadGroup(threadGroup);
360         // start 3 threads
361
threadGroup.startThreads(
362                 com.rift.coad.lib.thread.CoadunationThreadGroupTest.TestThread.class,
363                 "test", 3);
364         
365         // start 3 threads
366
threadGroup.startThreads(
367                 com.rift.coad.lib.thread.CoadunationThreadGroupTest.NoTerminateTestThread.class,
368                 "test", 1);
369         
370         if (TestMonitor.getInstance().waitForTest() == false) {
371             fail("Failed to run the test.");
372             return;
373         }
374         
375         // retrieve the thread list
376
List JavaDoc threadList = threadGroup.getThreadInfo();
377         if (threadList.size() != 4) {
378             fail("There should be 4 threads in the group there are [" +
379                     threadList.size() + "]");
380         }
381         
382         // loop through the results
383
boolean found = false;
384         for (int i = 0; i < threadList.size(); i++) {
385             ThreadInfo info = (ThreadInfo)threadList.get(i);
386             System.out.println("Class [" + info.getThreadClass().getName() + "] id ["
387                     + info.getThreadId() + "] username [" +
388                     info.getUser().getName() + "] info [" + info.getInfo() + "]");
389             if (info.getUser().getName().equals("test") == false) {
390                 fail("The invalid user [" + info.getUser().getName() + "] id ["
391                         + info.getThreadId() + "]");
392             }
393             if (info.getThreadClass().getName().equals(
394                     com.rift.coad.lib.thread.CoadunationThreadGroupTest.TestThread.class.
395                     getName())) {
396                 found = true;
397             }
398         }
399         
400         if (found == false) {
401             fail("Failed to find the matching threads");
402         }
403         
404         // create a child thread group
405
CoadunationThreadGroup childThreadGroup = threadGroup.createThreadGroup();
406         
407         // terminate
408
threadGroup.terminate();
409         ThreadGroupManager.getInstance().terminateThreadGroup();
410         
411         if (SubThread.subThreadTerminateCount != 3) {
412             fail("Failed to terminate the sub threads");
413         }
414     }
415 }
416
Popular Tags