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 *****************************************************************************/
010package org.picocontainer.containers;
011
012import static org.junit.Assert.assertEquals;
013import static org.junit.Assert.assertNotNull;
014import static org.junit.Assert.assertNull;
015import static org.junit.Assert.fail;
016
017import java.io.IOException;
018import java.io.StringReader;
019import org.junit.Test;
020import org.picocontainer.Characteristics;
021import org.picocontainer.DefaultPicoContainer;
022import org.picocontainer.annotations.Inject;
023import org.picocontainer.injectors.AbstractInjector;
024import org.picocontainer.injectors.AnnotatedFieldInjection;
025import org.picocontainer.injectors.SetterInjection;
026
027public class CommandLinePicoContainerTestCase {
028
029    @Test public void testBasicParsing() {
030        CommandLinePicoContainer apc = new CommandLinePicoContainer(new String[] {
031            "foo=bar", "foo2=12", "foo3=true", "foo4="
032        });
033        assertEquals("bar",apc.getComponent("foo"));
034        assertEquals("12",apc.getComponent("foo2"));
035        assertEquals("true",apc.getComponent("foo3"));
036        assertEquals("true",apc.getComponent("foo4"));
037    }
038
039    @Test public void testAsParentContainer() {
040        CommandLinePicoContainer apc = new CommandLinePicoContainer(new String[] {
041            "a=aaa", "b=bbb", "d=22"});
042        assertEquals("aaa",apc.getComponent("a"));
043        assertEquals("bbb",apc.getComponent("b"));
044        assertEquals("22",apc.getComponent("d"));
045
046        DefaultPicoContainer dpc = new DefaultPicoContainer(apc);
047        dpc.addComponent(NeedsString.class);
048        assertEquals("bbb", dpc.getComponent(NeedsString.class).val);
049    }
050
051    public static class NeedsString {
052        public String val;
053        public NeedsString(String b) {
054            val = b;
055        }
056    }
057
058    @Test public void testParsingWithDiffSeparator() {
059        CommandLinePicoContainer apc = new CommandLinePicoContainer(":", new String[] {
060            "foo:bar", "foo2:12", "foo3:true"
061        });
062        assertEquals("bar",apc.getComponent("foo"));
063        assertEquals("12",apc.getComponent("foo2"));
064        assertEquals("true",apc.getComponent("foo3"));
065    }
066
067    @Test public void testParsingWithWrongSeparator() {
068        CommandLinePicoContainer apc = new CommandLinePicoContainer(":", new String[] {
069            "foo=bar", "foo2=12", "foo3=true"
070        });
071        assertEquals("true",apc.getComponent("foo=bar"));
072        assertEquals("true",apc.getComponent("foo2=12"));
073        assertEquals("true",apc.getComponent("foo3=true"));
074    }
075
076    @Test public void testParsingOfPropertiesFile() throws IOException {
077        CommandLinePicoContainer apc = new CommandLinePicoContainer(":",
078                               new StringReader("foo:bar\nfoo2:12\nfoo3:true\n"));
079        assertEquals("bar",apc.getComponent("foo"));
080        assertEquals("12",apc.getComponent("foo2"));
081        assertEquals("true",apc.getComponent("foo3"));
082    }
083
084    @Test public void testParsingOfPropertiesFileAndArgs() throws IOException {
085        CommandLinePicoContainer apc = new CommandLinePicoContainer(":",
086                               new StringReader("foo:bar\nfoo2:12\n"), new String[] {"foo3:true"});
087        assertEquals("bar",apc.getComponent("foo"));
088        assertEquals("12",apc.getComponent("foo2"));
089        assertEquals("true",apc.getComponent("foo3"));
090    }
091
092    @Test public void testParsingOfPropertiesFileAndArgsWithClash() throws IOException {
093        CommandLinePicoContainer apc = new CommandLinePicoContainer(":",
094                               new StringReader("foo:bar\nfoo2:99\n"), new String[] {"foo2:12","foo3:true"});
095        assertEquals("bar",apc.getComponent("foo"));
096        assertEquals("12",apc.getComponent("foo2"));
097        assertEquals("true",apc.getComponent("foo3"));
098    }
099
100    @Test public void testbyTypeFailsEvenIfOneOfSameType() {
101        CommandLinePicoContainer apc = new CommandLinePicoContainer(new String[] {
102            "foo=bar"});
103        assertEquals("bar",apc.getComponent("foo"));
104        assertNull(apc.getComponent(String.class));
105    }
106
107    @Test public void testUnsatisfiableIfNoSuitableTyesForInjection() {
108        CommandLinePicoContainer apc = new CommandLinePicoContainer(new String[] {"zz=zz"});
109        DefaultPicoContainer pico = new DefaultPicoContainer(apc);
110        pico.as(Characteristics.USE_NAMES).addComponent(NeedsAFew.class);
111        try {
112            Object foo = pico.getComponent(NeedsAFew.class);
113            fail();
114        } catch (AbstractInjector.UnsatisfiableDependenciesException e) {
115            // expetced;
116        }
117    }
118    public static class NeedsAFew {
119        private final String a;
120        private final int b;
121        private final boolean c;
122        public NeedsAFew(String a, int b, boolean c) {
123            this.a = a;
124            this.b = b;
125            this.c = c;
126        }
127    }
128
129    @Test public void testConstructorInjectionComponentCanDependOnConfig() {
130        CommandLinePicoContainer apc = new CommandLinePicoContainer(new String[] {"a=a", "b=2", "c=true"});
131        DefaultPicoContainer pico = new DefaultPicoContainer(apc);
132        pico.addConfig("zzz","zzz");
133        pico.as(Characteristics.USE_NAMES).addComponent(NeedsAFew.class);
134        NeedsAFew needsAFew = pico.getComponent(NeedsAFew.class);
135        assertNotNull(needsAFew);
136        assertEquals("a", needsAFew.a);
137        assertEquals(2, needsAFew.b);
138        assertEquals(true, needsAFew.c);
139    }
140
141    public static class NeedsAFew2 {
142        private String a;
143        private int b;
144        private boolean c;
145
146        public void setA(String a) {
147            this.a = a;
148        }
149
150        public void setB(int b) {
151            this.b = b;
152        }
153
154        public void setC(boolean c) {
155            this.c = c;
156        }
157    }
158
159    @Test public void testSetterInjectionComponentCanDependOnConfig() {
160        CommandLinePicoContainer apc = new CommandLinePicoContainer(new String[] {"a=a", "b=2", "c=true"});
161        DefaultPicoContainer pico = new DefaultPicoContainer(new SetterInjection(), apc);
162        pico.addConfig("zzz","zzz");
163        pico.as(Characteristics.USE_NAMES).addComponent(NeedsAFew2.class);
164        NeedsAFew2 needsAFew = pico.getComponent(NeedsAFew2.class);
165        assertNotNull(needsAFew);
166        assertEquals("a", needsAFew.a);
167        assertEquals(2, needsAFew.b);
168        assertEquals(true, needsAFew.c);
169    }
170
171    public static class NeedsAFew3 {
172        @Inject
173        private String a;
174        @Inject
175        private int b;
176        @Inject
177        private boolean c;
178    }
179
180    @Test public void testAnnotatedFieldInjectionComponentCanDependOnConfig() {
181        CommandLinePicoContainer apc = new CommandLinePicoContainer(new String[] {"a=a", "b=2", "c=true"});
182        DefaultPicoContainer pico = new DefaultPicoContainer(new AnnotatedFieldInjection(), apc);
183        pico.addConfig("zzz","zzz");
184        pico.as(Characteristics.USE_NAMES).addComponent(NeedsAFew3.class);
185        NeedsAFew3 needsAFew = pico.getComponent(NeedsAFew3.class);
186        assertNotNull(needsAFew);
187        assertEquals("a", needsAFew.a);
188        assertEquals(2, needsAFew.b);
189        assertEquals(true, needsAFew.c);
190    }
191
192    @Test public void testRepresentationOfContainerTree() {
193        CommandLinePicoContainer parent = new CommandLinePicoContainer(new String[] {"a=a", "b=2", "c=true"});
194        parent.setName("parent");
195        DefaultPicoContainer child = new DefaultPicoContainer(parent);
196        child.setName("child");
197                child.addComponent("hello", "goodbye");
198        child.addComponent("bonjour", "aurevior");
199        String actual = child.toString();
200        assertEquals("child:2<[Immutable]:[CommandLine]:parent:3<|", actual);
201    }
202
203
204}