    001    package org.picocontainer.injectors;
    002
    003    import static org.junit.Assert.assertEquals;
    004    import static org.junit.Assert.assertSame;
    005    import org.junit.Test;
    006    import org.picocontainer.*;
    007    import static org.picocontainer.injectors.Injector.constructor;
    008    import org.picocontainer.monitors.NullComponentMonitor;
    009    import org.picocontainer.parameters.ConstantParameter;
    010    import org.picocontainer.tck.AbstractComponentAdapterTest;
    011    import org.picocontainer.testmodel.NullLifecycle;
    012    import org.picocontainer.testmodel.RecordingLifecycle;
    013    import org.picocontainer.visitors.AbstractPicoVisitor;
    014
    015    /**
    016     * test capabilities of injector factory. as this is mostly convenience wrapper around
    017     * constructors, we just test that everything was passed through
    018     *
    019     * @author ko5tik
    020     */
    021    public class InjectorTestCase {
    022
    023        final Object key = new Object();
    024        final Parameter checked = new ConstantParameter(null);
    025        final Parameter[] checkedArray = new Parameter[]{checked};
    026        final ComponentMonitor monitor = new NullComponentMonitor();
    027
    028
    029        final PicoVisitor parameterChecker = new AbstractPicoVisitor() {
    030
    031
    032            public boolean visitContainer(PicoContainer pico) {
    033                return false;  //To change body of implemented methods use File | Settings | File Templates.
    034            }
    035
    036            public void visitComponentAdapter(ComponentAdapter<?> componentAdapter) {
    037                //To change body of implemented methods use File | Settings | File Templates.
    038            }
    039
    040            public void visitComponentFactory(ComponentFactory componentFactory) {
    041                //To change body of implemented methods use File | Settings | File Templates.
    042            }
    043
    044            public void visitParameter(Parameter parameter) {
    045                assertSame(checked, parameter);
    046            }
    047        };
    048
    049        /**
    050         * test that all parameters were passed to respective constructor
    051         */
    052        @Test
    053        public void testSimpleConstructor() {
    054            ComponentAdapter adapter = constructor(key, getClass(), checked);
    055            assertSame(key, adapter.getComponentKey());
    056            assertSame(getClass(), adapter.getComponentImplementation());
    057            adapter.accept(parameterChecker);
    058        }
    059
    060        /**
    061         *
    062         */
    063        @Test
    064        public void testNotSoSimpleContstructor() {
    065            AbstractComponentAdapterTest.RecordingLifecycleStrategy strategy = new AbstractComponentAdapterTest.RecordingLifecycleStrategy(new StringBuffer());
    066            ConstructorInjector adapter = (ConstructorInjector) constructor(key, NullLifecycle.class, checkedArray, monitor, strategy, true);
    067            assertSame(key, adapter.getComponentKey());
    068            assertSame(NullLifecycle.class, adapter.getComponentImplementation());
    069            adapter.accept(parameterChecker);
    070            RecordingLifecycle.One one = new RecordingLifecycle.One(new StringBuffer());
    071            adapter.start(one);
    072            adapter.stop(one);
    073            adapter.dispose(one);
    074            assertEquals("<start<stop<dispose", strategy.recording());
    075        }
    076    }