KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > web > filter > DelegatingFilterProxyTests


1 /* Copyright 2004, 2005 Acegi Technology Pty Limited
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
16 package org.springframework.web.filter;
17
18 import java.io.IOException JavaDoc;
19
20 import javax.servlet.Filter JavaDoc;
21 import javax.servlet.FilterChain JavaDoc;
22 import javax.servlet.FilterConfig JavaDoc;
23 import javax.servlet.ServletContext JavaDoc;
24 import javax.servlet.ServletException JavaDoc;
25 import javax.servlet.ServletRequest JavaDoc;
26 import javax.servlet.ServletResponse JavaDoc;
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 /**
38  * @author Juergen Hoeller
39  * @since 08.05.2005
40  */

41 public class DelegatingFilterProxyTests extends TestCase {
42
43     public void testDelegatingFilterProxy() throws ServletException JavaDoc, IOException JavaDoc {
44         ServletContext JavaDoc 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 JavaDoc, IOException JavaDoc {
71         ServletContext JavaDoc 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 JavaDoc, IOException JavaDoc {
97         ServletContext JavaDoc 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 JavaDoc, IOException JavaDoc {
124         ServletContext JavaDoc 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 JavaDoc {
154
155         public FilterConfig JavaDoc filterConfig;
156
157         public void init(FilterConfig JavaDoc filterConfig) throws ServletException JavaDoc {
158             this.filterConfig = filterConfig;
159         }
160
161         public void doFilter(ServletRequest JavaDoc request, ServletResponse JavaDoc response, FilterChain JavaDoc filterChain) throws IOException JavaDoc, ServletException JavaDoc {
162             request.setAttribute("called", Boolean.TRUE);
163         }
164
165         public void destroy() {
166             this.filterConfig = null;
167         }
168     }
169
170 }
171
Popular Tags