servletunit
Class HttpServletResponseSimulator

java.lang.Object
  extended byservletunit.HttpServletResponseSimulator

public class HttpServletResponseSimulator
extends java.lang.Object


Field Summary
static int SC_ACCEPTED
           
static int SC_BAD_GATEWAY
           
static int SC_BAD_REQUEST
           
static int SC_CONFLICT
           
static int SC_CONTINUE
           
static int SC_CREATED
           
static int SC_EXPECTATION_FAILED
           
static int SC_FORBIDDEN
           
static int SC_GATEWAY_TIMEOUT
           
static int SC_GONE
           
static int SC_HTTP_VERSION_NOT_SUPPORTED
           
static int SC_INTERNAL_SERVER_ERROR
           
static int SC_LENGTH_REQUIRED
           
static int SC_METHOD_NOT_ALLOWED
           
static int SC_MOVED_PERMANENTLY
           
static int SC_MOVED_TEMPORARILY
           
static int SC_MULTIPLE_CHOICES
           
static int SC_NO_CONTENT
           
static int SC_NON_AUTHORITATIVE_INFORMATION
           
static int SC_NOT_ACCEPTABLE
           
static int SC_NOT_FOUND
           
static int SC_NOT_IMPLEMENTED
           
static int SC_NOT_MODIFIED
           
static int SC_OK
           
static int SC_PARTIAL_CONTENT
           
static int SC_PAYMENT_REQUIRED
           
static int SC_PRECONDITION_FAILED
           
static int SC_PROXY_AUTHENTICATION_REQUIRED
           
static int SC_REQUEST_ENTITY_TOO_LARGE
           
static int SC_REQUEST_TIMEOUT
           
static int SC_REQUEST_URI_TOO_LONG
           
static int SC_REQUESTED_RANGE_NOT_SATISFIABLE
           
static int SC_RESET_CONTENT
           
static int SC_SEE_OTHER
           
static int SC_SERVICE_UNAVAILABLE
           
static int SC_SWITCHING_PROTOCOLS
           
static int SC_UNAUTHORIZED
           
static int SC_UNSUPPORTED_MEDIA_TYPE
           
static int SC_USE_PROXY
           
 
Constructor Summary
HttpServletResponseSimulator()
           
 
Method Summary
 void addCookie(Cookie cookie)
          Add a cookie to this response, which will then be stored in the browser.
 void addDateHeader(java.lang.String name, long date)
          This method is not supported.
 void addHeader(java.lang.String name, java.lang.String value)
          Adds a response header with the given name and value.
 void addIntHeader(java.lang.String name, int value)
          Adds a response header with the given name and integer value.
 boolean containsHeader(java.lang.String name)
          returns true if a header with the given name has already been set
 java.lang.String encodeRedirectUrl(java.lang.String url)
          Returns the given URL unmodified
 java.lang.String encodeRedirectURL(java.lang.String url)
          Returns the given URL unmodified.
 java.lang.String encodeUrl(java.lang.String url)
          Returns the given URL unmodified.
 java.lang.String encodeURL(java.lang.String url)
          Returns the given URL unmodified
 Cookie findCookie(java.lang.String name)
          Returns a cookie with a given, or null if this cookie has not been added to the repsonse.
 void flushBuffer()
          This method is not supported.
 int getBufferSize()
          This method is not supported.
 java.lang.String getCharacterEncoding()
          This method is not supported.
 int getContentLength()
          returns the content length previously set in setContentLength()
 java.lang.String getContentType()
          returns the content type previously set in setContentType()
 java.lang.String getHeader(java.lang.String name)
          Returns a given header field, or null if this header has not been set.
 java.util.Locale getLocale()
          Returns the locale assigned to the response.
 java.lang.String getMessage()
           
 ServletOutputStream getOutputStream()
          Returns a ServletOutputStream suitable for writing binary data in the response.
 int getStatusCode()
          Returns the status code for this response, which is useful for testing expected errors.
 java.io.PrintWriter getWriter()
          Returns a PrintWriter object that can send character text to the client.
 java.lang.StringBuffer getWriterBuffer()
          Use this method to pick up the string buffer which will hold the contents of the string buffer.
 boolean isCommitted()
           
 void removeHeader(java.lang.String name)
          Removes a given header
 void reset()
          Reinitializes all local variables.
 void resetBuffer()
          This method is not supported.
 void sendError(int sc)
          Sends an error response to the client using the specified status clearing the buffer.
 void sendError(int sc, java.lang.String msg)
          Sends an error response to the client using the specified status clearing the buffer.
 void sendRedirect(java.lang.String location)
          Resets the response and sets the appropriate redirect headers.
 void setBufferSize(int size)
          This method is not supported.
 void setCharacterEncoding(java.lang.String charEncoding)
           
 void setContentLength(int len)
          Sets the length of the content body in the response In HTTP servlets, this method sets the HTTP Content-Length header.
 void setContentType(java.lang.String type)
          Sets the content type of the response being sent to the client.
 void setDateHeader(java.lang.String name, long date)
          This method is not supported.
 void setHeader(java.lang.String name, java.lang.String value)
          adds the name/value pair to the headers
 void setIntHeader(java.lang.String name, int value)
          Adds the given name/value pair to the headers collection.
 void setIsCommitted(boolean isCommitted)
           
 void setLocale(java.util.Locale loc)
          Sets the locale of the response, setting the headers (including the Content-Type's charset) as appropriate.
 void setOutputStream(java.io.OutputStream out)
          The default action of calling the getOutputStream method is to return a javax.servlet.ServletOutputStream object that sends the data to System.out.
 void setStatus(int sc)
          Sets the given status code.
 void setStatus(int sc, java.lang.String sm)
          Sets the given status and an associated message.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

SC_CONTINUE

public static final int SC_CONTINUE
See Also:
Constant Field Values

SC_SWITCHING_PROTOCOLS

public static final int SC_SWITCHING_PROTOCOLS
See Also:
Constant Field Values

SC_OK

public static final int SC_OK
See Also:
Constant Field Values

SC_CREATED

public static final int SC_CREATED
See Also:
Constant Field Values

SC_ACCEPTED

public static final int SC_ACCEPTED
See Also:
Constant Field Values

SC_NON_AUTHORITATIVE_INFORMATION

public static final int SC_NON_AUTHORITATIVE_INFORMATION
See Also:
Constant Field Values

SC_NO_CONTENT

public static final int SC_NO_CONTENT
See Also:
Constant Field Values

SC_RESET_CONTENT

public static final int SC_RESET_CONTENT
See Also:
Constant Field Values

SC_PARTIAL_CONTENT

public static final int SC_PARTIAL_CONTENT
See Also:
Constant Field Values

SC_MULTIPLE_CHOICES

public static final int SC_MULTIPLE_CHOICES
See Also:
Constant Field Values

SC_MOVED_PERMANENTLY

public static final int SC_MOVED_PERMANENTLY
See Also:
Constant Field Values

SC_MOVED_TEMPORARILY

public static final int SC_MOVED_TEMPORARILY
See Also:
Constant Field Values

SC_SEE_OTHER

public static final int SC_SEE_OTHER
See Also:
Constant Field Values

SC_NOT_MODIFIED

public static final int SC_NOT_MODIFIED
See Also:
Constant Field Values

SC_USE_PROXY

public static final int SC_USE_PROXY
See Also:
Constant Field Values

SC_BAD_REQUEST

public static final int SC_BAD_REQUEST
See Also:
Constant Field Values

SC_UNAUTHORIZED

public static final int SC_UNAUTHORIZED
See Also:
Constant Field Values

SC_PAYMENT_REQUIRED

public static final int SC_PAYMENT_REQUIRED
See Also:
Constant Field Values

SC_FORBIDDEN

public static final int SC_FORBIDDEN
See Also:
Constant Field Values

SC_NOT_FOUND

public static final int SC_NOT_FOUND
See Also:
Constant Field Values

SC_METHOD_NOT_ALLOWED

public static final int SC_METHOD_NOT_ALLOWED
See Also:
Constant Field Values

SC_NOT_ACCEPTABLE

public static final int SC_NOT_ACCEPTABLE
See Also:
Constant Field Values

SC_PROXY_AUTHENTICATION_REQUIRED

public static final int SC_PROXY_AUTHENTICATION_REQUIRED
See Also:
Constant Field Values

SC_REQUEST_TIMEOUT

public static final int SC_REQUEST_TIMEOUT
See Also:
Constant Field Values

SC_CONFLICT

public static final int SC_CONFLICT
See Also:
Constant Field Values

SC_GONE

public static final int SC_GONE
See Also:
Constant Field Values

SC_LENGTH_REQUIRED

public static final int SC_LENGTH_REQUIRED
See Also:
Constant Field Values

SC_PRECONDITION_FAILED

public static final int SC_PRECONDITION_FAILED
See Also:
Constant Field Values

SC_REQUEST_ENTITY_TOO_LARGE

public static final int SC_REQUEST_ENTITY_TOO_LARGE
See Also:
Constant Field Values

SC_REQUEST_URI_TOO_LONG

public static final int SC_REQUEST_URI_TOO_LONG
See Also:
Constant Field Values

SC_UNSUPPORTED_MEDIA_TYPE

public static final int SC_UNSUPPORTED_MEDIA_TYPE
See Also:
Constant Field Values

SC_REQUESTED_RANGE_NOT_SATISFIABLE

public static final int SC_REQUESTED_RANGE_NOT_SATISFIABLE
See Also:
Constant Field Values

SC_EXPECTATION_FAILED

public static final int SC_EXPECTATION_FAILED
See Also:
Constant Field Values

SC_INTERNAL_SERVER_ERROR

public static final int SC_INTERNAL_SERVER_ERROR
See Also:
Constant Field Values

SC_NOT_IMPLEMENTED

public static final int SC_NOT_IMPLEMENTED
See Also:
Constant Field Values

SC_BAD_GATEWAY

public static final int SC_BAD_GATEWAY
See Also:
Constant Field Values

SC_SERVICE_UNAVAILABLE

public static final int SC_SERVICE_UNAVAILABLE
See Also:
Constant Field Values

SC_GATEWAY_TIMEOUT

public static final int SC_GATEWAY_TIMEOUT
See Also:
Constant Field Values

SC_HTTP_VERSION_NOT_SUPPORTED

public static final int SC_HTTP_VERSION_NOT_SUPPORTED
See Also:
Constant Field Values
Constructor Detail

HttpServletResponseSimulator

public HttpServletResponseSimulator()
Method Detail

addCookie

public void addCookie(Cookie cookie)
Add a cookie to this response, which will then be stored in the browser.


findCookie

public Cookie findCookie(java.lang.String name)
Returns a cookie with a given, or null if this cookie has not been added to the repsonse.


addDateHeader

public void addDateHeader(java.lang.String name,
                          long date)
This method is not supported.


addHeader

public void addHeader(java.lang.String name,
                      java.lang.String value)
Adds a response header with the given name and value.


getHeader

public java.lang.String getHeader(java.lang.String name)
Returns a given header field, or null if this header has not been set.


addIntHeader

public void addIntHeader(java.lang.String name,
                         int value)
Adds a response header with the given name and integer value.


containsHeader

public boolean containsHeader(java.lang.String name)
returns true if a header with the given name has already been set


encodeRedirectUrl

public java.lang.String encodeRedirectUrl(java.lang.String url)
Returns the given URL unmodified


encodeRedirectURL

public java.lang.String encodeRedirectURL(java.lang.String url)
Returns the given URL unmodified.


encodeUrl

public java.lang.String encodeUrl(java.lang.String url)
Returns the given URL unmodified.


encodeURL

public java.lang.String encodeURL(java.lang.String url)
Returns the given URL unmodified


flushBuffer

public void flushBuffer()
                 throws java.io.IOException
This method is not supported.

Throws:
java.io.IOException

getBufferSize

public int getBufferSize()
This method is not supported.


getCharacterEncoding

public java.lang.String getCharacterEncoding()
This method is not supported.


getLocale

public java.util.Locale getLocale()
Returns the locale assigned to the response.

See Also:
setLocale(java.util.Locale)

getOutputStream

public ServletOutputStream getOutputStream()
                                    throws java.io.IOException
Returns a ServletOutputStream suitable for writing binary data in the response. The servlet container does not encode the binary data.

Calling flush() on the ServletOutputStream commits the response. Either this method or getWriter() may be called to write the body, not both.

Returns:
a ServletOutputStream for writing binary data
Throws:
java.lang.IllegalStateException - if the getWriter method has been called on this response
java.io.IOException - if an input or output exception occurred
See Also:
getWriter()

getWriter

public java.io.PrintWriter getWriter()
                              throws java.io.IOException
Returns a PrintWriter object that can send character text to the client. The character encoding used is the one specified in the charset= property of the setContentType(java.lang.String) method, which must be called before calling this method for the charset to take effect.

If necessary, the MIME type of the response is modified to reflect the character encoding used.

Calling flush() on the PrintWriter commits the response.

Either this method or getOutputStream() may be called to write the body, not both.

Returns:
a PrintWriter object that can return character data to the client
Throws:
java.io.UnsupportedEncodingException - if the charset specified in setContentType cannot be used
java.lang.IllegalStateException - if the getOutputStream method has already been called for this response object
java.io.IOException - if an input or output exception occurred
See Also:
getOutputStream(), setContentType(java.lang.String)

getWriterBuffer

public java.lang.StringBuffer getWriterBuffer()
Use this method to pick up the string buffer which will hold the contents of the string buffer. You can then write your test case to examine the contents of this buffer and match it against an expected output.


isCommitted

public boolean isCommitted()

setIsCommitted

public void setIsCommitted(boolean isCommitted)

reset

public void reset()
Reinitializes all local variables. Note, in most servlet containers, you may get an IllegalStateException if you call this method after committing the response. That behavior is not replicated here.


resetBuffer

public void resetBuffer()
This method is not supported.


sendError

public void sendError(int sc)
               throws java.io.IOException
Sends an error response to the client using the specified status clearing the buffer. This method always throws an AssertionFailedError with the corresponding error number.

Parameters:
sc - the error status code
Throws:
java.io.IOException

sendError

public void sendError(int sc,
                      java.lang.String msg)
               throws java.io.IOException
Sends an error response to the client using the specified status clearing the buffer. This method always throws an AssertionFailedError with the corresponding error number and descriptive text.

Parameters:
sc - the error status code
msg - the descriptive message
Throws:
java.io.IOException

sendRedirect

public void sendRedirect(java.lang.String location)
                  throws java.io.IOException
Resets the response and sets the appropriate redirect headers.

Throws:
java.io.IOException

setBufferSize

public void setBufferSize(int size)
This method is not supported.


setContentLength

public void setContentLength(int len)
Sets the length of the content body in the response In HTTP servlets, this method sets the HTTP Content-Length header.

Parameters:
len - an integer specifying the length of the content being returned to the client; sets the Content-Length header

getContentLength

public int getContentLength()
returns the content length previously set in setContentLength()

Returns:
the content length

setContentType

public void setContentType(java.lang.String type)
Sets the content type of the response being sent to the client. The content type may include the type of character encoding used, for example, text/html; charset=ISO-8859-4.

If obtaining a PrintWriter, this method should be called first.

Parameters:
type - a String specifying the MIME type of the content
See Also:
getOutputStream(), getWriter()

getContentType

public java.lang.String getContentType()
returns the content type previously set in setContentType()

Returns:
the content type

setDateHeader

public void setDateHeader(java.lang.String name,
                          long date)
This method is not supported.


setHeader

public void setHeader(java.lang.String name,
                      java.lang.String value)
adds the name/value pair to the headers


removeHeader

public void removeHeader(java.lang.String name)
Removes a given header


setIntHeader

public void setIntHeader(java.lang.String name,
                         int value)
Adds the given name/value pair to the headers collection.


setLocale

public void setLocale(java.util.Locale loc)
Sets the locale of the response, setting the headers (including the Content-Type's charset) as appropriate. This method should be called before a call to getWriter(). By default, the response locale is the default locale for the server.

Parameters:
loc - the locale of the response
See Also:
getLocale()

setOutputStream

public void setOutputStream(java.io.OutputStream out)
The default action of calling the getOutputStream method is to return a javax.servlet.ServletOutputStream object that sends the data to System.out. If you don't want the output sent to System.out you can use this method to set where the output will go. Please note, subsequent calls to getOutputStream will reset the output path to System.out. This prevents the OutputStream returned by calling getOutputStream from writing to a closed stream

Parameters:
out - The java.io.OutputStream that represents the real path of the output.

setStatus

public void setStatus(int sc)
Sets the given status code.


setStatus

public void setStatus(int sc,
                      java.lang.String sm)
Sets the given status and an associated message.


getStatusCode

public int getStatusCode()
Returns the status code for this response, which is useful for testing expected errors.

Returns:
the status code for this response.

setCharacterEncoding

public void setCharacterEncoding(java.lang.String charEncoding)

getMessage

public java.lang.String getMessage()


Copyright © Deryl Seale All Rights Reserved.