1 17 18 package org.sape.carbon.core.component.test; 19 20 import org.sape.carbon.core.bootstrap.BootStrapper; 21 import org.sape.carbon.core.component.Component; 22 import org.sape.carbon.core.component.ComponentKeeper; 23 import org.sape.carbon.core.component.ComponentNotFoundException; 24 import org.sape.carbon.core.component.lifecycle.LifecycleInterceptor; 25 import org.sape.carbon.core.component.lifecycle.LifecycleStateEnum; 26 import org.sape.carbon.core.exception.InvalidParameterException; 27 28 import junit.extensions.ActiveTestSuite; 29 import junit.framework.Test; 30 import junit.framework.TestCase; 31 import junit.framework.TestSuite; 32 33 50 public class ComponentKeeperTest extends TestCase { 51 private ComponentKeeper keeper; 52 53 public ComponentKeeperTest(String name) { 54 super(name); 55 } 56 57 62 public void testComponentState() { 63 LifecycleInterceptor component = (LifecycleInterceptor) this.keeper. 64 fetchComponent(EMPTY_TEST_COMPONENT); 65 if (component.getLifecycleState() != LifecycleStateEnum.RUNNING) { 66 TestCase.fail("Fetched component was not in " + 67 "LifecycleStateEnum.RUNNING state as was expected"); 68 } 69 } 70 71 77 public void testFetchDestroyedComponent() { 78 LifecycleInterceptor component = (LifecycleInterceptor) this.keeper. 79 fetchComponent(EMPTY_TEST_COMPONENT); 80 81 component.destroyComponent(); 82 83 component = (LifecycleInterceptor) this.keeper. 84 fetchComponent(EMPTY_TEST_COMPONENT); 85 86 if (component.getLifecycleState() == LifecycleStateEnum.DESTROYED) { 87 TestCase.fail("Fetched component was in " + 88 "LifecycleStateEnum.DESTROYED state, expected otherwise"); 89 } 90 } 91 92 96 public void testComponentConsistency() { 97 Component component = this.keeper.fetchComponent(EMPTY_TEST_COMPONENT); 98 if (component != this.keeper.fetchComponent(EMPTY_TEST_COMPONENT)) { 99 TestCase.fail("Keeper did not return consistent " + 100 "component references"); 101 } 102 } 103 104 109 public void testDestroyComponent() { 110 LifecycleInterceptor component = (LifecycleInterceptor) this.keeper. 111 fetchComponent(EMPTY_TEST_COMPONENT); 112 113 this.keeper.destroyComponent(EMPTY_TEST_COMPONENT); 114 115 if (component.getLifecycleState() != LifecycleStateEnum.DESTROYED) { 116 TestCase.fail("Fetched component was not in " + 117 "LifecycleStateEnum.DESTROYED state as was expected"); 118 } 119 if (component == this.keeper.fetchComponent(EMPTY_TEST_COMPONENT)) { 120 TestCase.fail("Keeper failed to destroy a component"); 121 } 122 } 123 124 128 public void testDestroyAllComponents() { 129 LifecycleInterceptor component1 = (LifecycleInterceptor) this.keeper. 130 fetchComponent(EMPTY_TEST_COMPONENT); 131 LifecycleInterceptor component2 = (LifecycleInterceptor) this.keeper. 132 fetchComponent(EMPTY_TEST_COMPONENT_2); 133 try { 134 this.keeper.destroyAllComponents(); 135 } catch(Exception e) { 136 TestCase.fail("Caught exception while destroying " + 137 "all components: " + e); 138 } 139 140 if ((component1.getLifecycleState() != LifecycleStateEnum.DESTROYED) || 141 (component2.getLifecycleState() != LifecycleStateEnum.DESTROYED)) { 142 143 TestCase.fail("Fetched component was not in " + 144 "LifecycleStateEnum.DESTROYED state as was expected"); 145 } 146 147 if (component1 == this.keeper.fetchComponent(EMPTY_TEST_COMPONENT) || 148 component2 == this.keeper.fetchComponent(EMPTY_TEST_COMPONENT_2)) { 149 150 TestCase.fail("Keeper failed to destroy all components"); 151 } 152 } 153 154 161 public void testCircularDependency() { 162 Thread testThread = new Thread (new Runnable () { 165 public void run() { 166 try { 167 keeper.fetchComponent(CIRCULAR_REFERENCE_A); 168 } catch (Throwable t) { 169 ComponentKeeperTest.threadException = t; 170 } 171 } 172 }, "testCircularDependency"); 173 testThread.start(); 175 try { 176 testThread.join(THREAD_TIMEOUT); 178 } catch (InterruptedException ie) { 179 throw new RuntimeException ("Caught InterruptedException: " + ie); 181 } 182 183 if (ComponentKeeperTest.threadException != null) { 184 TestCase.fail("Circular dependency test failed with exception " + 185 ComponentKeeperTest.threadException); 186 } 187 188 if (testThread.isAlive()) { 190 TestCase.fail("Circular dependency test did not complete within " + 191 THREAD_TIMEOUT + " ms, dead lock suspected"); 192 } 193 } 194 195 199 public void testComponentNotFound() { 200 boolean passedTest = false; 201 try { 202 this.keeper.fetchComponent(NON_EXISTENT_COMPONENT); 203 } catch(ComponentNotFoundException cnfe) { 204 passedTest = true; 205 } 206 if (!passedTest) { 207 TestCase.fail("Keeper did not throw exception when attempting " + 208 "to fetch a non-existent component"); 209 } 210 } 211 212 216 public void testNullComponentName() { 217 boolean passedTest = false; 218 try { 219 this.keeper.fetchComponent(null); 220 } catch(InvalidParameterException ipe) { 221 passedTest = true; 222 } 223 if (!passedTest) { 224 TestCase.fail("Keeper did not throw exception when attempting " + 225 "to fetch a component with a null name"); 226 } 227 } 228 229 236 public void testCreationNonBlocking() { 237 LifecycleInterceptor component = (LifecycleInterceptor) this.keeper. 239 fetchComponent(EMPTY_TEST_COMPONENT); 240 241 this.keeper.destroyComponent(LONG_INIT_COMPONENT); 242 243 Thread longThread = new Thread (new Runnable () { 245 public void run() { 246 keeper.fetchComponent(LONG_INIT_COMPONENT); 247 } 248 }); 249 250 Thread shortThread = new Thread (new Runnable () { 252 public void run() { 253 keeper.fetchComponent(EMPTY_TEST_COMPONENT); 254 } 255 }); 256 257 longThread.start(); 259 Thread.yield(); 261 shortThread.start(); 263 264 try { 265 shortThread.join(THREAD_TIMEOUT); 267 } catch (InterruptedException ie) { 268 throw new RuntimeException ("Caught InterruptedException: " + ie); 270 } 271 272 if (shortThread.isAlive()) { 273 synchronized(ComponentKeeperTest.TEST_LOCK) { 274 ComponentKeeperTest.TEST_LOCK.notifyAll(); 275 } 276 TestCase.fail("Creation of a component blocked retrieval of " + 277 "unrelated component"); 278 } 279 synchronized(ComponentKeeperTest.TEST_LOCK) { 280 ComponentKeeperTest.TEST_LOCK.notifyAll(); 281 } 282 } 283 284 public void testReturnedComponentState() { 285 this.keeper.destroyComponent(LONG_START_COMPONENT); 286 287 Runnable fetchLongStartComponent = new Runnable () { 288 public void run() { 289 keeper.fetchComponent(LONG_START_COMPONENT); 290 } 291 }; 292 293 Thread createThread = new Thread (fetchLongStartComponent, "createThread"); 296 297 createThread.start(); 299 300 LifecycleInterceptor component = (LifecycleInterceptor) 301 keeper.fetchComponent(LONG_START_COMPONENT); 302 303 if (component.getLifecycleState() != LifecycleStateEnum.RUNNING) { 304 TestCase.fail("Component was not returned in a running state"); 305 } 306 } 307 308 320 public void testCreationBlocking() { 321 this.keeper.destroyComponent(LONG_START_COMPONENT); 323 324 Runnable fetchLongStartComponent = new Runnable () { 325 public void run() { 326 keeper.fetchComponent(LONG_START_COMPONENT); 327 } 328 }; 329 330 Thread createThread = new Thread (fetchLongStartComponent, "createThread"); 333 334 Thread fetchThread = new Thread (fetchLongStartComponent, "fetchThread"); 338 339 createThread.start(); 341 try { 343 synchronized(ComponentKeeperTest.TEST_LOCK) { 344 ComponentKeeperTest.TEST_LOCK.wait(); 345 } 346 } catch (InterruptedException ie) { 347 throw new RuntimeException ("Caught InterruptedException: " + ie); 349 } 350 351 try { 352 fetchThread.start(); 354 355 try { 356 fetchThread.join(THREAD_TIMEOUT); 358 } catch (InterruptedException ie) { 359 throw new RuntimeException ("Caught InterruptedException: " + ie); 361 } 362 363 if (fetchThread.isAlive()) { 365 TestCase.fail("Creation of component blocked fetch of the " + 366 "same component by another thread"); 367 } 368 } catch(RuntimeException re) { 369 throw re; 371 } finally { 372 synchronized(ComponentKeeperTest.TEST_LOCK) { 374 ComponentKeeperTest.TEST_LOCK.notifyAll(); 375 } 376 } 377 } 378 379 384 public void testFetchComponentA() { 385 fetchComponentMultipleTimes(TEST_COMPONENT_A); 386 } 387 388 393 public void testFetchComponentB() { 394 fetchComponentMultipleTimes(TEST_COMPONENT_B); 395 } 396 397 402 public void testFetchComponentC() { 403 fetchComponentMultipleTimes(TEST_COMPONENT_C); 404 } 405 406 private void fetchComponentMultipleTimes(String componentName) { 407 Component component = this.keeper.fetchComponent(componentName); 408 for(int i=0; i<NUMBER_REPETITIONS; i++) { 409 if (component != this.keeper.fetchComponent(componentName)) { 410 TestCase.fail("keeper is returning inconsistent component " + 411 "references"); 412 } 413 } 414 } 415 416 417 protected void setUp() { 418 keeper = BootStrapper.getInstance().fetchComponentKeeper(); 419 } 420 421 423 427 public static final Object TEST_LOCK = new Object (); 428 429 private static final String EMPTY_TEST_COMPONENT = 430 "/core/test/KeeperTestComponent1"; 431 private static final String EMPTY_TEST_COMPONENT_2 = 432 "/core/test/KeeperTestComponent2"; 433 private static final String CIRCULAR_REFERENCE_A = 434 "/core/test/KeeperCircRefTestA"; 435 private static final String TEST_COMPONENT_A = 436 "/core/test/KeeperTestComponentA"; 437 private static final String TEST_COMPONENT_B = 438 "/core/test/KeeperTestComponentB"; 439 private static final String TEST_COMPONENT_C = 440 "/core/test/KeeperTestComponentC"; 441 private static final String LONG_INIT_COMPONENT = 442 "/core/test/KeeperLongInitTestComponent"; 443 private static final String LONG_START_COMPONENT = 444 "/core/test/KeeperLongStartTestComponent"; 445 private static final String NON_EXISTENT_COMPONENT = "/asasdfa"; 446 447 private static Throwable threadException = null; 448 449 private static final long THREAD_TIMEOUT = 3000; 450 private static final int NUMBER_REPETITIONS = 100; 451 452 453 public static Test suite() { 454 TestSuite masterSuite = new TestSuite(); 455 Test singleThreadedTests = getSingleThreadedTests(); 457 if (singleThreadedTests != null) { 458 masterSuite.addTest(singleThreadedTests); 459 } 460 Test multiThreadedTests = getMultiThreadedTests(); 462 if (multiThreadedTests != null) { 463 masterSuite.addTest(multiThreadedTests); 464 } 465 return masterSuite; 466 } 467 468 483 private static Test getSingleThreadedTests() { 484 TestSuite suite = new TestSuite("ComponentKeeperTest"); 485 486 suite.addTest(new ComponentKeeperTest("testComponentNotFound")); 487 suite.addTest(new ComponentKeeperTest("testNullComponentName")); 488 suite.addTest(new ComponentKeeperTest("testComponentState")); 489 suite.addTest(new ComponentKeeperTest("testComponentConsistency")); 490 suite.addTest(new ComponentKeeperTest("testDestroyComponent")); 491 suite.addTest(new ComponentKeeperTest("testDestroyAllComponents")); 492 suite.addTest(new ComponentKeeperTest("testCircularDependency")); 493 suite.addTest(new ComponentKeeperTest("testCreationNonBlocking")); 494 suite.addTest(new ComponentKeeperTest("testReturnedComponentState")); 495 suite.addTest(new ComponentKeeperTest("testFetchDestroyedComponent")); 496 497 return suite; 498 } 499 500 509 private static Test getMultiThreadedTests() { 510 TestSuite suite = new ActiveTestSuite(); 511 512 addTest(suite, "testFetchComponentA", 3); 513 addTest(suite, "testFetchComponentB", 3); 514 addTest(suite, "testFetchComponentC", 3); 515 516 return suite; 517 } 518 519 524 private static void addTest(TestSuite suite, String testName, int number) { 525 for (int count = 0; count < number; count++) { 526 suite.addTest(new ComponentKeeperTest(testName)); 527 } 528 } 529 } 530 | Popular Tags |