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     * Idea by Rachel Davies, Original code by Aslak Hellesoy and Paul Hammant   *
009     *****************************************************************************/
010    
011    package org.picocontainer.defaults;
012    
013    import static org.junit.Assert.assertEquals;
014    import static org.junit.Assert.assertFalse;
015    import static org.junit.Assert.assertNotNull;
016    import static org.junit.Assert.assertNull;
017    import static org.junit.Assert.assertSame;
018    import static org.junit.Assert.assertTrue;
019    import static org.junit.Assert.fail;
020    
021    import java.util.ArrayList;
022    import java.util.List;
023    
024    import org.junit.Test;
025    import org.picocontainer.DefaultPicoContainer;
026    import org.picocontainer.MutablePicoContainer;
027    import org.picocontainer.NameBinding;
028    import org.picocontainer.PicoCompositionException;
029    import org.picocontainer.parameters.ComponentParameter;
030    import org.picocontainer.parameters.ConstantParameter;
031    import org.picocontainer.testmodel.DependsOnTouchable;
032    import org.picocontainer.testmodel.SimpleTouchable;
033    import org.picocontainer.testmodel.Touchable;
034    import org.picocontainer.testmodel.Webster;
035    
036    public final class NoneOfTheseTestsAffectCoverageMeaningTheyCouldGoTestCase {
037    
038        //TODO - move to AbstractComponentRegistryTestCase
039        @Test public void testGetComponentSpecification() throws PicoCompositionException {
040            DefaultPicoContainer pico = new DefaultPicoContainer();
041    
042            assertNull(pico.getComponentAdapter(Touchable.class, (NameBinding) null));
043            pico.addComponent(SimpleTouchable.class);
044            assertNotNull(pico.getComponentAdapter(SimpleTouchable.class, (NameBinding) null));
045            assertNotNull(pico.getComponentAdapter(Touchable.class,(NameBinding)  null));
046        }
047    
048    
049        //TODO move
050        @Test public void testMultipleImplementationsAccessedThroughKey()
051                throws PicoCompositionException
052        {
053            SimpleTouchable Touchable1 = new SimpleTouchable();
054            SimpleTouchable Touchable2 = new SimpleTouchable();
055            DefaultPicoContainer pico = new DefaultPicoContainer();
056            pico.addComponent("Touchable1", Touchable1);
057            pico.addComponent("Touchable2", Touchable2);
058            pico.addComponent("fred1", DependsOnTouchable.class, new ComponentParameter("Touchable1"));
059            pico.addComponent("fred2", DependsOnTouchable.class, new ComponentParameter("Touchable2"));
060    
061            DependsOnTouchable fred1 = (DependsOnTouchable) pico.getComponent("fred1");
062            DependsOnTouchable fred2 = (DependsOnTouchable) pico.getComponent("fred2");
063    
064            assertFalse(fred1 == fred2);
065            assertSame(Touchable1, fred1.getTouchable());
066            assertSame(Touchable2, fred2.getTouchable());
067        }
068    
069        //TODO - move
070        @Test public void testRegistrationByName() throws Exception {
071            DefaultPicoContainer pico = new DefaultPicoContainer();
072    
073            Webster one = new Webster(new ArrayList());
074            Touchable two = new SimpleTouchable();
075    
076            pico.addComponent("one", one);
077            pico.addComponent("two", two);
078    
079            assertEquals("Wrong number of comps in the internals", 2, pico.getComponents().size());
080    
081            assertEquals("Looking up one Touchable", one, pico.getComponent("one"));
082            assertEquals("Looking up two Touchable", two, pico.getComponent("two"));
083    
084            assertTrue("Object one the same", one == pico.getComponent("one"));
085            assertTrue("Object two the same", two == pico.getComponent("two"));
086    
087            assertEquals("Lookup of unknown key should return null", null, pico.getComponent("unknown"));
088        }
089    
090        @Test public void testRegistrationByNameAndClassWithResolving() throws Exception {
091            DefaultPicoContainer pico = new DefaultPicoContainer();
092    
093            pico.addComponent(List.class, new ArrayList());
094            pico.addComponent("one", Webster.class);
095            pico.addComponent("two", SimpleTouchable.class);
096    
097            assertEquals("Wrong number of comps in the internals", 3, pico.getComponents().size());
098    
099            assertNotNull("Object one the same", pico.getComponent("one"));
100            assertNotNull("Object two the same", pico.getComponent("two"));
101    
102            assertNull("Lookup of unknown key should return null", pico.getComponent("unknown"));
103        }
104    
105        @Test public void testDuplicateRegistrationWithTypeAndObject() throws PicoCompositionException {
106            DefaultPicoContainer pico = new DefaultPicoContainer();
107    
108            pico.addComponent(SimpleTouchable.class);
109            try {
110                pico.addComponent(SimpleTouchable.class, new SimpleTouchable());
111                fail("Should have barfed with dupe registration");
112            } catch (PicoCompositionException e) {
113                // expected
114                assertTrue(e.getMessage().startsWith("Duplicate"));
115                assertTrue(e.getMessage().indexOf(SimpleTouchable.class.getName()) > 0);
116            }
117        }
118    
119    
120        @Test public void testComponentRegistrationMismatch() throws PicoCompositionException {
121            MutablePicoContainer pico = new DefaultPicoContainer();
122    
123            try {
124                pico.addComponent(List.class, SimpleTouchable.class);
125            } catch (ClassCastException e) {
126                // not worded in message
127                assertTrue(e.getMessage().indexOf(List.class.getName()) > 0);
128                assertTrue(e.getMessage().indexOf(SimpleTouchable.class.getName()) == 0);
129            }
130    
131        }
132    
133        interface Animal {
134    
135            String getFood();
136        }
137    
138        public static class Dino implements Animal {
139            final String food;
140    
141            public Dino(String food) {
142                this.food = food;
143            }
144    
145            public String getFood() {
146                return food;
147            }
148        }
149    
150        public static class Dino2 extends Dino {
151            public Dino2(int number) {
152                super(String.valueOf(number));
153            }
154        }
155    
156        public static class Dino3 extends Dino {
157            public Dino3(String a, String b) {
158                super(a + b);
159            }
160        }
161    
162        public static class Dino4 extends Dino {
163            public Dino4(String a, int n, String b, Touchable Touchable) {
164                super(a + n + b + " " + Touchable.getClass().getName());
165            }
166        }
167    
168        @Test public void testParameterCanBePassedToConstructor() throws Exception {
169            DefaultPicoContainer pico = new DefaultPicoContainer();
170            pico.addComponent(Animal.class,
171                    Dino.class,
172                    new ConstantParameter("bones"));
173    
174            Animal animal = pico.getComponent(Animal.class);
175            assertNotNull("Component not null", animal);
176            assertEquals("bones", animal.getFood());
177        }
178    
179        @Test public void testParameterCanBePrimitive() throws Exception {
180            DefaultPicoContainer pico = new DefaultPicoContainer();
181            pico.addComponent(Animal.class, Dino2.class, new ConstantParameter(22));
182    
183            Animal animal = pico.getComponent(Animal.class);
184            assertNotNull("Component not null", animal);
185            assertEquals("22", animal.getFood());
186        }
187    
188        @Test public void testMultipleParametersCanBePassed() throws Exception {
189            DefaultPicoContainer pico = new DefaultPicoContainer();
190            pico.addComponent(Animal.class, Dino3.class, new ConstantParameter("a"),
191                    new ConstantParameter("b"));
192    
193            Animal animal = pico.getComponent(Animal.class);
194            assertNotNull("Component not null", animal);
195            assertEquals("ab", animal.getFood());
196    
197        }
198    
199        @Test public void testParametersCanBeMixedWithComponentsCanBePassed() throws Exception {
200            DefaultPicoContainer pico = new DefaultPicoContainer();
201            pico.addComponent(Touchable.class, SimpleTouchable.class);
202            pico.addComponent(Animal.class, Dino4.class, new ConstantParameter("a"),
203                    new ConstantParameter(3),
204                    new ConstantParameter("b"),
205                    ComponentParameter.DEFAULT);
206    
207            Animal animal = pico.getComponent(Animal.class);
208            assertNotNull("Component not null", animal);
209            assertEquals("a3b org.picocontainer.testmodel.SimpleTouchable", animal.getFood());
210        }
211    
212    }