Skip to content

Commit 8745024

Browse files
committed
added getContentAsByteArray() and getContentAsString() methods (SPR-8575); actually implemented forward, include and handlePageException methods
1 parent df1f3d4 commit 8745024

3 files changed

Lines changed: 91 additions & 54 deletions

File tree

‎org.springframework.test/src/main/java/org/springframework/mock/web/MockPageContext.java‎

Lines changed: 46 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
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.
@@ -17,11 +17,11 @@
1717
package org.springframework.mock.web;
1818

1919
import java.io.IOException;
20+
import java.io.UnsupportedEncodingException;
21+
import java.util.Collections;
2022
import java.util.Enumeration;
2123
import java.util.LinkedHashMap;
2224
import java.util.Map;
23-
import java.util.Vector;
24-
2525
import javax.el.ELContext;
2626
import javax.servlet.Servlet;
2727
import javax.servlet.ServletConfig;
@@ -41,18 +41,17 @@
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")
5655
public 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
}

‎org.springframework.web.servlet/src/test/java/org/springframework/mock/web/MockPageContext.java‎

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package org.springframework.mock.web;
1818

1919
import java.io.IOException;
20+
import java.io.UnsupportedEncodingException;
2021
import java.util.Collections;
2122
import java.util.Enumeration;
2223
import java.util.LinkedHashMap;
@@ -333,24 +334,37 @@ public ServletContext getServletContext() {
333334
return this.servletContext;
334335
}
335336

336-
public void forward(String url) throws ServletException, IOException {
337-
throw new UnsupportedOperationException("forward");
337+
public void forward(String path) throws ServletException, IOException {
338+
this.request.getRequestDispatcher(path).forward(this.request, this.response);
338339
}
339340

340-
public void include(String url) throws ServletException, IOException {
341-
throw new UnsupportedOperationException("include");
341+
public void include(String path) throws ServletException, IOException {
342+
this.request.getRequestDispatcher(path).include(this.request, this.response);
342343
}
343344

344-
public void include(String url, boolean flush) throws ServletException, IOException {
345-
throw new UnsupportedOperationException("include");
345+
public void include(String path, boolean flush) throws ServletException, IOException {
346+
this.request.getRequestDispatcher(path).include(this.request, this.response);
347+
if (flush) {
348+
this.response.flushBuffer();
349+
}
350+
}
351+
352+
public byte[] getContentAsByteArray() {
353+
Assert.isTrue(this.response instanceof MockHttpServletResponse);
354+
return ((MockHttpServletResponse) this.response).getContentAsByteArray();
355+
}
356+
357+
public String getContentAsString() throws UnsupportedEncodingException {
358+
Assert.isTrue(this.response instanceof MockHttpServletResponse);
359+
return ((MockHttpServletResponse) this.response).getContentAsString();
346360
}
347361

348362
public void handlePageException(Exception ex) throws ServletException, IOException {
349-
throw new UnsupportedOperationException("handlePageException");
363+
throw new ServletException("Page exception", ex);
350364
}
351365

352366
public void handlePageException(Throwable ex) throws ServletException, IOException {
353-
throw new UnsupportedOperationException("handlePageException");
367+
throw new ServletException("Page exception", ex);
354368
}
355369

356370
}

‎org.springframework.web/src/test/java/org/springframework/mock/web/MockPageContext.java‎

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2009 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.
@@ -17,6 +17,7 @@
1717
package org.springframework.mock.web;
1818

1919
import java.io.IOException;
20+
import java.io.UnsupportedEncodingException;
2021
import java.util.Collections;
2122
import java.util.Enumeration;
2223
import java.util.LinkedHashMap;
@@ -314,24 +315,37 @@ public ServletContext getServletContext() {
314315
return this.servletContext;
315316
}
316317

317-
public void forward(String url) throws ServletException, IOException {
318-
throw new UnsupportedOperationException("forward");
318+
public void forward(String path) throws ServletException, IOException {
319+
this.request.getRequestDispatcher(path).forward(this.request, this.response);
319320
}
320321

321-
public void include(String url) throws ServletException, IOException {
322-
throw new UnsupportedOperationException("include");
322+
public void include(String path) throws ServletException, IOException {
323+
this.request.getRequestDispatcher(path).include(this.request, this.response);
323324
}
324325

325-
public void include(String url, boolean flush) throws ServletException, IOException {
326-
throw new UnsupportedOperationException("include");
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+
}
331+
}
332+
333+
public byte[] getContentAsByteArray() {
334+
Assert.isTrue(this.response instanceof MockHttpServletResponse);
335+
return ((MockHttpServletResponse) this.response).getContentAsByteArray();
336+
}
337+
338+
public String getContentAsString() throws UnsupportedEncodingException {
339+
Assert.isTrue(this.response instanceof MockHttpServletResponse);
340+
return ((MockHttpServletResponse) this.response).getContentAsString();
327341
}
328342

329343
public void handlePageException(Exception ex) throws ServletException, IOException {
330-
throw new UnsupportedOperationException("handlePageException");
344+
throw new ServletException("Page exception", ex);
331345
}
332346

333347
public void handlePageException(Throwable ex) throws ServletException, IOException {
334-
throw new UnsupportedOperationException("handlePageException");
348+
throw new ServletException("Page exception", ex);
335349
}
336350

337351
}

0 commit comments

Comments
 (0)