final Component real_engine =
Components.useKey("real engine").singleton(new ThreadLocalScope());
final DefaultLifecycleDescriptor desc = new DefaultLifecycleDescriptor();
desc.setCloser(new Phase(desc.getCloser().getPhaseKey(), ExceptionHandlers.suppresser()));
desc.setStopper(new Phase(desc.getStopper().getPhaseKey(), ExceptionHandlers.suppresser()));
final DefaultLifecycleManager lm = new DefaultLifecycleManager(desc);
final DefaultLifecycleManager.DefaultLifecycle lc = lm.newLifecycle()
.starter("start")
.stopper("stop")
.initializer("initialize")
.disposer("destroy");
final Component engine_lc = lc.manage(real_engine);
yan.registerComponent(CarEngine.class, engine_lc);
/*
yan.registerComponent(CarEngine.class, LifecycleDescriptor.instance(
Components.useKey("real engine").singleton(new ThreadLocalScope()),
ExceptionHandlers.suppresser())
.starter("start")
.stopper("stop")
.initializer("initialize")
.disposer("destroy")
.get());
*/
yan.registerConstructor("real engine", CarEngine.class);
final Component sttfrc = Components.ctor(StatefulResource.class)
.withArgument(0, Components.useType(CarEngine.class));
yan.registerComponent(lc.manage(sttfrc));
/*
yan.registerComponent(LifecycleDescriptor.instance(
Components.ctor(StatefulResource.class)
.withArgument(0, Components.useType(CarEngine.class))
, ExceptionHandlers.suppresser())
.initializer("initialize")
.disposer("destroy")
.starter("start")
.stopper("stop")
.get());
*/
final Component car_engine = yan.getComponentOfType(CarEngine.class);
assertNull(car_engine.getType());
final StatefulResource resource = (StatefulResource)yan.getInstanceOfType(StatefulResource.class);
final CarEngine engine = (CarEngine)yan.getInstanceOfType(CarEngine.class);
assertEquals(0, engine.getStarted());
assertEquals(0, engine.getStopped());
assertEquals(0, engine.getInit());
assertEquals(0, engine.getDisposed());
assertEquals(0, resource.getInitCount());
assertEquals(0, resource.getDestroyedCount());
//new DefaultLifecycle(yan).init();
lm.init();
assertEquals(0, engine.getStarted());
assertEquals(0, engine.getStopped());
assertEquals(1, engine.getInit());
assertEquals(0, engine.getDisposed());
assertEquals(1, resource.getInitCount());
assertEquals(0, resource.getDestroyedCount());
try{
//new DefaultLifecycle(yan).start();
lm.start();
fail("should have failed with unsupported");
}
catch(UnsupportedOperationException e){
assertEquals("start", e.getMessage());
}
assertEquals(1, engine.getStarted());
assertEquals(0, engine.getStopped());
assertEquals(1, engine.getInit());
assertEquals(0, engine.getDisposed());
assertEquals(1, resource.getInitCount());
assertEquals(0, resource.getDestroyedCount());
//new DefaultLifecycle(yan).init();
lm.init();
assertEquals(1, engine.getStarted());
assertEquals(0, engine.getStopped());
assertEquals(1, engine.getInit());
assertEquals(0, engine.getDisposed());
assertEquals(1, resource.getInitCount());
assertEquals(0, resource.getDestroyedCount());
//new DefaultLifecycle(yan).stop();
lm.stop();
assertEquals(1, engine.getStarted());
assertEquals(1, engine.getStopped());
assertEquals(1, engine.getInit());
assertEquals(0, engine.getDisposed());
assertEquals(1, resource.getInitCount());
assertEquals(0, resource.getDestroyedCount());
//final DefaultLifecycle lc = new DefaultLifecycle(yan);
//lc.dispose();
lm.dispose();
assertEquals(1, engine.getStarted());
assertEquals(1, engine.getStopped());
assertEquals(1, engine.getInit());
assertEquals(1, engine.getDisposed());
assertEquals(1, resource.getInitCount());
assertEquals(1, resource.getDestroyedCount());
try{
//lc.start();
lm.start();
fail("should have failed with unsupported");
}
catch(UnsupportedOperationException e){
assertEquals("start", e.getMessage());
}
assertEquals(2, engine.getStarted());
assertEquals(1, engine.getStopped());
assertEquals(1, engine.getInit());
assertEquals(1, engine.getDisposed());
assertEquals(1, resource.getInitCount());
assertEquals(1, resource.getDestroyedCount());
//lc.stop();
lm.stop();
assertEquals(2, engine.getStarted());
assertEquals(2, engine.getStopped());
assertEquals(1, engine.getInit());
assertEquals(1, engine.getDisposed());