1 15 package org.apache.hivemind.servlet; 16 17 import java.io.IOException ; 18 19 import javax.servlet.Filter ; 20 import javax.servlet.FilterChain ; 21 import javax.servlet.FilterConfig ; 22 import javax.servlet.ServletContext ; 23 import javax.servlet.ServletException ; 24 import javax.servlet.ServletRequest ; 25 import javax.servlet.ServletResponse ; 26 import javax.servlet.http.HttpServletRequest ; 27 import javax.servlet.http.HttpServletResponse ; 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 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 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 config) 99 { 100 _registry = super.constructRegistry(config); 101 return _registry; 102 } 103 104 } 105 106 private static class RebuildRegistryChainFixture implements FilterChain 107 { 108 public void doFilter(ServletRequest request, ServletResponse response) throws IOException , 109 ServletException 110 { 111 AutoloadingHiveMindFilter.rebuildRegistry((HttpServletRequest ) request); 112 } 113 } 114 115 public void testBasic() throws Exception 116 { 117 FilterConfig 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 .class); 140 HttpServletRequest request = (HttpServletRequest ) requestControl.getMock(); 141 HttpServletResponse response = (HttpServletResponse ) newMock(HttpServletResponse .class); 142 FilterChain chain = (FilterChain ) newMock(FilterChain .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 178 { 179 MockControl contextc = newControl(ServletContext .class); 180 ServletContext context = (ServletContext ) contextc.getMock(); 181 182 MockControl configc = newControl(FilterConfig .class); 183 FilterConfig filterConfig = (FilterConfig ) 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 .class); 208 HttpServletRequest request = (HttpServletRequest ) requestControl.getMock(); 209 HttpServletResponse response = (HttpServletResponse ) newMock(HttpServletResponse .class); 210 FilterChain 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 newFilterConfig() throws Exception 232 { 233 MockControl control = newControl(ServletContext .class); 234 235 ServletContext context = (ServletContext ) control.getMock(); 236 237 return newFilterConfig(context); 238 } 239 240 private FilterConfig newFilterConfig(ServletContext context) 241 { 242 MockControl control = newControl(FilterConfig .class); 243 FilterConfig config = (FilterConfig ) control.getMock(); 244 245 config.getServletContext(); 246 control.setReturnValue(context); 247 248 return config; 249 } 250 251 public void testExceptionInInit() throws Exception 252 { 253 Filter 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 .class); 262 HttpServletRequest request = (HttpServletRequest ) requestControl.getMock(); 263 HttpServletResponse response = (HttpServletResponse ) newMock(HttpServletResponse .class); 264 FilterChain chain = (FilterChain ) newMock(FilterChain .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 f = new AutoloadingHiveMindFilter(); 285 286 f.destroy(); 287 } 288 289 public void testFilterWithoutRepository() throws Exception 290 { 291 Filter f = new AutoloadingHiveMindFilter(); 292 293 interceptLogging(AutoloadingHiveMindFilter.class.getName()); 294 295 MockControl requestControl = newControl(HttpServletRequest .class); 296 HttpServletRequest request = (HttpServletRequest ) requestControl.getMock(); 297 HttpServletResponse response = (HttpServletResponse ) newMock(HttpServletResponse .class); 298 FilterChain chain = (FilterChain ) newMock(FilterChain .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 |