KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > hivemind > test > TestServicesByInterface


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 hivemind.test;
16
17 import org.apache.hivemind.ApplicationRuntimeException;
18 import org.apache.hivemind.Registry;
19 import org.apache.hivemind.definition.ImplementationDefinition;
20 import org.apache.hivemind.definition.impl.ModuleDefinitionImpl;
21 import org.apache.hivemind.definition.impl.ServicePointDefinitionImpl;
22
23 /**
24  * Tests the Registry.getService(java.lang.Class) functionality.
25  *
26  * @author Marcus Brito
27  */

28 public class TestServicesByInterface extends FrameworkTestCase
29 {
30     private Registry registry;
31
32     protected void setUp() throws Exception JavaDoc
33     {
34         super.setUp();
35         
36         ModuleDefinitionImpl module = createModuleDefinition("hivemind.tests.serviceByInterface");
37
38         ServicePointDefinitionImpl sp1 = createServicePointDefinition(module, "uniqueService", IUniqueService.class);
39         ImplementationDefinition impl1 = createServiceImplementationDefinition(module, UniqueServiceImpl.class);
40         sp1.addImplementation(impl1);
41
42         module.addServicePoint(sp1);
43
44         ServicePointDefinitionImpl sp2 = createServicePointDefinition(module, "multipleServiceOne", IMultipleService.class);
45         ImplementationDefinition impl2 = createServiceImplementationDefinition(module, MultipleServiceImpl.class);
46         sp2.addImplementation(impl2);
47
48         module.addServicePoint(sp2);
49
50         ServicePointDefinitionImpl sp3 = createServicePointDefinition(module, "multipleServiceTwo", IMultipleService.class);
51         sp3.addImplementation(impl2);
52         
53         module.addServicePoint(sp3);
54
55         registry = buildFrameworkRegistry(module);
56     }
57
58     protected void tearDown() throws Exception JavaDoc
59     {
60         super.tearDown();
61         
62         registry.shutdown();
63     }
64
65     public void testUniqueGetServiceByInterface()
66     {
67         IUniqueService service = (IUniqueService) registry.getService(IUniqueService.class);
68
69         assertNotNull(service);
70     }
71
72     public void testNonExistentGetServiceByInterface()
73     {
74         try
75         {
76             registry.getService(INonExistentService.class);
77             unreachable();
78         }
79         catch (ApplicationRuntimeException ex)
80         {
81             assertExceptionSubstring(
82                 ex,
83                 "There is no service point for interface hivemind.test.INonExistentService.");
84         }
85     }
86
87     public void testMultipleExistentGetServiceByInterface()
88     {
89         try
90         {
91             registry.getService(IMultipleService.class);
92             unreachable();
93         }
94         catch (ApplicationRuntimeException ex)
95         {
96             assertExceptionSubstring(
97                 ex,
98                 "There are multiple service points for interface hivemind.test.IMultipleService: "
99                     + "{hivemind.tests.serviceByInterface.multipleServiceTwo,"
100                     + " hivemind.tests.serviceByInterface.multipleServiceOne}.");
101         }
102     }
103 }
104
Popular Tags