Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

servletunit
Class HttpServletResponseSimulator  view HttpServletResponseSimulator download HttpServletResponseSimulator.java

java.lang.Object
  extended byservletunit.HttpServletResponseSimulator
All Implemented Interfaces:
javax.servlet.http.HttpServletResponse, javax.servlet.ServletResponse

public class HttpServletResponseSimulator
extends java.lang.Object
implements javax.servlet.http.HttpServletResponse


Field Summary
private  boolean calledGetOutputStream
           
private  boolean calledGetWriter
           
(package private)  java.lang.String charEncoding
           
private  int contentLength
           
private  java.lang.String contentType
           
private  java.util.HashMap cookies
           
private  java.util.HashMap headers
           
private  boolean isCommitted
           
private  java.util.Locale locale
           
private  java.lang.String message
           
private  java.io.PrintWriter printWriter
           
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
           
private  java.io.OutputStream servOStream
           
private  int status
           
private  java.io.StringWriter stringWriter
           
 
Fields inherited from interface javax.servlet.http.HttpServletResponse
SC_FOUND, SC_TEMPORARY_REDIRECT
 
Constructor Summary
HttpServletResponseSimulator()
           
 
Method Summary
 void addCookie(javax.servlet.http.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
 javax.servlet.http.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()
           
 javax.servlet.ServletOutputStream getOutputStream()
          Returns a javax.servlet.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()
          Returns a boolean indicating if the response has been committed.
 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)
          Sets the character encoding (MIME charset) of the response being sent to the client, for example, to UTF-8.
 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

servOStream

private java.io.OutputStream servOStream

calledGetWriter

private boolean calledGetWriter

calledGetOutputStream

private boolean calledGetOutputStream

stringWriter

private java.io.StringWriter stringWriter

printWriter

private java.io.PrintWriter printWriter

locale

private java.util.Locale locale

contentLength

private int contentLength

contentType

private java.lang.String contentType

status

private int status

message

private java.lang.String message

headers

private java.util.HashMap headers

cookies

private java.util.HashMap cookies

charEncoding

java.lang.String charEncoding

isCommitted

private boolean isCommitted

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(javax.servlet.http.Cookie cookie)
Add a cookie to this response, which will then be stored in the browser.

Specified by:
addCookie in interface javax.servlet.http.HttpServletResponse

findCookie

public javax.servlet.http.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.

Specified by:
addDateHeader in interface javax.servlet.http.HttpServletResponse

addHeader

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

Specified by:
addHeader in interface javax.servlet.http.HttpServletResponse

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.

Specified by:
addIntHeader in interface javax.servlet.http.HttpServletResponse

containsHeader

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

Specified by:
containsHeader in interface javax.servlet.http.HttpServletResponse

encodeRedirectUrl

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

Specified by:
encodeRedirectUrl in interface javax.servlet.http.HttpServletResponse

encodeRedirectURL

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

Specified by:
encodeRedirectURL in interface javax.servlet.http.HttpServletResponse

encodeUrl

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

Specified by:
encodeUrl in interface javax.servlet.http.HttpServletResponse

encodeURL

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

Specified by:
encodeURL in interface javax.servlet.http.HttpServletResponse

flushBuffer

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

Specified by:
flushBuffer in interface javax.servlet.ServletResponse

getBufferSize

public int getBufferSize()
This method is not supported.

Specified by:
getBufferSize in interface javax.servlet.ServletResponse

getCharacterEncoding

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

Specified by:
getCharacterEncoding in interface javax.servlet.ServletResponse

getLocale

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

Specified by:
getLocale in interface javax.servlet.ServletResponse

getOutputStream

public javax.servlet.ServletOutputStream getOutputStream()
                                                  throws java.io.IOException
Returns a javax.servlet.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() 55 may be called to write the body, not both.

Specified by:
getOutputStream in interface javax.servlet.ServletResponse

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) 55 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() 55 may be called to write the body, not both.

Specified by:
getWriter in interface javax.servlet.ServletResponse

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()
Description copied from interface: javax.servlet.ServletResponse
Returns a boolean indicating if the response has been committed. A committed response has already had its status code and headers written.

Specified by:
isCommitted in interface javax.servlet.ServletResponse

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.

Specified by:
reset in interface javax.servlet.ServletResponse

resetBuffer

public void resetBuffer()
This method is not supported.

Specified by:
resetBuffer in interface javax.servlet.ServletResponse

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.

Specified by:
sendError in interface javax.servlet.http.HttpServletResponse

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.

Specified by:
sendError in interface javax.servlet.http.HttpServletResponse

sendRedirect

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

Specified by:
sendRedirect in interface javax.servlet.http.HttpServletResponse

setBufferSize

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

Specified by:
setBufferSize in interface javax.servlet.ServletResponse

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.

Specified by:
setContentLength in interface javax.servlet.ServletResponse

getContentLength

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


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.

Specified by:
setContentType in interface javax.servlet.ServletResponse

getContentType

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

Specified by:
getContentType in interface javax.servlet.ServletResponse

setDateHeader

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

Specified by:
setDateHeader in interface javax.servlet.http.HttpServletResponse

setHeader

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

Specified by:
setHeader in interface javax.servlet.http.HttpServletResponse

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.

Specified by:
setIntHeader in interface javax.servlet.http.HttpServletResponse

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() 55 . By default, the response locale is the default locale for the server.

Specified by:
setLocale in interface javax.servlet.ServletResponse

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


setStatus

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

Specified by:
setStatus in interface javax.servlet.http.HttpServletResponse

setStatus

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

Specified by:
setStatus in interface javax.servlet.http.HttpServletResponse

getStatusCode

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


setCharacterEncoding

public void setCharacterEncoding(java.lang.String charEncoding)
Description copied from interface: javax.servlet.ServletResponse
Sets the character encoding (MIME charset) of the response being sent to the client, for example, to UTF-8. If the character encoding has already been set by ServletResponse.setContentType(java.lang.String)>ServletResponse.setContentType(java.lang.String) 55 or ServletResponse.setLocale(java.util.Locale)>ServletResponse.setLocale(java.util.Locale) 55 , this method overrides it. Calling ServletResponse.setContentType(java.lang.String)>ServletResponse.setContentType(java.lang.String) 55 with the String of text/html and calling this method with the String of UTF-8 is equivalent with calling setContentType with the String of text/html; charset=UTF-8.

This method can be called repeatedly to change the character encoding. This method has no effect if it is called after getWriter has been called or after the response has been committed.

Containers must communicate the character encoding used for the servlet response's writer to the client if the protocol provides a way for doing so. In the case of HTTP, the character encoding is communicated as part of the Content-Type header for text media types. Note that the character encoding cannot be communicated via HTTP headers if the servlet does not specify a content type; however, it is still used to encode text written via the servlet response's writer.

Specified by:
setCharacterEncoding in interface javax.servlet.ServletResponse

getMessage

public java.lang.String getMessage()