net.sourceforge.stripes.mock
Class MockHttpServletResponse

java.lang.Object
  extended by net.sourceforge.stripes.mock.MockHttpServletResponse
All Implemented Interfaces:
HttpServletResponse, ServletResponse

public class MockHttpServletResponse
extends Object
implements HttpServletResponse

Mock implementation of an HttpServletResponse. Captures any output is written along with any headers, status information etc. and makes it available through various getter methods.

Of major note is the fact that none of the setStatus(), sendError() or sendRedirect() methods have any real effect on the request processing lifecycle. Information is recorded so it can be verified what was invoked, but that is all.

Since:
Stripes 1.1.1
Author:
Tim Fennell

Field Summary
 
Fields inherited from interface javax.servlet.http.HttpServletResponse
SC_ACCEPTED, SC_BAD_GATEWAY, SC_BAD_REQUEST, SC_CONFLICT, SC_CONTINUE, SC_CREATED, SC_EXPECTATION_FAILED, SC_FORBIDDEN, SC_FOUND, SC_GATEWAY_TIMEOUT, SC_GONE, SC_HTTP_VERSION_NOT_SUPPORTED, SC_INTERNAL_SERVER_ERROR, SC_LENGTH_REQUIRED, SC_METHOD_NOT_ALLOWED, SC_MOVED_PERMANENTLY, SC_MOVED_TEMPORARILY, SC_MULTIPLE_CHOICES, SC_NO_CONTENT, SC_NON_AUTHORITATIVE_INFORMATION, SC_NOT_ACCEPTABLE, SC_NOT_FOUND, SC_NOT_IMPLEMENTED, SC_NOT_MODIFIED, SC_OK, SC_PARTIAL_CONTENT, SC_PAYMENT_REQUIRED, SC_PRECONDITION_FAILED, SC_PROXY_AUTHENTICATION_REQUIRED, SC_REQUEST_ENTITY_TOO_LARGE, SC_REQUEST_TIMEOUT, SC_REQUEST_URI_TOO_LONG, SC_REQUESTED_RANGE_NOT_SATISFIABLE, SC_RESET_CONTENT, SC_SEE_OTHER, SC_SERVICE_UNAVAILABLE, SC_SWITCHING_PROTOCOLS, SC_TEMPORARY_REDIRECT, SC_UNAUTHORIZED, SC_UNSUPPORTED_MEDIA_TYPE, SC_USE_PROXY
 
Constructor Summary
MockHttpServletResponse()
           
 
Method Summary
 void addCookie(Cookie cookie)
          Adds a cookie to the set of cookies in the response.
 void addDateHeader(String name, long value)
          Adds the specified value for the named header (does not remove/replace existing values).
 void addHeader(String name, String value)
          Adds the specified value for the named header (does not remove/replace existing values).
 void addIntHeader(String name, int value)
          Adds the specified value for the named header (does not remove/replace existing values).
 boolean containsHeader(String name)
          Returns true if the specified header was placed in the response.
 String encodeRedirectUrl(String url)
          Returns the URL unchanged.
 String encodeRedirectURL(String url)
          Returns the URL unchanged.
 String encodeUrl(String url)
          Returns the URL unchanged.
 String encodeURL(String url)
          Returns the URL unchanged.
 void flushBuffer()
          Has no effect.
 int getBufferSize()
          Always returns 0.
 String getCharacterEncoding()
          Gets the character encoding (defaults to UTF-8).
 int getContentLength()
          Returns the content length if one was set on the response by calling setContentLength().
 String getContentType()
          Gets the content type for the response.
 Cookie[] getCookies()
          Gets the set of cookies stored in the response.
 String getErrorMessage()
          Gets the error message if one was set with setStatus() or sendError().
 Map<String,List<Object>> getHeaderMap()
          Provides access to all headers that were set.
 Locale getLocale()
          Gets the response locale.
 byte[] getOutputBytes()
          Gets the output that was written to the output stream, as a byte[].
 ServletOutputStream getOutputStream()
          Returns a reference to a ServletOutputStream to be used for output.
 String getOutputString()
          Gets the output that was written to the output stream, as a character String.
 String getRedirectUrl()
          If a call was made to sendRedirect() this method will return the URL that was supplied.
 int getStatus()
          Gets the status (or error) code if one was set.
 PrintWriter getWriter()
          Returns a reference to a PrintWriter to be used for character output.
 boolean isCommitted()
          Always returns true.
 void reset()
          Always throws an IllegalStateException.
 void resetBuffer()
          Always throws IllegalStateException.
 void sendError(int status)
          Sets that status code to the error code provided.
 void sendError(int status, String errorMessage)
          Sets the status code and saves the message so it can be retrieved later.
 void sendRedirect(String url)
          Simply stores the URL that was supplied, so that it can be examined later with getRedirectUrl.
 void setBufferSize(int i)
          Has no effect.
 void setCharacterEncoding(String encoding)
          Sets the character encoding on the request.
 void setContentLength(int contentLength)
          Sets a custom content length on the response.
 void setContentType(String contentType)
          Sets the content type for the response.
 void setDateHeader(String name, long value)
          Stores the value in a Long and saves it as a header.
 void setHeader(String name, String value)
          Sets the value of the specified header to the single value provided.
 void setIntHeader(String name, int value)
          Stores the value in an Integer and saves it as a header.
 void setLocale(Locale locale)
          Sets the response locale to the one specified.
 void setStatus(int statusCode)
          Sets the HTTP Status code of the response.
 void setStatus(int status, String errorMessage)
          Saves the HTTP status code and the message provided.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

MockHttpServletResponse

public MockHttpServletResponse()
Method Detail

addCookie

public void addCookie(Cookie cookie)
Adds a cookie to the set of cookies in the response.

Specified by:
addCookie in interface HttpServletResponse

getCookies

public Cookie[] getCookies()
Gets the set of cookies stored in the response.


containsHeader

public boolean containsHeader(String name)
Returns true if the specified header was placed in the response.

Specified by:
containsHeader in interface HttpServletResponse

encodeURL

public String encodeURL(String url)
Returns the URL unchanged.

Specified by:
encodeURL in interface HttpServletResponse

encodeRedirectURL

public String encodeRedirectURL(String url)
Returns the URL unchanged.

Specified by:
encodeRedirectURL in interface HttpServletResponse

encodeUrl

public String encodeUrl(String url)
Returns the URL unchanged.

Specified by:
encodeUrl in interface HttpServletResponse

encodeRedirectUrl

public String encodeRedirectUrl(String url)
Returns the URL unchanged.

Specified by:
encodeRedirectUrl in interface HttpServletResponse

sendError

public void sendError(int status,
                      String errorMessage)
               throws IOException
Sets the status code and saves the message so it can be retrieved later.

Specified by:
sendError in interface HttpServletResponse
Throws:
IOException

sendError

public void sendError(int status)
               throws IOException
Sets that status code to the error code provided.

Specified by:
sendError in interface HttpServletResponse
Throws:
IOException

sendRedirect

public void sendRedirect(String url)
                  throws IOException
Simply stores the URL that was supplied, so that it can be examined later with getRedirectUrl.

Specified by:
sendRedirect in interface HttpServletResponse
Throws:
IOException

getRedirectUrl

public String getRedirectUrl()
If a call was made to sendRedirect() this method will return the URL that was supplied. Otherwise it will return null.


setDateHeader

public void setDateHeader(String name,
                          long value)
Stores the value in a Long and saves it as a header.

Specified by:
setDateHeader in interface HttpServletResponse

addDateHeader

public void addDateHeader(String name,
                          long value)
Adds the specified value for the named header (does not remove/replace existing values).

Specified by:
addDateHeader in interface HttpServletResponse

setHeader

public void setHeader(String name,
                      String value)
Sets the value of the specified header to the single value provided.

Specified by:
setHeader in interface HttpServletResponse

addHeader

public void addHeader(String name,
                      String value)
Adds the specified value for the named header (does not remove/replace existing values).

Specified by:
addHeader in interface HttpServletResponse

setIntHeader

public void setIntHeader(String name,
                         int value)
Stores the value in an Integer and saves it as a header.

Specified by:
setIntHeader in interface HttpServletResponse

addIntHeader

public void addIntHeader(String name,
                         int value)
Adds the specified value for the named header (does not remove/replace existing values).

Specified by:
addIntHeader in interface HttpServletResponse

getHeaderMap

public Map<String,List<Object>> getHeaderMap()
Provides access to all headers that were set. The format is a Map which uses the header name as the key, and stores a List of Objects, one per header value. The Objects will be either Strings (if setHeader() was used), Integers (if setIntHeader() was used) or Longs (if setDateHeader() was used).


setStatus

public void setStatus(int statusCode)
Sets the HTTP Status code of the response.

Specified by:
setStatus in interface HttpServletResponse

setStatus

public void setStatus(int status,
                      String errorMessage)
Saves the HTTP status code and the message provided.

Specified by:
setStatus in interface HttpServletResponse

getStatus

public int getStatus()
Gets the status (or error) code if one was set. Defaults to 200 (HTTP OK).


getErrorMessage

public String getErrorMessage()
Gets the error message if one was set with setStatus() or sendError().


setCharacterEncoding

public void setCharacterEncoding(String encoding)
Sets the character encoding on the request.

Specified by:
setCharacterEncoding in interface ServletResponse

getCharacterEncoding

public String getCharacterEncoding()
Gets the character encoding (defaults to UTF-8).

Specified by:
getCharacterEncoding in interface ServletResponse

setContentType

public void setContentType(String contentType)
Sets the content type for the response.

Specified by:
setContentType in interface ServletResponse

getContentType

public String getContentType()
Gets the content type for the response. Defaults to text/html.

Specified by:
getContentType in interface ServletResponse

getOutputStream

public ServletOutputStream getOutputStream()
                                    throws IOException
Returns a reference to a ServletOutputStream to be used for output. The output is captured and can be examined at the end of a test run by calling getOutputBytes() or getOutputString().

Specified by:
getOutputStream in interface ServletResponse
Throws:
IOException

getWriter

public PrintWriter getWriter()
                      throws IOException
Returns a reference to a PrintWriter to be used for character output. The output is captured and can be examined at the end of a test run by calling getOutputBytes() or getOutputString().

Specified by:
getWriter in interface ServletResponse
Throws:
IOException

getOutputBytes

public byte[] getOutputBytes()
Gets the output that was written to the output stream, as a byte[].


getOutputString

public String getOutputString()
Gets the output that was written to the output stream, as a character String.


setContentLength

public void setContentLength(int contentLength)
Sets a custom content length on the response.

Specified by:
setContentLength in interface ServletResponse

getContentLength

public int getContentLength()
Returns the content length if one was set on the response by calling setContentLength().


setBufferSize

public void setBufferSize(int i)
Has no effect.

Specified by:
setBufferSize in interface ServletResponse

getBufferSize

public int getBufferSize()
Always returns 0.

Specified by:
getBufferSize in interface ServletResponse

flushBuffer

public void flushBuffer()
                 throws IOException
Has no effect.

Specified by:
flushBuffer in interface ServletResponse
Throws:
IOException

resetBuffer

public void resetBuffer()
Always throws IllegalStateException.

Specified by:
resetBuffer in interface ServletResponse

isCommitted

public boolean isCommitted()
Always returns true.

Specified by:
isCommitted in interface ServletResponse

reset

public void reset()
Always throws an IllegalStateException.

Specified by:
reset in interface ServletResponse

setLocale

public void setLocale(Locale locale)
Sets the response locale to the one specified.

Specified by:
setLocale in interface ServletResponse

getLocale

public Locale getLocale()
Gets the response locale. Default to the system default locale.

Specified by:
getLocale in interface ServletResponse


? Copyright 2005-2006, Stripes Development Team.