    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     * Original code by                                                          *
    009     *****************************************************************************/
    010    package org.picocontainer.injectors;
    011
    012    import static org.junit.Assert.assertNotNull;
    013
    014    import java.lang.annotation.ElementType;
    015    import java.lang.annotation.Retention;
    016    import java.lang.annotation.RetentionPolicy;
    017    import java.lang.annotation.Target;
    018
    019    import org.junit.Test;
    020    import org.picocontainer.DefaultPicoContainer;
    021    import org.picocontainer.MutablePicoContainer;
    022    import org.picocontainer.Parameter;
    023    import org.picocontainer.annotations.Inject;
    024    import org.picocontainer.lifecycle.NullLifecycleStrategy;
    025    import org.picocontainer.monitors.NullComponentMonitor;
    026
    027    public class AnnotatedMethodInjectorTestCase  {
    028
    029        public static class AnnotatedBurp {
    030
    031            private Wind wind;
    032
    033            @Inject
    034            public void windyWind(Wind wind) {
    035                this.wind = wind;
    036            }
    037        }
    038
    039        public static class SetterBurp {
    040
    041            private Wind wind;
    042
    043            public void setWind(Wind wind) {
    044                this.wind = wind;
    045            }
    046        }
    047
    048        public static class Wind {
    049        }
    050
    051        @Test public void testSetterMethodInjectionToContrastWithThatBelow() {
    052
    053            MutablePicoContainer pico = new DefaultPicoContainer();
    054            pico.addAdapter(new SetterInjector(SetterBurp.class, SetterBurp.class, Parameter.DEFAULT, new NullComponentMonitor(), new NullLifecycleStrategy(),
    055                                               "set", false));
    056            pico.addComponent(Wind.class, new Wind());
    057            SetterBurp burp = pico.getComponent(SetterBurp.class);
    058            assertNotNull(burp);
    059            assertNotNull(burp.wind);
    060        }
    061
    062        @Test public void testNonSetterMethodInjection() {
    063            MutablePicoContainer pico = new DefaultPicoContainer();
    064            pico.addAdapter(new AnnotatedMethodInjector(AnnotatedBurp.class, AnnotatedBurp.class, Parameter.DEFAULT,
    065                                                   new NullComponentMonitor(), new NullLifecycleStrategy(),
    066                                                   Inject.class, false));
    067            pico.addComponent(Wind.class, new Wind());
    068            AnnotatedBurp burp = pico.getComponent(AnnotatedBurp.class);
    069            assertNotNull(burp);
    070            assertNotNull(burp.wind);
    071        }
    072
    073        @Retention(RetentionPolicy.RUNTIME)
    074        @Target(value={ ElementType.METHOD, ElementType.FIELD})
    075        public @interface AlternativeInject {
    076        }
    077
    078        public static class AnotherAnnotatedBurp {
    079            private Wind wind;
    080            @AlternativeInject
    081            public void windyWind(Wind wind) {
    082                this.wind = wind;
    083            }
    084        }
    085
    086
    087        @Test public void testNonSetterMethodInjectionWithAlternativeAnnotation() {
    088            MutablePicoContainer pico = new DefaultPicoContainer();
    089            pico.addAdapter(new AnnotatedMethodInjector(AnotherAnnotatedBurp.class, AnotherAnnotatedBurp.class, Parameter.DEFAULT,
    090                                                   new NullComponentMonitor(), new NullLifecycleStrategy(),
    091                                                   AlternativeInject.class, false));
    092            pico.addComponent(Wind.class, new Wind());
    093            AnotherAnnotatedBurp burp = pico.getComponent(AnotherAnnotatedBurp.class);
    094            assertNotNull(burp);
    095            assertNotNull(burp.wind);
    096        }
    097
    098
    099    }