KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > picocontainer > defaults > UserQuestionTestCase


1 package org.picocontainer.defaults;
2
3 import junit.framework.TestCase;
4 import org.picocontainer.MutablePicoContainer;
5 import org.picocontainer.PicoContainer;
6 import org.picocontainer.PicoInitializationException;
7 import org.picocontainer.PicoIntrospectionException;
8
9 import java.util.HashMap JavaDoc;
10 import java.util.Map JavaDoc;
11
12 /**
13  * This class can be used to test out various things asked on the mailing list.
14  * Or to answer questions.
15  *
16  * @author Aslak Hellesøy
17  * @version $Revision: 1595 $
18  */

19 public class UserQuestionTestCase extends TestCase {
20
21     // From Scott Farquahsr
22
public static class CheeseComponentAdapter extends AbstractComponentAdapter {
23         private Map JavaDoc bla;
24
25         public CheeseComponentAdapter(Object JavaDoc componentKey, Class JavaDoc componentImplementation, Map JavaDoc cheeseMap) throws AssignabilityRegistrationException, NotConcreteRegistrationException {
26             super(componentKey, componentImplementation);
27             this.bla = cheeseMap;
28         }
29
30         public Object JavaDoc getComponentInstance(PicoContainer pico) throws PicoInitializationException, PicoIntrospectionException {
31             return bla.get("cheese");
32         }
33
34         public void verify(PicoContainer pico) throws UnsatisfiableDependenciesException {
35         }
36     }
37
38     public static interface Cheese {
39         String JavaDoc getName();
40     }
41
42     public static class Gouda implements Cheese {
43         public String JavaDoc getName() {
44             return "Gouda";
45         }
46     }
47
48     public static class Roquefort implements Cheese {
49         public String JavaDoc getName() {
50             return "Roquefort";
51         }
52     }
53
54     public static class Omelette {
55         private final Cheese cheese;
56
57         public Omelette(Cheese cheese) {
58             this.cheese = cheese;
59         }
60
61         public Cheese getCheese() {
62             return cheese;
63         }
64     }
65
66     public void testOmeletteCanHaveDifferentCheeseWithAFunnyComponentAdapter() {
67         Map JavaDoc cheeseMap = new HashMap JavaDoc();
68
69         MutablePicoContainer pico = new DefaultPicoContainer(new ConstructorInjectionComponentAdapterFactory());
70         pico.registerComponentImplementation(Omelette.class);
71         pico.registerComponent(new CheeseComponentAdapter("scott", Gouda.class, cheeseMap));
72
73         Cheese gouda = new Gouda();
74         cheeseMap.put("cheese", gouda);
75         Omelette goudaOmelette = (Omelette) pico.getComponentInstance(Omelette.class);
76         assertSame(gouda, goudaOmelette.getCheese());
77
78         Cheese roquefort = new Roquefort();
79         cheeseMap.put("cheese", roquefort);
80         Omelette roquefortOmelette = (Omelette) pico.getComponentInstance(Omelette.class);
81         assertSame(roquefort, roquefortOmelette.getCheese());
82     }
83
84     public static interface InterfaceX {
85         String JavaDoc getIt();
86     }
87
88     public static class Enabled implements InterfaceX {
89         public String JavaDoc getIt() {
90             return "Enabled";
91         }
92     }
93
94     public static class Disabled implements InterfaceX {
95         public String JavaDoc getIt() {
96             return "Disabled";
97         }
98     }
99
100     public static class Something implements InterfaceX {
101         private final Disabled disabled;
102         private final Enabled enabled;
103         private final Map JavaDoc map;
104
105         public Something(Disabled disabled, Enabled enabled, Map JavaDoc map) {
106             this.disabled = disabled;
107             this.enabled = enabled;
108             this.map = map;
109         }
110
111         public String JavaDoc getIt() {
112             if (map.get("enabled") == null) {
113                 return disabled.getIt();
114             } else {
115                 return enabled.getIt();
116             }
117         }
118     }
119
120     public static class NeedsInterfaceX {
121         private final InterfaceX interfaceX;
122
123         public NeedsInterfaceX(InterfaceX interfaceX) {
124             this.interfaceX = interfaceX;
125         }
126
127         public String JavaDoc getIt() {
128             return interfaceX.getIt();
129         }
130     }
131
132     public void testMoreWeirdness() {
133         MutablePicoContainer pico = new DefaultPicoContainer();
134         Map JavaDoc map = new HashMap JavaDoc();
135         pico.registerComponentInstance(map);
136         // See class level javadoc in DefaultPicoContainer - about precedence.
137
pico.registerComponentImplementation(InterfaceX.class, Something.class);
138         pico.registerComponentImplementation(Disabled.class);
139         pico.registerComponentImplementation(Enabled.class);
140         pico.registerComponentImplementation(NeedsInterfaceX.class);
141
142         NeedsInterfaceX needsInterfaceX = (NeedsInterfaceX) pico.getComponentInstance(NeedsInterfaceX.class);
143         assertEquals("Disabled", needsInterfaceX.getIt());
144         map.put("enabled", "blah");
145         assertEquals("Enabled", needsInterfaceX.getIt());
146     }
147
148     // From John Tal 23/03/2004
149
public static interface ABC {
150     }
151
152     public static interface DEF {
153     }
154
155     public static class ABCImpl implements ABC {
156         public ABCImpl(DEF def) {
157         }
158     }
159
160     public static class DEFImpl implements DEF {
161         public DEFImpl() {
162         }
163     }
164
165     public void testJohnTalOne() {
166         MutablePicoContainer picoContainer = new DefaultPicoContainer();
167
168         picoContainer.registerComponentImplementation("ABC", ABCImpl.class);
169         picoContainer.registerComponentImplementation("DEF", DEFImpl.class);
170
171         assertEquals(ABCImpl.class, picoContainer.getComponentInstance("ABC").getClass());
172     }
173
174     public static interface Foo {
175     }
176
177     public static interface Bar {
178     }
179
180     public static class FooBar implements Foo, Bar {
181     }
182
183     public static class NeedsFoo {
184         private final Foo foo;
185
186         public NeedsFoo(Foo foo) {
187             this.foo = foo;
188         }
189
190         public Foo getFoo() {
191             return foo;
192         }
193     }
194
195     public static class NeedsBar {
196         private final Bar bar;
197
198         public NeedsBar(Bar bar) {
199             this.bar = bar;
200         }
201
202         public Bar getBar() {
203             return bar;
204         }
205     }
206
207     public void testShouldBeAbleShareSameReferenceForDifferentTypes() {
208         MutablePicoContainer pico = new DefaultPicoContainer();
209         pico.registerComponentImplementation(FooBar.class);
210         pico.registerComponentImplementation(NeedsFoo.class);
211         pico.registerComponentImplementation(NeedsBar.class);
212         NeedsFoo needsFoo = (NeedsFoo) pico.getComponentInstance(NeedsFoo.class);
213         NeedsBar needsBar = (NeedsBar) pico.getComponentInstance(NeedsBar.class);
214         assertSame(needsFoo.getFoo(), needsBar.getBar());
215     }
216
217     public void testSeveralDifferentInstancesCanBeCreatedWithOnePreconfiguredContainer() {
218         // create a container that doesn't cache instances
219
MutablePicoContainer container = new DefaultPicoContainer(new ConstructorInjectionComponentAdapterFactory());
220         container.registerComponentImplementation(NeedsBar.class);
221
222         Bar barOne = new FooBar();
223         container.registerComponentInstance(Bar.class, barOne);
224         NeedsBar needsBarOne = (NeedsBar) container.getComponentInstance(NeedsBar.class);
225         assertSame(barOne, needsBarOne.getBar());
226
227         // reuse the same container - just flip out the existing foo.
228
Bar barTwo = new FooBar();
229         container.unregisterComponent(Bar.class);
230         container.registerComponentInstance(Bar.class, barTwo);
231         NeedsBar needsBarTwo = (NeedsBar) container.getComponentInstance(NeedsBar.class);
232         assertSame(barTwo, needsBarTwo.getBar());
233
234         assertNotSame(needsBarOne, needsBarTwo);
235     }
236 }
Popular Tags