KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > hivemind > test > services > TestShutdown


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.services;
16
17 import hivemind.test.FrameworkTestCase;
18 import hivemind.test.services.impl.RunnableImpl;
19
20 import org.apache.hivemind.ApplicationRuntimeException;
21 import org.apache.hivemind.Registry;
22 import org.apache.hivemind.definition.ImplementationConstructionContext;
23 import org.apache.hivemind.definition.ImplementationConstructor;
24 import org.apache.hivemind.definition.ImplementationDefinition;
25 import org.apache.hivemind.definition.impl.ModuleDefinitionImpl;
26 import org.apache.hivemind.definition.impl.ImplementationDefinitionImpl;
27 import org.apache.hivemind.definition.impl.ServicePointDefinitionImpl;
28 import org.apache.hivemind.events.RegistryShutdownListener;
29 import org.apache.hivemind.internal.AbstractServiceImplementationConstructor;
30 import org.apache.hivemind.internal.ServiceModel;
31
32 /**
33  * Tests shutdown on the registry and on deferred and threaded services.
34  *
35  * @author Howard Lewis Ship
36  */

37 public class TestShutdown extends FrameworkTestCase
38 {
39
40     public void testShutdownSingleton() throws Exception JavaDoc
41     {
42         Registry r = buildFrameworkRegistry(new SimpleModule());
43         SimpleService s = (SimpleService) r.getService(
44                 "hivemind.test.services.Simple",
45                 SimpleService.class);
46
47         assertEquals(11, s.add(4, 7));
48
49         r.shutdown();
50
51         try
52         {
53             s.add(9, 5);
54             unreachable();
55         }
56         catch (ApplicationRuntimeException ex)
57         {
58             assertExceptionSubstring(ex, "The HiveMind Registry has been shutdown.");
59         }
60     }
61
62     public void testRegistryShutdownUnrepeatable() throws Exception JavaDoc
63     {
64         Registry r = buildFrameworkRegistry(new SimpleModule());
65
66         r.shutdown();
67
68         try
69         {
70             r.getConfiguration("foo.bar");
71             unreachable();
72         }
73         catch (ApplicationRuntimeException ex)
74         {
75             assertExceptionSubstring(ex, "The HiveMind Registry has been shutdown.");
76         }
77
78         try
79         {
80             r.shutdown();
81             unreachable();
82         }
83         catch (ApplicationRuntimeException ex)
84         {
85             assertExceptionSubstring(ex, "The HiveMind Registry has been shutdown.");
86         }
87     }
88
89     public void testShutdownThreaded() throws Exception JavaDoc
90     {
91         Registry r = buildFrameworkRegistry(new StringHolderModule(ServiceModel.THREADED));
92
93         StringHolder h = (StringHolder) r.getService(
94                 "hivemind.test.services.StringHolder",
95                 StringHolder.class);
96
97         assertNull(h.getValue());
98
99         h.setValue("fred");
100
101         assertEquals("fred", h.getValue());
102
103         r.shutdown();
104
105         try
106         {
107             h.getValue();
108             unreachable();
109         }
110         catch (ApplicationRuntimeException ex)
111         {
112             assertExceptionSubstring(ex, "The HiveMind Registry has been shutdown.");
113         }
114     }
115
116     public void testSingletonCore() throws Exception JavaDoc
117     {
118         Registry r = createModuleWithShutdownListener(ServiceModel.SINGLETON);
119
120         Runnable JavaDoc s = (Runnable JavaDoc) r.getService("module1.Listener", Runnable JavaDoc.class);
121
122         interceptLogging("hivemind.test.services.impl.RunnableImpl");
123
124         s.run();
125
126         assertLoggedMessage("run -- singleton");
127
128         r.shutdown();
129
130         assertLoggedMessage("registryDidShutdown -- singleton");
131     }
132
133     public void testPrimitiveCore() throws Exception JavaDoc
134     {
135         Registry r = createModuleWithShutdownListener(ServiceModel.PRIMITIVE);
136
137         Runnable JavaDoc s = (Runnable JavaDoc) r.getService("module1.Listener", Runnable JavaDoc.class);
138
139         interceptLogging("hivemind.test.services.impl.RunnableImpl");
140
141         s.run();
142
143         assertLoggedMessage("run -- primitive");
144
145         r.shutdown();
146
147         assertLoggedMessage("registryDidShutdown -- primitive");
148     }
149
150     /**
151      * Builds a registry containing a single service "Listener" that implements the {@link RegistryShutdownListener}
152      * interface.
153      */

154     private Registry createModuleWithShutdownListener(final String JavaDoc serviceModel)
155     {
156         ModuleDefinitionImpl module = createModuleDefinition("module1");
157         
158         ServicePointDefinitionImpl sp1 = createServicePointDefinition(module, "Listener", Runnable JavaDoc.class);
159         
160         ImplementationConstructor constructor = new AbstractServiceImplementationConstructor(newLocation()) {
161
162             public Object JavaDoc constructCoreServiceImplementation(ImplementationConstructionContext context)
163             {
164                 RunnableImpl result = new RunnableImpl();
165                 result.setType(serviceModel);
166                 return result;
167             }};
168         
169         ImplementationDefinition impl = new ImplementationDefinitionImpl(module, newLocation(),
170                 constructor, serviceModel, true);
171         sp1.addImplementation(impl);
172         module.addServicePoint(sp1);
173         return buildFrameworkRegistry(module);
174     }
175     
176 }
Popular Tags