KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > hivemind > lib > pipeline > TestPipelineAssembler


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.pipeline;
16
17 import java.util.ArrayList JavaDoc;
18 import java.util.List JavaDoc;
19
20 import org.apache.hivemind.ErrorLog;
21 import org.apache.hivemind.Registry;
22 import org.apache.hivemind.ServiceImplementationFactoryParameters;
23 import org.apache.hivemind.lib.impl.DefaultImplementationBuilderImpl;
24 import org.apache.hivemind.service.ClassFactory;
25 import org.apache.hivemind.service.impl.ClassFactoryImpl;
26 import org.apache.hivemind.xml.XmlTestCase;
27 import org.easymock.MockControl;
28
29 /**
30  * Tests for the {@link org.apache.hivemind.lib.pipeline.PipelineAssembler} and
31  * {@link org.apache.hivemind.lib.pipeline.PipelineFactory} classes.
32  *
33  * @author Howard Lewis Ship
34  */

35 public class TestPipelineAssembler extends XmlTestCase
36 {
37     private static class StandardInner implements StandardService
38     {
39         private String JavaDoc _desciption;
40
41         private StandardInner(String JavaDoc description)
42         {
43             _desciption = description;
44         }
45
46         public String JavaDoc toString()
47         {
48             return _desciption;
49         }
50
51         public int run(int i)
52         {
53             return i;
54         }
55     }
56
57     public void testTerminatorConflict()
58     {
59         ErrorLog log = newErrorLog();
60
61         log.error(
62                 "Terminator ss2 for pipeline service foo.bar conflicts with "
63                         + "previous terminator (ss1, at unknown location) and has been ignored.",
64                 null,
65                 null);
66
67         replayControls();
68
69         PipelineAssembler pa = new PipelineAssembler(log, "foo.bar", StandardService.class,
70                 StandardFilter.class, null, null);
71
72         StandardService ss1 = new StandardInner("ss1");
73         StandardService ss2 = new StandardInner("ss2");
74
75         pa.setTerminator(ss1, null);
76         pa.setTerminator(ss2, null);
77
78         assertSame(ss1, pa.getTerminator());
79
80         verifyControls();
81     }
82
83     public void testIncorrectTerminatorType()
84     {
85         ErrorLog log = newErrorLog();
86
87         log.error("-String- is not an instance of interface "
88                 + "org.apache.hivemind.lib.pipeline.StandardService suitable for "
89                 + "use as part of the pipeline for service foo.bar.", null, null);
90
91         replayControls();
92
93         PipelineAssembler pa = new PipelineAssembler(log, "foo.bar", StandardService.class,
94                 StandardFilter.class, null, null);
95
96         pa.setTerminator("-String-", null);
97
98         assertNull(pa.getTerminator());
99
100         verifyControls();
101     }
102
103     public void testIncorrectFilterType()
104     {
105         ErrorLog log = newErrorLog();
106
107         log.error("-String- is not an instance of interface "
108                 + "org.apache.hivemind.lib.pipeline.StandardFilter suitable for "
109                 + "use as part of the pipeline for service foo.bar.", null, null);
110
111         replayControls();
112
113         PipelineAssembler pa = new PipelineAssembler(log, "foo.bar", StandardService.class,
114                 StandardFilter.class, null, null);
115
116         pa.addFilter("filter-name", null, null, "-String-", null);
117
118         verifyControls();
119     }
120
121     public void testPassThruToPlaceholder()
122     {
123         ClassFactory cf = new ClassFactoryImpl();
124         DefaultImplementationBuilderImpl dib = new DefaultImplementationBuilderImpl();
125
126         dib.setClassFactory(cf);
127
128         ErrorLog log = newErrorLog();
129
130         replayControls();
131
132         PipelineAssembler pa = new PipelineAssembler(log, "foo.bar", StandardService.class,
133                 StandardFilter.class, new ClassFactoryImpl(), dib);
134
135         StandardService pipeline = (StandardService) pa.createPipeline();
136
137         assertEquals(0, pipeline.run(99));
138
139         verifyControls();
140     }
141
142     public void testFilterChain()
143     {
144         ClassFactory cf = new ClassFactoryImpl();
145         DefaultImplementationBuilderImpl dib = new DefaultImplementationBuilderImpl();
146
147         dib.setClassFactory(cf);
148
149         ErrorLog log = newErrorLog();
150
151         PipelineAssembler pa = new PipelineAssembler(log, "foo.bar", StandardService.class,
152                 StandardFilter.class, new ClassFactoryImpl(), dib);
153
154         replayControls();
155
156         pa.setTerminator(new StandardInner("ss"), null);
157
158         StandardFilter adder = new StandardFilter()
159         {
160             public int run(int i, StandardService service)
161             {
162                 return service.run(i + 3);
163             }
164         };
165
166         StandardFilter multiplier = new StandardFilter()
167         {
168             public int run(int i, StandardService service)
169             {
170                 return 2 * service.run(i);
171             }
172         };
173
174         StandardFilter subtracter = new StandardFilter()
175         {
176             public int run(int i, StandardService service)
177             {
178                 return service.run(i) - 2;
179             }
180         };
181
182         pa.addFilter("subtracter", null, "adder", subtracter, null);
183         pa.addFilter("adder", "multiplier", null, adder, null);
184         pa.addFilter("multiplier", null, null, multiplier, null);
185
186         StandardService pipeline = (StandardService) pa.createPipeline();
187
188         // Should be order subtracter, multipler, adder
189
assertEquals(14, pipeline.run(5));
190         assertEquals(24, pipeline.run(10));
191
192         verifyControls();
193     }
194
195     public void testPipelineFactoryWithTerminator()
196     {
197         MockControl fpc = newControl(ServiceImplementationFactoryParameters.class);
198         ServiceImplementationFactoryParameters fp = (ServiceImplementationFactoryParameters) fpc
199                 .getMock();
200
201         ClassFactory cf = new ClassFactoryImpl();
202         DefaultImplementationBuilderImpl dib = new DefaultImplementationBuilderImpl();
203
204         dib.setClassFactory(cf);
205
206         PipelineFactory factory = new PipelineFactory();
207         factory.setClassFactory(cf);
208         factory.setDefaultImplementationBuilder(dib);
209         factory.setErrorLog(newErrorLog());
210
211         PipelineParameters pp = new PipelineParameters();
212         pp.setFilterInterface(StandardFilter.class);
213         pp.setTerminator(new StandardInner("terminator"));
214
215         List JavaDoc l = new ArrayList JavaDoc();
216
217         FilterContribution fc = new FilterContribution();
218         fc.setFilter(new StandardFilterImpl());
219         fc.setName("multiplier-filter");
220
221         l.add(fc);
222
223         pp.setPipelineConfiguration(l);
224
225         fp.getFirstParameter();
226         fpc.setReturnValue(pp);
227
228         fp.getServiceId();
229         fpc.setReturnValue("example");
230
231         fp.getServiceInterface();
232         fpc.setReturnValue(StandardService.class);
233
234         replayControls();
235
236         StandardService s = (StandardService) factory.createCoreServiceImplementation(fp);
237
238         assertEquals(24, s.run(12));
239         assertEquals(18, s.run(9));
240
241         verifyControls();
242     }
243
244     public void testPipelineFactoryNoTerminator()
245     {
246         MockControl fpc = newControl(ServiceImplementationFactoryParameters.class);
247         ServiceImplementationFactoryParameters fp = (ServiceImplementationFactoryParameters) fpc
248                 .getMock();
249
250         ClassFactory cf = new ClassFactoryImpl();
251         DefaultImplementationBuilderImpl dib = new DefaultImplementationBuilderImpl();
252
253         dib.setClassFactory(cf);
254
255         PipelineFactory factory = new PipelineFactory();
256         factory.setClassFactory(cf);
257         factory.setDefaultImplementationBuilder(dib);
258         factory.setErrorLog(newErrorLog());
259
260         PipelineParameters pp = new PipelineParameters();
261         pp.setFilterInterface(StandardFilter.class);
262
263         List JavaDoc l = new ArrayList JavaDoc();
264
265         FilterContribution fc = new FilterContribution();
266         fc.setFilter(new StandardFilterImpl());
267         fc.setName("multiplier-filter");
268
269         l.add(fc);
270
271         TerminatorContribution tc = new TerminatorContribution();
272         tc.setTerminator(new StandardServiceImpl());
273
274         l.add(tc);
275
276         pp.setPipelineConfiguration(l);
277
278         fp.getFirstParameter();
279         fpc.setReturnValue(pp);
280
281         fp.getServiceId();
282         fpc.setReturnValue("example");
283
284         fp.getServiceInterface();
285         fpc.setReturnValue(StandardService.class);
286
287         replayControls();
288
289         StandardService s = (StandardService) factory.createCoreServiceImplementation(fp);
290
291         assertEquals(24, s.run(12));
292         assertEquals(18, s.run(9));
293
294         verifyControls();
295     }
296
297     private ErrorLog newErrorLog()
298     {
299         return (ErrorLog) newMock(ErrorLog.class);
300     }
301
302     /**
303      * Try it integrated now!
304      */

305     public void testFactoryWithServices() throws Exception JavaDoc
306     {
307         Registry r = buildFrameworkRegistry("Pipeline.xml");
308
309         StandardService s = (StandardService) r.getService(
310                 "hivemind.lib.test.Pipeline",
311                 StandardService.class);
312
313         assertEquals(24, s.run(12));
314         assertEquals(18, s.run(9));
315     }
316
317     public void testFactoryWithObjects() throws Exception JavaDoc
318     {
319         Registry r = buildFrameworkRegistry("Pipeline.xml");
320
321         StandardService s = (StandardService) r.getService(
322                 "hivemind.lib.test.ObjectPipeline",
323                 StandardService.class);
324
325         assertEquals(24, s.run(12));
326         assertEquals(18, s.run(9));
327     }
328 }
Popular Tags