    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.assertEquals;
    013    import static org.junit.Assert.assertNotNull;
    014    import static org.junit.Assert.assertTrue;
    015    import static org.junit.Assert.fail;
    016
    017    import java.lang.reflect.Method;
    018
    019    import org.junit.Test;
    020    import org.picocontainer.Characteristics;
    021    import org.picocontainer.DefaultPicoContainer;
    022    import org.picocontainer.MutablePicoContainer;
    023    import org.picocontainer.PicoBuilder;
    024    import org.picocontainer.PicoCompositionException;
    025    import org.picocontainer.annotations.Nullable;
    026    import org.picocontainer.lifecycle.NullLifecycleStrategy;
    027    import org.picocontainer.monitors.NullComponentMonitor;
    028
    029    public class MethodInjectionTestCase {
    030
    031        public static interface IFoo {
    032            void inject(Bar bar, String string);
    033        }
    034        public static class Foo implements IFoo {
    035            private Bar bar;
    036            private String string;
    037
    038            public void inject(Bar bar, String string) {
    039                this.bar = bar;
    040                this.string = string;
    041            }
    042        }
    043
    044        public static class Bar {
    045            public Bar() {
    046            }
    047        }
    048
    049        @Test public void testMethodInjection() {
    050            DefaultPicoContainer pico = new DefaultPicoContainer(new MethodInjection());
    051            pico.addComponent("hello");
    052            pico.addComponent(Foo.class);
    053            pico.addComponent(Bar.class);
    054            Foo foo = pico.getComponent(Foo.class);
    055            assertNotNull(foo.bar);
    056            assertNotNull(foo.string);
    057            assertEquals("MethodInjector-class org.picocontainer.injectors.MethodInjectionTestCase$Foo", pico.getComponentAdapter(Foo.class).toString());
    058        }
    059
    060        @Test public void testMethodInjectionViaMethodDef() {
    061            Method mthd = Foo.class.getMethods()[0];
    062            DefaultPicoContainer pico = new DefaultPicoContainer(new MethodInjection(mthd));
    063            pico.addComponent("hello");
    064            pico.addComponent(Foo.class);
    065            pico.addComponent(new Bar());
    066            Foo foo = pico.getComponent(Foo.class);
    067            assertNotNull(foo.bar);
    068            assertNotNull(foo.string);
    069            assertEquals("ReflectionMethodInjector["+mthd+"]-class org.picocontainer.injectors.MethodInjectionTestCase$Foo", pico.getComponentAdapter(Foo.class).toString());
    070        }
    071
    072        @Test public void testMethodInjectionViaMethodDefViaInterface() {
    073            Method mthd = IFoo.class.getMethods()[0];
    074            DefaultPicoContainer pico = new DefaultPicoContainer(new MethodInjection(mthd));
    075            pico.addComponent("hello");
    076            pico.addComponent(Foo.class);
    077            pico.addComponent(new Bar());
    078            Foo foo = pico.getComponent(Foo.class);
    079            assertNotNull(foo.bar);
    080            assertNotNull(foo.string);
    081            assertEquals("ReflectionMethodInjector["+mthd+"]-class org.picocontainer.injectors.MethodInjectionTestCase$Foo", pico.getComponentAdapter(Foo.class).toString());
    082        }
    083
    084
    085        @Test public void testMethodInjectionViaCharacteristics() {
    086            DefaultPicoContainer pico = new DefaultPicoContainer();
    087            pico.addComponent("hello");
    088            pico.as(Characteristics.METHOD_INJECTION).addComponent(Foo.class);
    089            pico.addComponent(Bar.class);
    090            Foo foo = pico.getComponent(Foo.class);
    091            assertNotNull(foo.bar);
    092            assertNotNull(foo.string);
    093            assertEquals("MethodInjector-class org.picocontainer.injectors.MethodInjectionTestCase$Foo", pico.getComponentAdapter(Foo.class).toString());
    094        }
    095
    096        @Test public void testMethodInjectionViaAdapter() {
    097            DefaultPicoContainer pico = new DefaultPicoContainer(new MethodInjection());
    098            pico.addComponent("hello");
    099            pico.addAdapter(new MethodInjector(Foo.class, Foo.class, null, new NullComponentMonitor(), new NullLifecycleStrategy(), "inject", false));
    100            pico.addComponent(Bar.class);
    101            Foo foo = pico.getComponent(Foo.class);
    102            assertNotNull(foo.bar);
    103            assertNotNull(foo.string);
    104            assertEquals("MethodInjector-class org.picocontainer.injectors.MethodInjectionTestCase$Foo", pico.getComponentAdapter(Foo.class).toString());
    105        }
    106
    107        @Test public void testMethodInjectionByBuilder() {
    108            MutablePicoContainer pico = new PicoBuilder().withMethodInjection().build();
    109            pico.addComponent("hello");
    110            pico.addComponent(Foo.class);
    111            pico.addComponent(Bar.class);
    112            Foo foo = pico.getComponent(Foo.class);
    113            assertNotNull(foo.bar);
    114            assertNotNull(foo.string);
    115            assertEquals("MethodInjector-class org.picocontainer.injectors.MethodInjectionTestCase$Foo", pico.getComponentAdapter(Foo.class).toString());
    116        }
    117
    118        public static class Foo2 implements IFoo {
    119            private Bar bar;
    120            private String string;
    121
    122            public void inject(Bar bar, @Nullable String string) {
    123                this.bar = bar;
    124                this.string = string;
    125            }
    126        }
    127
    128        @Test public void testMethodInjectionWithAllowedNullableParam() {
    129            DefaultPicoContainer pico = new DefaultPicoContainer(new MethodInjection());
    130            pico.addComponent(Foo2.class);
    131            pico.addComponent(Bar.class);
    132            Foo2 foo = pico.getComponent(Foo2.class);
    133            assertNotNull(foo.bar);
    134            assertTrue(foo.string == null);
    135            assertEquals("MethodInjector-class org.picocontainer.injectors.MethodInjectionTestCase$Foo2", pico.getComponentAdapter(Foo2.class).toString());
    136        }
    137
    138        @Test public void testMethodInjectionWithDisallowedNullableParam() {
    139            DefaultPicoContainer pico = new DefaultPicoContainer(new MethodInjection());
    140            pico.addComponent(Foo.class);
    141            pico.addComponent(Bar.class);
    142            try {
    143                Foo foo = pico.getComponent(Foo.class);
    144                fail("should have barfed");
    145            } catch (SingleMemberInjector.ParameterCannotBeNullException e) {
    146                assertEquals("string", e.getParameterName());
    147                assertTrue(e.getMessage().indexOf("Parameter 1") != -1);
    148                assertTrue(e.getMessage().indexOf(Foo.class.getMethods()[0].toString()) != -1);
    149            }
    150        }
    151
    152
    153    }