KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.io.IOException JavaDoc;
20 import java.util.Enumeration JavaDoc;
21 import java.util.Hashtable JavaDoc;
22
23 import javax.servlet.Servlet JavaDoc;
24 import javax.servlet.ServletConfig JavaDoc;
25 import javax.servlet.ServletContext JavaDoc;
26 import javax.servlet.ServletException JavaDoc;
27 import javax.servlet.ServletRequest JavaDoc;
28 import javax.servlet.ServletResponse JavaDoc;
29 import javax.servlet.http.HttpServletRequest JavaDoc;
30 import javax.servlet.http.HttpServletResponse JavaDoc;
31 import javax.servlet.http.HttpSession JavaDoc;
32 import javax.servlet.jsp.JspWriter JavaDoc;
33 import javax.servlet.jsp.PageContext JavaDoc;
34 import javax.servlet.jsp.el.ExpressionEvaluator JavaDoc;
35 import javax.servlet.jsp.el.VariableResolver JavaDoc;
36
37 import org.springframework.util.Assert;
38
39 /**
40  * Mock implementation of the {@link javax.servlet.jsp.PageContext} interface.
41  *
42  * <p>Used for testing the web framework; only necessary for testing
43  * applications when testing custom JSP tags.
44  *
45  * <p>Note: Expects initialization via the constructor rather than via the
46  * <code>PageContext.initialize</code> method. Does not support writing to
47  * a JspWriter, request dispatching, and <code>handlePageException</code> calls.
48  *
49  * @author Juergen Hoeller
50  * @since 1.0.2
51  */

52 public class MockPageContext extends PageContext JavaDoc {
53
54     private final ServletContext JavaDoc servletContext;
55
56     private final HttpServletRequest JavaDoc request;
57
58     private final HttpServletResponse JavaDoc response;
59
60     private final ServletConfig JavaDoc servletConfig;
61
62     private final Hashtable JavaDoc attributes = new Hashtable JavaDoc();
63
64
65     /**
66      * Create new MockPageContext with a default {@link MockServletContext},
67      * {@link MockHttpServletRequest}, {@link MockHttpServletResponse},
68      * {@link MockServletConfig}.
69      */

70     public MockPageContext() {
71         this(null, null, null, null);
72     }
73
74     /**
75      * Create new MockPageContext with a default {@link MockHttpServletRequest},
76      * {@link MockHttpServletResponse}, {@link MockServletConfig}.
77      * @param servletContext the ServletContext that the JSP page runs in
78      * (only necessary when actually accessing the ServletContext)
79      */

80     public MockPageContext(ServletContext JavaDoc servletContext) {
81         this(servletContext, null, null, null);
82     }
83
84     /**
85      * Create new MockPageContext with a MockHttpServletResponse,
86      * MockServletConfig.
87      * @param servletContext the ServletContext that the JSP page runs in
88      * @param request the current HttpServletRequest
89      * (only necessary when actually accessing the request)
90      */

91     public MockPageContext(ServletContext JavaDoc servletContext, HttpServletRequest JavaDoc request) {
92         this(servletContext, request, null, null);
93     }
94
95     /**
96      * Create new MockPageContext with a MockServletConfig.
97      * @param servletContext the ServletContext that the JSP page runs in
98      * @param request the current HttpServletRequest
99      * @param response the current HttpServletResponse
100      * (only necessary when actually writing to the response)
101      */

102     public MockPageContext(ServletContext JavaDoc servletContext, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) {
103         this(servletContext, request, response, null);
104     }
105
106     /**
107      * Create new MockServletConfig.
108      * @param servletContext the ServletContext that the JSP page runs in
109      * @param request the current HttpServletRequest
110      * @param response the current HttpServletResponse
111      * @param servletConfig the ServletConfig (hardly ever accessed from within a tag)
112      */

113     public MockPageContext(ServletContext JavaDoc servletContext, HttpServletRequest JavaDoc request,
114             HttpServletResponse JavaDoc response, ServletConfig JavaDoc servletConfig) {
115
116         this.servletContext = (servletContext != null ? servletContext : new MockServletContext());
117         this.request = (request != null ? request : new MockHttpServletRequest(servletContext));
118         this.response = (response != null ? response : new MockHttpServletResponse());
119         this.servletConfig = (servletConfig != null ? servletConfig : new MockServletConfig(servletContext));
120     }
121
122
123     public void initialize(
124             Servlet JavaDoc servlet, ServletRequest JavaDoc request, ServletResponse JavaDoc response,
125             String JavaDoc errorPageURL, boolean needsSession, int bufferSize, boolean autoFlush) {
126
127         throw new UnsupportedOperationException JavaDoc("Use appropriate constructor");
128     }
129
130     public void release() {
131     }
132
133     public void setAttribute(String JavaDoc name, Object JavaDoc value) {
134         Assert.notNull(name, "Attribute name must not be null");
135         if (value != null) {
136             this.attributes.put(name, value);
137         }
138         else {
139             this.attributes.remove(name);
140         }
141     }
142
143     public void setAttribute(String JavaDoc name, Object JavaDoc value, int scope) {
144         Assert.notNull(name, "Attribute name must not be null");
145         switch (scope) {
146             case PAGE_SCOPE:
147                 setAttribute(name, value);
148                 break;
149             case REQUEST_SCOPE:
150                 this.request.setAttribute(name, value);
151                 break;
152             case SESSION_SCOPE:
153                 this.request.getSession().setAttribute(name, value);
154                 break;
155             case APPLICATION_SCOPE:
156                 this.servletContext.setAttribute(name, value);
157                 break;
158             default:
159                 throw new IllegalArgumentException JavaDoc("Invalid scope: " + scope);
160         }
161     }
162
163     public Object JavaDoc getAttribute(String JavaDoc name) {
164         Assert.notNull(name, "Attribute name must not be null");
165         return this.attributes.get(name);
166     }
167
168     public Object JavaDoc getAttribute(String JavaDoc name, int scope) {
169         Assert.notNull(name, "Attribute name must not be null");
170         switch (scope) {
171             case PAGE_SCOPE:
172                 return getAttribute(name);
173             case REQUEST_SCOPE:
174                 return this.request.getAttribute(name);
175             case SESSION_SCOPE:
176                 HttpSession JavaDoc session = this.request.getSession(false);
177                 return (session != null ? session.getAttribute(name) : null);
178             case APPLICATION_SCOPE:
179                 return this.servletContext.getAttribute(name);
180             default:
181                 throw new IllegalArgumentException JavaDoc("Invalid scope: " + scope);
182         }
183     }
184
185     public Object JavaDoc findAttribute(String JavaDoc name) {
186         Object JavaDoc value = getAttribute(name);
187         if (value == null) {
188             value = getAttribute(name, REQUEST_SCOPE);
189             if (value == null) {
190                 value = getAttribute(name, SESSION_SCOPE);
191                 if (value == null) {
192                     value = getAttribute(name, APPLICATION_SCOPE);
193                 }
194             }
195         }
196         return value;
197     }
198
199     public void removeAttribute(String JavaDoc name) {
200         Assert.notNull(name, "Attribute name must not be null");
201         this.removeAttribute(name, PageContext.PAGE_SCOPE);
202         this.removeAttribute(name, PageContext.REQUEST_SCOPE);
203         this.removeAttribute(name, PageContext.SESSION_SCOPE);
204         this.removeAttribute(name, PageContext.APPLICATION_SCOPE);
205     }
206
207     public void removeAttribute(String JavaDoc name, int scope) {
208         Assert.notNull(name, "Attribute name must not be null");
209         switch (scope) {
210             case PAGE_SCOPE:
211                 this.attributes.remove(name);
212                 break;
213             case REQUEST_SCOPE:
214                 this.request.removeAttribute(name);
215                 break;
216             case SESSION_SCOPE:
217                 this.request.getSession().removeAttribute(name);
218                 break;
219             case APPLICATION_SCOPE:
220                 this.servletContext.removeAttribute(name);
221                 break;
222             default:
223                 throw new IllegalArgumentException JavaDoc("Invalid scope: " + scope);
224         }
225     }
226
227     public int getAttributesScope(String JavaDoc name) {
228         if (getAttribute(name) != null) {
229             return PAGE_SCOPE;
230         }
231         else if (getAttribute(name, REQUEST_SCOPE) != null) {
232             return REQUEST_SCOPE;
233         }
234         else if (getAttribute(name, SESSION_SCOPE) != null) {
235             return SESSION_SCOPE;
236         }
237         else if (getAttribute(name, APPLICATION_SCOPE) != null) {
238             return APPLICATION_SCOPE;
239         }
240         else {
241             return 0;
242         }
243     }
244
245     public Enumeration JavaDoc getAttributeNames() {
246         return this.attributes.keys();
247     }
248
249     public Enumeration JavaDoc getAttributeNamesInScope(int scope) {
250         switch (scope) {
251             case PAGE_SCOPE:
252                 return getAttributeNames();
253             case REQUEST_SCOPE:
254                 return this.request.getAttributeNames();
255             case SESSION_SCOPE:
256                 HttpSession JavaDoc session = this.request.getSession(false);
257                 return (session != null ? session.getAttributeNames() : null);
258             case APPLICATION_SCOPE:
259                 return this.servletContext.getAttributeNames();
260             default:
261                 throw new IllegalArgumentException JavaDoc("Invalid scope: " + scope);
262         }
263     }
264
265     public JspWriter JavaDoc getOut() {
266         throw new UnsupportedOperationException JavaDoc("getOut");
267     }
268
269     public ExpressionEvaluator JavaDoc getExpressionEvaluator() {
270         return new MockExpressionEvaluator(this);
271     }
272
273     public VariableResolver JavaDoc getVariableResolver() {
274         return null;
275     }
276
277     public HttpSession JavaDoc getSession() {
278         return this.request.getSession();
279     }
280
281     public Object JavaDoc getPage() {
282         throw new UnsupportedOperationException JavaDoc("getPage");
283     }
284
285     public ServletRequest JavaDoc getRequest() {
286         return request;
287     }
288
289     public ServletResponse JavaDoc getResponse() {
290         return response;
291     }
292
293     public Exception JavaDoc getException() {
294         throw new UnsupportedOperationException JavaDoc("getException");
295     }
296
297     public ServletConfig JavaDoc getServletConfig() {
298         return servletConfig;
299     }
300
301     public ServletContext JavaDoc getServletContext() {
302         return servletContext;
303     }
304
305     public void forward(String JavaDoc url) throws ServletException JavaDoc, IOException JavaDoc {
306         throw new UnsupportedOperationException JavaDoc("forward");
307     }
308
309     public void include(String JavaDoc url) throws ServletException JavaDoc, IOException JavaDoc {
310         throw new UnsupportedOperationException JavaDoc("include");
311     }
312
313     public void include(String JavaDoc url, boolean flush) throws ServletException JavaDoc, IOException JavaDoc {
314         throw new UnsupportedOperationException JavaDoc("include");
315     }
316
317     public void handlePageException(Exception JavaDoc ex) throws ServletException JavaDoc, IOException JavaDoc {
318         throw new UnsupportedOperationException JavaDoc("handlePageException");
319     }
320
321     public void handlePageException(Throwable JavaDoc ex) throws ServletException JavaDoc, IOException JavaDoc {
322         throw new UnsupportedOperationException JavaDoc("handlePageException");
323     }
324
325 }
326
Popular Tags