1 17 18 package org.sape.carbon.core.component.lifecycle.test; 19 20 import java.lang.reflect.Method ; 21 22 import org.sape.carbon.core.component.ComponentConfiguration; 23 import org.sape.carbon.core.component.FunctionalInterface; 24 import org.sape.carbon.core.component.lifecycle.DefaultLifecycleInterceptor; 25 import org.sape.carbon.core.component.lifecycle.LifecycleInterceptor; 26 import org.sape.carbon.core.component.lifecycle.LifecycleStateEnum; 27 import org.sape.carbon.core.component.proxy.ComponentProxyInvocationHandler; 28 import org.sape.carbon.core.component.proxy.Decorator; 29 import org.sape.carbon.core.util.thread.ReadWriteLock; 30 import org.sape.carbon.core.util.thread.ReentrantWriterPreferenceReadWriteLock; 31 32 import junit.extensions.ActiveTestSuite; 33 import junit.framework.Test; 34 import junit.framework.TestCase; 35 import junit.framework.TestSuite; 36 37 49 public class InternalLifecycleTest extends TestCase implements TestListener { 50 51 private boolean configureCalled = false; 52 private boolean destroyCalled = false; 53 private boolean initializeCalled = false; 54 private boolean resumeCalled = false; 55 private boolean startCalled = false; 56 private boolean stopCalled = false; 57 private boolean suspendCalled = false; 58 59 public InternalLifecycleTest(String name) { 60 super(name); 61 } 62 63 66 private FunctionalInterface prepareComponentFunctionalImpl() { 67 return new TestComponentImpl(this); 68 } 69 70 private ComponentProxyInvocationHandler prepareComponentProxy() { 71 return new ComponentProxyInvocationHandler() { 72 73 public void addDecorator(Decorator decorator) { 74 } 75 76 public void setFunctionalImplementation( 77 Class [] implementedInterfaces, 78 FunctionalInterface functionalImplementation) { 79 } 80 81 public Object getDelegate(Class delegateInterface) { 82 return null; 83 } 84 85 public ReadWriteLock getMonitor() { 86 return new ReentrantWriterPreferenceReadWriteLock(); 87 } 88 89 public Object invoke(Object proxy, Method method, Object [] args) { 90 return null; 91 } 92 93 public String getComponentName() { 94 return null; 95 } 96 97 public void setComponentName(String name) { 98 } 99 }; 100 } 101 102 105 private LifecycleInterceptor prepareLifecycleInterceptor() { 106 return new DefaultLifecycleInterceptor( 107 prepareComponentFunctionalImpl(), 108 prepareComponentProxy(), 109 null); 110 } 111 112 117 public void testInitialize() { 118 LifecycleInterceptor lifecycleInterceptor = prepareLifecycleInterceptor(); 119 120 TestCase.assertTrue( 122 "Component+Interceptor did not begin in CREATING state", 123 lifecycleInterceptor.getLifecycleState() 124 == LifecycleStateEnum.CREATING); 125 126 lifecycleInterceptor.initializeComponent(null); 128 129 TestCase.assertTrue( 131 "Component did not end up in STOPPED state", 132 lifecycleInterceptor.getLifecycleState() == LifecycleStateEnum.STOPPED); 133 134 TestCase.assertTrue( 135 "initialize method on functional implementation was not called", 136 this.initializeCalled); 137 138 } 139 140 144 public void testStart() { 145 LifecycleInterceptor lifecycleInterceptor = prepareLifecycleInterceptor(); 146 147 lifecycleInterceptor.initializeComponent(null); 149 150 lifecycleInterceptor.startComponent(); 152 153 TestCase.assertTrue( 155 "Component did not end up in RUNNING state", 156 lifecycleInterceptor.getLifecycleState() == LifecycleStateEnum.RUNNING); 157 158 TestCase.assertTrue( 159 "start method on functional implementation was not called", 160 this.startCalled); 161 } 162 163 167 public void testStop() { 168 LifecycleInterceptor lifecycleInterceptor = prepareLifecycleInterceptor(); 169 170 lifecycleInterceptor.initializeComponent(null); 172 lifecycleInterceptor.startComponent(); 174 175 lifecycleInterceptor.stopComponent(); 177 178 TestCase.assertTrue( 180 "Component did not end up in STOPPED state", 181 lifecycleInterceptor.getLifecycleState() == LifecycleStateEnum.STOPPED); 182 183 TestCase.assertTrue( 184 "stop method on functional implementation was not called", 185 this.stopCalled); 186 } 187 188 192 public void testSuspend() { 193 LifecycleInterceptor lifecycleInterceptor = prepareLifecycleInterceptor(); 194 195 lifecycleInterceptor.initializeComponent(null); 197 lifecycleInterceptor.startComponent(); 199 200 lifecycleInterceptor.suspendComponent(); 202 203 TestCase.assertTrue( 205 "Component did not end up in SUSPENDED state", 206 lifecycleInterceptor.getLifecycleState() 207 == LifecycleStateEnum.SUSPENDED); 208 209 TestCase.assertTrue( 210 "suspend method on functional implementation was not called", 211 this.suspendCalled); 212 } 213 214 218 public void testResume() { 219 LifecycleInterceptor lifecycleInterceptor = prepareLifecycleInterceptor(); 220 221 lifecycleInterceptor.initializeComponent(null); 223 lifecycleInterceptor.startComponent(); 225 226 lifecycleInterceptor.suspendComponent(); 228 229 lifecycleInterceptor.resumeComponent(); 231 232 TestCase.assertTrue( 234 "Component did not end up in RUNNING state", 235 lifecycleInterceptor.getLifecycleState() == LifecycleStateEnum.RUNNING); 236 237 TestCase.assertTrue( 238 "resume method on functional implementation was not called", 239 this.resumeCalled); 240 } 241 242 246 public void testConfigureWhileRunning() { 247 LifecycleInterceptor lifecycleInterceptor = prepareLifecycleInterceptor(); 248 ComponentConfiguration config = null; 250 251 lifecycleInterceptor.initializeComponent(null); 253 lifecycleInterceptor.startComponent(); 255 256 TestCase.assertTrue( 258 "Component not in RUNNING state prior to config test", 259 lifecycleInterceptor.getLifecycleState() == LifecycleStateEnum.RUNNING); 260 261 lifecycleInterceptor.configureComponent(config); 263 264 TestCase.assertTrue( 266 "Component did not return RUNNING state. " 267 + "Was attempting configuration while RUNNING", 268 lifecycleInterceptor.getLifecycleState() == LifecycleStateEnum.RUNNING); 269 270 TestCase.assertTrue( 271 "configure method on functional implementation was not called", 272 this.configureCalled); 273 } 274 275 279 public void testConfigureWhileStopped() { 280 LifecycleInterceptor lifecycleInterceptor = prepareLifecycleInterceptor(); 281 282 ComponentConfiguration config = null; 284 285 lifecycleInterceptor.initializeComponent(null); 287 lifecycleInterceptor.startComponent(); 289 290 lifecycleInterceptor.stopComponent(); 292 293 TestCase.assertTrue( 295 "Component not in STOPPED state prior to config test", 296 lifecycleInterceptor.getLifecycleState() == LifecycleStateEnum.STOPPED); 297 298 lifecycleInterceptor.configureComponent(config); 300 301 TestCase.assertTrue( 303 "Component did not return STOPPED state. " 304 + "Was attempting configuration while STOPPED", 305 lifecycleInterceptor.getLifecycleState() == LifecycleStateEnum.STOPPED); 306 307 TestCase.assertTrue( 308 "configure method on functional implementation was not called", 309 this.configureCalled); 310 } 311 312 316 public void testConfigureWhileSuspended() { 317 318 LifecycleInterceptor lifecycleInterceptor = prepareLifecycleInterceptor(); 319 320 ComponentConfiguration config = null; 322 323 lifecycleInterceptor.initializeComponent(null); 325 lifecycleInterceptor.startComponent(); 327 328 lifecycleInterceptor.suspendComponent(); 330 331 TestCase.assertTrue( 333 "Component not in SUSPENDED state prior to config test", 334 lifecycleInterceptor.getLifecycleState() 335 == LifecycleStateEnum.SUSPENDED); 336 337 lifecycleInterceptor.configureComponent(config); 339 340 TestCase.assertTrue( 342 "Component did not return to SUSPENDED state. " 343 + "Was attempting configuration while SUSPENDED", 344 lifecycleInterceptor.getLifecycleState() 345 == LifecycleStateEnum.SUSPENDED); 346 347 TestCase.assertTrue( 348 "configure method on functional implementation was not called", 349 this.configureCalled); 350 } 351 352 356 public void testDestroyWhileRunning() { 357 358 LifecycleInterceptor lifecycleInterceptor = prepareLifecycleInterceptor(); 359 360 lifecycleInterceptor.initializeComponent(null); 362 lifecycleInterceptor.startComponent(); 364 365 TestCase.assertTrue( 367 "Component not in STOPPED state prior to destroy test", 368 lifecycleInterceptor.getLifecycleState() == LifecycleStateEnum.RUNNING); 369 370 lifecycleInterceptor.destroyComponent(); 372 373 TestCase.assertTrue( 375 "Component did not move to DESTROYED state.", 376 lifecycleInterceptor.getLifecycleState() 377 == LifecycleStateEnum.DESTROYED); 378 379 TestCase.assertTrue( 380 "stop method on functional implementation was not called", 381 this.stopCalled); 382 383 TestCase.assertTrue( 384 "destroy method on functional implementation was not called", 385 this.destroyCalled); 386 } 387 388 392 public void testDestroyWhileStopped() { 393 394 LifecycleInterceptor lifecycleInterceptor = prepareLifecycleInterceptor(); 395 396 lifecycleInterceptor.initializeComponent(null); 398 lifecycleInterceptor.startComponent(); 400 lifecycleInterceptor.stopComponent(); 402 403 TestCase.assertTrue( 405 "Component not in STOPPED state prior to destroy test", 406 lifecycleInterceptor.getLifecycleState() == LifecycleStateEnum.STOPPED); 407 408 lifecycleInterceptor.destroyComponent(); 410 411 TestCase.assertTrue( 413 "Component did not move to DESTROYED state.", 414 lifecycleInterceptor.getLifecycleState() 415 == LifecycleStateEnum.DESTROYED); 416 417 TestCase.assertTrue( 418 "destroy method on functional implementation was not called", 419 this.destroyCalled); 420 } 421 422 423 public static Test suite() { 424 TestSuite masterSuite = new TestSuite(); 425 Test singleThreadedTests = getSingleThreadedTests(); 427 if (singleThreadedTests != null) { 428 masterSuite.addTest(singleThreadedTests); 429 } 430 Test multiThreadedTests = getMultiThreadedTests(); 432 if (multiThreadedTests != null) { 433 masterSuite.addTest(multiThreadedTests); 434 } 435 return masterSuite; 436 } 437 438 443 private static Test getSingleThreadedTests() { 444 TestSuite suite = new TestSuite(); 445 446 suite.addTest(new InternalLifecycleTest("testInitialize")); 447 suite.addTest(new InternalLifecycleTest("testStart")); 448 suite.addTest(new InternalLifecycleTest("testStop")); 449 suite.addTest(new InternalLifecycleTest("testSuspend")); 450 suite.addTest(new InternalLifecycleTest("testResume")); 451 suite.addTest(new InternalLifecycleTest("testConfigureWhileRunning")); 452 suite.addTest( 453 new InternalLifecycleTest("testConfigureWhileSuspended")); 454 suite.addTest(new InternalLifecycleTest("testConfigureWhileStopped")); 455 suite.addTest(new InternalLifecycleTest("testDestroyWhileRunning")); 456 suite.addTest(new InternalLifecycleTest("testDestroyWhileStopped")); 457 return suite; 458 } 459 460 464 private static Test getMultiThreadedTests() { 465 TestSuite suite = new ActiveTestSuite(); 466 467 473 474 return suite; 475 } 476 477 482 private static void addTest( 483 TestSuite suite, 484 String testName, 485 int number) { 486 for (int count = 0; count < number; count++) { 487 suite.addTest(new InternalLifecycleTest(testName)); 488 } 489 } 490 491 494 public void configureCalled() { 495 this.configureCalled = true; 496 } 497 498 501 public void destroyCalled() { 502 this.destroyCalled = true; 503 } 504 505 508 public void initializeCalled() { 509 this.initializeCalled = true; 510 } 511 512 515 public void resumeCalled() { 516 this.resumeCalled = true; 517 } 518 519 522 public void startCalled() { 523 this.startCalled = true; 524 } 525 526 529 public void stopCalled() { 530 this.stopCalled = true; 531 } 532 533 536 public void suspendCalled() { 537 this.suspendCalled = true; 538 } 539 540 } | Popular Tags |