1 package picocontainer.hierarchical;
2
3 import junit.framework.TestCase;
4 import picocontainer.PicoInitializationException;
5 import picocontainer.defaults.DefaultPicoContainer;
6 import picocontainer.testmodel.FredImpl;
7 import picocontainer.testmodel.WilmaImpl;
8
9 import java.util.ArrayList;
10 import java.util.List;
11
12 public class ClassicLifecycleTestCase extends TestCase {
13
14 public static class One implements Startable, Stoppable, Disposable {
15
16 List instantiating = new ArrayList();
17 List starting = new ArrayList();
18 List stopping = new ArrayList();
19 List disposing = new ArrayList();
20
21 public One() {
22 instantiation("One");
23 }
24
25 public void instantiation(String s) {
26 instantiating.add(s);
27 }
28
29 public List getInstantiating() {
30 return instantiating;
31 }
32
33 public List getStarting() {
34 return starting;
35 }
36
37 public List getStopping() {
38 return stopping;
39 }
40
41 public List getDisposing() {
42 return disposing;
43 }
44
45 public void start() throws Exception {
46 startCalled("One");
47 }
48
49 public void stop() throws Exception {
50 stopCalled("One");
51 }
52
53 public void dispose() throws Exception {
54 disposeCalled("One");
55 }
56
57 public void startCalled(String msg) {
58 starting.add(msg);
59 }
60
61 public void stopCalled(String msg) {
62 stopping.add(msg);
63 }
64
65 public void disposeCalled(String msg) {
66 disposing.add(msg);
67 }
68
69 }
70
71 public static class Two implements Startable, Stoppable, Disposable {
72 One one;
73
74 public Two(One one) {
75 one.instantiation("Two");
76 this.one = one;
77 }
78
79 public void start() throws Exception {
80 one.startCalled("Two");
81 }
82
83 public void stop() throws Exception {
84 one.stopCalled("Two");
85 }
86
87 public void dispose() throws Exception {
88 one.disposeCalled("Two");
89 }
90 }
91
92 public static class Three implements Startable, Stoppable, Disposable {
93 One one;
94
95 public Three(One one, Two two) {
96 one.instantiation("Three");
97 this.one = one;
98 }
99
100 public void start() throws Exception {
101 one.startCalled("Three");
102 }
103
104 public void stop() throws Exception {
105 one.stopCalled("Three");
106 }
107
108 public void dispose() throws Exception {
109 one.disposeCalled("Three");
110 }
111 }
112
113 public static class Four implements Startable, Stoppable, Disposable {
114 One one;
115
116 public Four(Two two, Three three, One one) {
117 one.instantiation("Four");
118 this.one = one;
119 }
120
121 public void start() throws Exception {
122 one.startCalled("Four");
123 }
124
125 public void stop() throws Exception {
126 one.stopCalled("Four");
127 }
128
129 public void dispose() throws Exception {
130 one.disposeCalled("Four");
131 }
132 }
133
134
135 public static interface Startable {
136 void start() throws Exception;
137 }
138
139 public static interface Stoppable {
140 void stop() throws Exception;
141 }
142
143 public static interface Disposable {
144 void dispose() throws Exception;
145 }
146
147
148 public void testOrderOfInstantiation() throws Exception {
149
150 DefaultPicoContainer pico = new DefaultPicoContainer.Default();
151
152 pico.registerComponent(Four.class);
153 pico.registerComponent(Two.class);
154 pico.registerComponent(One.class);
155 pico.registerComponent(Three.class);
156
157 pico.instantiateComponents();
158
159 Startable startup = (Startable) pico.getAggregateComponentProxy(true, false);
160 Stoppable shutdown = (Stoppable) pico.getAggregateComponentProxy(false, false);
161 Disposable disposal = (Disposable) pico.getAggregateComponentProxy(false, false);
162
163 assertTrue("There should have been a 'One' in the container", pico.hasComponent(One.class));
164
165 One one = (One) pico.getComponent(One.class);
166
167 // instantiation - would be difficult to do these in the wrong order!!
168 assertEquals("Should be four elems", 4, one.getInstantiating().size());
169 assertEquals("Incorrect Order of Instantiation", "One", one.getInstantiating().get(0));
170 assertEquals("Incorrect Order of Instantiation", "Two", one.getInstantiating().get(1));
171 assertEquals("Incorrect Order of Instantiation", "Three", one.getInstantiating().get(2));
172 assertEquals("Incorrect Order of Instantiation", "Four", one.getInstantiating().get(3));
173
174 startup.start();
175
176 // post instantiation startup
177 assertEquals("Should be four elems", 4, one.getStarting().size());
178 assertEquals("Incorrect Order of Starting", "One", one.getStarting().get(0));
179 assertEquals("Incorrect Order of Starting", "Two", one.getStarting().get(1));
180 assertEquals("Incorrect Order of Starting", "Three", one.getStarting().get(2));
181 assertEquals("Incorrect Order of Starting", "Four", one.getStarting().get(3));
182
183 shutdown.stop();
184
185 // post instantiation shutdown - REVERSE order.
186 assertEquals("Should be four elems", 4, one.getStopping().size());
187 assertEquals("Incorrect Order of Stopping", "Four", one.getStopping().get(0));
188 assertEquals("Incorrect Order of Stopping", "Three", one.getStopping().get(1));
189 assertEquals("Incorrect Order of Stopping", "Two", one.getStopping().get(2));
190 assertEquals("Incorrect Order of Stopping", "One", one.getStopping().get(3));
191
192 disposal.dispose();
193
194 // post instantiation shutdown - REVERSE order.
195 assertEquals("Should be four elems", 4, one.getDisposing().size());
196 assertEquals("Incorrect Order of Stopping", "Four", one.getDisposing().get(0));
197 assertEquals("Incorrect Order of Stopping", "Three", one.getDisposing().get(1));
198 assertEquals("Incorrect Order of Stopping", "Two", one.getDisposing().get(2));
199 assertEquals("Incorrect Order of Stopping", "One", one.getDisposing().get(3));
200
201 }
202
203
204 public static class ForgivingLifecyclePicoContainer extends DefaultPicoContainer.Default implements Startable, Stoppable {
205
206 private Startable startableAggregatedComponent;
207 private Stoppable stoppingAggregatedComponent;
208 private Disposable disposingAggregatedComponent;
209 private boolean started;
210 private boolean disposed;
211
212 public ForgivingLifecyclePicoContainer() {
213 }
214
215 public void instantiateComponents() throws PicoInitializationException {
216 super.instantiateComponents();
217 try {
218 startableAggregatedComponent = (Startable) getAggregateComponentProxy(true, false);
219 } catch (ClassCastException e) {
220 }
221 try {
222
223 stoppingAggregatedComponent = (Stoppable) getAggregateComponentProxy(false, false);
224 } catch (ClassCastException e) {
225 }
226
227 }
228
229 public void start() throws Exception {
230 checkDisposed();
231 if (started) {
232 throw new IllegalStateException("Already started.");
233 }
234 started = true;
235 if (startableAggregatedComponent != null) {
236 startableAggregatedComponent.start();
237 }
238 }
239
240 public void stop() throws Exception {
241 checkDisposed();
242 if (started == false) {
243 throw new IllegalStateException("Already stopped.");
244 }
245 started = false;
246 if (stoppingAggregatedComponent != null) {
247 stoppingAggregatedComponent.stop();
248 }
249 }
250
251 private void checkDisposed() {
252 if (disposed) {
253 throw new IllegalStateException("Components Disposed Of");
254 }
255 }
256
257 public void dispose() throws Exception {
258 checkDisposed();
259 disposed = true;
260 if (disposingAggregatedComponent != null) {
261 disposingAggregatedComponent.dispose();
262 }
263 }
264 }
265
266 public void testStartStopStartStopAndDispose() throws Exception {
267
268 ForgivingLifecyclePicoContainer pico = new ForgivingLifecyclePicoContainer();
269
270 pico.registerComponent(FredImpl.class);
271 pico.registerComponent(WilmaImpl.class);
272
273 pico.instantiateComponents();
274
275 pico.start();
276 pico.stop();
277
278 pico.start();
279 pico.stop();
280
281 pico.dispose();
282
283 }
284
285 public void testStartStartCausingBarf() throws Exception {
286
287 ForgivingLifecyclePicoContainer pico = new ForgivingLifecyclePicoContainer();
288
289 pico.registerComponent(FredImpl.class);
290 pico.registerComponent(WilmaImpl.class);
291
292 pico.instantiateComponents();
293
294 pico.start();
295 try {
296 pico.start();
297 fail("Should have barfed");
298 } catch (IllegalStateException e) {
299 // expected;
300 }
301 }
302
303 public void testStartStopStopCausingBarf() throws Exception {
304 ForgivingLifecyclePicoContainer pico = new ForgivingLifecyclePicoContainer();
305
306 pico.registerComponent(FredImpl.class);
307 pico.registerComponent(WilmaImpl.class);
308
309 pico.instantiateComponents();
310 pico.start();
311 pico.stop();
312 try {
313 pico.stop();
314 fail("Should have barfed");
315 } catch (IllegalStateException e) {
316 // expected;
317 }
318 }
319
320 public void testStartStopDisposeDisposeCausingBarf() throws Exception {
321 ForgivingLifecyclePicoContainer pico = new ForgivingLifecyclePicoContainer();
322
323 pico.registerComponent(FredImpl.class);
324 pico.registerComponent(WilmaImpl.class);
325
326 pico.instantiateComponents();
327 pico.start();
328 pico.stop();
329 pico.dispose();
330 try {
331 pico.dispose();
332 fail("Should have barfed");
333 } catch (IllegalStateException e) {
334 // expected;
335 }
336 }
337
338 public static class FooRunnable implements Runnable, Startable, Stoppable {
339 private int runCount;
340 private Thread thread = new Thread();
341 private boolean interrupted;
342
343 public FooRunnable() {
344 }
345
346 public int runCount() {
347 return runCount;
348 }
349
350 public boolean isInterrupted() {
351 return interrupted;
352 }
353
354 public void start() {
355 thread = new Thread(this);
356 thread.start();
357 }
358
359 public void stop() {
360 thread.interrupt();
361 }
362
363 // this would do something a bit more concrete
364 // than counting in real life !
365 public void run() {
366 runCount++;
367 try {
368 Thread.sleep(10000);
369 } catch (InterruptedException e) {
370 interrupted = true;
371 }
372 }
373 }
374
375 public void testStartStopOfDaemonizedThread() throws Exception {
376 ForgivingLifecyclePicoContainer pico = new ForgivingLifecyclePicoContainer();
377
378 pico.registerComponent(FredImpl.class);
379 pico.registerComponent(WilmaImpl.class);
380 pico.registerComponent(FooRunnable.class);
381
382 pico.instantiateComponents();
383 pico.start();
384 Thread.sleep(100);
385 pico.stop();
386
387 FooRunnable foo = (FooRunnable) pico.getComponent(FooRunnable.class);
388 assertEquals(1, foo.runCount());
389 pico.start();
390 Thread.sleep(100);
391 pico.stop();
392 assertEquals(2, foo.runCount());
393
394 }
395
396
397
398 }
This page was automatically generated by Maven