KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > picocontainer > testmodel > RecordingLifecycle


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.html file. *
7  * *
8  * Idea by Rachel Davies, Original code by Aslak Hellesoy and Paul Hammant *
9  *****************************************************************************/

10 package org.picocontainer.testmodel;
11
12 import junit.framework.TestCase;
13
14 import org.picocontainer.Disposable;
15 import org.picocontainer.PicoContainer;
16 import org.picocontainer.Startable;
17
18
19 public abstract class RecordingLifecycle implements Startable, Disposable {
20     private final StringBuffer JavaDoc recording;
21
22     protected RecordingLifecycle(StringBuffer JavaDoc recording) {
23         this.recording = recording;
24     }
25
26     public void start() {
27         recording.append("<" + code());
28     }
29
30     public void stop() {
31         recording.append(code() + ">");
32     }
33
34     public void dispose() {
35         recording.append("!" + code());
36     }
37     
38     public String JavaDoc recording() {
39         return recording.toString();
40     }
41
42     private String JavaDoc code() {
43         String JavaDoc name = getClass().getName();
44         int idx = Math.max(name.lastIndexOf('$'), name.lastIndexOf('.'));
45         return name.substring(idx + 1);
46     }
47     
48     public interface Recorder extends Startable, Disposable {
49         String JavaDoc recording();
50     }
51
52     public static class One extends RecordingLifecycle implements Recorder {
53         public One(StringBuffer JavaDoc sb) {
54             super(sb);
55         }
56     }
57
58     public static class Two extends RecordingLifecycle {
59         public Two(StringBuffer JavaDoc sb, One one) {
60             super(sb);
61             TestCase.assertNotNull(one);
62         }
63     }
64
65     public static class Three extends RecordingLifecycle {
66         public Three(StringBuffer JavaDoc sb, One one, Two two) {
67             super(sb);
68             TestCase.assertNotNull(one);
69             TestCase.assertNotNull(two);
70         }
71     }
72
73     public static class Four extends RecordingLifecycle {
74         public Four(StringBuffer JavaDoc sb, Two two, Three three, One one) {
75             super(sb);
76             TestCase.assertNotNull(one);
77             TestCase.assertNotNull(two);
78             TestCase.assertNotNull(three);
79         }
80     }
81
82     public static class FiveTriesToBeMalicious extends RecordingLifecycle {
83         public FiveTriesToBeMalicious(StringBuffer JavaDoc sb, PicoContainer pc) {
84             super(sb);
85             TestCase.assertNotNull(pc);
86             sb.append("Whao! Should not get instantiated!!");
87         }
88     }
89     
90 }
Popular Tags