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