1 15 16 package org.springframework.web.filter; 17 18 import java.io.IOException ; 19 20 import javax.servlet.Filter ; 21 import javax.servlet.FilterChain ; 22 import javax.servlet.FilterConfig ; 23 import javax.servlet.ServletContext ; 24 import javax.servlet.ServletException ; 25 import javax.servlet.ServletRequest ; 26 import javax.servlet.ServletResponse ; 27 28 import junit.framework.TestCase; 29 30 import org.springframework.mock.web.MockFilterConfig; 31 import org.springframework.mock.web.MockHttpServletRequest; 32 import org.springframework.mock.web.MockHttpServletResponse; 33 import org.springframework.mock.web.MockServletContext; 34 import org.springframework.web.context.WebApplicationContext; 35 import org.springframework.web.context.support.StaticWebApplicationContext; 36 37 41 public class DelegatingFilterProxyTests extends TestCase { 42 43 public void testDelegatingFilterProxy() throws ServletException , IOException { 44 ServletContext sc = new MockServletContext(); 45 46 StaticWebApplicationContext wac = new StaticWebApplicationContext(); 47 wac.setServletContext(sc); 48 wac.registerSingleton("targetFilter", MockFilter.class); 49 wac.refresh(); 50 sc.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac); 51 52 MockFilter targetFilter = (MockFilter) wac.getBean("targetFilter"); 53 54 MockFilterConfig proxyConfig = new MockFilterConfig(sc); 55 proxyConfig.addInitParameter("targetBeanName", "targetFilter"); 56 DelegatingFilterProxy filterProxy = new DelegatingFilterProxy(); 57 filterProxy.init(proxyConfig); 58 59 MockHttpServletRequest request = new MockHttpServletRequest(); 60 MockHttpServletResponse response = new MockHttpServletResponse(); 61 filterProxy.doFilter(request, response, null); 62 63 assertNull(targetFilter.filterConfig); 64 assertEquals(Boolean.TRUE, request.getAttribute("called")); 65 66 filterProxy.destroy(); 67 assertNull(targetFilter.filterConfig); 68 } 69 70 public void testDelegatingFilterProxyWithFilterName() throws ServletException , IOException { 71 ServletContext sc = new MockServletContext(); 72 73 StaticWebApplicationContext wac = new StaticWebApplicationContext(); 74 wac.setServletContext(sc); 75 wac.registerSingleton("targetFilter", MockFilter.class); 76 wac.refresh(); 77 sc.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac); 78 79 MockFilter targetFilter = (MockFilter) wac.getBean("targetFilter"); 80 81 MockFilterConfig proxyConfig = new MockFilterConfig(sc, "targetFilter"); 82 DelegatingFilterProxy filterProxy = new DelegatingFilterProxy(); 83 filterProxy.init(proxyConfig); 84 85 MockHttpServletRequest request = new MockHttpServletRequest(); 86 MockHttpServletResponse response = new MockHttpServletResponse(); 87 filterProxy.doFilter(request, response, null); 88 89 assertNull(targetFilter.filterConfig); 90 assertEquals(Boolean.TRUE, request.getAttribute("called")); 91 92 filterProxy.destroy(); 93 assertNull(targetFilter.filterConfig); 94 } 95 96 public void testDelegatingFilterProxyWithLazyContextStartup() throws ServletException , IOException { 97 ServletContext sc = new MockServletContext(); 98 99 MockFilterConfig proxyConfig = new MockFilterConfig(sc); 100 proxyConfig.addInitParameter("targetBeanName", "targetFilter"); 101 DelegatingFilterProxy filterProxy = new DelegatingFilterProxy(); 102 filterProxy.init(proxyConfig); 103 104 StaticWebApplicationContext wac = new StaticWebApplicationContext(); 105 wac.setServletContext(sc); 106 wac.registerSingleton("targetFilter", MockFilter.class); 107 wac.refresh(); 108 sc.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac); 109 110 MockFilter targetFilter = (MockFilter) wac.getBean("targetFilter"); 111 112 MockHttpServletRequest request = new MockHttpServletRequest(); 113 MockHttpServletResponse response = new MockHttpServletResponse(); 114 filterProxy.doFilter(request, response, null); 115 116 assertNull(targetFilter.filterConfig); 117 assertEquals(Boolean.TRUE, request.getAttribute("called")); 118 119 filterProxy.destroy(); 120 assertNull(targetFilter.filterConfig); 121 } 122 123 public void testDelegatingFilterProxyWithTargetFilterLifecycle() throws ServletException , IOException { 124 ServletContext sc = new MockServletContext(); 125 126 StaticWebApplicationContext wac = new StaticWebApplicationContext(); 127 wac.setServletContext(sc); 128 wac.registerSingleton("targetFilter", MockFilter.class); 129 wac.refresh(); 130 sc.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac); 131 132 MockFilter targetFilter = (MockFilter) wac.getBean("targetFilter"); 133 134 MockFilterConfig proxyConfig = new MockFilterConfig(sc); 135 proxyConfig.addInitParameter("targetBeanName", "targetFilter"); 136 proxyConfig.addInitParameter("targetFilterLifecycle", "true"); 137 DelegatingFilterProxy filterProxy = new DelegatingFilterProxy(); 138 filterProxy.init(proxyConfig); 139 assertEquals(proxyConfig, targetFilter.filterConfig); 140 141 MockHttpServletRequest request = new MockHttpServletRequest(); 142 MockHttpServletResponse response = new MockHttpServletResponse(); 143 filterProxy.doFilter(request, response, null); 144 145 assertEquals(proxyConfig, targetFilter.filterConfig); 146 assertEquals(Boolean.TRUE, request.getAttribute("called")); 147 148 filterProxy.destroy(); 149 assertNull(targetFilter.filterConfig); 150 } 151 152 153 public static class MockFilter implements Filter { 154 155 public FilterConfig filterConfig; 156 157 public void init(FilterConfig filterConfig) throws ServletException { 158 this.filterConfig = filterConfig; 159 } 160 161 public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws IOException , ServletException { 162 request.setAttribute("called", Boolean.TRUE); 163 } 164 165 public void destroy() { 166 this.filterConfig = null; 167 } 168 } 169 170 } 171 | Popular Tags |