KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.net.URL JavaDoc;
19
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.SymbolExpander;
34 import org.apache.hivemind.events.RegistryShutdownListener;
35 import org.apache.hivemind.service.ThreadCleanupListener;
36 import org.apache.hivemind.service.ThreadEventNotifier;
37 import org.apache.hivemind.test.HiveMindTestCase;
38 import org.easymock.MockControl;
39
40 /**
41  * Tests for {@link org.apache.hivemind.servlet.HiveMindFilter}.
42  *
43  * @author Howard Lewis Ship
44  */

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