KickJava   Java API By Example, From Geeks To Geeks.

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


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 package org.picocontainer.defaults;
9
10 import java.io.FileNotFoundException JavaDoc;
11
12 import org.jmock.MockObjectTestCase;
13 import org.picocontainer.MutablePicoContainer;
14 import org.picocontainer.Parameter;
15 import org.picocontainer.PicoIntrospectionException;
16
17
18 /**
19  * @author Aslak Hellesøy
20  * @author Jörg Schaible
21  * @version $Revision: 2291 $
22  */

23 public class LifecycleVisitorTestCase extends MockObjectTestCase {
24
25     public abstract static class RecordingLifecycle {
26         private final StringBuffer JavaDoc recording;
27
28         protected RecordingLifecycle(StringBuffer JavaDoc recording) {
29             this.recording = recording;
30         }
31
32         public void demarrer() {
33             recording.append("<" + code());
34         }
35
36         public void arreter() {
37             recording.append(code() + ">");
38         }
39
40         public void ecraser() {
41             recording.append("!" + code());
42         }
43
44         private String JavaDoc code() {
45             String JavaDoc name = getClass().getName();
46             return name.substring(name.indexOf('$') + 1);
47         }
48
49         public void uncallableByVisitor(final String JavaDoc s) {
50         }
51
52         public void throwsAtVisit() throws FileNotFoundException JavaDoc {
53             throw new FileNotFoundException JavaDoc();
54         }
55
56         private void callMe() {
57         }
58     }
59
60     public static class One extends RecordingLifecycle {
61         public One(StringBuffer JavaDoc sb) {
62             super(sb);
63         }
64     }
65
66     public static class Two extends RecordingLifecycle {
67         public Two(StringBuffer JavaDoc sb, One one) {
68             super(sb);
69             assertNotNull(one);
70         }
71     }
72
73     public static class Three extends RecordingLifecycle {
74         public Three(StringBuffer JavaDoc sb, One one, Two two) {
75             super(sb);
76             assertNotNull(one);
77             assertNotNull(two);
78         }
79     }
80
81     public static class Four extends RecordingLifecycle {
82         public Four(StringBuffer JavaDoc sb, Two two, Three three, One one) {
83             super(sb);
84             assertNotNull(one);
85             assertNotNull(two);
86             assertNotNull(three);
87         }
88     }
89
90     public void testShouldAllowCustomLifecycle() throws NoSuchMethodException JavaDoc {
91         LifecycleVisitor starter = new LifecycleVisitor(
92                 RecordingLifecycle.class.getMethod("demarrer", null), RecordingLifecycle.class, true);
93         LifecycleVisitor stopper = new LifecycleVisitor(
94                 RecordingLifecycle.class.getMethod("arreter", null), RecordingLifecycle.class, false);
95         LifecycleVisitor disposer = new LifecycleVisitor(
96                 RecordingLifecycle.class.getMethod("ecraser", null), RecordingLifecycle.class, false);
97
98         MutablePicoContainer parent = new DefaultPicoContainer();
99         MutablePicoContainer child = parent.makeChildContainer();
100         parent.registerComponentImplementation("recording", StringBuffer JavaDoc.class);
101         child.registerComponentImplementation(Four.class);
102         parent.registerComponentImplementation(Two.class);
103         parent.registerComponentImplementation(One.class, One.class, new Parameter[]{ComponentParameter.DEFAULT});
104         child.registerComponentImplementation(Three.class);
105
106         starter.traverse(parent);
107         stopper.traverse(parent);
108         disposer.traverse(parent);
109
110         assertEquals("<One<Two<Three<FourFour>Three>Two>One>!Four!Three!Two!One", parent.getComponentInstance(
111                 "recording").toString());
112     }
113
114     public void testPicoIntrospectionExceptionForInvalidMethod() throws NoSuchMethodException JavaDoc {
115         LifecycleVisitor visitor = new LifecycleVisitor(RecordingLifecycle.class.getMethod(
116                 "uncallableByVisitor", new Class JavaDoc[]{String JavaDoc.class}), RecordingLifecycle.class, true);
117         MutablePicoContainer pico = new DefaultPicoContainer();
118         pico.registerComponentImplementation(StringBuffer JavaDoc.class);
119         pico.registerComponentImplementation(One.class);
120         try {
121             visitor.traverse(pico);
122             fail("PicoIntrospectionException expected");
123         } catch (PicoIntrospectionException e) {
124             assertTrue(e.getCause() instanceof IllegalArgumentException JavaDoc);
125         }
126     }
127
128     public void testPicoIntrospectionExceptionForThrownException() throws NoSuchMethodException JavaDoc {
129         LifecycleVisitor visitor = new LifecycleVisitor(
130                 RecordingLifecycle.class.getMethod("throwsAtVisit", null), RecordingLifecycle.class, true);
131         MutablePicoContainer pico = new DefaultPicoContainer();
132         pico.registerComponentImplementation(StringBuffer JavaDoc.class);
133         pico.registerComponentImplementation(One.class);
134         try {
135             visitor.traverse(pico);
136             fail("PicoIntrospectionException expected");
137         } catch (PicoIntrospectionException e) {
138             assertTrue(e.getCause() instanceof FileNotFoundException JavaDoc);
139         }
140     }
141
142     public void testPicoIntrospectionExceptionForInaccessibleMethod() throws NoSuchMethodException JavaDoc {
143         LifecycleVisitor visitor = new LifecycleVisitor(
144                 RecordingLifecycle.class.getDeclaredMethod("callMe", null), RecordingLifecycle.class, true);
145         MutablePicoContainer pico = new DefaultPicoContainer();
146         pico.registerComponentImplementation(StringBuffer JavaDoc.class);
147         pico.registerComponentImplementation(One.class);
148         try {
149             visitor.traverse(pico);
150             fail("PicoIntrospectionException expected");
151         } catch (PicoIntrospectionException e) {
152             assertTrue(e.getCause() instanceof IllegalAccessException JavaDoc);
153         }
154     }
155 }
Popular Tags