KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > hivemind > lib > strategy > TestStrategyFactory


1 // Copyright 2004, 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.strategy;
16
17 import java.lang.reflect.Modifier JavaDoc;
18 import java.util.Collections JavaDoc;
19 import java.util.List JavaDoc;
20
21 import org.apache.hivemind.ErrorLog;
22 import org.apache.hivemind.Location;
23 import org.apache.hivemind.Registry;
24 import org.apache.hivemind.ServiceImplementationFactoryParameters;
25 import org.apache.hivemind.lib.util.StrategyRegistry;
26 import org.apache.hivemind.service.ClassFab;
27 import org.apache.hivemind.service.ClassFabUtils;
28 import org.apache.hivemind.service.ClassFactory;
29 import org.apache.hivemind.service.MethodFab;
30 import org.apache.hivemind.service.MethodSignature;
31 import org.apache.hivemind.test.AggregateArgumentsMatcher;
32 import org.apache.hivemind.test.ArgumentMatcher;
33 import org.apache.hivemind.test.ArrayMatcher;
34 import org.apache.hivemind.test.TypeMatcher;
35 import org.apache.hivemind.xml.XmlTestCase;
36 import org.easymock.MockControl;
37
38 /**
39  * Test for the {@link org.apache.hivemind.lib.strategy.StrategyFactory} service
40  * implementation factory.
41  *
42  * @author Howard M. Lewis Ship
43  * @since 1.1
44  */

45 public class TestStrategyFactory extends XmlTestCase
46 {
47     private List JavaDoc buildContributions(Class JavaDoc registerClass, Object JavaDoc adapter, Location location)
48     {
49         StrategyContribution c = new StrategyContribution();
50
51         c.setRegisterClass(registerClass);
52         c.setStrategy(adapter);
53         c.setLocation(location);
54
55         return Collections.singletonList(c);
56     }
57
58     private StrategyParameter buildParameter(Class JavaDoc registerClass, Object JavaDoc adapter,
59             Location contributionLocation, Location parameterLocation)
60     {
61         StrategyParameter result = new StrategyParameter();
62
63         result.setContributions(buildContributions(registerClass, adapter, contributionLocation));
64         result.setLocation(parameterLocation);
65
66         return result;
67     }
68
69     private StrategyParameter buildParameter(Class JavaDoc registerClass, Object JavaDoc adapter)
70     {
71         return buildParameter(registerClass, adapter, null, null);
72     }
73
74     public void testBuildRegistry()
75     {
76         StrategyRegistry ar = (StrategyRegistry) newMock(StrategyRegistry.class);
77         ToStringStrategy adapter = (ToStringStrategy) newMock(ToStringStrategy.class);
78
79         MockControl fpc = newControl(ServiceImplementationFactoryParameters.class);
80         ServiceImplementationFactoryParameters fp = (ServiceImplementationFactoryParameters) fpc
81                 .getMock();
82
83         fp.getServiceInterface();
84         fpc.setReturnValue(ToStringStrategy.class);
85
86         StrategyParameter p = buildParameter(Number JavaDoc.class, adapter);
87
88         fp.getFirstParameter();
89         fpc.setReturnValue(p);
90
91         ar.register(Number JavaDoc.class, adapter);
92
93         replayControls();
94
95         new StrategyFactory().buildRegistry(fp, ar);
96
97         verifyControls();
98     }
99
100     public void testBuildRegistryWrongAdapterType()
101     {
102         Location l = newLocation();
103
104         StrategyRegistry ar = (StrategyRegistry) newMock(StrategyRegistry.class);
105         ToStringStrategy adapter = (ToStringStrategy) newMock(ToStringStrategy.class);
106
107         MockControl fpc = newControl(ServiceImplementationFactoryParameters.class);
108         ServiceImplementationFactoryParameters fp = (ServiceImplementationFactoryParameters) fpc
109                 .getMock();
110
111         MockControl logc = newControl(ErrorLog.class);
112         ErrorLog log = (ErrorLog) logc.getMock();
113
114         fp.getServiceInterface();
115         fpc.setReturnValue(Runnable JavaDoc.class);
116
117         StrategyParameter p = buildParameter(Number JavaDoc.class, adapter, l, null);
118
119         fp.getFirstParameter();
120         fpc.setReturnValue(p);
121
122         fp.getErrorLog();
123         fpc.setReturnValue(log);
124
125         log.error(
126                 StrategyMessages.strategyWrongInterface(adapter, Number JavaDoc.class, Runnable JavaDoc.class),
127                 l,
128                 new ClassCastException JavaDoc());
129         logc.setMatcher(new AggregateArgumentsMatcher(new ArgumentMatcher[]
130         { null, null, new TypeMatcher() }));
131
132         replayControls();
133
134         new StrategyFactory().buildRegistry(fp, ar);
135
136         verifyControls();
137     }
138
139     public void testBuildImplementationClass()
140     {
141         MockControl factoryControl = newControl(ClassFactory.class);
142         ClassFactory factory = (ClassFactory) factoryControl.getMock();
143
144         MockControl cfc = newControl(ClassFab.class);
145         ClassFab cf = (ClassFab) cfc.getMock();
146
147         MethodFab mf = (MethodFab) newMock(MethodFab.class);
148
149         MockControl fpc = newControl(ServiceImplementationFactoryParameters.class);
150         ServiceImplementationFactoryParameters fp = (ServiceImplementationFactoryParameters) fpc
151                 .getMock();
152
153         fp.getServiceInterface();
154         fpc.setReturnValue(ToStringStrategy.class);
155
156         factory.newClass("NewClass", Object JavaDoc.class);
157         factoryControl.setReturnValue(cf);
158
159         cf.addInterface(ToStringStrategy.class);
160         cf.addField("_registry", StrategyRegistry.class);
161
162         cf.addConstructor(new Class JavaDoc[]
163         { StrategyRegistry.class }, null, "_registry = $1;");
164         cfc.setMatcher(new AggregateArgumentsMatcher(new ArrayMatcher()));
165
166         cf
167                 .addMethod(
168                         Modifier.PRIVATE,
169                         new MethodSignature(ToStringStrategy.class, "_getStrategy", new Class JavaDoc[]
170                         { Object JavaDoc.class }, null),
171                         "return (org.apache.hivemind.lib.strategy.ToStringStrategy) _registry.getStrategy($1.getClass());");
172         cfc.setReturnValue(mf);
173
174         cf.addMethod(Modifier.PUBLIC, new MethodSignature(String JavaDoc.class, "toString", new Class JavaDoc[]
175         { Object JavaDoc.class }, null), "return ($r) _getStrategy($1).toString($$);");
176         cfc.setReturnValue(mf);
177
178         fp.getServiceId();
179         fpc.setReturnValue("foo.Bar");
180
181         ClassFabUtils.addToStringMethod(cf, StrategyMessages.toString(
182                 "foo.Bar",
183                 ToStringStrategy.class));
184         cfc.setReturnValue(mf);
185
186         cf.createClass();
187         cfc.setReturnValue(String JavaDoc.class);
188
189         replayControls();
190
191         StrategyFactory f = new StrategyFactory();
192         f.setClassFactory(factory);
193
194         f.buildImplementationClass(fp, "NewClass");
195
196         verifyControls();
197     }
198
199     public void testBuildImplementationClassImproperMethod()
200     {
201         Location l = newLocation();
202
203         MockControl factoryControl = newControl(ClassFactory.class);
204         ClassFactory factory = (ClassFactory) factoryControl.getMock();
205
206         MockControl cfc = newControl(ClassFab.class);
207         ClassFab cf = (ClassFab) cfc.getMock();
208
209         MethodFab mf = (MethodFab) newMock(MethodFab.class);
210
211         MockControl fpc = newControl(ServiceImplementationFactoryParameters.class);
212         ServiceImplementationFactoryParameters fp = (ServiceImplementationFactoryParameters) fpc
213                 .getMock();
214
215         ErrorLog log = (ErrorLog) newMock(ErrorLog.class);
216
217         fp.getServiceInterface();
218         fpc.setReturnValue(Runnable JavaDoc.class);
219
220         factory.newClass("NewClass", Object JavaDoc.class);
221         factoryControl.setReturnValue(cf);
222
223         cf.addInterface(Runnable JavaDoc.class);
224         cf.addField("_registry", StrategyRegistry.class);
225
226         cf.addConstructor(new Class JavaDoc[]
227         { StrategyRegistry.class }, null, "_registry = $1;");
228         cfc.setMatcher(new AggregateArgumentsMatcher(new ArrayMatcher()));
229
230         cf.addMethod(
231                 Modifier.PRIVATE,
232                 new MethodSignature(Runnable JavaDoc.class, "_getStrategy", new Class JavaDoc[]
233                 { Object JavaDoc.class }, null),
234                 "return (java.lang.Runnable) _registry.getStrategy($1.getClass());");
235         cfc.setReturnValue(mf);
236
237         MethodSignature sig = new MethodSignature(void.class, "run", null, null);
238
239         cf.addMethod(Modifier.PUBLIC, sig, "{ }");
240         cfc.setReturnValue(mf);
241
242         fp.getErrorLog();
243         fpc.setReturnValue(log);
244
245         fp.getFirstParameter();
246         // Slight fudge: we return the location itself when we should return
247
// an object with this location.
248
fpc.setReturnValue(l);
249
250         log.error(StrategyMessages.improperServiceMethod(sig), l, null);
251
252         fp.getServiceId();
253         fpc.setReturnValue("foo.Bar");
254
255         ClassFabUtils.addToStringMethod(cf, StrategyMessages.toString("foo.Bar", Runnable JavaDoc.class));
256         cfc.setReturnValue(mf);
257
258         cf.createClass();
259
260         cfc.setReturnValue(String JavaDoc.class);
261
262         replayControls();
263
264         StrategyFactory f = new StrategyFactory();
265         f.setClassFactory(factory);
266
267         f.buildImplementationClass(fp, "NewClass");
268
269         verifyControls();
270     }
271
272     public void testIntegration() throws Exception JavaDoc
273     {
274         Registry r = buildFrameworkRegistry("AdapterFactoryIntegration.xml");
275
276         ToStringStrategy ts = (ToStringStrategy) r.getService(ToStringStrategy.class);
277
278         assertEquals("5150", ts.toString(new Integer JavaDoc(5150)));
279     }
280 }
Popular Tags