KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > hivemind > lib > chain > TestChainBuilder


1 // Copyright 2005 The Apache Software Foundation
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15 package org.apache.hivemind.lib.chain;
16
17 import java.lang.reflect.Modifier JavaDoc;
18 import java.util.ArrayList JavaDoc;
19 import java.util.List JavaDoc;
20
21 import org.apache.hivemind.Registry;
22 import org.apache.hivemind.impl.RegistryBuilder;
23 import org.apache.hivemind.service.BodyBuilder;
24 import org.apache.hivemind.service.ClassFab;
25 import org.apache.hivemind.service.MethodFab;
26 import org.apache.hivemind.service.MethodSignature;
27 import org.apache.hivemind.xml.XmlTestCase;
28 import org.easymock.MockControl;
29
30 /**
31  * Tests for {@link org.apache.hivemind.lib.chain.ChainBuilderImpl} and
32  * {@link org.apache.hivemind.lib.chain.ChainFactory}.
33  *
34  * @author Howard M. Lewis Ship
35  * @since 1.1
36  */

37 public class TestChainBuilder extends XmlTestCase
38 {
39     public void testDefaultforReturnType()
40     {
41         ChainBuilderImpl cb = new ChainBuilderImpl();
42
43         assertEquals("null", cb.defaultForReturnType(Object JavaDoc.class));
44         assertEquals("false", cb.defaultForReturnType(boolean.class));
45         assertEquals("null", cb.defaultForReturnType(Boolean JavaDoc.class));
46         assertEquals("null", cb.defaultForReturnType(boolean[].class));
47         assertEquals("0", cb.defaultForReturnType(int.class));
48         assertEquals("null", cb.defaultForReturnType(Integer JavaDoc.class));
49     }
50
51     private MethodFab newMethodFab()
52     {
53         return (MethodFab) newMock(MethodFab.class);
54     }
55
56     /**
57      * Tests adding a void method, also tests creation of a toString() method.
58      */

59     public void testAddVoidMethod()
60     {
61         MockControl cfc = newControl(ClassFab.class);
62         ClassFab cf = (ClassFab) cfc.getMock();
63
64         MethodSignature sig = new MethodSignature(void.class, "run", null, null);
65
66         BodyBuilder builder = new BodyBuilder();
67         builder.begin();
68         builder.addln("for (int i = 0; i < _commands.length; i++)");
69         builder.addln("_commands[i].run($$);");
70         builder.end();
71
72         cf.addMethod(Modifier.PUBLIC, sig, builder.toString());
73         cfc.setReturnValue(newMethodFab());
74
75         replayControls();
76
77         ChainBuilderImpl cb = new ChainBuilderImpl();
78
79         cb.addMethod(cf, Runnable JavaDoc.class, sig);
80
81         verifyControls();
82     }
83
84     public void testAddNonVoidMethod()
85     {
86         MockControl cfc = newControl(ClassFab.class);
87         ClassFab cf = (ClassFab) cfc.getMock();
88
89         MethodSignature sig = new MethodSignature(boolean.class, "execute", new Class JavaDoc[]
90         { String JavaDoc.class }, null);
91
92         BodyBuilder builder = new BodyBuilder();
93         builder.begin();
94         builder.addln("boolean result = false;");
95         builder.addln("for (int i = 0; i < _commands.length; i++)");
96         builder.begin();
97         builder.addln("result = _commands[i].execute($$);");
98         builder.addln("if (result != false) break;");
99         builder.end();
100         builder.addln("return result;");
101         builder.end();
102
103         cf.addMethod(Modifier.PUBLIC, sig, builder.toString());
104         cfc.setReturnValue(newMethodFab());
105
106         replayControls();
107
108         ChainBuilderImpl cb = new ChainBuilderImpl();
109
110         cb.addMethod(cf, ChainInterface.class, sig);
111
112         verifyControls();
113     }
114
115     /**
116      * Test it all together inside the descriptor.
117      */

118
119     private ChainInterface newCommand(String JavaDoc parameter, boolean returnValue)
120     {
121         MockControl control = newControl(ChainInterface.class);
122         ChainInterface chain = (ChainInterface) control.getMock();
123
124         chain.execute(parameter);
125         control.setReturnValue(returnValue);
126
127         return chain;
128     }
129
130     public void testIntegration()
131     {
132         Registry r = RegistryBuilder.constructDefaultRegistry();
133
134         ChainBuilder cb = (ChainBuilder) r.getService(ChainBuilder.class);
135
136         List JavaDoc commands = new ArrayList JavaDoc();
137
138         commands.add(newCommand("fred", false));
139         commands.add(newCommand("fred", false));
140
141         ChainInterface chain = (ChainInterface) cb.buildImplementation(
142                 ChainInterface.class,
143                 commands,
144                 "<Chain>");
145
146         ChainInterface chain2 = (ChainInterface) cb.buildImplementation(
147                 ChainInterface.class,
148                 commands,
149                 "<Chain2>");
150
151         replayControls();
152
153         assertEquals(false, chain.execute("fred"));
154         assertEquals("<Chain>", chain.toString());
155
156         // This checks that implementations are cached, but that toString is unique
157
// for each instance.
158

159         assertEquals("<Chain2>", chain2.toString());
160         assertSame(chain.getClass(), chain2.getClass());
161
162         verifyControls();
163     }
164
165     /**
166      * Confirm that proceses of commands stops with the first one that returns a non-default value
167      * (i.e., true).
168      */

169
170     public void testIntegrationWithCancel()
171     {
172         Registry r = RegistryBuilder.constructDefaultRegistry();
173
174         ChainBuilder cb = (ChainBuilder) r.getService(ChainBuilder.class);
175
176         List JavaDoc commands = new ArrayList JavaDoc();
177
178         commands.add(newCommand("barney", true));
179         commands.add(newMock(ChainInterface.class));
180
181         ChainInterface chain = (ChainInterface) cb.buildImplementation(
182                 ChainInterface.class,
183                 commands,
184                 "<Chain>");
185
186         replayControls();
187
188         assertEquals(true, chain.execute("barney"));
189
190         verifyControls();
191     }
192
193     public void testChainFactoryIntegration() throws Exception JavaDoc
194     {
195         Registry r = buildFrameworkRegistry("ChainFactoryIntegration.xml");
196
197         ChainInterface chain = (ChainInterface) r.getService(ChainInterface.class);
198
199         assertEquals(true, chain.execute("whatever"));
200     }
201
202 }
Popular Tags