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