    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     *****************************************************************************/
    009    package org.picocontainer.injectors;
    010
    011    import static org.junit.Assert.assertNotNull;
    012    import static org.junit.Assert.assertEquals;
    013
    014    import org.junit.Test;
    015    import org.picocontainer.DefaultPicoContainer;
    016    import static org.picocontainer.injectors.NamedFieldInjection.injectionFieldNames;
    017    import org.picocontainer.annotations.Inject;
    018
    019    /**
    020     * @author Paul Hammant
    021     */
    022    public class CompositeInjectionTestCase {
    023
    024        public static class Bar {
    025        }
    026        public static class Baz {
    027        }
    028
    029        public static class Foo {
    030            private final Bar bar;
    031            private Baz baz;
    032
    033            public Foo(Bar bar) {
    034                this.bar = bar;
    035            }
    036
    037            public void setBaz(Baz baz) {
    038                this.baz = baz;
    039            }
    040        }
    041
    042        public static class Foo2 {
    043            private final Bar bar;
    044            private Baz baz;
    045
    046            public Foo2(Bar bar) {
    047                this.bar = bar;
    048            }
    049
    050            public void injectBaz(Baz baz) {
    051                this.baz = baz;
    052            }
    053        }
    054
    055        public static class Foo3 {
    056            private final Bar bar;
    057            private Baz baz;
    058
    059            public Foo3(Bar bar) {
    060                this.bar = bar;
    061            }
    062
    063            @Inject
    064            public void fjshdfkjhsdkfjh(Baz baz) {
    065                this.baz = baz;
    066            }
    067        }
    068        public static class Foo4 {
    069            private final Bar bar;
    070            private String one;
    071            private String two;
    072
    073            public Foo4(Bar bar) {
    074                this.bar = bar;
    075            }
    076
    077        }
    078
    079        @Test public void testComponentWithCtorAndSetterDiCanHaveAllDepsSatisfied() throws NoSuchMethodException {
    080            DefaultPicoContainer dpc = new DefaultPicoContainer(
    081                    new CompositeInjection(new ConstructorInjection(), new SetterInjection()));
    082            dpc.addComponent(Bar.class);
    083            dpc.addComponent(Baz.class);
    084            dpc.addComponent(Foo.class);
    085            Foo foo = dpc.getComponent(Foo.class);
    086            assertNotNull(foo);
    087            assertNotNull(foo.bar);
    088            assertNotNull(foo.baz);
    089        }
    090
    091        @Test public void testComponentWithCtorAndSetterDiCanHaveAllDepsSatisfiedWithANonSetInjectMethod() throws NoSuchMethodException {
    092            DefaultPicoContainer dpc = new DefaultPicoContainer(
    093                    new CompositeInjection(new ConstructorInjection(), new SetterInjection("inject")));
    094            dpc.addComponent(Bar.class);
    095            dpc.addComponent(Baz.class);
    096            dpc.addComponent(Foo2.class);
    097            Foo2 foo = dpc.getComponent(Foo2.class);
    098            assertNotNull(foo);
    099            assertNotNull(foo.bar);
    100            assertNotNull(foo.baz);
    101        }
    102
    103        @Test public void testComponentWithCtorAndMethodAnnotatedDiCanHaveAllDepsSatisfied() throws NoSuchMethodException {
    104            DefaultPicoContainer dpc = new DefaultPicoContainer(
    105                    new CompositeInjection(new ConstructorInjection(), new AnnotatedMethodInjection()));
    106            dpc.addComponent(Bar.class);
    107            dpc.addComponent(Baz.class);
    108            dpc.addComponent(Foo3.class);
    109            Foo3 foo = dpc.getComponent(Foo3.class);
    110            assertNotNull(foo);
    111            assertNotNull(foo.bar);
    112            assertNotNull(foo.baz);
    113        }
    114
    115
    116        @Test public void testComponentWithCtorAndNamedFieldWorkToegether() throws NoSuchMethodException {
    117            DefaultPicoContainer dpc = new DefaultPicoContainer(
    118                    new CompositeInjection(new ConstructorInjection(), new NamedFieldInjection()));
    119            dpc.addComponent(Bar.class);
    120            dpc.addConfig("one", "1");
    121            dpc.addConfig("two", "2");
    122            dpc.as(injectionFieldNames("one", "two")).addComponent(Foo4.class);
    123            Foo4 foo4 = dpc.getComponent(Foo4.class);
    124            assertNotNull(foo4);
    125            assertNotNull(foo4.bar);
    126            assertNotNull(foo4.one);
    127            assertEquals("1", foo4.one);
    128            assertNotNull(foo4.two);
    129            assertEquals("2", foo4.two);
    130        }
    131
    132    }