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.acegisecurity.util;
17  
18  import junit.framework.TestCase;
19  
20  import org.acegisecurity.MockFilterConfig;
21  
22  import org.springframework.context.ApplicationContext;
23  import org.springframework.context.support.ClassPathXmlApplicationContext;
24  import org.springframework.mock.web.MockHttpServletResponse;
25  import org.springframework.mock.web.MockHttpServletRequest;
26  
27  import java.io.IOException;
28  
29  import javax.servlet.Filter;
30  import javax.servlet.FilterChain;
31  import javax.servlet.FilterConfig;
32  import javax.servlet.ServletException;
33  import javax.servlet.ServletRequest;
34  import javax.servlet.ServletResponse;
35  
36  
37  /***
38   * Tests {@link FilterToBeanProxy}.
39   *
40   * @author Ben Alex
41   * @version $Id: FilterToBeanProxyTests.java,v 1.7 2005/11/17 00:56:08 benalex Exp $
42   */
43  public class FilterToBeanProxyTests extends TestCase {
44      //~ Constructors ===========================================================
45  
46      public FilterToBeanProxyTests() {
47          super();
48      }
49  
50      public FilterToBeanProxyTests(String arg0) {
51          super(arg0);
52      }
53  
54      //~ Methods ================================================================
55  
56      public final void setUp() throws Exception {
57          super.setUp();
58      }
59  
60      public static void main(String[] args) {
61          junit.textui.TestRunner.run(FilterToBeanProxyTests.class);
62      }
63  
64      public void testDetectsClassNotInClassLoader() throws Exception {
65          // Setup our filter
66          MockFilterConfig config = new MockFilterConfig();
67          config.setInitParmeter("targetClass", "net.sf.DOES.NOT.EXIST");
68  
69          FilterToBeanProxy filter = new MockFilterToBeanProxy(
70                  "org/acegisecurity/util/filtertest-valid.xml");
71  
72          try {
73              filter.init(config);
74              fail("Should have thrown ServletException");
75          } catch (ServletException expected) {
76              assertEquals("Class of type net.sf.DOES.NOT.EXIST not found in classloader",
77                  expected.getMessage());
78          }
79      }
80  
81      public void testDetectsNeitherPropertyBeingSet() throws Exception {
82          // Setup our filter
83          MockFilterConfig config = new MockFilterConfig();
84  
85          FilterToBeanProxy filter = new MockFilterToBeanProxy(
86                  "org/acegisecurity/util/filtertest-valid.xml");
87  
88          try {
89              filter.init(config);
90              fail("Should have thrown ServletException");
91          } catch (ServletException expected) {
92              assertEquals("targetClass or targetBean must be specified",
93                  expected.getMessage());
94          }
95      }
96  
97      public void testDetectsTargetBeanIsNotAFilter() throws Exception {
98          // Setup our filter
99          MockFilterConfig config = new MockFilterConfig();
100         config.setInitParmeter("targetClass",
101             "org.acegisecurity.util.MockNotAFilter");
102 
103         FilterToBeanProxy filter = new MockFilterToBeanProxy(
104                 "org/acegisecurity/util/filtertest-valid.xml");
105 
106         try {
107             filter.init(config);
108             fail("Should have thrown ServletException");
109         } catch (ServletException expected) {
110             assertEquals("Bean 'mockNotAFilter' does not implement javax.servlet.Filter",
111                 expected.getMessage());
112         }
113     }
114 
115     public void testDetectsTargetBeanNotInBeanContext()
116         throws Exception {
117         // Setup our filter
118         MockFilterConfig config = new MockFilterConfig();
119         config.setInitParmeter("targetBean", "WRONG_NAME");
120 
121         FilterToBeanProxy filter = new MockFilterToBeanProxy(
122                 "org/acegisecurity/util/filtertest-valid.xml");
123 
124         try {
125             filter.init(config);
126             fail("Should have thrown ServletException");
127         } catch (ServletException expected) {
128             assertEquals("targetBean 'WRONG_NAME' not found in context",
129                 expected.getMessage());
130         }
131     }
132 
133     public void testDetectsTargetClassNotInBeanContext()
134         throws Exception {
135         // Setup our filter
136         MockFilterConfig config = new MockFilterConfig();
137         config.setInitParmeter("targetClass",
138             "org.acegisecurity.util.FilterToBeanProxyTests");
139 
140         FilterToBeanProxy filter = new MockFilterToBeanProxy(
141                 "org/acegisecurity/util/filtertest-valid.xml");
142 
143         try {
144             filter.init(config);
145             fail("Should have thrown ServletException");
146         } catch (ServletException expected) {
147             assertEquals("Bean context must contain at least one bean of type org.acegisecurity.util.FilterToBeanProxyTests",
148                 expected.getMessage());
149         }
150     }
151 
152     public void testIgnoresEmptyTargetBean() throws Exception {
153         // Setup our filter
154         MockFilterConfig config = new MockFilterConfig();
155         config.setInitParmeter("targetClass",
156             "org.acegisecurity.util.MockFilter");
157         config.setInitParmeter("targetBean", "");
158 
159         // Setup our expectation that the filter chain will be invoked
160         MockFilterChain chain = new MockFilterChain(true);
161 
162         MockHttpServletResponse response = new MockHttpServletResponse();
163         MockHttpServletRequest request = new MockHttpServletRequest();
164 
165         FilterToBeanProxy filter = new MockFilterToBeanProxy(
166                 "org/acegisecurity/util/filtertest-valid.xml");
167 
168         executeFilterInContainerSimulator(config, filter, request, response,
169             chain);
170     }
171 
172     public void testNormalOperationWithLazyTrue() throws Exception {
173         // Setup our filter
174         MockFilterConfig config = new MockFilterConfig();
175         config.setInitParmeter("targetBean", "mockFilter");
176         config.setInitParmeter("init", "lazy");
177 
178         // Setup our expectation that the filter chain will be invoked
179         MockFilterChain chain = new MockFilterChain(true);
180 
181         MockHttpServletResponse response = new MockHttpServletResponse();
182         MockHttpServletRequest request = new MockHttpServletRequest();
183 
184         FilterToBeanProxy filter = new MockFilterToBeanProxy(
185                 "org/acegisecurity/util/filtertest-valid.xml");
186 
187         executeFilterInContainerSimulator(config, filter, request, response,
188             chain);
189     }
190 
191     public void testNormalOperationWithSpecificBeanName()
192         throws Exception {
193         // Setup our filter
194         MockFilterConfig config = new MockFilterConfig();
195         config.setInitParmeter("targetBean", "mockFilter");
196 
197         // Setup our expectation that the filter chain will be invoked
198         MockFilterChain chain = new MockFilterChain(true);
199 
200         MockHttpServletResponse response = new MockHttpServletResponse();
201         MockHttpServletRequest request = new MockHttpServletRequest();
202 
203         FilterToBeanProxy filter = new MockFilterToBeanProxy(
204                 "org/acegisecurity/util/filtertest-valid.xml");
205 
206         executeFilterInContainerSimulator(config, filter, request, response,
207             chain);
208     }
209 
210     public void testNormalOperationWithTargetClass() throws Exception {
211         // Setup our filter
212         MockFilterConfig config = new MockFilterConfig();
213         config.setInitParmeter("targetClass",
214             "org.acegisecurity.util.MockFilter");
215 
216         // Setup our expectation that the filter chain will be invoked
217         MockFilterChain chain = new MockFilterChain(true);
218 
219         MockHttpServletResponse response = new MockHttpServletResponse();
220         MockHttpServletRequest request = new MockHttpServletRequest();
221 
222         FilterToBeanProxy filter = new MockFilterToBeanProxy(
223                 "org/acegisecurity/util/filtertest-valid.xml");
224 
225         executeFilterInContainerSimulator(config, filter, request, response,
226             chain);
227     }
228 
229     public void testNullDelegateDoesNotCauseNullPointerException()
230         throws Exception {
231         // Setup our filter
232         MockFilterConfig config = new MockFilterConfig();
233         config.setInitParmeter("targetBean", "aFilterThatDoesntExist");
234         config.setInitParmeter("init", "lazy");
235 
236         // Setup our expectation that the filter chain will be invoked
237         MockFilterChain chain = new MockFilterChain(true);
238 
239         MockHttpServletResponse response = new MockHttpServletResponse();
240         MockHttpServletRequest request = new MockHttpServletRequest();
241 
242         FilterToBeanProxy filter = new MockFilterToBeanProxy(
243                 "org/acegisecurity/util/filtertest-valid.xml");
244 
245         // do not init (which would hapen if called .doFilter)
246         filter.destroy();
247     }
248 
249     private void executeFilterInContainerSimulator(FilterConfig filterConfig,
250         Filter filter, ServletRequest request, ServletResponse response,
251         FilterChain filterChain) throws ServletException, IOException {
252         filter.init(filterConfig);
253         filter.doFilter(request, response, filterChain);
254         filter.destroy();
255     }
256 
257     //~ Inner Classes ==========================================================
258 
259     private class MockFilterToBeanProxy extends FilterToBeanProxy {
260         private String appContextLocation;
261 
262         public MockFilterToBeanProxy(String appContextLocation) {
263             this.appContextLocation = appContextLocation;
264         }
265 
266         private MockFilterToBeanProxy() {
267             super();
268         }
269 
270         protected ApplicationContext getContext(FilterConfig filterConfig) {
271             return new ClassPathXmlApplicationContext(appContextLocation);
272         }
273     }
274 }