1 23 24 package com.rift.coad.lib.thread; 25 26 import java.net.URLClassLoader ; 27 import java.net.URL ; 28 import junit.framework.*; 29 import java.util.Vector ; 30 import java.util.List ; 31 import java.util.Date ; 32 import java.util.ArrayList ; 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 52 public class CoadunationThreadGroupTest extends TestCase { 53 54 57 public static class TestMonitor { 58 private static final long TIMEOUT = 10000; 60 61 private static TestMonitor singleton = null; 63 64 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 82 public synchronized void notifyTest() { 83 count++; 84 notify(); 85 } 86 87 88 94 public synchronized boolean waitForTest() throws Exception { 95 try { 96 long startTime = new Date ().getTime(); 97 while (count < 6) { 98 wait(TIMEOUT); 99 long currentTime = new Date ().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 ex) { 108 throw new Exception ("Failed to wait for test : " + 109 ex.getMessage(),ex); 110 } 111 } 112 } 113 114 117 public static class SubThread extends CoadunationThread { 118 public static int subThreadTerminateCount = 0; 120 protected Logger log = 121 Logger.getLogger(TestThread.class.getName()); 122 123 private boolean terminated = false; 125 126 130 public SubThread() throws Exception { 131 } 132 133 134 139 public void process() throws Exception { 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 162 public synchronized void terminate() { 163 terminated = true; 164 notify(); 165 } 166 167 168 173 private synchronized boolean isTerminated() { 174 return terminated; 175 } 176 177 178 183 private synchronized void delay(long time) { 184 try{ 185 wait(time); 186 } catch (Exception ex) { 187 } 189 } 190 } 191 192 193 196 public static class TestThread extends BasicThread { 197 protected Logger log = 199 Logger.getLogger(TestThread.class.getName()); 200 201 private boolean terminated = false; 203 204 208 public TestThread() throws Exception { 209 } 210 211 212 217 public void process() throws Exception { 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 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 246 public synchronized void terminate() { 247 terminated = true; 248 notify(); 249 } 250 251 252 257 private synchronized boolean isTerminated() { 258 return terminated; 259 } 260 261 262 267 private synchronized void delay(long time) { 268 try{ 269 wait(time); 270 } catch (Exception ex) { 271 } 273 } 274 } 275 276 277 280 public static class NoTerminateTestThread extends BasicThread { 281 protected Logger log = 283 Logger.getLogger(NoTerminateTestThread.class.getName()); 284 285 289 public NoTerminateTestThread() throws Exception { 290 } 291 292 293 298 public void process() throws Exception { 299 while(true) { 300 System.out.println("Message from thread : " + this.getId()); 301 Thread.sleep(2000); 302 } 303 } 304 305 306 310 public synchronized void terminate() { 311 } 313 } 314 315 316 317 public CoadunationThreadGroupTest(String testName) { 318 super(testName); 319 } 320 321 protected void setUp() throws Exception { 322 } 323 324 protected void tearDown() throws Exception { 325 } 326 327 public static Test suite() { 328 TestSuite suite = new TestSuite(CoadunationThreadGroupTest.class); 329 330 return suite; 331 } 332 333 336 public void testThreadGroup() throws Exception { 337 System.out.println("testThreadGroup"); 338 339 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 RoleManager.getInstance(); 349 350 352 353 CoadunationThreadGroup threadGroup = new CoadunationThreadGroup(sessionManager, 355 userStoreManager); 356 ClassLoader loader = new URLClassLoader (new URL [0], 357 this.getClass().getClassLoader()); 358 Thread.currentThread().setContextClassLoader(loader); 359 ThreadGroupManager.getInstance().initThreadGroup(threadGroup); 360 threadGroup.startThreads( 362 com.rift.coad.lib.thread.CoadunationThreadGroupTest.TestThread.class, 363 "test", 3); 364 365 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 List 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 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 CoadunationThreadGroup childThreadGroup = threadGroup.createThreadGroup(); 406 407 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 |