KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > mock > web > MockRequestDispatcher


1 /*
2  * Copyright 2002-2006 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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 JavaDoc;
20 import javax.servlet.ServletRequest JavaDoc;
21 import javax.servlet.ServletResponse JavaDoc;
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 JavaDoc {
39
40     private final Log logger = LogFactory.getLog(getClass());
41
42     private final String JavaDoc url;
43
44
45     /**
46      * Create a new MockRequestDispatcher for the given URL.
47      * @param url the URL to dispatch to.
48      */

49     public MockRequestDispatcher(String JavaDoc url) {
50         Assert.notNull(url, "URL must not be null");
51         this.url = url;
52     }
53
54
55     public void forward(ServletRequest JavaDoc request, ServletResponse JavaDoc response) {
56         Assert.notNull(request, "Request must not be null");
57         Assert.notNull(response, "Response must not be null");
58         if (response.isCommitted()) {
59             throw new IllegalStateException JavaDoc("Cannot perform forward - response is already committed");
60         }
61         if (!(response instanceof MockHttpServletResponse)) {
62             throw new IllegalArgumentException JavaDoc("MockRequestDispatcher requires MockHttpServletResponse");
63         }
64         ((MockHttpServletResponse) response).setForwardedUrl(this.url);
65         if (logger.isDebugEnabled()) {
66             logger.debug("MockRequestDispatcher: forwarding to URL [" + this.url + "]");
67         }
68     }
69
70     public void include(ServletRequest JavaDoc request, ServletResponse JavaDoc response) {
71         Assert.notNull(request, "Request must not be null");
72         Assert.notNull(response, "Response must not be null");
73         if (!(response instanceof MockHttpServletResponse)) {
74             throw new IllegalArgumentException JavaDoc("MockRequestDispatcher requires MockHttpServletResponse");
75         }
76         ((MockHttpServletResponse) response).setIncludedUrl(this.url);
77         if (logger.isDebugEnabled()) {
78             logger.debug("MockRequestDispatcher: including URL [" + this.url + "]");
79         }
80     }
81
82 }
83
Popular Tags