001: /* Copyright 2004, 2005, 2006 Acegi Technology Pty Limited
002: *
003: * Licensed under the Apache License, Version 2.0 (the "License");
004: * you may not use this file except in compliance with the License.
005: * You may obtain a copy of the License at
006: *
007: * http://www.apache.org/licenses/LICENSE-2.0
008: *
009: * Unless required by applicable law or agreed to in writing, software
010: * distributed under the License is distributed on an "AS IS" BASIS,
011: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: * See the License for the specific language governing permissions and
013: * limitations under the License.
014: */
015:
016: package org.acegisecurity.intercept.web;
017:
018: import org.acegisecurity.MockFilterChain;
019:
020: import org.jmock.MockObjectTestCase;
021:
022: import org.springframework.mock.web.MockHttpServletRequest;
023: import org.springframework.mock.web.MockHttpServletResponse;
024:
025: import javax.servlet.ServletRequest;
026: import javax.servlet.ServletResponse;
027:
028: /**
029: * Tests {@link FilterInvocation}.
030: *
031: * @author Ben Alex
032: * @author colin sampaleanu
033: * @version $Id: FilterInvocationTests.java 1496 2006-05-23 13:38:33Z benalex $
034: */
035: public class FilterInvocationTests extends MockObjectTestCase {
036: //~ Constructors ===================================================================================================
037:
038: public FilterInvocationTests() {
039: super ();
040: }
041:
042: public FilterInvocationTests(String arg0) {
043: super (arg0);
044: }
045:
046: //~ Methods ========================================================================================================
047:
048: public static void main(String[] args) {
049: junit.textui.TestRunner.run(FilterInvocationTests.class);
050: }
051:
052: public final void setUp() throws Exception {
053: super .setUp();
054: }
055:
056: public void testGettersAndStringMethods() {
057: MockHttpServletRequest request = new MockHttpServletRequest(
058: null, null);
059: request.setServletPath("/HelloWorld");
060: request.setPathInfo("/some/more/segments.html");
061: request.setServerName("www.example.com");
062: request.setScheme("http");
063: request.setServerPort(80);
064: request.setContextPath("/mycontext");
065: request
066: .setRequestURI("/mycontext/HelloWorld/some/more/segments.html");
067:
068: MockHttpServletResponse response = new MockHttpServletResponse();
069: MockFilterChain chain = new MockFilterChain();
070: FilterInvocation fi = new FilterInvocation(request, response,
071: chain);
072: assertEquals(request, fi.getRequest());
073: assertEquals(request, fi.getHttpRequest());
074: assertEquals(response, fi.getResponse());
075: assertEquals(response, fi.getHttpResponse());
076: assertEquals(chain, fi.getChain());
077: assertEquals("/HelloWorld/some/more/segments.html", fi
078: .getRequestUrl());
079: assertEquals(
080: "FilterInvocation: URL: /HelloWorld/some/more/segments.html",
081: fi.toString());
082: assertEquals(
083: "http://www.example.com/mycontext/HelloWorld/some/more/segments.html",
084: fi.getFullRequestUrl());
085: }
086:
087: public void testNoArgConstructorDoesntExist() {
088: Class clazz = FilterInvocation.class;
089:
090: try {
091: clazz.getDeclaredConstructor((Class[]) null);
092: fail("Should have thrown NoSuchMethodException");
093: } catch (NoSuchMethodException expected) {
094: assertTrue(true);
095: }
096: }
097:
098: public void testRejectsNullFilterChain() {
099: MockHttpServletRequest request = new MockHttpServletRequest(
100: null, null);
101: MockHttpServletResponse response = new MockHttpServletResponse();
102:
103: try {
104: new FilterInvocation(request, response, null);
105: fail("Should have thrown IllegalArgumentException");
106: } catch (IllegalArgumentException expected) {
107: assertTrue(true);
108: }
109: }
110:
111: public void testRejectsNullServletRequest() {
112: MockHttpServletResponse response = new MockHttpServletResponse();
113: MockFilterChain chain = new MockFilterChain();
114:
115: try {
116: new FilterInvocation(null, response, chain);
117: fail("Should have thrown IllegalArgumentException");
118: } catch (IllegalArgumentException expected) {
119: assertTrue(true);
120: }
121: }
122:
123: public void testRejectsNullServletResponse() {
124: MockHttpServletRequest request = new MockHttpServletRequest(
125: null, null);
126: MockFilterChain chain = new MockFilterChain();
127:
128: try {
129: new FilterInvocation(request, null, chain);
130: fail("Should have thrown IllegalArgumentException");
131: } catch (IllegalArgumentException expected) {
132: assertTrue(true);
133: }
134: }
135:
136: public void testRejectsServletRequestWhichIsNotHttpServletRequest() {
137: ServletRequest request = (ServletRequest) newDummy(ServletRequest.class);
138: MockHttpServletResponse response = new MockHttpServletResponse();
139: MockFilterChain chain = new MockFilterChain();
140:
141: try {
142: new FilterInvocation(request, response, chain);
143: fail("Should have thrown IllegalArgumentException");
144: } catch (IllegalArgumentException expected) {
145: assertEquals("Can only process HttpServletRequest",
146: expected.getMessage());
147: }
148: }
149:
150: public void testRejectsServletResponseWhichIsNotHttpServletResponse() {
151: MockHttpServletRequest request = new MockHttpServletRequest(
152: null, null);
153: ServletResponse response = (ServletResponse) newDummy(ServletResponse.class);
154: MockFilterChain chain = new MockFilterChain();
155:
156: try {
157: new FilterInvocation(request, response, chain);
158: fail("Should have thrown IllegalArgumentException");
159: } catch (IllegalArgumentException expected) {
160: assertEquals("Can only process HttpServletResponse",
161: expected.getMessage());
162: }
163: }
164:
165: public void testStringMethodsWithAQueryString() {
166: MockHttpServletRequest request = new MockHttpServletRequest();
167: request.setQueryString("foo=bar");
168: request.setServletPath("/HelloWorld");
169: request.setServerName("www.example.com");
170: request.setScheme("http");
171: request.setServerPort(80);
172: request.setContextPath("/mycontext");
173: request.setRequestURI("/mycontext/HelloWorld");
174:
175: MockHttpServletResponse response = new MockHttpServletResponse();
176: MockFilterChain chain = new MockFilterChain();
177: FilterInvocation fi = new FilterInvocation(request, response,
178: chain);
179: assertEquals("/HelloWorld?foo=bar", fi.getRequestUrl());
180: assertEquals("FilterInvocation: URL: /HelloWorld?foo=bar", fi
181: .toString());
182: assertEquals(
183: "http://www.example.com/mycontext/HelloWorld?foo=bar",
184: fi.getFullRequestUrl());
185: }
186:
187: public void testStringMethodsWithoutAnyQueryString() {
188: MockHttpServletRequest request = new MockHttpServletRequest(
189: null, null);
190: request.setServletPath("/HelloWorld");
191: request.setServerName("www.example.com");
192: request.setScheme("http");
193: request.setServerPort(80);
194: request.setContextPath("/mycontext");
195: request.setRequestURI("/mycontext/HelloWorld");
196:
197: MockHttpServletResponse response = new MockHttpServletResponse();
198: MockFilterChain chain = new MockFilterChain();
199: FilterInvocation fi = new FilterInvocation(request, response,
200: chain);
201: assertEquals("/HelloWorld", fi.getRequestUrl());
202: assertEquals("FilterInvocation: URL: /HelloWorld", fi
203: .toString());
204: assertEquals("http://www.example.com/mycontext/HelloWorld", fi
205: .getFullRequestUrl());
206: }
207: }
|