001package org.picocontainer.foo;
002
003import org.junit.Test;
004import org.picocontainer.Characteristics;
005import org.picocontainer.DefaultPicoContainer;
006import org.picocontainer.Emjection;
007import org.picocontainer.MutablePicoContainer;
008import org.picocontainer.behaviors.Caching;
009
010import static junit.framework.Assert.assertEquals;
011import static junit.framework.Assert.assertSame;
012import static org.junit.Assert.assertNotSame;
013import static org.picocontainer.Characteristics.EMJECTION_ENABLED;
014
015public class EmjectionTestCase {
016
017    @Test
018    public void basicEmjection() {
019        StringBuilder sb = new StringBuilder();
020        MutablePicoContainer pico = new DefaultPicoContainer();
021        pico.addComponent(Antelope.class);
022        pico.as(EMJECTION_ENABLED).addComponent(ZooKeeper.class);
023        pico.addComponent(sb);
024        ZooKeeper zooKeeper = pico.getComponent(ZooKeeper.class);
025        zooKeeper.doHeadCount();
026        assertEquals("giraffe=true, antelope=true", sb.toString());
027    }
028
029
030    @Test
031    public void testThatTransientNatureOfAdhocDependencies() {
032        MutablePicoContainer pico = new DefaultPicoContainer(new Caching());
033        pico.addComponent(Antelope.class);
034        pico.as(EMJECTION_ENABLED).addComponent(ZooKeeper.class);
035        pico.addComponent(new StringBuilder());
036        ZooKeeper component = pico.getComponent(ZooKeeper.class);
037        component.doHeadCount();
038        Zoo zoo1 = component.zoo;
039        component = pico.getComponent(ZooKeeper.class);
040        component.doHeadCount();
041        Zoo zoo2 = component.zoo;
042        zoo2.headCount();
043        assertNotSame(zoo1, zoo2); // made one the fly
044        assertNotSame(zoo1.giraffe, zoo2.giraffe); // made one the fly
045        assertSame(zoo1.antelope, zoo2.antelope); // in the parent-most picocontainer
046        assertSame(zoo1.sb, zoo2.sb); // in the parent-most picocontainer
047    }
048
049
050    public static class Zoo {
051        private final Emjection emjection = new Emjection();
052
053        private Giraffe giraffe;
054        private Antelope antelope;
055        private StringBuilder sb;
056
057        public Zoo(Giraffe giraffe, Antelope antelope, StringBuilder sb) {
058            this.giraffe = giraffe;
059            this.antelope = antelope;
060            this.sb = sb;
061        }
062
063        public void headCount() {
064            sb.append("giraffe=").append(giraffe != null);
065            sb.append(", antelope=").append(antelope != null);
066        }
067    }
068
069
070    public static class ZooKeeper {
071
072        private final Emjection emjection = new Emjection();
073        private Zoo zoo;
074
075        public void doHeadCount() {
076            zoo = neu(Zoo.class, new Giraffe());
077            zoo.headCount();
078        }
079
080        <T> T neu(Class<T> type, Object... args) {
081            return Emjection.neu(type, emjection, args);
082        }
083
084    }
085
086    public static class Giraffe {
087    }
088    public static class Antelope {
089    }
090
091
092}