11/*
2- * Copyright 2002-2010 the original author or authors.
2+ * Copyright 2002-2011 the original author or authors.
33 *
44 * Licensed under the Apache License, Version 2.0 (the "License");
55 * you may not use this file except in compliance with the License.
1717package org .springframework .mock .web ;
1818
1919import java .io .IOException ;
20+ import java .io .UnsupportedEncodingException ;
21+ import java .util .Collections ;
2022import java .util .Enumeration ;
2123import java .util .LinkedHashMap ;
2224import java .util .Map ;
23- import java .util .Vector ;
24-
2525import javax .el .ELContext ;
2626import javax .servlet .Servlet ;
2727import javax .servlet .ServletConfig ;
4141
4242/**
4343 * Mock implementation of the {@link javax.servlet.jsp.PageContext} interface.
44- * <p>
45- * Used for testing the web framework; only necessary for testing applications
46- * when testing custom JSP tags.
47- * <p>
48- * Note: Expects initialization via the constructor rather than via the
49- * <code>PageContext.initialize</code> method. Does not support writing to a
50- * JspWriter, request dispatching, and <code>handlePageException</code> calls.
51- *
44+ *
45+ * <p> Used for testing the web framework; only necessary for testing
46+ * applications when testing custom JSP tags.
47+ *
48+ * <p> Note: Expects initialization via the constructor rather than via the
49+ * <code>PageContext.initialize</code> method. Does not support writing to
50+ * a JspWriter, request dispatching, and <code>handlePageException</code> calls.
51+ *
5252 * @author Juergen Hoeller
5353 * @since 1.0.2
5454 */
55- @ SuppressWarnings ("deprecation" )
5655public class MockPageContext extends PageContext {
5756
5857 private final ServletContext servletContext ;
@@ -80,9 +79,8 @@ public MockPageContext() {
8079 /**
8180 * Create new MockPageContext with a default {@link MockHttpServletRequest},
8281 * {@link MockHttpServletResponse}, {@link MockServletConfig}.
83- *
84- * @param servletContext the ServletContext that the JSP page runs in (only
85- * necessary when actually accessing the ServletContext)
82+ * @param servletContext the ServletContext that the JSP page runs in
83+ * (only necessary when actually accessing the ServletContext)
8684 */
8785 public MockPageContext (ServletContext servletContext ) {
8886 this (servletContext , null , null , null );
@@ -91,47 +89,45 @@ public MockPageContext(ServletContext servletContext) {
9189 /**
9290 * Create new MockPageContext with a MockHttpServletResponse,
9391 * MockServletConfig.
94- *
9592 * @param servletContext the ServletContext that the JSP page runs in
96- * @param request the current HttpServletRequest (only necessary when
97- * actually accessing the request)
93+ * @param request the current HttpServletRequest
94+ * (only necessary when actually accessing the request)
9895 */
9996 public MockPageContext (ServletContext servletContext , HttpServletRequest request ) {
10097 this (servletContext , request , null , null );
10198 }
10299
103100 /**
104101 * Create new MockPageContext with a MockServletConfig.
105- *
106102 * @param servletContext the ServletContext that the JSP page runs in
107103 * @param request the current HttpServletRequest
108- * @param response the current HttpServletResponse (only necessary when
109- * actually writing to the response)
104+ * @param response the current HttpServletResponse
105+ * (only necessary when actually writing to the response)
110106 */
111107 public MockPageContext (ServletContext servletContext , HttpServletRequest request , HttpServletResponse response ) {
112108 this (servletContext , request , response , null );
113109 }
114110
115111 /**
116112 * Create new MockServletConfig.
117- *
118113 * @param servletContext the ServletContext that the JSP page runs in
119114 * @param request the current HttpServletRequest
120115 * @param response the current HttpServletResponse
121- * @param servletConfig the ServletConfig (hardly ever accessed from within
122- * a tag)
116+ * @param servletConfig the ServletConfig (hardly ever accessed from within a tag)
123117 */
124- public MockPageContext (ServletContext servletContext , HttpServletRequest request , HttpServletResponse response ,
125- ServletConfig servletConfig ) {
118+ public MockPageContext (ServletContext servletContext , HttpServletRequest request ,
119+ HttpServletResponse response , ServletConfig servletConfig ) {
126120
127121 this .servletContext = (servletContext != null ? servletContext : new MockServletContext ());
128122 this .request = (request != null ? request : new MockHttpServletRequest (servletContext ));
129123 this .response = (response != null ? response : new MockHttpServletResponse ());
130124 this .servletConfig = (servletConfig != null ? servletConfig : new MockServletConfig (servletContext ));
131125 }
132126
133- public void initialize (Servlet servlet , ServletRequest request , ServletResponse response , String errorPageURL ,
134- boolean needsSession , int bufferSize , boolean autoFlush ) {
127+
128+ public void initialize (
129+ Servlet servlet , ServletRequest request , ServletResponse response ,
130+ String errorPageURL , boolean needsSession , int bufferSize , boolean autoFlush ) {
135131
136132 throw new UnsupportedOperationException ("Use appropriate constructor" );
137133 }
@@ -252,7 +248,7 @@ else if (getAttribute(name, APPLICATION_SCOPE) != null) {
252248 }
253249
254250 public Enumeration <String > getAttributeNames () {
255- return new Vector < String > (this .attributes .keySet ()). elements ( );
251+ return Collections . enumeration (this .attributes .keySet ());
256252 }
257253
258254 @ SuppressWarnings ("unchecked" )
@@ -319,24 +315,37 @@ public ServletContext getServletContext() {
319315 return this .servletContext ;
320316 }
321317
322- public void forward (String url ) throws ServletException , IOException {
323- throw new UnsupportedOperationException ("forward" );
318+ public void forward (String path ) throws ServletException , IOException {
319+ this .request .getRequestDispatcher (path ).forward (this .request , this .response );
320+ }
321+
322+ public void include (String path ) throws ServletException , IOException {
323+ this .request .getRequestDispatcher (path ).include (this .request , this .response );
324+ }
325+
326+ public void include (String path , boolean flush ) throws ServletException , IOException {
327+ this .request .getRequestDispatcher (path ).include (this .request , this .response );
328+ if (flush ) {
329+ this .response .flushBuffer ();
330+ }
324331 }
325332
326- public void include (String url ) throws ServletException , IOException {
327- throw new UnsupportedOperationException ("include" );
333+ public byte [] getContentAsByteArray () {
334+ Assert .isTrue (this .response instanceof MockHttpServletResponse );
335+ return ((MockHttpServletResponse ) this .response ).getContentAsByteArray ();
328336 }
329337
330- public void include (String url , boolean flush ) throws ServletException , IOException {
331- throw new UnsupportedOperationException ("include" );
338+ public String getContentAsString () throws UnsupportedEncodingException {
339+ Assert .isTrue (this .response instanceof MockHttpServletResponse );
340+ return ((MockHttpServletResponse ) this .response ).getContentAsString ();
332341 }
333342
334343 public void handlePageException (Exception ex ) throws ServletException , IOException {
335- throw new UnsupportedOperationException ( "handlePageException" );
344+ throw new ServletException ( "Page exception" , ex );
336345 }
337346
338347 public void handlePageException (Throwable ex ) throws ServletException , IOException {
339- throw new UnsupportedOperationException ( "handlePageException" );
348+ throw new ServletException ( "Page exception" , ex );
340349 }
341350
342351}
0 commit comments