01: /*
02: * Copyright 2002-2006 the original author or authors.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16:
17: package org.springframework.mock.web;
18:
19: import javax.servlet.RequestDispatcher;
20: import javax.servlet.ServletRequest;
21: import javax.servlet.ServletResponse;
22:
23: import org.apache.commons.logging.Log;
24: import org.apache.commons.logging.LogFactory;
25:
26: import org.springframework.util.Assert;
27:
28: /**
29: * Mock implementation of the {@link javax.servlet.RequestDispatcher} interface.
30: *
31: * <p>Used for testing the web framework; typically not necessary for
32: * testing application controllers.
33: *
34: * @author Rod Johnson
35: * @author Juergen Hoeller
36: * @since 1.0.2
37: */
38: public class MockRequestDispatcher implements RequestDispatcher {
39:
40: private final Log logger = LogFactory.getLog(getClass());
41:
42: private final String url;
43:
44: /**
45: * Create a new MockRequestDispatcher for the given URL.
46: * @param url the URL to dispatch to.
47: */
48: public MockRequestDispatcher(String url) {
49: Assert.notNull(url, "URL must not be null");
50: this .url = url;
51: }
52:
53: public void forward(ServletRequest request, ServletResponse response) {
54: Assert.notNull(request, "Request must not be null");
55: Assert.notNull(response, "Response must not be null");
56: if (response.isCommitted()) {
57: throw new IllegalStateException(
58: "Cannot perform forward - response is already committed");
59: }
60: if (!(response instanceof MockHttpServletResponse)) {
61: throw new IllegalArgumentException(
62: "MockRequestDispatcher requires MockHttpServletResponse");
63: }
64: ((MockHttpServletResponse) response).setForwardedUrl(this .url);
65: if (logger.isDebugEnabled()) {
66: logger.debug("MockRequestDispatcher: forwarding to URL ["
67: + this .url + "]");
68: }
69: }
70:
71: public void include(ServletRequest request, ServletResponse response) {
72: Assert.notNull(request, "Request must not be null");
73: Assert.notNull(response, "Response must not be null");
74: if (!(response instanceof MockHttpServletResponse)) {
75: throw new IllegalArgumentException(
76: "MockRequestDispatcher requires MockHttpServletResponse");
77: }
78: ((MockHttpServletResponse) response).setIncludedUrl(this .url);
79: if (logger.isDebugEnabled()) {
80: logger.debug("MockRequestDispatcher: including URL ["
81: + this .url + "]");
82: }
83: }
84:
85: }
|