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