1 17 18 package org.sape.carbon.core.component.lifecycle.test; 19 20 import org.sape.carbon.core.component.Component; 21 import org.sape.carbon.core.component.ComponentConfiguration; 22 import org.sape.carbon.core.component.Lookup; 23 import org.sape.carbon.core.component.lifecycle.ComponentUnavailableException; 24 import org.sape.carbon.core.component.lifecycle.LifecycleInterceptor; 25 import org.sape.carbon.core.component.lifecycle.LifecycleStateEnum; 26 import org.sape.carbon.core.component.lifecycle.StateTransitionException; 27 import org.sape.carbon.core.config.Config; 28 29 import junit.extensions.ActiveTestSuite; 30 import junit.framework.Test; 31 import junit.framework.TestCase; 32 import junit.framework.TestSuite; 33 34 44 public class ExternalLifecycleTest extends TestCase { 45 public ExternalLifecycleTest(String name) { 46 super(name); 47 } 48 49 private Component fetchTestComponent() { 50 return Lookup.getInstance().fetchComponent(TEST_COMPONENT_NAME); 51 } 52 53 54 57 public void testCallWhileSuspended() { 58 TestComponent component = (TestComponent) fetchTestComponent(); 59 long endTime = System.currentTimeMillis() + 2000; 60 ((LifecycleInterceptor) component).suspendComponent(); 61 try { 62 component.doSomething(); 63 64 TestCase.fail( 65 "Did not catch expected ComponentUnavailableException"); 66 } catch (ComponentUnavailableException cue) { 67 if (System.currentTimeMillis() < endTime) { 68 TestCase.fail( 69 "Component threw ComponentUnavailableException too soon"); 70 } 71 } finally { 72 ((LifecycleInterceptor) component).resumeComponent(); 73 } 74 } 75 76 79 public void testCallWhileStopped() { 80 TestComponent component = (TestComponent) fetchTestComponent(); 81 ((LifecycleInterceptor) component).stopComponent(); 82 try { 83 component.doSomething(); 84 85 TestCase.fail( 86 "Did not catch expected ComponentUnavailableException"); 87 } catch (ComponentUnavailableException cue) { 88 } finally { 90 ((LifecycleInterceptor) component).startComponent(); 91 } 92 } 93 94 public void testCallWhileDestroyed() { 95 TestComponent component = (TestComponent) fetchTestComponent(); 96 97 Lookup.getInstance().getComponentKeeper(). 98 destroyComponent(TEST_COMPONENT_NAME); 99 100 try { 101 component.doSomething(); 102 103 TestCase.fail( 104 "Did not catch expected ComponentUnavailableException"); 105 } catch (ComponentUnavailableException cue) { 106 } 108 } 109 110 public void testDestroyOnException() { 111 DestroyOnExceptionTestComponent component = getTestComponent(); 112 113 component.crashOnSuspend(); 114 try { 115 ((LifecycleInterceptor) component).suspendComponent(); 116 } catch (StateTransitionException ste) { 117 } 119 checkComponent(component, LifecycleStateEnum.DESTROYED); 120 121 component = getTestComponent(); 122 component.crashOnResume(); 123 ((LifecycleInterceptor) component).suspendComponent(); 124 try { 125 ((LifecycleInterceptor) component).resumeComponent(); 126 } catch (StateTransitionException ste) { 127 } 129 checkComponent(component, LifecycleStateEnum.DESTROYED); 130 131 component = getTestComponent(); 132 component.crashOnStop(); 133 try { 134 ((LifecycleInterceptor) component).stopComponent(); 135 } catch (StateTransitionException ste) { 136 } 138 checkComponent(component, LifecycleStateEnum.DESTROYED); 139 140 component = getTestComponent(); 141 component.crashOnStart(); 142 ((LifecycleInterceptor) component).stopComponent(); 143 try { 144 ((LifecycleInterceptor) component).startComponent(); 145 } catch (StateTransitionException ste) { 146 } 148 checkComponent(component, LifecycleStateEnum.DESTROYED); 149 150 component = getTestComponent(); 151 component.crashOnConfigure(); 152 try { 153 ((LifecycleInterceptor) component).configureComponent( 154 (ComponentConfiguration) Config.getInstance(). 155 createConfiguration(ComponentConfiguration.class)); 156 157 } catch (StateTransitionException ste) { 158 } 160 checkComponent(component, LifecycleStateEnum.DESTROYED); 161 162 } 163 164 public void testNonFatalExceptionHandling() { 165 DestroyOnExceptionTestComponent component = getNFTestComponent(); 166 167 component.crashOnSuspend(); 168 try { 169 ((LifecycleInterceptor) component).suspendComponent(); 170 } catch (StateTransitionException ste) { 171 } 173 checkComponent(component, LifecycleStateEnum.RUNNING); 174 175 component = getNFTestComponent(); 176 component.crashOnResume(); 177 ((LifecycleInterceptor) component).suspendComponent(); 178 try { 179 ((LifecycleInterceptor) component).resumeComponent(); 180 } catch (StateTransitionException ste) { 181 } 183 checkComponent(component, LifecycleStateEnum.SUSPENDED); 184 185 component = getNFTestComponent(); 186 component.crashOnStop(); 187 try { 188 ((LifecycleInterceptor) component).stopComponent(); 189 } catch (StateTransitionException ste) { 190 } 192 checkComponent(component, LifecycleStateEnum.RUNNING); 193 194 component = getNFTestComponent(); 195 component.crashOnStart(); 196 ((LifecycleInterceptor) component).stopComponent(); 197 try { 198 ((LifecycleInterceptor) component).startComponent(); 199 } catch (StateTransitionException ste) { 200 } 202 checkComponent(component, LifecycleStateEnum.STOPPED); 203 204 component = getNFTestComponent(); 205 component.crashOnConfigure(); 206 ((LifecycleInterceptor) component).stopComponent(); 207 try { 208 ((LifecycleInterceptor) component).configureComponent( 209 (ComponentConfiguration) Config.getInstance(). 210 createConfiguration(ComponentConfiguration.class)); 211 212 } catch (StateTransitionException ste) { 213 } 215 checkComponent(component, LifecycleStateEnum.STOPPED); 216 217 component = getNFTestComponent(); 218 component.crashOnConfigure(); 219 try { 220 ((LifecycleInterceptor) component).configureComponent( 221 (ComponentConfiguration) Config.getInstance(). 222 createConfiguration(ComponentConfiguration.class)); 223 224 } catch (StateTransitionException ste) { 225 } 227 checkComponent(component, LifecycleStateEnum.SUSPENDED); 228 229 component = getNFTestComponent(); 230 component.crashOnResume(); 231 try { 232 ((LifecycleInterceptor) component).configureComponent( 233 (ComponentConfiguration) Config.getInstance(). 234 createConfiguration(ComponentConfiguration.class)); 235 236 } catch (StateTransitionException ste) { 237 } 239 checkComponent(component, LifecycleStateEnum.SUSPENDED); 240 241 } 242 243 public void testInternalComponentReference() { 244 TestComponent testComponent = (TestComponent) fetchTestComponent(); 245 TestCase.assertTrue( 246 "Internal component self-reference did not == the external " + 247 "component reference", 248 testComponent == testComponent.getThisComponent()); 249 } 250 251 private DestroyOnExceptionTestComponent getTestComponent() { 252 return (DestroyOnExceptionTestComponent) Lookup.getInstance(). 253 fetchComponent(TEST_DESTROY_ON_EXCPETION_COMPONENT_NAME); 254 } 255 256 private DestroyOnExceptionTestComponent getNFTestComponent() { 257 Lookup.getInstance().getComponentKeeper().destroyComponent( 258 TEST_NON_FATAL_EXCPETION_COMPONENT_NAME); 259 260 return (DestroyOnExceptionTestComponent) Lookup.getInstance(). 261 fetchComponent(TEST_NON_FATAL_EXCPETION_COMPONENT_NAME); 262 } 263 264 private void checkComponent( 265 DestroyOnExceptionTestComponent component, 266 LifecycleStateEnum expectedState) { 267 268 if (((LifecycleInterceptor) component).getLifecycleState() != 269 expectedState) { 270 271 fail("Component was not in expected state, state was [" 272 + ((LifecycleInterceptor) component).getLifecycleState() 273 + "] expected state was [" 274 + expectedState 275 + "]"); 276 } 277 } 278 279 private static final String TEST_COMPONENT_NAME = 280 "/core/test/LifecycleTestComponent"; 281 private static final String TEST_DESTROY_ON_EXCPETION_COMPONENT_NAME = 282 "/core/test/DestroyOnExceptionTestComponent"; 283 private static final String TEST_NON_FATAL_EXCPETION_COMPONENT_NAME = 284 "/core/test/NonFatalExceptionTestComponent"; 285 286 287 public static Test suite() { 288 TestSuite masterSuite = new TestSuite(); 289 Test singleThreadedTests = getSingleThreadedTests(); 291 if (singleThreadedTests != null) { 292 masterSuite.addTest(singleThreadedTests); 293 } 294 Test multiThreadedTests = getMultiThreadedTests(); 296 if (multiThreadedTests != null) { 297 masterSuite.addTest(multiThreadedTests); 298 } 299 return masterSuite; 300 } 301 302 306 private static Test getSingleThreadedTests() { 307 TestSuite suite = new TestSuite(); 308 309 suite.addTest(new ExternalLifecycleTest("testInternalComponentReference")); 310 suite.addTest(new ExternalLifecycleTest("testCallWhileSuspended")); 311 suite.addTest(new ExternalLifecycleTest("testCallWhileStopped")); 312 suite.addTest(new ExternalLifecycleTest("testCallWhileDestroyed")); 313 suite.addTest(new ExternalLifecycleTest("testDestroyOnException")); 314 suite.addTest(new ExternalLifecycleTest("testNonFatalExceptionHandling")); 315 316 return suite; 317 } 318 319 323 private static Test getMultiThreadedTests() { 324 TestSuite suite = new ActiveTestSuite(); 325 326 332 333 return suite; 334 } 335 336 341 private static void addTest(TestSuite suite, String testName, int number) { 342 for (int count = 0; count < number; count++) { 343 suite.addTest(new ExternalLifecycleTest(testName)); 344 } 345 } 346 } 347 | Popular Tags |