    001    /*****************************************************************************
    002     * Copyright (C) PicoContainer Organization. All rights reserved.            *
    003     * ------------------------------------------------------------------------- *
    004     * The software in this package is published under the terms of the BSD      *
    005     * style license a copy of which has been included with this distribution in *
    006     * the LICENSE.txt file.                                                     *
    007     *                                                                           *
    008     * Idea by Rachel Davies, Original code by Aslak Hellesoy and Paul Hammant   *
    009     *****************************************************************************/
    010    package org.picocontainer.injectors;
    011
    012    import static org.junit.Assert.assertEquals;
    013    import static org.junit.Assert.assertFalse;
    014    import static org.junit.Assert.fail;
    015
    016    import java.util.Properties;
    017
    018    import org.junit.Before;
    019    import org.junit.Test;
    020    import org.picocontainer.ComponentFactory;
    021    import org.picocontainer.DefaultPicoContainer;
    022    import org.picocontainer.PicoCompositionException;
    023    import org.picocontainer.monitors.NullComponentMonitor;
    024    import org.picocontainer.tck.AbstractComponentFactoryTest;
    025    import org.picocontainer.tck.AbstractComponentAdapterTest.RecordingLifecycleStrategy;
    026    import org.picocontainer.testmodel.NullLifecycle;
    027    import org.picocontainer.testmodel.RecordingLifecycle;
    028    import org.picocontainer.testmodel.RecordingLifecycle.One;
    029
    030    /**
    031     * @author J&ouml;rg Schaible
    032     */
    033    public class SetterInjectionTestCase extends AbstractComponentFactoryTest {
    034
    035            @Before
    036        public void setUp() throws Exception {
    037            picoContainer = new DefaultPicoContainer(createComponentFactory());
    038        }
    039
    040        protected ComponentFactory createComponentFactory() {
    041            return new SetterInjection();
    042        }
    043
    044        public static interface Bean {
    045        }
    046
    047        public static class NamedBean implements Bean {
    048            private String name;
    049
    050            public String getName() {
    051                return name;
    052            }
    053
    054            public void setName(String name) {
    055                this.name = name;
    056            }
    057        }
    058
    059        public static class NamedBeanWithPossibleDefault extends NamedBean {
    060            private boolean byDefault;
    061
    062            public NamedBeanWithPossibleDefault() {
    063            }
    064
    065            public NamedBeanWithPossibleDefault(String name) {
    066                setName(name);
    067                byDefault = true;
    068            }
    069
    070            public boolean getByDefault() {
    071                return byDefault;
    072            }
    073        }
    074
    075        public static class NoBean extends NamedBean {
    076            public NoBean(String name) {
    077                setName(name);
    078            }
    079        }
    080
    081        @Test public void testContainerUsesStandardConstructor() {
    082            picoContainer.addComponent(Bean.class, NamedBeanWithPossibleDefault.class);
    083            picoContainer.addComponent("Tom");
    084            NamedBeanWithPossibleDefault bean = (NamedBeanWithPossibleDefault) picoContainer.getComponent(Bean.class);
    085            assertFalse(bean.getByDefault());
    086        }
    087
    088        @Test public void testContainerUsesOnlyStandardConstructor() {
    089            picoContainer.addComponent(Bean.class, NoBean.class);
    090            picoContainer.addComponent("Tom");
    091            try {
    092                picoContainer.getComponent(Bean.class);
    093                fail("Instantiation should have failed.");
    094            } catch (PicoCompositionException e) {
    095            }
    096        }
    097
    098        @Test public void testCustomLifecycleCanBeInjected() throws NoSuchMethodException {
    099            RecordingLifecycleStrategy strategy = new RecordingLifecycleStrategy(new StringBuffer());
    100            SetterInjection componentFactory = new SetterInjection();
    101            SetterInjector sica = (SetterInjector)componentFactory.createComponentAdapter(new NullComponentMonitor(), strategy, new Properties(), NullLifecycle.class, NullLifecycle.class);
    102            One one = new RecordingLifecycle.One(new StringBuffer());
    103            sica.start(one);
    104            sica.stop(one);
    105            sica.dispose(one);
    106            assertEquals("<start<stop<dispose", strategy.recording());
    107        }
    108
    109        public static class AnotherNamedBean implements Bean {
    110            private String name;
    111
    112            public String getName() {
    113                return name;
    114            }
    115
    116            public void initName(String name) {
    117                this.name = name;
    118            }
    119        }
    120
    121        @Test public void testAlternatePrefixWorks() {
    122            picoContainer = new DefaultPicoContainer(new SetterInjection("init"));
    123            picoContainer.addComponent(Bean.class, AnotherNamedBean.class);
    124            picoContainer.addComponent("Tom");
    125            AnotherNamedBean bean = picoContainer.getComponent(AnotherNamedBean.class);
    126            assertEquals("Tom", bean.getName());
    127        }
    128
    129
    130    }