KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.Serializable JavaDoc;
18
19 import org.apache.hivemind.ApplicationRuntimeException;
20 import org.apache.hivemind.ErrorLog;
21 import org.apache.hivemind.service.ClassFactory;
22 import org.apache.hivemind.service.impl.ClassFactoryImpl;
23 import org.apache.hivemind.test.HiveMindTestCase;
24
25 /**
26  * Tests the {@link org.apache.hivemind.lib.pipeline.BridgeBuilder} class.
27  *
28  * @author Howard Lewis Ship
29  */

30
31 public class TestBridgeBuilder extends HiveMindTestCase
32 {
33     private ClassFactory _classFactory = new ClassFactoryImpl();
34
35     public void testStandard()
36     {
37         replayControls();
38
39         BridgeBuilder bb = new BridgeBuilder(null, "foo.bar", StandardService.class,
40                 StandardFilter.class, _classFactory);
41
42         StandardFilter sf = new StandardFilter()
43         {
44             public int run(int i, StandardService ss)
45             {
46                 return ss.run(i + 1);
47             }
48         };
49
50         StandardService ss = new StandardService()
51         {
52             public int run(int i)
53             {
54                 return i * 3;
55             }
56         };
57
58         StandardService bridge = (StandardService) bb.instantiateBridge(ss, sf);
59
60         // The filter adds 1, then the service multiplies by 3.
61
// (5 +_1) * 3 = 18.
62

63         assertEquals(18, bridge.run(5));
64
65         // Since toString() is not part of the service interface,
66
// it will be implemented in the bridge.
67

68         assertEquals(
69                 "<PipelineBridge for service foo.bar(org.apache.hivemind.lib.pipeline.StandardService)>",
70                 bridge.toString());
71
72         verifyControls();
73     }
74
75     public void testToString()
76     {
77         replayControls();
78
79         BridgeBuilder bb = new BridgeBuilder(null, "foo.bar", ToStringService.class,
80                 ToStringFilter.class, _classFactory);
81
82         ToStringFilter f = new ToStringFilter()
83         {
84             public String JavaDoc toString(ToStringService s)
85             {
86                 return s.toString().toUpperCase();
87             }
88         };
89
90         ToStringService s = new ToStringService()
91         {
92             public String JavaDoc toString()
93             {
94                 return "Service";
95             }
96         };
97
98         ToStringService bridge = (ToStringService) bb.instantiateBridge(s, f);
99
100         assertEquals("SERVICE", bridge.toString());
101
102         verifyControls();
103     }
104
105     public void testExtraServiceMethod()
106     {
107         ErrorLog log = (ErrorLog) newMock(ErrorLog.class);
108
109         log
110                 .error(
111                         "Service interface method void extraServiceMethod() has no match in filter interface java.io.Serializable.",
112                         null,
113                         null);
114
115         replayControls();
116
117         BridgeBuilder bb = new BridgeBuilder(log, "foo.bar", ExtraServiceMethod.class,
118                 Serializable JavaDoc.class, _classFactory);
119
120         ExtraServiceMethod esm = (ExtraServiceMethod) bb.instantiateBridge(null, null);
121
122         try
123         {
124             esm.extraServiceMethod();
125             unreachable();
126         }
127         catch (ApplicationRuntimeException ex)
128         {
129             assertEquals(
130                     "Service interface method void extraServiceMethod() has no match in filter interface java.io.Serializable.",
131                     ex.getMessage());
132         }
133
134         verifyControls();
135     }
136
137     public void testExtraFilterMethod()
138     {
139         ErrorLog log = (ErrorLog) newMock(ErrorLog.class);
140
141         log
142                 .error(
143                         "Method void extraFilterMethod() of filter interface "
144                                 + "org.apache.hivemind.lib.pipeline.ExtraFilterMethod does not have a matching service "
145                                 + "interface method (in interface java.io.Serializable, service foo.bar).",
146                         null,
147                         null);
148
149         replayControls();
150
151         BridgeBuilder bb = new BridgeBuilder(log, "foo.bar", Serializable JavaDoc.class,
152                 ExtraFilterMethod.class, _classFactory);
153
154         Object JavaDoc bridge = bb.instantiateBridge(null, null);
155
156         assertEquals(true, bridge instanceof Serializable JavaDoc);
157
158         verifyControls();
159     }
160
161     public void testServiceInTheMiddle()
162     {
163         replayControls();
164
165         BridgeBuilder bb = new BridgeBuilder(null, "foo.bar", MiddleService.class,
166                 MiddleFilter.class, _classFactory);
167
168         MiddleFilter mf = new MiddleFilter()
169         {
170             public void execute(int count, char ch, MiddleService service, StringBuffer JavaDoc buffer)
171             {
172                 service.execute(count, ch, buffer);
173
174                 buffer.append(' ');
175
176                 service.execute(count + 1, Character.toUpperCase(ch), buffer);
177
178             }
179         };
180
181         MiddleService ms = new MiddleService()
182         {
183             public void execute(int count, char ch, StringBuffer JavaDoc buffer)
184             {
185                 for (int i = 0; i < count; i++)
186                     buffer.append(ch);
187             }
188         };
189
190         // This also tests building the bridge methods with a void return type.
191

192         MiddleService bridge = (MiddleService) bb.instantiateBridge(ms, mf);
193
194         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc("CODE: ");
195
196         bridge.execute(3, 'a', buffer);
197
198         assertEquals("CODE: aaa AAAA", buffer.toString());
199
200         verifyControls();
201     }
202 }
Popular Tags