001    package org.picocontainer.injectors;
002    
003    import org.junit.Test;
004    import org.picocontainer.*;
005    import org.picocontainer.monitors.NullComponentMonitor;
006    import org.picocontainer.monitors.WriterComponentMonitor;
007    import org.picocontainer.tck.AbstractComponentAdapterTest;
008    import org.picocontainer.testmodel.NullLifecycle;
009    import org.picocontainer.testmodel.SimpleTouchable;
010    import org.picocontainer.testmodel.Touchable;
011    
012    import java.io.PrintWriter;
013    import java.util.Properties;
014    
015    import static org.junit.Assert.assertEquals;
016    import static org.junit.Assert.assertTrue;
017    
018    public class LifecycleAdapterTestCase {
019    
020        private final ConstructorInjector INJECTOR = new ConstructorInjector(
021                Foo.class, Foo.class, new Parameter[0],
022                new NullComponentMonitor(), false);
023    
024        private AbstractComponentAdapterTest.RecordingLifecycleStrategy strategy = new AbstractComponentAdapterTest.RecordingLifecycleStrategy(new StringBuffer());
025    
026        AbstractInjectionFactory ais = new AbstractInjectionFactory() {
027            public <T> ComponentAdapter<T> createComponentAdapter(ComponentMonitor componentMonitor, LifecycleStrategy lifecycleStrategy, Properties componentProperties, Object componentKey, Class<T> componentImplementation, Parameter... parameters) throws PicoCompositionException {
028                return wrapLifeCycle(INJECTOR, lifecycleStrategy);
029            }
030        };
031    
032        @Test
033        public void passesOnLifecycleOperations() {
034    
035            LifecycleStrategy adapter = (LifecycleStrategy) ais.createComponentAdapter(new NullComponentMonitor(), strategy, new Properties(), null, null, new Parameter[0]);
036            assertEquals("org.picocontainer.injectors.AbstractInjectionFactory$LifecycleAdapter", adapter.getClass().getName());
037            Touchable touchable = new SimpleTouchable();
038            adapter.start(touchable);
039            adapter.stop(touchable);
040            adapter.dispose(touchable);
041            assertEquals("<start<stop<dispose", strategy.recording());
042        }
043    
044        @Test
045        public void canHaveMonitorChanged() {
046            ComponentMonitorStrategy adapter = (ComponentMonitorStrategy) ais.createComponentAdapter(new NullComponentMonitor(), strategy, new Properties(), Foo.class, Foo.class, new Parameter[0]);
047            assertTrue(adapter.currentMonitor() instanceof NullComponentMonitor);
048            adapter.changeMonitor(new WriterComponentMonitor(new PrintWriter(System.out)));
049            assertTrue(adapter.currentMonitor() instanceof WriterComponentMonitor);
050    
051        }
052    
053        public static class Foo implements Startable {
054            public void start() {
055            }
056            public void stop() {
057            }
058        }
059    
060    }