KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > hivemind > servlet > TestAutoloadingHiveMindFilter


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.servlet;
16
17 import java.io.IOException JavaDoc;
18
19 import javax.servlet.Filter JavaDoc;
20 import javax.servlet.FilterChain JavaDoc;
21 import javax.servlet.FilterConfig JavaDoc;
22 import javax.servlet.ServletContext JavaDoc;
23 import javax.servlet.ServletException JavaDoc;
24 import javax.servlet.ServletRequest JavaDoc;
25 import javax.servlet.ServletResponse JavaDoc;
26 import javax.servlet.http.HttpServletRequest JavaDoc;
27 import javax.servlet.http.HttpServletResponse JavaDoc;
28
29 import org.apache.hivemind.ApplicationRuntimeException;
30 import org.apache.hivemind.HiveMind;
31 import org.apache.hivemind.Registry;
32 import org.apache.hivemind.ShutdownCoordinator;
33 import org.apache.hivemind.events.RegistryShutdownListener;
34 import org.apache.hivemind.service.ThreadCleanupListener;
35 import org.apache.hivemind.service.ThreadEventNotifier;
36 import org.apache.hivemind.test.HiveMindTestCase;
37 import org.easymock.MockControl;
38
39 /**
40  * Tests for {@link org.apache.hivemind.servlet.AutoloadingHiveMindFilter}.
41  *
42  * @author Howard Lewis Ship
43  */

44 public class TestAutoloadingHiveMindFilter extends HiveMindTestCase
45 {
46     private static class ThreadListenerFixture implements ThreadCleanupListener
47     {
48         private boolean _cleanup;
49
50         public void threadDidCleanup()
51         {
52             _cleanup = true;
53         }
54
55         public boolean getCleanup()
56         {
57             return _cleanup;
58         }
59
60     }
61
62     private static class ShutdownListenerFixture implements RegistryShutdownListener
63     {
64         private boolean _didShutdown;
65
66         public void registryDidShutdown()
67         {
68             _didShutdown = true;
69         }
70
71         public boolean getDidShutdown()
72         {
73             return _didShutdown;
74         }
75
76     }
77
78     private static class FailingAutoloadingHiveMindFilterFixture extends AutoloadingHiveMindFilter
79     {
80
81         protected Registry constructRegistry(FilterConfig JavaDoc config)
82         {
83             throw new ApplicationRuntimeException("Forced failure.");
84         }
85
86     }
87
88     private static class RegistryExposingAutoloadingHiveMindFilterFixture extends AutoloadingHiveMindFilter
89     {
90
91         private Registry _registry;
92
93         public Registry getRegistry()
94         {
95             return _registry;
96         }
97
98         protected Registry constructRegistry(FilterConfig JavaDoc config)
99         {
100             _registry = super.constructRegistry(config);
101             return _registry;
102         }
103
104     }
105
106     private static class RebuildRegistryChainFixture implements FilterChain JavaDoc
107     {
108         public void doFilter(ServletRequest JavaDoc request, ServletResponse JavaDoc response) throws IOException JavaDoc,
109                 ServletException JavaDoc
110         {
111             AutoloadingHiveMindFilter.rebuildRegistry((HttpServletRequest JavaDoc) request);
112         }
113     }
114
115     public void testBasic() throws Exception JavaDoc
116     {
117         FilterConfig JavaDoc filterConfig = newFilterConfig();
118
119         replayControls();
120
121         RegistryExposingAutoloadingHiveMindFilterFixture f = new RegistryExposingAutoloadingHiveMindFilterFixture();
122
123         f.init(filterConfig);
124
125         verifyControls();
126
127         Registry r = f.getRegistry();
128
129         assertNotNull(r);
130
131         ThreadEventNotifier t = (ThreadEventNotifier) r.getService(
132                 HiveMind.THREAD_EVENT_NOTIFIER_SERVICE,
133                 ThreadEventNotifier.class);
134
135         ThreadListenerFixture l = new ThreadListenerFixture();
136
137         t.addThreadCleanupListener(l);
138
139         MockControl requestControl = newControl(HttpServletRequest JavaDoc.class);
140         HttpServletRequest JavaDoc request = (HttpServletRequest JavaDoc) requestControl.getMock();
141         HttpServletResponse JavaDoc response = (HttpServletResponse JavaDoc) newMock(HttpServletResponse JavaDoc.class);
142         FilterChain JavaDoc chain = (FilterChain JavaDoc) newMock(FilterChain JavaDoc.class);
143
144         request.setAttribute(AutoloadingHiveMindFilter.REQUEST_KEY, r);
145
146         chain.doFilter(request, response);
147
148         request.getAttribute(AutoloadingHiveMindFilter.REBUILD_REQUEST_KEY);
149         requestControl.setReturnValue(null);
150
151         request.getAttribute(AutoloadingHiveMindFilter.REQUEST_KEY);
152         requestControl.setReturnValue(r);
153
154         replayControls();
155
156         f.doFilter(request, response, chain);
157
158         assertSame(r, AutoloadingHiveMindFilter.getRegistry(request));
159
160         assertEquals(true, l.getCleanup());
161
162         f.destroy();
163
164         try
165         {
166             t.addThreadCleanupListener(null);
167             unreachable();
168         }
169         catch (ApplicationRuntimeException ex)
170         {
171             assertExceptionSubstring(ex, "The HiveMind Registry has been shutdown.");
172         }
173
174         verifyControls();
175     }
176
177     public void testShutdown() throws Exception JavaDoc
178     {
179         MockControl contextc = newControl(ServletContext JavaDoc.class);
180         ServletContext JavaDoc context = (ServletContext JavaDoc) contextc.getMock();
181
182         MockControl configc = newControl(FilterConfig JavaDoc.class);
183         FilterConfig JavaDoc filterConfig = (FilterConfig JavaDoc) configc.getMock();
184
185         filterConfig.getServletContext();
186         configc.setReturnValue(context);
187
188         replayControls();
189
190         RegistryExposingAutoloadingHiveMindFilterFixture f = new RegistryExposingAutoloadingHiveMindFilterFixture();
191
192         f.init(filterConfig);
193
194         verifyControls();
195
196         Registry r = f.getRegistry();
197
198         assertNotNull(r);
199
200         ShutdownCoordinator coordinator = (ShutdownCoordinator) r
201                 .getService(ShutdownCoordinator.class);
202
203         ShutdownListenerFixture l = new ShutdownListenerFixture();
204
205         coordinator.addRegistryShutdownListener(l);
206
207         MockControl requestControl = newControl(HttpServletRequest JavaDoc.class);
208         HttpServletRequest JavaDoc request = (HttpServletRequest JavaDoc) requestControl.getMock();
209         HttpServletResponse JavaDoc response = (HttpServletResponse JavaDoc) newMock(HttpServletResponse JavaDoc.class);
210         FilterChain JavaDoc chain = new RebuildRegistryChainFixture();
211
212         request.setAttribute(AutoloadingHiveMindFilter.REQUEST_KEY, r);
213
214         request.setAttribute(AutoloadingHiveMindFilter.REBUILD_REQUEST_KEY, Boolean.TRUE);
215
216         request.getAttribute(AutoloadingHiveMindFilter.REBUILD_REQUEST_KEY);
217         requestControl.setReturnValue(Boolean.TRUE);
218
219         filterConfig.getServletContext();
220         configc.setReturnValue(context);
221
222         replayControls();
223
224         f.doFilter(request, response, chain);
225
226         verifyControls();
227
228         assertEquals(true, l.getDidShutdown());
229     }
230
231     private FilterConfig JavaDoc newFilterConfig() throws Exception JavaDoc
232     {
233         MockControl control = newControl(ServletContext JavaDoc.class);
234
235         ServletContext JavaDoc context = (ServletContext JavaDoc) control.getMock();
236
237         return newFilterConfig(context);
238     }
239
240     private FilterConfig JavaDoc newFilterConfig(ServletContext JavaDoc context)
241     {
242         MockControl control = newControl(FilterConfig JavaDoc.class);
243         FilterConfig JavaDoc config = (FilterConfig JavaDoc) control.getMock();
244
245         config.getServletContext();
246         control.setReturnValue(context);
247
248         return config;
249     }
250
251     public void testExceptionInInit() throws Exception JavaDoc
252     {
253         Filter JavaDoc f = new FailingAutoloadingHiveMindFilterFixture();
254
255         interceptLogging(AutoloadingHiveMindFilter.class.getName());
256
257         f.init(null);
258
259         assertLoggedMessage("Forced failure");
260
261         MockControl requestControl = newControl(HttpServletRequest JavaDoc.class);
262         HttpServletRequest JavaDoc request = (HttpServletRequest JavaDoc) requestControl.getMock();
263         HttpServletResponse JavaDoc response = (HttpServletResponse JavaDoc) newMock(HttpServletResponse JavaDoc.class);
264         FilterChain JavaDoc chain = (FilterChain JavaDoc) newMock(FilterChain JavaDoc.class);
265
266         request.setAttribute(AutoloadingHiveMindFilter.REQUEST_KEY, null);
267
268         chain.doFilter(request, response);
269
270         request.getAttribute(AutoloadingHiveMindFilter.REBUILD_REQUEST_KEY);
271         requestControl.setReturnValue(null);
272
273         replayControls();
274
275         f.doFilter(request, response, chain);
276
277         verifyControls();
278
279         f.destroy();
280     }
281
282     public void testDestroyWithoutRepository()
283     {
284         Filter JavaDoc f = new AutoloadingHiveMindFilter();
285
286         f.destroy();
287     }
288
289     public void testFilterWithoutRepository() throws Exception JavaDoc
290     {
291         Filter JavaDoc f = new AutoloadingHiveMindFilter();
292
293         interceptLogging(AutoloadingHiveMindFilter.class.getName());
294
295         MockControl requestControl = newControl(HttpServletRequest JavaDoc.class);
296         HttpServletRequest JavaDoc request = (HttpServletRequest JavaDoc) requestControl.getMock();
297         HttpServletResponse JavaDoc response = (HttpServletResponse JavaDoc) newMock(HttpServletResponse JavaDoc.class);
298         FilterChain JavaDoc chain = (FilterChain JavaDoc) newMock(FilterChain JavaDoc.class);
299
300         request.setAttribute(AutoloadingHiveMindFilter.REQUEST_KEY, null);
301
302         chain.doFilter(request, response);
303
304         request.getAttribute(AutoloadingHiveMindFilter.REBUILD_REQUEST_KEY);
305         requestControl.setReturnValue(null);
306
307         replayControls();
308
309         f.doFilter(request, response, chain);
310
311         assertLoggedMessage("Unable to cleanup current thread");
312
313         verifyControls();
314     }
315
316 }
Popular Tags