01: /* Copyright 2004, 2005, 2006 Acegi Technology Pty Limited
02: *
03: * Licensed under the Apache License, Version 2.0 (the "License");
04: * you may not use this file except in compliance with the License.
05: * You may obtain a copy of the License at
06: *
07: * http://www.apache.org/licenses/LICENSE-2.0
08: *
09: * 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.wrapper;
17:
18: import junit.framework.TestCase;
19:
20: import org.acegisecurity.MockFilterConfig;
21:
22: import org.springframework.mock.web.MockHttpServletRequest;
23:
24: import java.io.IOException;
25:
26: import javax.servlet.FilterChain;
27: import javax.servlet.ServletException;
28: import javax.servlet.ServletRequest;
29: import javax.servlet.ServletResponse;
30:
31: /**
32: * Tests {@link SecurityContextHolderAwareRequestFilter}.
33: *
34: * @author Ben Alex
35: * @version $Id: SecurityContextHolderAwareRequestFilterTests.java 1496 2006-05-23 13:38:33Z benalex $
36: */
37: public class SecurityContextHolderAwareRequestFilterTests extends
38: TestCase {
39: //~ Constructors ===================================================================================================
40:
41: public SecurityContextHolderAwareRequestFilterTests() {
42: super ();
43: }
44:
45: public SecurityContextHolderAwareRequestFilterTests(String arg0) {
46: super (arg0);
47: }
48:
49: //~ Methods ========================================================================================================
50:
51: public static void main(String[] args) {
52: junit.textui.TestRunner
53: .run(SecurityContextHolderAwareRequestFilterTests.class);
54: }
55:
56: public final void setUp() throws Exception {
57: super .setUp();
58: }
59:
60: public void testCorrectOperation() throws Exception {
61: SecurityContextHolderAwareRequestFilter filter = new SecurityContextHolderAwareRequestFilter();
62: filter.init(new MockFilterConfig());
63: filter.doFilter(new MockHttpServletRequest(null, null), null,
64: new MockFilterChain(SavedRequestAwareWrapper.class));
65:
66: // Now re-execute the filter, ensuring our replacement wrapper is still used
67: filter.doFilter(new MockHttpServletRequest(null, null), null,
68: new MockFilterChain(SavedRequestAwareWrapper.class));
69:
70: filter.destroy();
71: }
72:
73: //~ Inner Classes ==================================================================================================
74:
75: private class MockFilterChain implements FilterChain {
76: private Class expectedServletRequest;
77:
78: public MockFilterChain(Class expectedServletRequest) {
79: this .expectedServletRequest = expectedServletRequest;
80: }
81:
82: private MockFilterChain() {
83: super ();
84: }
85:
86: public void doFilter(ServletRequest request,
87: ServletResponse response) throws IOException,
88: ServletException {
89: if (request.getClass().isAssignableFrom(
90: expectedServletRequest)) {
91: assertTrue(true);
92: } else {
93: fail("Expected class to be of type "
94: + expectedServletRequest + " but was: "
95: + request.getClass());
96: }
97: }
98: }
99: }
|