001: /*
002: * Copyright 2002-2006 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.springframework.mock.web;
018:
019: import java.io.IOException;
020: import java.util.Enumeration;
021: import java.util.Hashtable;
022:
023: import javax.servlet.Servlet;
024: import javax.servlet.ServletConfig;
025: import javax.servlet.ServletContext;
026: import javax.servlet.ServletException;
027: import javax.servlet.ServletRequest;
028: import javax.servlet.ServletResponse;
029: import javax.servlet.http.HttpServletRequest;
030: import javax.servlet.http.HttpServletResponse;
031: import javax.servlet.http.HttpSession;
032: import javax.servlet.jsp.JspWriter;
033: import javax.servlet.jsp.PageContext;
034: import javax.servlet.jsp.el.ExpressionEvaluator;
035: import javax.servlet.jsp.el.VariableResolver;
036:
037: import org.springframework.util.Assert;
038:
039: /**
040: * Mock implementation of the {@link javax.servlet.jsp.PageContext} interface.
041: *
042: * <p>Used for testing the web framework; only necessary for testing
043: * applications when testing custom JSP tags.
044: *
045: * <p>Note: Expects initialization via the constructor rather than via the
046: * <code>PageContext.initialize</code> method. Does not support writing to
047: * a JspWriter, request dispatching, and <code>handlePageException</code> calls.
048: *
049: * @author Juergen Hoeller
050: * @since 1.0.2
051: */
052: public class MockPageContext extends PageContext {
053:
054: private final ServletContext servletContext;
055:
056: private final HttpServletRequest request;
057:
058: private final HttpServletResponse response;
059:
060: private final ServletConfig servletConfig;
061:
062: private final Hashtable attributes = new Hashtable();
063:
064: /**
065: * Create new MockPageContext with a default {@link MockServletContext},
066: * {@link MockHttpServletRequest}, {@link MockHttpServletResponse},
067: * {@link MockServletConfig}.
068: */
069: public MockPageContext() {
070: this (null, null, null, null);
071: }
072:
073: /**
074: * Create new MockPageContext with a default {@link MockHttpServletRequest},
075: * {@link MockHttpServletResponse}, {@link MockServletConfig}.
076: * @param servletContext the ServletContext that the JSP page runs in
077: * (only necessary when actually accessing the ServletContext)
078: */
079: public MockPageContext(ServletContext servletContext) {
080: this (servletContext, null, null, null);
081: }
082:
083: /**
084: * Create new MockPageContext with a MockHttpServletResponse,
085: * MockServletConfig.
086: * @param servletContext the ServletContext that the JSP page runs in
087: * @param request the current HttpServletRequest
088: * (only necessary when actually accessing the request)
089: */
090: public MockPageContext(ServletContext servletContext,
091: HttpServletRequest request) {
092: this (servletContext, request, null, null);
093: }
094:
095: /**
096: * Create new MockPageContext with a MockServletConfig.
097: * @param servletContext the ServletContext that the JSP page runs in
098: * @param request the current HttpServletRequest
099: * @param response the current HttpServletResponse
100: * (only necessary when actually writing to the response)
101: */
102: public MockPageContext(ServletContext servletContext,
103: HttpServletRequest request, HttpServletResponse response) {
104: this (servletContext, request, response, null);
105: }
106:
107: /**
108: * Create new MockServletConfig.
109: * @param servletContext the ServletContext that the JSP page runs in
110: * @param request the current HttpServletRequest
111: * @param response the current HttpServletResponse
112: * @param servletConfig the ServletConfig (hardly ever accessed from within a tag)
113: */
114: public MockPageContext(ServletContext servletContext,
115: HttpServletRequest request, HttpServletResponse response,
116: ServletConfig servletConfig) {
117:
118: this .servletContext = (servletContext != null ? servletContext
119: : new MockServletContext());
120: this .request = (request != null ? request
121: : new MockHttpServletRequest(servletContext));
122: this .response = (response != null ? response
123: : new MockHttpServletResponse());
124: this .servletConfig = (servletConfig != null ? servletConfig
125: : new MockServletConfig(servletContext));
126: }
127:
128: public void initialize(Servlet servlet, ServletRequest request,
129: ServletResponse response, String errorPageURL,
130: boolean needsSession, int bufferSize, boolean autoFlush) {
131:
132: throw new UnsupportedOperationException(
133: "Use appropriate constructor");
134: }
135:
136: public void release() {
137: }
138:
139: public void setAttribute(String name, Object value) {
140: Assert.notNull(name, "Attribute name must not be null");
141: if (value != null) {
142: this .attributes.put(name, value);
143: } else {
144: this .attributes.remove(name);
145: }
146: }
147:
148: public void setAttribute(String name, Object value, int scope) {
149: Assert.notNull(name, "Attribute name must not be null");
150: switch (scope) {
151: case PAGE_SCOPE:
152: setAttribute(name, value);
153: break;
154: case REQUEST_SCOPE:
155: this .request.setAttribute(name, value);
156: break;
157: case SESSION_SCOPE:
158: this .request.getSession().setAttribute(name, value);
159: break;
160: case APPLICATION_SCOPE:
161: this .servletContext.setAttribute(name, value);
162: break;
163: default:
164: throw new IllegalArgumentException("Invalid scope: "
165: + scope);
166: }
167: }
168:
169: public Object getAttribute(String name) {
170: Assert.notNull(name, "Attribute name must not be null");
171: return this .attributes.get(name);
172: }
173:
174: public Object getAttribute(String name, int scope) {
175: Assert.notNull(name, "Attribute name must not be null");
176: switch (scope) {
177: case PAGE_SCOPE:
178: return getAttribute(name);
179: case REQUEST_SCOPE:
180: return this .request.getAttribute(name);
181: case SESSION_SCOPE:
182: HttpSession session = this .request.getSession(false);
183: return (session != null ? session.getAttribute(name) : null);
184: case APPLICATION_SCOPE:
185: return this .servletContext.getAttribute(name);
186: default:
187: throw new IllegalArgumentException("Invalid scope: "
188: + scope);
189: }
190: }
191:
192: public Object findAttribute(String name) {
193: Object value = getAttribute(name);
194: if (value == null) {
195: value = getAttribute(name, REQUEST_SCOPE);
196: if (value == null) {
197: value = getAttribute(name, SESSION_SCOPE);
198: if (value == null) {
199: value = getAttribute(name, APPLICATION_SCOPE);
200: }
201: }
202: }
203: return value;
204: }
205:
206: public void removeAttribute(String name) {
207: Assert.notNull(name, "Attribute name must not be null");
208: this .removeAttribute(name, PageContext.PAGE_SCOPE);
209: this .removeAttribute(name, PageContext.REQUEST_SCOPE);
210: this .removeAttribute(name, PageContext.SESSION_SCOPE);
211: this .removeAttribute(name, PageContext.APPLICATION_SCOPE);
212: }
213:
214: public void removeAttribute(String name, int scope) {
215: Assert.notNull(name, "Attribute name must not be null");
216: switch (scope) {
217: case PAGE_SCOPE:
218: this .attributes.remove(name);
219: break;
220: case REQUEST_SCOPE:
221: this .request.removeAttribute(name);
222: break;
223: case SESSION_SCOPE:
224: this .request.getSession().removeAttribute(name);
225: break;
226: case APPLICATION_SCOPE:
227: this .servletContext.removeAttribute(name);
228: break;
229: default:
230: throw new IllegalArgumentException("Invalid scope: "
231: + scope);
232: }
233: }
234:
235: public int getAttributesScope(String name) {
236: if (getAttribute(name) != null) {
237: return PAGE_SCOPE;
238: } else if (getAttribute(name, REQUEST_SCOPE) != null) {
239: return REQUEST_SCOPE;
240: } else if (getAttribute(name, SESSION_SCOPE) != null) {
241: return SESSION_SCOPE;
242: } else if (getAttribute(name, APPLICATION_SCOPE) != null) {
243: return APPLICATION_SCOPE;
244: } else {
245: return 0;
246: }
247: }
248:
249: public Enumeration getAttributeNames() {
250: return this .attributes.keys();
251: }
252:
253: public Enumeration getAttributeNamesInScope(int scope) {
254: switch (scope) {
255: case PAGE_SCOPE:
256: return getAttributeNames();
257: case REQUEST_SCOPE:
258: return this .request.getAttributeNames();
259: case SESSION_SCOPE:
260: HttpSession session = this .request.getSession(false);
261: return (session != null ? session.getAttributeNames()
262: : null);
263: case APPLICATION_SCOPE:
264: return this .servletContext.getAttributeNames();
265: default:
266: throw new IllegalArgumentException("Invalid scope: "
267: + scope);
268: }
269: }
270:
271: public JspWriter getOut() {
272: throw new UnsupportedOperationException("getOut");
273: }
274:
275: public ExpressionEvaluator getExpressionEvaluator() {
276: return new MockExpressionEvaluator(this );
277: }
278:
279: public VariableResolver getVariableResolver() {
280: return null;
281: }
282:
283: public HttpSession getSession() {
284: return this .request.getSession();
285: }
286:
287: public Object getPage() {
288: throw new UnsupportedOperationException("getPage");
289: }
290:
291: public ServletRequest getRequest() {
292: return request;
293: }
294:
295: public ServletResponse getResponse() {
296: return response;
297: }
298:
299: public Exception getException() {
300: throw new UnsupportedOperationException("getException");
301: }
302:
303: public ServletConfig getServletConfig() {
304: return servletConfig;
305: }
306:
307: public ServletContext getServletContext() {
308: return servletContext;
309: }
310:
311: public void forward(String url) throws ServletException,
312: IOException {
313: throw new UnsupportedOperationException("forward");
314: }
315:
316: public void include(String url) throws ServletException,
317: IOException {
318: throw new UnsupportedOperationException("include");
319: }
320:
321: public void include(String url, boolean flush)
322: throws ServletException, IOException {
323: throw new UnsupportedOperationException("include");
324: }
325:
326: public void handlePageException(Exception ex)
327: throws ServletException, IOException {
328: throw new UnsupportedOperationException("handlePageException");
329: }
330:
331: public void handlePageException(Throwable ex)
332: throws ServletException, IOException {
333: throw new UnsupportedOperationException("handlePageException");
334: }
335:
336: }
|