1 /******************************************************************************
2 * Copyright (C) PicoContainer Organization. All rights reserved. *
3 * ------------------------------------------------------------------------- *
4 * The software in this package is published under the terms of the BSD *
5 * style license a copy of which has been included with this distribution in *
6 * the LICENSE.txt file. *
7 * *
8 * Idea by Rachel Davies, Original code by Aslak Hellesoy and Paul Hammant *
9 *****************************************************************************/
10
11 package picocontainer.defaults;
12
13 import junit.framework.TestCase;
14 import picocontainer.PicoRegistrationException;
15 import picocontainer.PicoInitializationException;
16 import picocontainer.hierarchical.WrongNumberOfConstructorsRegistrationException;
17
18 import java.util.ArrayList;
19 import java.util.List;
20
21 public class DefaultPicoContainerTestCase extends TestCase {
22
23 public interface Peelable {
24 void peel();
25 }
26
27 public interface Washable {
28 void wash();
29 }
30
31 public interface Startable {
32 void start();
33
34 void stop();
35 }
36
37 public static class PeelableComponent implements Peelable {
38 boolean wasPeeled;
39
40 public PeelableComponent() {
41 }
42
43 public void peel() {
44 wasPeeled = true;
45 }
46 }
47
48 public static class NotReallyPeelableComponent {
49 boolean wasPeeled;
50
51 public void peel() {
52 wasPeeled = true;
53 }
54 }
55
56 public static class PeelableAndWashableComponent extends PeelableComponent implements Washable {
57 boolean wasWashed;
58
59 public void wash() {
60 wasWashed = true;
61 }
62 }
63
64 public static class Recorder {
65 List thingsThatHappened = new ArrayList();
66
67 public void record(String something) {
68 thingsThatHappened.add(something);
69 }
70
71 public String getWhatHappened(int i) {
72 return (String) thingsThatHappened.get(i);
73 }
74
75 public void clear() {
76 thingsThatHappened.clear();
77 }
78 }
79
80 public static abstract class RecordingAware {
81 protected Recorder recorder;
82
83 public RecordingAware(Recorder recorder) {
84 this.recorder = recorder;
85 String name = getClass().getName();
86 String niceName = name.substring(name.lastIndexOf("$") + 1);
87 recorder.record("instantiated " + niceName);
88 }
89 }
90
91 public static class A extends RecordingAware implements Washable {
92 public A(Recorder recorder) {
93 super(recorder);
94 }
95
96 public void wash() {
97 recorder.record("A.wash()");
98 }
99 }
100
101 public static class B extends RecordingAware implements Washable {
102 public B(Recorder recorder, A a) {
103 super(recorder);
104 a.toString();
105 }
106
107 public void wash() {
108 recorder.record("B.wash()");
109 }
110 }
111
112 public static class C extends RecordingAware implements Washable {
113 public C(Recorder recorder, A a, B b) {
114 super(recorder);
115 a.toString();
116 b.toString();
117 }
118
119 public void wash() {
120 recorder.record("C.wash()");
121 }
122 }
123
124 public void testApplyInterfaceMethodsToWholeContainer() throws PicoRegistrationException, PicoInitializationException {
125
126 DefaultPicoContainer pico = new DefaultPicoContainer.Default();
127 pico.registerComponent(PeelableComponent.class);
128 pico.registerComponent(NotReallyPeelableComponent.class);
129 pico.instantiateComponents();
130
131 assertEquals(2, pico.getComponents().length);
132
133 Peelable myPeelableContainer = (Peelable) pico.getMultipleInheritanceProxy();
134
135 myPeelableContainer.peel();
136
137 PeelableComponent peelableComponent =
138 (PeelableComponent) pico.getComponent(PeelableComponent.class);
139 NotReallyPeelableComponent notReallyPeelableComponent =
140 (NotReallyPeelableComponent) pico.getComponent(NotReallyPeelableComponent.class);
141
142 assertTrue(peelableComponent.wasPeeled);
143 assertFalse(notReallyPeelableComponent.wasPeeled);
144 }
145
146 public void testWorksWithMultipleInterfaces() throws PicoRegistrationException, PicoInitializationException {
147
148 DefaultPicoContainer pico = new DefaultPicoContainer.Default();
149
150 pico.registerComponent(PeelableComponent.class);
151 pico.registerComponent(NotReallyPeelableComponent.class);
152 pico.registerComponent(PeelableAndWashableComponent.class);
153
154 pico.instantiateComponents();
155
156 Object myPeelableAndWashableContainer = pico.getMultipleInheritanceProxy();
157
158 ((Washable) myPeelableAndWashableContainer).wash();
159
160 PeelableComponent peelableComponent =
161 (PeelableComponent) pico.getComponent(PeelableComponent.class);
162 NotReallyPeelableComponent notReallyPeelableComponent =
163 (NotReallyPeelableComponent) pico.getComponent(NotReallyPeelableComponent.class);
164 PeelableAndWashableComponent washAndPeel =
165 (PeelableAndWashableComponent) pico.getComponent(PeelableAndWashableComponent.class);
166 ;
167
168 assertFalse(washAndPeel.wasPeeled);
169 assertTrue(washAndPeel.wasWashed);
170
171 ((Peelable) myPeelableAndWashableContainer).peel();
172
173 assertTrue(peelableComponent.wasPeeled);
174 assertTrue(washAndPeel.wasPeeled);
175 assertTrue(washAndPeel.wasWashed);
176 assertFalse(notReallyPeelableComponent.wasPeeled);
177
178 }
179
180 public void testAsCallsAllComponents() throws PicoRegistrationException, PicoInitializationException {
181
182 DefaultPicoContainer pico = new DefaultPicoContainer.Default();
183
184 //an unmanaged component
185 PeelableComponent peelableComponent = new PeelableComponent();
186 pico.registerComponent(PeelableComponent.class, peelableComponent);
187
188 //some managed ones
189 pico.registerComponent(NotReallyPeelableComponent.class);
190 pico.registerComponent(PeelableAndWashableComponent.class);
191
192 pico.instantiateComponents();
193
194 Object myPeelableAndWashableContainer = pico.getMultipleInheritanceProxy();
195
196 ((Washable) myPeelableAndWashableContainer).wash();
197
198 NotReallyPeelableComponent notReallyPeelableComponent =
199 (NotReallyPeelableComponent) pico.getComponent(NotReallyPeelableComponent.class);
200 PeelableAndWashableComponent washAndPeel =
201 (PeelableAndWashableComponent) pico.getComponent(PeelableAndWashableComponent.class);
202
203 assertFalse(washAndPeel.wasPeeled);
204 assertTrue(washAndPeel.wasWashed);
205
206 ((Peelable) myPeelableAndWashableContainer).peel();
207
208 assertTrue(peelableComponent.wasPeeled);
209 assertTrue(washAndPeel.wasPeeled);
210 assertTrue(washAndPeel.wasWashed);
211 assertFalse(notReallyPeelableComponent.wasPeeled);
212
213 }
214
215 public void testLifecycleCallsComponentsInReverseOrder() throws PicoRegistrationException, PicoInitializationException {
216
217 DefaultPicoContainer pico = new DefaultPicoContainer.Default();
218
219 Recorder recorder = new Recorder();
220
221 pico.registerComponent(Recorder.class, recorder);
222 pico.registerComponent(A.class, A.class);
223 pico.registerComponent(B.class, B.class);
224 pico.registerComponent(C.class, C.class);
225
226 pico.instantiateComponents();
227
228 assertEquals("instantiated A", recorder.getWhatHappened(0));
229 assertEquals("instantiated B", recorder.getWhatHappened(1));
230 assertEquals("instantiated C", recorder.getWhatHappened(2));
231
232 recorder.clear();
233
234 Object washableContainer =
235 pico.getAggregateComponentProxy(false,true);
236
237 ((Washable) washableContainer).wash();
238
239 assertEquals("C.wash()", recorder.getWhatHappened(0));
240 assertEquals("B.wash()", recorder.getWhatHappened(1));
241 assertEquals("A.wash()", recorder.getWhatHappened(2));
242
243 }
244
245 public void testAsLifecycleOnlyCallsManagedComponents() throws PicoRegistrationException, PicoInitializationException {
246
247 DefaultPicoContainer pico = new DefaultPicoContainer.Default();
248
249 Recorder recorder = new Recorder();
250
251 A unmanagedComponent = new A(recorder);
252
253 recorder.clear();
254
255 pico.registerComponent(Recorder.class, recorder);
256 pico.registerComponent(A.class, unmanagedComponent);
257 pico.registerComponent(B.class);
258 pico.registerComponent(C.class);
259
260 pico.instantiateComponents();
261
262 assertEquals("instantiated B", recorder.getWhatHappened(0));
263 assertEquals("instantiated C", recorder.getWhatHappened(1));
264
265 recorder.clear();
266
267 Object washableContainer =
268 pico.getAggregateComponentProxy(false,false);
269
270 ((Washable) washableContainer).wash();
271
272 assertEquals("C.wash()", recorder.getWhatHappened(0));
273 assertEquals("B.wash()", recorder.getWhatHappened(1));
274 assertTrue(
275 "Unmanaged components should not be called by an asLifecycle() proxy",
276 !recorder.thingsThatHappened.contains("A.wash()"));
277 }
278
279 public void testPeelableAndWashable() throws WrongNumberOfConstructorsRegistrationException, PicoRegistrationException, PicoInitializationException {
280
281 DefaultPicoContainer pico = new DefaultPicoContainer.Default();
282
283 pico.registerComponent(PeelableComponent.class);
284 pico.registerComponent(PeelableAndWashableComponent.class);
285
286 pico.instantiateComponents();
287
288 Object proxy = pico.getMultipleInheritanceProxy();
289
290 Peelable peelable = (Peelable) proxy;
291 peelable.peel();
292
293 Washable washable = (Washable) proxy;
294 washable.wash();
295
296 PeelableComponent pComp = (PeelableComponent) pico.getComponent(PeelableComponent.class);
297 PeelableAndWashableComponent peelNWash = (PeelableAndWashableComponent) pico.getComponent(PeelableAndWashableComponent.class);
298
299 assertTrue(pComp.wasPeeled);
300 assertTrue(peelNWash.wasWashed);
301
302 }
303
304 // public static interface PeelableAndWashableContainer extends PeelableAndWashable, ClassRegistrationPicoContainer {
305 //
306 // }
307
308 // public void testPeelableAndWashableContainer() throws WrongNumberOfConstructorsRegistrationException, PicoRegistrationException, PicoStartException {
309 //
310 // PeelableAndWashableContainer pawContainer = (PeelableAndWashableContainer)
311 // new MorphingHierarchicalPicoContainer(
312 // new NullContainer(),
313 // new NullLifecycleManager(),
314 // new DefaultComponentFactory())
315 // .as(PeelableAndWashableContainer.class);
316 //
317 // pawContainer.registerComponent(PeelableComponent.class);
318 // pawContainer.registerComponent(PeelableAndWashableComponent.class);
319 //
320 // pawContainer.instantiateComponents();
321 //
322 // pawContainer.wash();
323 // pawContainer.peel();
324 //
325 // PeelableComponent pComp = (PeelableComponent) pawContainer.getComponent(PeelableComponent.class);
326 // PeelableAndWashableComponent peelNWash = (PeelableAndWashableComponent) pawContainer.getComponent(PeelableAndWashableComponent.class);
327 //
328 // assertTrue(pComp.wasPeeled);
329 // assertTrue(peelNWash.wasWashed);
330 //
331 // }
332
333
334 }
This page was automatically generated by Maven