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