KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > hivemind > impl > TestVisibility


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.impl;
16
17 import java.util.List JavaDoc;
18
19 import hivemind.test.FrameworkTestCase;
20
21 import org.apache.hivemind.ApplicationRuntimeException;
22 import org.apache.hivemind.definition.Visibility;
23 import org.apache.hivemind.definition.impl.ConfigurationPointDefinitionImpl;
24 import org.apache.hivemind.internal.ConfigurationPoint;
25 import org.apache.hivemind.internal.Module;
26 import org.apache.hivemind.internal.ServicePoint;
27 import org.apache.hivemind.service.ClassFactory;
28 import org.easymock.MockControl;
29
30 /**
31  * Tests the logic related to service point and configuration point visibility.
32  *
33  * @since 1.1
34  */

35 public class TestVisibility extends FrameworkTestCase
36 {
37     public void testPublicConfigurationVisibleToOtherModule()
38     {
39         Module m = (Module) newMock(Module.class);
40
41         Module om = (Module) newMock(Module.class);
42
43         replayControls();
44
45         ConfigurationPointDefinitionImpl cpd = new ConfigurationPointDefinitionImpl(createModuleDefinition("module"));
46         cpd.setVisibility(Visibility.PUBLIC);
47         ConfigurationPointImpl cp = new ConfigurationPointImpl(m, cpd);
48
49         assertEquals(true, cp.visibleToModule(om));
50
51         verifyControls();
52     }
53
54     public void testPublicConfigurationVisibleToApplication()
55     {
56         Module m = (Module) newMock(Module.class);
57
58         replayControls();
59
60         ConfigurationPointDefinitionImpl cpd = new ConfigurationPointDefinitionImpl(createModuleDefinition("module"));
61         cpd.setVisibility(Visibility.PUBLIC);
62         ConfigurationPointImpl cp = new ConfigurationPointImpl(m, cpd);
63
64         assertEquals(true, cp.visibleToModule(null));
65
66         verifyControls();
67     }
68
69     public void testPrivateConfigurationInvisibleToOtherModule()
70     {
71         Module m = (Module) newMock(Module.class);
72         Module om = (Module) newMock(Module.class);
73
74         replayControls();
75
76         ConfigurationPointDefinitionImpl cpd = new ConfigurationPointDefinitionImpl(createModuleDefinition("module"));
77         cpd.setVisibility(Visibility.PRIVATE);
78         ConfigurationPointImpl cp = new ConfigurationPointImpl(m, cpd);
79
80         assertEquals(false, cp.visibleToModule(om));
81
82         verifyControls();
83     }
84
85     public void testPrivateConfigurationInvisibleToApplication()
86     {
87         Module m = (Module) newMock(Module.class);
88
89         replayControls();
90
91         ConfigurationPointDefinitionImpl cpd = new ConfigurationPointDefinitionImpl(createModuleDefinition("module"));
92         cpd.setVisibility(Visibility.PRIVATE);
93         ConfigurationPointImpl cp = new ConfigurationPointImpl(m, cpd);
94
95         assertEquals(false, cp.visibleToModule(null));
96
97         verifyControls();
98     }
99
100     public void testGetServiceNotVisibleToApplication()
101     {
102         RegistryInfrastructureImpl rf = new RegistryInfrastructureImpl(null, null);
103
104         MockControl spc = newControl(ServicePoint.class);
105         ServicePoint sp = (ServicePoint) spc.getMock();
106
107         // Training
108

109         sp.getExtensionPointId();
110         spc.setReturnValue("foo.bar.Baz");
111
112         sp.getServiceInterfaceClassName();
113         spc.setReturnValue(Runnable JavaDoc.class.getName());
114
115         sp.visibleToModule(null);
116         spc.setReturnValue(false);
117
118         replayControls();
119
120         rf.addServicePoint(sp);
121
122         try
123         {
124             rf.getService("foo.bar.Baz", Runnable JavaDoc.class, null);
125
126             unreachable();
127         }
128         catch (ApplicationRuntimeException ex)
129         {
130             assertEquals(ImplMessages.serviceNotVisible("foo.bar.Baz", null), ex.getMessage());
131         }
132
133         verifyControls();
134     }
135
136     public void testGetServiceNotVisibleToModule()
137     {
138         RegistryInfrastructureImpl rf = new RegistryInfrastructureImpl(null, null);
139
140         MockControl spc = newControl(ServicePoint.class);
141         ServicePoint sp = (ServicePoint) spc.getMock();
142
143         ModuleImpl m = new ModuleImpl();
144         m.setModuleId("zip.zap.Zoom");
145
146         // Training
147

148         sp.getExtensionPointId();
149         spc.setReturnValue("foo.bar.Baz");
150
151         sp.getServiceInterfaceClassName();
152         spc.setReturnValue(Runnable JavaDoc.class.getName());
153
154         sp.visibleToModule(m);
155         spc.setReturnValue(false);
156
157         replayControls();
158
159         rf.addServicePoint(sp);
160
161         try
162         {
163             rf.getService("foo.bar.Baz", Runnable JavaDoc.class, m);
164
165             unreachable();
166         }
167         catch (ApplicationRuntimeException ex)
168         {
169             assertEquals(ImplMessages.serviceNotVisible("foo.bar.Baz", m), ex.getMessage());
170         }
171
172         verifyControls();
173     }
174
175     public void testGetConfigurationNotVisibleToModule()
176     {
177         RegistryInfrastructureImpl rf = new RegistryInfrastructureImpl(null, null);
178
179         MockControl control = newControl(ConfigurationPoint.class);
180         ConfigurationPoint point = (ConfigurationPoint) control.getMock();
181
182         ModuleImpl m = new ModuleImpl();
183         m.setModuleId("zip.zap.Zoom");
184
185         // Training
186

187         point.getExtensionPointId();
188         control.setReturnValue("foo.bar.Baz");
189
190         point.getConfigurationType();
191         control.setReturnValue(List JavaDoc.class);
192         
193         point.visibleToModule(m);
194         control.setReturnValue(false);
195
196         replayControls();
197
198         rf.addConfigurationPoint(point);
199
200         try
201         {
202             rf.getConfiguration("foo.bar.Baz", m);
203
204             unreachable();
205         }
206         catch (ApplicationRuntimeException ex)
207         {
208             assertEquals(ImplMessages.configurationNotVisible("foo.bar.Baz", m), ex.getMessage());
209         }
210
211         verifyControls();
212     }
213
214     /**
215      * Ensure that, when searching for services (or service points) by service interface, the
216      * non-visible service points are filtered out, before any complaint of too few or too many.
217      *
218      * @since 1.1
219      */

220     public void testGetServiceMatchesPublicOnly()
221     {
222         MockControl spc1 = newControl(ServicePoint.class);
223         ServicePoint sp1 = (ServicePoint) spc1.getMock();
224
225         MockControl spc2 = newControl(ServicePoint.class);
226         ServicePoint sp2 = (ServicePoint) spc2.getMock();
227
228         ClassFactory service = (ClassFactory) newMock(ClassFactory.class);
229
230         // Training
231

232         sp1.getExtensionPointId();
233         spc1.setReturnValue("foo.Private");
234
235         sp1.getServiceInterfaceClassName();
236         spc1.setReturnValue(ClassFactory.class.getName());
237
238         sp2.getExtensionPointId();
239         spc2.setReturnValue("foo.Public");
240
241         sp2.getServiceInterfaceClassName();
242         spc2.setReturnValue(ClassFactory.class.getName());
243
244         sp1.visibleToModule(null);
245         spc1.setReturnValue(false);
246
247         sp2.visibleToModule(null);
248         spc2.setReturnValue(true);
249
250         sp2.getService(ClassFactory.class);
251         spc2.setReturnValue(service);
252
253         replayControls();
254
255         RegistryInfrastructureImpl r = new RegistryInfrastructureImpl(null, null);
256
257         r.addServicePoint(sp1);
258         r.addServicePoint(sp2);
259
260         Object JavaDoc actual = r.getService(ClassFactory.class, null);
261
262         assertSame(service, actual);
263
264         verifyControls();
265     }
266
267     public void testContributionToNonVisibleConfigurationPoint()
268     {
269         // TODO annotations: Redo when error handling of registry api is stable
270

271 // MockControl ehc = newControl(ErrorHandler.class);
272
// ErrorHandler errorHandler = (ErrorHandler) ehc.getMock();
273
//
274
// RegistryInfrastructureImpl rf = new RegistryInfrastructureImpl(null, null);
275
//
276
// MockControl control = newControl(ConfigurationPoint.class);
277
// ConfigurationPoint point = (ConfigurationPoint) control.getMock();
278
//
279
// ModuleImpl m = new ModuleImpl();
280
// m.setModuleId("zip.zap.Zoom");
281
//
282
// // Training
283
//
284
// point.getExtensionPointId();
285
// control.setReturnValue("foo.bar.Baz");
286
//
287
// point.visibleToModule(m);
288
// control.setReturnValue(false);
289
//
290
// replayControls();
291
//
292
// rf.addConfigurationPoint(point);
293
//
294
// try
295
// {
296
// rf.getConfiguration("foo.bar.Baz", m);
297
//
298
// unreachable();
299
// }
300
// catch (ApplicationRuntimeException ex)
301
// {
302
// assertEquals(ImplMessages.configurationNotVisible("foo.bar.Baz", m), ex.getMessage());
303
// }
304
//
305
// verifyControls();
306
//
307
// // Training
308
//
309
// errorHandler
310
// .error(
311
// LOG,
312
// "Service point hivemind.test.privates.PrivateService is not visible to module hivemind.test.contribprivates.",
313
// id.getInstanceBuilder().getLocation(),
314
// null);
315
//
316
// errorHandler
317
// .error(
318
// LOG,
319
// "Service point hivemind.test.privates.PrivateService is not visible to module hivemind.test.contribprivates.",
320
// itd.getLocation(),
321
// null);
322
//
323
// errorHandler
324
// .error(
325
// LOG,
326
// "Configuration point hivemind.test.privates.PrivateConfig is not visible to module hivemind.test.contribprivates.",
327
// cd.getLocation(),
328
// null);
329
//
330
// errorHandler
331
// .error(
332
// LOG,
333
// "No module has contributed a service constructor for service point hivemind.test.privates.PrivateService.",
334
// null,
335
// null);
336
//
337
// replayControls();
338
//
339
// cons.constructRegistryInfrastructure(Locale.getDefault());
340
//
341
// verifyControls();
342
}
343 }
Popular Tags