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ö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        public static class AnotherNamedBean implements Bean {
099            private String name;
100    
101            public String getName() {
102                return name;
103            }
104    
105            public void initName(String name) {
106                this.name = name;
107            }
108        }
109    
110        @Test public void testAlternatePrefixWorks() {
111            picoContainer = new DefaultPicoContainer(new SetterInjection("init"));
112            picoContainer.addComponent(Bean.class, AnotherNamedBean.class);
113            picoContainer.addComponent("Tom");
114            AnotherNamedBean bean = picoContainer.getComponent(AnotherNamedBean.class);
115            assertEquals("Tom", bean.getName());
116        }
117    
118    
119    }