    001    package org.picocontainer.defaults.issues;
    002
    003    import org.junit.Test;
    004    import org.picocontainer.MutablePicoContainer;
    005    import org.picocontainer.PicoBuilder;
    006    import org.picocontainer.Characteristics;
    007
    008    import org.picocontainer.injectors.SetterInjection;
    009    import static junit.framework.Assert.assertEquals;
    010
    011    public class Issue0316TestCase {
    012
    013        //@Test
    014        public void testGood() {
    015            doTest(GoodTarget.class);
    016        }
    017
    018        @Test
    019        public void testBad() {
    020            doTest(BadTarget.class);
    021        }
    022
    023
    024        private void doTest(Class targetClass) {
    025            MutablePicoContainer pico = new PicoBuilder(new SetterInjection("inject")).build();
    026            pico.addComponent("fruit1", Apple.class);
    027            pico.addComponent("fruit2", Pear.class);
    028            pico.addComponent(new Integer(42));
    029            pico.as(Characteristics.USE_NAMES).addComponent(targetClass);
    030
    031            Target target = (Target)pico.getComponent(targetClass);
    032            assertEquals("apple", target.eat1());
    033            assertEquals("pear", target.eat2());
    034            assertEquals(42, target.getNumber());
    035        }
    036
    037
    038        public static interface Fruit {
    039            public String eat();
    040        }
    041        public static class Apple implements Fruit {
    042            public String eat() { return "apple"; }
    043        }
    044        public static class Pear implements Fruit {
    045            public String eat() { return "pear"; }
    046        }
    047
    048        public static interface Target {
    049            String eat1();
    050            String eat2();
    051            int getNumber();
    052        }
    053
    054        public static class GoodTarget implements Target {
    055            private Fruit fruit1;
    056            private Fruit fruit2;
    057            private Integer number;
    058
    059            public void injectNumber(Integer number)  {
    060                this.number = number;
    061            }
    062
    063            public void injectFruit1(Fruit fruit1) {
    064                this.fruit1 = fruit1;
    065            }
    066
    067            public void injectFruit2(Fruit fruit2) {
    068                this.fruit2 = fruit2;
    069            }
    070            public int getNumber() {
    071                return number.intValue();
    072            }
    073
    074            public String eat1() { return fruit1.eat(); }
    075            public String eat2() { return fruit2.eat(); }
    076        }
    077
    078        public static class BadTarget implements Target {
    079            private Fruit fruit1;
    080            private Fruit fruit2;
    081            private Integer number;
    082
    083            public void injectFruit1(Fruit fruit1) {
    084                this.fruit1 = fruit1;
    085            }
    086
    087            public void injectFruit2(Fruit fruit2) {
    088                this.fruit2 = fruit2;
    089            }
    090
    091            public void injectNumber(Integer number)  {
    092                this.number = number;
    093            }
    094
    095            public int getNumber() {
    096                return number.intValue();
    097            }
    098
    099            public String eat1() { return fruit1.eat(); }
    100            public String eat2() { return fruit2.eat(); }
    101        }
    102
    103    }