1 /****************************************************************************** 2 * Copyright (C) PicoContainer Organization. All rights reserved. * 3 * ------------------------------------------------------------------------- * 4 * The software in this package is published under the terms of the BSD * 5 * style license a copy of which has been included with this distribution in * 6 * the license.html file. * 7 * * 8 * Idea by Rachel Davies, Original code by Aslak Hellesoy and Paul Hammant * 9 *****************************************************************************/ 10 11 package org.picocontainer.extras; 12 13 import junit.framework.TestCase; 14 import org.picocontainer.defaults.DefaultPicoContainer; 15 import org.picocontainer.testmodel.DependsOnTouchable; 16 import org.picocontainer.testmodel.SimpleTouchable; 17 import org.picocontainer.extras.DefaultLifecyclePicoAdaptor; 18 import org.picocontainer.lifecycle.Startable; 19 import org.picocontainer.lifecycle.Stoppable; 20 import org.picocontainer.lifecycle.Disposable; 21 import org.picocontainer.lifecycle.LifecyclePicoAdaptor; 22 23 import java.util.ArrayList; 24 import java.util.List; 25 import java.util.Vector; 26 import java.util.HashMap; 27 import java.util.Map; 28 29 public class DefaultLifecyclePicoAdaptorTestCase extends TestCase { 30 31 public static class One implements Startable, Stoppable, Disposable { 32 33 List instantiating = new ArrayList(); 34 List starting = new ArrayList(); 35 List stopping = new ArrayList(); 36 List disposing = new ArrayList(); 37 38 public One() { 39 instantiation("One"); 40 } 41 42 public void instantiation(String s) { 43 instantiating.add(s); 44 } 45 46 public List getInstantiating() { 47 return instantiating; 48 } 49 50 public List getStarting() { 51 return starting; 52 } 53 54 public List getStopping() { 55 return stopping; 56 } 57 58 public List getDisposing() { 59 return disposing; 60 } 61 62 public void start() throws Exception { 63 startCalled("One"); 64 } 65 66 public void stop() throws Exception { 67 stopCalled("One"); 68 } 69 70 public void dispose() throws Exception { 71 disposeCalled("One"); 72 } 73 74 public void startCalled(String msg) { 75 starting.add(msg); 76 } 77 78 public void stopCalled(String msg) { 79 stopping.add(msg); 80 } 81 82 public void disposeCalled(String msg) { 83 disposing.add(msg); 84 } 85 86 } 87 88 public static class Two implements Startable, Stoppable, Disposable { 89 One one; 90 91 public Two(One one) { 92 one.instantiation("Two"); 93 this.one = one; 94 } 95 96 public void start() throws Exception { 97 one.startCalled("Two"); 98 } 99 100 public void stop() throws Exception { 101 one.stopCalled("Two"); 102 } 103 104 public void dispose() throws Exception { 105 one.disposeCalled("Two"); 106 } 107 } 108 109 public static class Three implements Startable, Stoppable, Disposable { 110 One one; 111 112 public Three(One one, Two two) { 113 one.instantiation("Three"); 114 this.one = one; 115 } 116 117 public void start() throws Exception { 118 one.startCalled("Three"); 119 } 120 121 public void stop() throws Exception { 122 one.stopCalled("Three"); 123 } 124 125 public void dispose() throws Exception { 126 one.disposeCalled("Three"); 127 } 128 } 129 130 public static class Four implements Startable, Stoppable, Disposable { 131 One one; 132 133 public Four(Two two, Three three, One one) { 134 one.instantiation("Four"); 135 this.one = one; 136 } 137 138 public void start() throws Exception { 139 one.startCalled("Four"); 140 } 141 142 public void stop() throws Exception { 143 one.stopCalled("Four"); 144 } 145 146 public void dispose() throws Exception { 147 one.disposeCalled("Four"); 148 } 149 } 150 151 152 public void testOrderOfInstantiationWithoutAdaptor() throws Exception { 153 154 DefaultPicoContainer pico = new DefaultPicoContainer.Default(); 155 156 pico.registerComponentByClass(Four.class); 157 pico.registerComponentByClass(Two.class); 158 pico.registerComponentByClass(One.class); 159 pico.registerComponentByClass(Three.class); 160 161 pico.instantiateComponents(); 162 163 Startable startup = (Startable) pico.getComponentMulticaster(true, false); 164 Stoppable shutdown = (Stoppable) pico.getComponentMulticaster(false, false); 165 Disposable disposal = (Disposable) pico.getComponentMulticaster(false, false); 166 167 assertTrue("There should have been a 'One' in the internals", pico.hasComponent(One.class)); 168 169 One one = (One) pico.getComponent(One.class); 170 171 // instantiation - would be difficult to do these in the wrong order!! 172 assertEquals("Should be four elems", 4, one.getInstantiating().size()); 173 assertEquals("Incorrect Order of Instantiation", "One", one.getInstantiating().get(0)); 174 assertEquals("Incorrect Order of Instantiation", "Two", one.getInstantiating().get(1)); 175 assertEquals("Incorrect Order of Instantiation", "Three", one.getInstantiating().get(2)); 176 assertEquals("Incorrect Order of Instantiation", "Four", one.getInstantiating().get(3)); 177 178 startStopDisposeLifecycleComps(startup, shutdown, disposal, one); 179 180 } 181 182 183 public void testStartStopStartStopAndDispose() throws Exception { 184 185 DefaultPicoContainer pico = new DefaultPicoContainer.Default(); 186 LifecyclePicoAdaptor lifecycle = new DefaultLifecyclePicoAdaptor(pico); 187 188 pico.registerComponentByClass(One.class); 189 pico.registerComponentByClass(Two.class); 190 pico.registerComponentByClass(Three.class); 191 pico.registerComponentByClass(Four.class); 192 193 pico.instantiateComponents(); 194 195 One one = (One) pico.getComponent(One.class); 196 197 // instantiation - would be difficult to do these in the wrong order!! 198 assertEquals("Should be four elems", 4, one.getInstantiating().size()); 199 assertEquals("Incorrect Order of Instantiation", "One", one.getInstantiating().get(0)); 200 assertEquals("Incorrect Order of Instantiation", "Two", one.getInstantiating().get(1)); 201 assertEquals("Incorrect Order of Instantiation", "Three", one.getInstantiating().get(2)); 202 assertEquals("Incorrect Order of Instantiation", "Four", one.getInstantiating().get(3)); 203 204 startStopDisposeLifecycleComps(lifecycle, lifecycle, lifecycle, one); 205 206 } 207 208 private void startStopDisposeLifecycleComps(Startable start, Stoppable stop, Disposable disp, One one) throws Exception { 209 start.start(); 210 211 // post instantiation startup 212 assertEquals("Should be four elems", 4, one.getStarting().size()); 213 assertEquals("Incorrect Order of Starting", "One", one.getStarting().get(0)); 214 assertEquals("Incorrect Order of Starting", "Two", one.getStarting().get(1)); 215 assertEquals("Incorrect Order of Starting", "Three", one.getStarting().get(2)); 216 assertEquals("Incorrect Order of Starting", "Four", one.getStarting().get(3)); 217 218 stop.stop(); 219 220 // post instantiation shutdown - REVERSE order. 221 assertEquals("Should be four elems", 4, one.getStopping().size()); 222 assertEquals("Incorrect Order of Stopping", "Four", one.getStopping().get(0)); 223 assertEquals("Incorrect Order of Stopping", "Three", one.getStopping().get(1)); 224 assertEquals("Incorrect Order of Stopping", "Two", one.getStopping().get(2)); 225 assertEquals("Incorrect Order of Stopping", "One", one.getStopping().get(3)); 226 227 disp.dispose(); 228 229 // post instantiation shutdown - REVERSE order. 230 assertEquals("Should be four elems", 4, one.getDisposing().size()); 231 assertEquals("Incorrect Order of Stopping", "Four", one.getDisposing().get(0)); 232 assertEquals("Incorrect Order of Stopping", "Three", one.getDisposing().get(1)); 233 assertEquals("Incorrect Order of Stopping", "Two", one.getDisposing().get(2)); 234 assertEquals("Incorrect Order of Stopping", "One", one.getDisposing().get(3)); 235 } 236 237 238 public void testStartStartCausingBarf() throws Exception { 239 240 DefaultPicoContainer pico = new DefaultPicoContainer.Default(); 241 LifecyclePicoAdaptor lifecycle = new DefaultLifecyclePicoAdaptor(pico); 242 243 244 pico.registerComponentByClass(DependsOnTouchable.class); 245 pico.registerComponentByClass(SimpleTouchable.class); 246 247 pico.instantiateComponents(); 248 249 assertTrue(lifecycle.isStopped()); 250 lifecycle.start(); 251 assertTrue(lifecycle.isStarted()); 252 try { 253 lifecycle.start(); 254 fail("Should have barfed"); 255 } catch (IllegalStateException e) { 256 // expected; 257 assertTrue(lifecycle.isStarted()); 258 } 259 } 260 261 public void testStartStopStopCausingBarf() throws Exception { 262 DefaultPicoContainer pico = new DefaultPicoContainer.Default(); 263 LifecyclePicoAdaptor lifecycle = new DefaultLifecyclePicoAdaptor(pico); 264 265 266 pico.registerComponentByClass(DependsOnTouchable.class); 267 pico.registerComponentByClass(SimpleTouchable.class); 268 269 pico.instantiateComponents(); 270 assertTrue(lifecycle.isStopped()); 271 lifecycle.start(); 272 assertTrue(lifecycle.isStarted()); 273 lifecycle.stop(); 274 assertTrue(lifecycle.isStopped()); 275 try { 276 lifecycle.stop(); 277 fail("Should have barfed"); 278 } catch (IllegalStateException e) { 279 // expected; 280 assertTrue(lifecycle.isStopped()); 281 } 282 } 283 284 public void testDisposeDisposeCausingBarf() throws Exception { 285 DefaultPicoContainer pico = new DefaultPicoContainer.Default(); 286 LifecyclePicoAdaptor lifecycle = new DefaultLifecyclePicoAdaptor(pico); 287 288 289 pico.registerComponentByClass(DependsOnTouchable.class); 290 pico.registerComponentByClass(SimpleTouchable.class); 291 292 pico.instantiateComponents(); 293 lifecycle.start(); 294 lifecycle.stop(); 295 assertFalse(lifecycle.isDisposed()); 296 lifecycle.dispose(); 297 assertTrue(lifecycle.isDisposed()); 298 try { 299 lifecycle.dispose(); 300 fail("Should have barfed"); 301 } catch (IllegalStateException e) { 302 // expected; 303 assertTrue(lifecycle.isDisposed()); 304 } 305 } 306 307 308 public void testStartStopDisposeDisposeCausingBarf() throws Exception { 309 DefaultPicoContainer pico = new DefaultPicoContainer.Default(); 310 LifecyclePicoAdaptor lifecycle = new DefaultLifecyclePicoAdaptor(pico); 311 312 313 pico.registerComponentByClass(DependsOnTouchable.class); 314 pico.registerComponentByClass(SimpleTouchable.class); 315 316 pico.instantiateComponents(); 317 lifecycle.start(); 318 lifecycle.stop(); 319 lifecycle.dispose(); 320 try { 321 lifecycle.dispose(); 322 fail("Should have barfed"); 323 } catch (IllegalStateException e) { 324 // expected; 325 } 326 } 327 328 public static class FooRunnable implements Runnable, Startable, Stoppable { 329 private int runCount; 330 private Thread thread = new Thread(); 331 private boolean interrupted; 332 333 public FooRunnable() { 334 } 335 336 public int runCount() { 337 return runCount; 338 } 339 340 public boolean isInterrupted() { 341 return interrupted; 342 } 343 344 public void start() { 345 thread = new Thread(this); 346 thread.start(); 347 } 348 349 public void stop() { 350 thread.interrupt(); 351 } 352 353 // this would do something a bit more concrete 354 // than counting in real life ! 355 public void run() { 356 runCount++; 357 try { 358 Thread.sleep(10000); 359 } catch (InterruptedException e) { 360 interrupted = true; 361 } 362 } 363 } 364 365 public void testStartStopOfDaemonizedThread() throws Exception { 366 DefaultPicoContainer pico = new DefaultPicoContainer.Default(); 367 LifecyclePicoAdaptor lifecycle = new DefaultLifecyclePicoAdaptor(pico); 368 369 370 pico.registerComponentByClass(DependsOnTouchable.class); 371 pico.registerComponentByClass(SimpleTouchable.class); 372 pico.registerComponentByClass(FooRunnable.class); 373 374 pico.instantiateComponents(); 375 lifecycle.start(); 376 assertTrue(lifecycle.isStarted()); 377 Thread.sleep(100); 378 lifecycle.stop(); 379 assertTrue(lifecycle.isStopped()); 380 381 FooRunnable foo = (FooRunnable) pico.getComponent(FooRunnable.class); 382 assertEquals(1, foo.runCount()); 383 lifecycle.start(); 384 assertTrue(lifecycle.isStarted()); 385 Thread.sleep(100); 386 lifecycle.stop(); 387 assertTrue(lifecycle.isStopped()); 388 assertEquals(2, foo.runCount()); 389 390 } 391 392 public void testForgivingNatureOfLifecycleAdapter() throws Exception { 393 394 DefaultPicoContainer pico = new DefaultPicoContainer.Default(); 395 LifecyclePicoAdaptor lifecycle = new DefaultLifecyclePicoAdaptor(pico); 396 397 398 // Touchable is not Startable (etc). This internals should be able to handle the 399 // fact that none of the comps are Startable (etc). 400 pico.registerComponentByClass(SimpleTouchable.class); 401 402 pico.instantiateComponents(); 403 404 assertTrue(lifecycle.isStopped()); 405 lifecycle.start(); 406 assertTrue(lifecycle.isStarted()); 407 408 } 409 410 }

This page was automatically generated by Maven