1 15 package org.apache.hivemind.servlet; 16 17 import java.io.IOException ; 18 import java.net.URL ; 19 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.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 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 config) 90 { 91 _registry = super.constructRegistry(config); 92 return _registry; 93 } 94 95 } 96 97 private static class RebuildRegistryChainFixture implements FilterChain 98 { 99 public void doFilter(ServletRequest request, ServletResponse response) throws IOException , 100 ServletException 101 { 102 HiveMindFilter.rebuildRegistry((HttpServletRequest ) request); 103 } 104 } 105 106 107 108 public void testLoadsFromWebInf() throws Exception 109 { 110 MockControl contextc = newControl(ServletContext .class); 111 ServletContext context = (ServletContext ) contextc.getMock(); 112 113 MockControl configc = newControl(FilterConfig .class); 114 FilterConfig config = (FilterConfig ) configc.getMock(); 115 116 config.getServletContext(); 117 configc.setReturnValue(context); 118 119 URL 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 139 { 140 FilterConfig 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 .class); 163 HttpServletRequest request = (HttpServletRequest ) requestControl.getMock(); 164 HttpServletResponse response = (HttpServletResponse ) newMock(HttpServletResponse .class); 165 FilterChain chain = (FilterChain ) newMock(FilterChain .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 201 { 202 MockControl contextc = newControl(ServletContext .class); 203 ServletContext context = (ServletContext ) contextc.getMock(); 204 205 MockControl configc = newControl(FilterConfig .class); 206 FilterConfig filterConfig = (FilterConfig ) 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 .class); 234 HttpServletRequest request = (HttpServletRequest ) requestControl.getMock(); 235 HttpServletResponse response = (HttpServletResponse ) newMock(HttpServletResponse .class); 236 FilterChain 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 newFilterConfig() throws Exception 261 { 262 MockControl control = newControl(ServletContext .class); 263 264 ServletContext context = (ServletContext ) control.getMock(); 265 266 context.getResource(HiveMindFilter.HIVE_MODULE_XML); 267 control.setReturnValue(null); 268 269 return newFilterConfig(context); 270 } 271 272 private FilterConfig newFilterConfig(ServletContext context) 273 { 274 MockControl control = newControl(FilterConfig .class); 275 FilterConfig config = (FilterConfig ) control.getMock(); 276 277 config.getServletContext(); 278 control.setReturnValue(context); 279 280 return config; 281 } 282 283 284 } | Popular Tags |