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.defaults;
011    
012    import org.picocontainer.Parameter;
013    import org.picocontainer.PicoInitializationException;
014    import org.picocontainer.tck.AbstractComponentAdapterFactoryTestCase;
015    import org.picocontainer.tck.AbstractComponentAdapterTestCase.RecordingLifecycleStrategy;
016    import org.picocontainer.testmodel.NullLifecycle;
017    import org.picocontainer.testmodel.RecordingLifecycle;
018    import org.picocontainer.testmodel.RecordingLifecycle.One;
019    
020    /**
021     * @author J&ouml;rg Schaible</a>
022     * @version $Revision: 3139 $
023     */
024    public class SetterInjectionComponentAdapterFactoryTestCase extends AbstractComponentAdapterFactoryTestCase {
025        protected void setUp() throws Exception {
026            picoContainer = new DefaultPicoContainer(createComponentAdapterFactory());
027        }
028    
029        protected ComponentAdapterFactory createComponentAdapterFactory() {
030            return new SetterInjectionComponentAdapterFactory();
031        }
032    
033        public static interface Bean {
034        }
035    
036        public static class NamedBean implements Bean {
037            private String name;
038    
039            public String getName() {
040                return name;
041            }
042    
043            public void setName(String name) {
044                this.name = name;
045            }
046        }
047    
048        public static class NamedBeanWithPossibleDefault extends NamedBean {
049            private boolean byDefault;
050    
051            public NamedBeanWithPossibleDefault() {
052            }
053    
054            public NamedBeanWithPossibleDefault(String name) {
055                setName(name);
056                byDefault = true;
057            }
058    
059            public boolean getByDefault() {
060                return byDefault;
061            }
062        }
063    
064        public static class NoBean extends NamedBean {
065            public NoBean(String name) {
066                setName(name);
067            }
068        }
069    
070        public void testContainerUsesStandardConstructor() {
071            picoContainer.registerComponentImplementation(Bean.class, NamedBeanWithPossibleDefault.class);
072            picoContainer.registerComponentInstance("Tom");
073            NamedBeanWithPossibleDefault bean = (NamedBeanWithPossibleDefault) picoContainer.getComponentInstance(Bean.class);
074            assertFalse(bean.getByDefault());
075        }
076    
077        public void testContainerUsesOnlyStandardConstructor() {
078            picoContainer.registerComponentImplementation(Bean.class, NoBean.class);
079            picoContainer.registerComponentInstance("Tom");
080            try {
081                picoContainer.getComponentInstance(Bean.class);
082                fail("Instantiation should have failed.");
083            } catch (PicoInitializationException e) {
084            }
085        }
086    
087        public void testCustomLifecycleCanBeInjected() throws NoSuchMethodException {
088            RecordingLifecycleStrategy strategy = new RecordingLifecycleStrategy(new StringBuffer());
089            SetterInjectionComponentAdapterFactory caf = new SetterInjectionComponentAdapterFactory(false, strategy);
090            SetterInjectionComponentAdapter sica = (SetterInjectionComponentAdapter)caf.createComponentAdapter(NullLifecycle.class, NullLifecycle.class, new Parameter[0]);
091            One one = new RecordingLifecycle.One(new StringBuffer());
092            sica.start(one);
093            sica.stop(one);        
094            sica.dispose(one);
095            assertEquals("<start<stop<dispose", strategy.recording());
096        }    
097    }