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 org.junit.Before;
017    import org.junit.Test;
018    import org.picocontainer.ComponentFactory;
019    import org.picocontainer.DefaultPicoContainer;
020    import org.picocontainer.PicoCompositionException;
021    import org.picocontainer.monitors.NullComponentMonitor;
022    import org.picocontainer.tck.AbstractComponentFactoryTest;
023    import org.picocontainer.tck.AbstractComponentAdapterTest.RecordingLifecycleStrategy;
024    import org.picocontainer.testmodel.NullLifecycle;
025    import org.picocontainer.testmodel.RecordingLifecycle.One;
026    
027    /**
028     * @author Jörg Schaible
029     */
030    public class AnnotatedMethodInjectionTestCase extends AbstractComponentFactoryTest {
031            
032            @Before
033        public void setUp() throws Exception {
034            picoContainer = new DefaultPicoContainer(createComponentFactory());
035        }
036    
037        protected ComponentFactory createComponentFactory() {
038            return new AnnotatedMethodInjection();
039        }
040    
041        public static interface Bean {
042        }
043    
044        public static class NamedBean implements Bean {
045            private String name;
046    
047            public String getName() {
048                return name;
049            }
050    
051            public void setName(String name) {
052                this.name = name;
053            }
054        }
055    
056        public static class NamedBeanWithPossibleDefault extends NamedBean {
057            private boolean byDefault;
058    
059            public NamedBeanWithPossibleDefault() {
060            }
061    
062            public NamedBeanWithPossibleDefault(String name) {
063                setName(name);
064                byDefault = true;
065            }
066    
067            public boolean getByDefault() {
068                return byDefault;
069            }
070        }
071    
072        public static class NoBean extends NamedBean {
073            public NoBean(String name) {
074                setName(name);
075            }
076        }
077    
078        @Test public void testContainerUsesStandardConstructor() {
079            picoContainer.addComponent(Bean.class, NamedBeanWithPossibleDefault.class);
080            picoContainer.addComponent("Tom");
081            NamedBeanWithPossibleDefault bean = (NamedBeanWithPossibleDefault) picoContainer.getComponent(Bean.class);
082            assertFalse(bean.getByDefault());
083        }
084    
085        @Test public void testContainerUsesOnlyStandardConstructor() {
086            picoContainer.addComponent(Bean.class, NoBean.class);
087            picoContainer.addComponent("Tom");
088            try {
089                picoContainer.getComponent(Bean.class);
090                fail("Instantiation should have failed.");
091            } catch (PicoCompositionException e) {
092            }
093        }
094    }