001 package com.mockrunner.mock.web;
002
003 import java.io.IOException;
004 import java.io.Writer;
005 import java.util.Enumeration;
006 import java.util.HashMap;
007 import java.util.Iterator;
008 import java.util.NoSuchElementException;
009 import java.util.Stack;
010
011 import javax.servlet.RequestDispatcher;
012 import javax.servlet.Servlet;
013 import javax.servlet.ServletConfig;
014 import javax.servlet.ServletContext;
015 import javax.servlet.ServletException;
016 import javax.servlet.ServletRequest;
017 import javax.servlet.ServletResponse;
018 import javax.servlet.http.HttpServletRequest;
019 import javax.servlet.http.HttpSession;
020 import javax.servlet.jsp.JspWriter;
021 import javax.servlet.jsp.PageContext;
022 import javax.servlet.jsp.el.ExpressionEvaluator;
023 import javax.servlet.jsp.el.VariableResolver;
024 import javax.servlet.jsp.tagext.BodyContent;
025
026 /**
027 * Mock implementation of <code>PageContext</code>.
028 */
029 //Some methods of this class were copied from org.apache.struts.mock.MockPageContext
030 //and modified
031 public class MockPageContext extends PageContext
032 {
033 protected ServletConfig config;
034 protected ServletRequest request;
035 protected ServletResponse response;
036 private JspWriter jspWriter;
037 private Stack outStack;
038 private Exception exception;
039 private Object page;
040 private HashMap attributes;
041 private ExpressionEvaluator evaluator;
042 private VariableResolver resolver;
043
044 public MockPageContext()
045 {
046 this(null, null, null);
047 }
048
049 public MockPageContext(ServletConfig config, ServletRequest request, ServletResponse response)
050 {
051 this.config = config;
052 this.request = request;
053 this.response = response;
054 jspWriter = new MockJspWriter();
055 outStack = new Stack();
056 attributes = new HashMap();
057 }
058
059 /**
060 * This method allows to set custom implementations
061 * of <code>JspWriter</code>. Per default, {@link MockJspWriter}
062 * is used.
063 * @param jspWriter the <code>JspWriter</code>
064 */
065 public void setJspWriter(JspWriter jspWriter)
066 {
067 this.jspWriter = jspWriter;
068 }
069
070 public void setPage(Object page)
071 {
072 this.page = page;
073 }
074
075 public void setServletConfig(ServletConfig config)
076 {
077 this.config = config;
078 }
079
080 public void setServletRequest(ServletRequest request)
081 {
082 this.request = request;
083 }
084
085 public void setServletResponse(ServletResponse response)
086 {
087 this.response = response;
088 }
089
090 public void setException(Exception exception)
091 {
092 this.exception = exception;
093 }
094
095 public Object findAttribute(String name)
096 {
097 Object value = getAttribute(name, PageContext.PAGE_SCOPE);
098 if(value == null)
099 {
100 value = getAttribute(name, PageContext.REQUEST_SCOPE);
101 }
102 if(value == null)
103 {
104 value = getAttribute(name, PageContext.SESSION_SCOPE);
105 }
106 if(value == null)
107 {
108 value = getAttribute(name, PageContext.APPLICATION_SCOPE);
109 }
110 return value;
111 }
112
113 public Object getAttribute(String name)
114 {
115 return getAttribute(name, PageContext.PAGE_SCOPE);
116 }
117
118 public Object getAttribute(String name, int scope)
119 {
120 if(scope == PageContext.PAGE_SCOPE)
121 {
122 return attributes.get(name);
123 }
124 else if(scope == PageContext.REQUEST_SCOPE)
125 {
126 if(null == request) return null;
127 return request.getAttribute(name);
128 }
129 else if(scope == PageContext.SESSION_SCOPE)
130 {
131 if(null == getSession()) return null;
132 return getSession().getAttribute(name);
133 }
134 else if(scope == PageContext.APPLICATION_SCOPE)
135 {
136 if(null == getServletContext()) return null;
137 return getServletContext().getAttribute(name);
138 }
139 else
140 {
141 throw new IllegalArgumentException("Invalid scope " + scope);
142 }
143 }
144
145 public void removeAttribute(String name)
146 {
147 int scope = getAttributesScope(name);
148 if (scope != 0)
149 {
150 removeAttribute(name, scope);
151 }
152 }
153
154 public void removeAttribute(String name, int scope)
155 {
156 if(scope == PageContext.PAGE_SCOPE)
157 {
158 attributes.remove(name);
159 }
160 else if(scope == PageContext.REQUEST_SCOPE)
161 {
162 if(request != null)
163 {
164 request.removeAttribute(name);
165 }
166 }
167 else if(scope == PageContext.SESSION_SCOPE)
168 {
169 if(getSession() != null)
170 {
171 getSession().removeAttribute(name);
172 }
173 }
174 else if(scope == PageContext.APPLICATION_SCOPE)
175 {
176 if(getServletContext() != null)
177 {
178 getServletContext().removeAttribute(name);
179 }
180 }
181 else
182 {
183 throw new IllegalArgumentException("Invalid scope " + scope);
184 }
185 }
186
187 public void setAttribute(String name, Object value)
188 {
189 setAttribute(name, value, PageContext.PAGE_SCOPE);
190 }
191
192
193 public void setAttribute(String name, Object value, int scope)
194 {
195 if(scope == PageContext.PAGE_SCOPE)
196 {
197 attributes.put(name, value);
198 }
199 else if(scope == PageContext.REQUEST_SCOPE)
200 {
201 if(request != null)
202 {
203 request.setAttribute(name, value);
204 }
205 }
206 else if(scope == PageContext.SESSION_SCOPE)
207 {
208 if(getSession() != null)
209 {
210 getSession().setAttribute(name, value);
211 }
212 }
213 else if(scope == PageContext.APPLICATION_SCOPE)
214 {
215 if(getServletContext() != null)
216 {
217 getServletContext().setAttribute(name, value);
218 }
219 }
220 else
221 {
222 throw new IllegalArgumentException("Invalid scope " + scope);
223 }
224 }
225
226 public int getAttributesScope(String name)
227 {
228 if(getAttribute(name, PageContext.PAGE_SCOPE) != null)
229 {
230 return PageContext.PAGE_SCOPE;
231 }
232 else if(getAttribute(name, PageContext.REQUEST_SCOPE) != null)
233 {
234 return PageContext.REQUEST_SCOPE;
235 }
236 else if(getAttribute(name, PageContext.SESSION_SCOPE) != null)
237 {
238 return PageContext.SESSION_SCOPE;
239 }
240 else if(getAttribute(name, PageContext.APPLICATION_SCOPE) != null)
241 {
242 return PageContext.APPLICATION_SCOPE;
243 }
244 return 0;
245 }
246
247 public Enumeration getAttributeNamesInScope(int scope)
248 {
249 if(scope == PageContext.PAGE_SCOPE)
250 {
251 return new WrappedEnumeration(attributes.keySet().iterator());
252 }
253 else if(scope == PageContext.REQUEST_SCOPE)
254 {
255 if(request == null) return new NullEnumeration();
256 return request.getAttributeNames();
257 }
258 else if(scope == PageContext.SESSION_SCOPE)
259 {
260 if(getSession() == null) return new NullEnumeration();
261 return getSession().getAttributeNames();
262 }
263 else if(scope == PageContext.APPLICATION_SCOPE)
264 {
265 if(getServletContext() == null) return new NullEnumeration();
266 return getServletContext().getAttributeNames();
267 }
268 else
269 {
270 throw new IllegalArgumentException("Invalid scope " + scope);
271 }
272 }
273
274 public JspWriter getOut()
275 {
276 return jspWriter;
277 }
278
279 public Exception getException()
280 {
281 return exception;
282 }
283
284 public Object getPage()
285 {
286 return page;
287 }
288
289 public ServletRequest getRequest()
290 {
291 return request;
292 }
293
294 public ServletResponse getResponse()
295 {
296 return response;
297 }
298
299 public ServletConfig getServletConfig()
300 {
301 return config;
302 }
303
304 public ServletContext getServletContext()
305 {
306 if(null == config) return null;
307 return config.getServletContext();
308 }
309
310
311 public HttpSession getSession()
312 {
313 if(null == request) return null;
314 return ((HttpServletRequest)request).getSession();
315 }
316
317 public void handlePageException(Exception exc)
318 {
319
320 }
321
322 public void handlePageException(Throwable thr)
323 {
324
325 }
326
327 public void forward(String path) throws ServletException, IOException
328 {
329 if(null != request)
330 {
331 RequestDispatcher dispatcher = request.getRequestDispatcher(path);
332 if(null != dispatcher)
333 {
334 dispatcher.forward(request, response);
335 }
336 }
337 }
338
339 public void include(String path) throws ServletException, IOException
340 {
341 if(null != request)
342 {
343 RequestDispatcher dispatcher = request.getRequestDispatcher(path);
344 if(null != dispatcher)
345 {
346 dispatcher.include(request, response);
347 }
348 }
349 }
350
351 public void include(String path, boolean flush) throws ServletException, IOException
352 {
353 if(flush)
354 {
355 jspWriter.flush();
356 }
357 include(path);
358 }
359
360 public void initialize(Servlet servlet, ServletRequest request,
361 ServletResponse response, String errorPageURL,
362 boolean needsSession, int bufferSize,
363 boolean autoFlush)
364 {
365 this.config = servlet.getServletConfig();
366 this.request = request;
367 this.response = response;
368 jspWriter = new MockJspWriter();
369 outStack = new Stack();
370 attributes = new HashMap();
371 }
372
373 public JspWriter popBody()
374 {
375 jspWriter = (JspWriter)outStack.pop();
376 return jspWriter;
377 }
378
379 public BodyContent pushBody()
380 {
381 outStack.push(jspWriter);
382 jspWriter = new MockBodyContent(jspWriter);
383 return (BodyContent)jspWriter;
384 }
385
386 public JspWriter pushBody(Writer writer)
387 {
388 outStack.push(jspWriter);
389 jspWriter = new MockJspWriter(writer);
390 return jspWriter;
391 }
392
393 public void release()
394 {
395 jspWriter = new MockJspWriter();
396 outStack = new Stack();
397 }
398
399 public void setExpressionEvaluator(ExpressionEvaluator evaluator)
400 {
401 this.evaluator = evaluator;
402 }
403
404 public void setVariableResolver(VariableResolver resolver)
405 {
406 this.resolver = resolver;
407 }
408
409 public ExpressionEvaluator getExpressionEvaluator()
410 {
411 return evaluator;
412 }
413
414 public VariableResolver getVariableResolver()
415 {
416 return resolver;
417 }
418
419 private class NullEnumeration implements Enumeration
420 {
421 public boolean hasMoreElements()
422 {
423 return false;
424 }
425
426 public Object nextElement()
427 {
428 throw new NoSuchElementException();
429 }
430 }
431
432 private class WrappedEnumeration implements Enumeration
433 {
434 private Iterator iterator;
435
436 public WrappedEnumeration(Iterator iterator)
437 {
438 this.iterator = iterator;
439 }
440
441 public boolean hasMoreElements()
442 {
443 return iterator.hasNext();
444 }
445
446 public Object nextElement()
447 {
448 return iterator.next();
449 }
450 }
451 }