KickJava   Java API By Example, From Geeks To Geeks.

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


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.DiscardableStringHolderImpl;
19 import hivemind.test.services.impl.RegistryShutdownStringHolderImpl;
20
21 import org.apache.hivemind.HiveMind;
22 import org.apache.hivemind.Registry;
23 import org.apache.hivemind.definition.ServicePointDefinition;
24 import org.apache.hivemind.definition.impl.ModuleDefinitionHelper;
25 import org.apache.hivemind.definition.impl.ModuleDefinitionImpl;
26 import org.apache.hivemind.internal.ServiceModel;
27 import org.apache.hivemind.service.ThreadEventNotifier;
28
29 /**
30  * Tests for {@link org.apache.hivemind.impl.servicemodel.ThreadedServiceModel}.
31  *
32  * @author Howard Lewis Ship
33  */

34 public class TestThreadedModel extends FrameworkTestCase
35 {
36     /**
37      * Runnable that executes in another thread to ensure that the data really is seperate.
38      */

39     class TestOther implements Runnable JavaDoc
40     {
41         StringHolder _holder;
42
43         public void run()
44         {
45             _otherStartValue = _holder.getValue();
46
47             _holder.setValue("barney");
48
49             _otherEndValue = _holder.getValue();
50         }
51
52     }
53
54     private String JavaDoc _otherStartValue;
55
56     private String JavaDoc _otherEndValue;
57
58     public void testSingleThread() throws Exception JavaDoc
59     {
60         Registry r = buildFrameworkRegistry(new StringHolderModule(ServiceModel.THREADED));
61
62         StringHolder h = (StringHolder) r.getService(
63                 "hivemind.test.services.StringHolder",
64                 StringHolder.class);
65         ThreadEventNotifier n = (ThreadEventNotifier) r.getService(
66                 HiveMind.THREAD_EVENT_NOTIFIER_SERVICE,
67                 ThreadEventNotifier.class);
68
69         interceptLogging("hivemind.test.services.StringHolder");
70
71         assertNull(h.getValue());
72
73         h.setValue("fred");
74
75         assertEquals("fred", h.getValue());
76
77         n.fireThreadCleanup();
78
79         assertNull(h.getValue());
80
81         assertEquals(
82                 "<OuterProxy for hivemind.test.services.StringHolder(hivemind.test.services.StringHolder)>",
83                 h.toString());
84
85         assertLoggedMessages(new String JavaDoc[]
86         {
87                 "BEGIN getValue()",
88                 "Constructing core service implementation for service hivemind.test.services.StringHolder",
89                 "END getValue() [<null>]",
90                 "BEGIN setValue(fred)",
91                 "END setValue()",
92                 "BEGIN getValue()",
93                 "END getValue() [fred]",
94                 "BEGIN getValue()",
95                 "Constructing core service implementation for service hivemind.test.services.StringHolder",
96                 "END getValue() [<null>]" });
97
98     }
99
100     /**
101      * Uses a second thread to ensure that the data in different threads is seperate.
102      */

103     public void testThreaded() throws Exception JavaDoc
104     {
105         Registry r = buildFrameworkRegistry(new StringHolderModule(ServiceModel.THREADED));
106
107         StringHolder h = (StringHolder) r.getService(
108                 "hivemind.test.services.StringHolder",
109                 StringHolder.class);
110
111         interceptLogging("hivemind.test.services.StringHolder");
112
113         assertNull(h.getValue());
114
115         h.setValue("fred");
116
117         assertEquals("fred", h.getValue());
118
119         TestOther other = new TestOther();
120         other._holder = h;
121
122         Thread JavaDoc thread = new Thread JavaDoc(other);
123
124         thread.start();
125
126         // Wait up-to 2sec for the other thread to finish; should take just a couple
127
// of millis.
128
thread.join(2000);
129
130         assertNull(_otherStartValue);
131         assertEquals("barney", _otherEndValue);
132
133         // Make sure these are really seperate instances.
134

135         assertEquals("fred", h.getValue());
136
137         assertLoggedMessages(new String JavaDoc[]
138         {
139                 "BEGIN getValue()",
140                 "Constructing core service implementation for service hivemind.test.services.StringHolder",
141                 "END getValue() [<null>]",
142                 "BEGIN setValue(fred)",
143                 "END setValue()",
144                 "BEGIN getValue()",
145                 "END getValue() [fred]",
146                 "BEGIN getValue()",
147                 "Constructing core service implementation for service hivemind.test.services.StringHolder",
148                 "END getValue() [<null>]", "BEGIN setValue(barney)", "END setValue()",
149                 "BEGIN getValue()", "END getValue() [barney]", "BEGIN getValue()",
150                 "END getValue() [fred]" });
151     }
152
153     // Set by RegistryShutdownStringHolderImpl to true (except it doesn't,
154
// because the registryDidShutdown() method doesn't get invoked.
155

156     public static boolean _didShutdown = false;
157
158     protected void tearDown() throws Exception JavaDoc
159     {
160         super.tearDown();
161
162         _didShutdown = false;
163     }
164
165     public void testIgnoreRegistyShutdownListener() throws Exception JavaDoc
166     {
167         Registry r = createRegistryShutdownListener(RegistryShutdownStringHolderImpl.class);
168
169         StringHolder h = (StringHolder) r.getService(
170                 "hivemind.test.services.StringHolder",
171                 StringHolder.class);
172
173         interceptLogging("hivemind.test.services");
174
175         h.setValue("foo");
176
177         assertLoggedMessage("Core implementation of service hivemind.test.services.StringHolder implements the RegistryCleanupListener interface, which is not supported by the threaded service model.");
178
179         r.shutdown();
180
181         assertEquals(false, _didShutdown);
182     }
183
184     /**
185      * Creates a Registry with one module, that defines a threaded Service "StringHolder"
186      * @param implementationClass implementation class of the service
187      */

188     private Registry createRegistryShutdownListener(Class JavaDoc implementationClass)
189     {
190         ModuleDefinitionImpl module = createModuleDefinition("hivemind.test.services");
191         ModuleDefinitionHelper helper = new ModuleDefinitionHelper(module);
192
193         ServicePointDefinition sp1 = helper.addServicePoint("StringHolder", StringHolder.class.getName());
194         helper.addSimpleServiceImplementation(sp1, implementationClass.getName(), ServiceModel.THREADED);
195
196         return buildFrameworkRegistry(module);
197     }
198     
199     public void testDiscardable() throws Exception JavaDoc
200     {
201         Registry r = createRegistryShutdownListener(DiscardableStringHolderImpl.class);
202
203         StringHolder h = (StringHolder) r.getService(
204                 "hivemind.test.services.StringHolder",
205                 StringHolder.class);
206
207         h.setValue("bar");
208
209         ThreadEventNotifier n = (ThreadEventNotifier) r.getService(
210                 "hivemind.ThreadEventNotifier",
211                 ThreadEventNotifier.class);
212
213         interceptLogging("hivemind.test.services");
214
215         n.fireThreadCleanup();
216
217         assertLoggedMessage("threadDidDiscardService() has been invoked.");
218     }
219 }
Popular Tags