com.mockrunner.servlet
Class ServletTestModule

java.lang.Object
  extended by com.mockrunner.base.WebTestModule
      extended by com.mockrunner.base.HTMLOutputModule
          extended by com.mockrunner.servlet.ServletTestModule

public class ServletTestModule
extends HTMLOutputModule

Module for servlet and filter tests. Can test single servlets and filters and simulate a filter chain.


Constructor Summary
ServletTestModule(WebMockObjectFactory mockFactory)
           
 
Method Summary
 void addFilter(javax.servlet.Filter filter)
          Adds the specified filter to the filter chain without initializing it.
 void addFilter(javax.servlet.Filter filter, boolean doInit)
          Adds the specified filter it to the filter chain.
 void clearOutput()
          Clears the output content
 javax.servlet.Filter createFilter(java.lang.Class filterClass)
          Creates a filter, initializes it and adds it to the filter chain.
 javax.servlet.http.HttpServlet createServlet(java.lang.Class servletClass)
          Creates a servlet and initializes it.
 void doDelete()
          Calls the current servlets doDelete method.
 void doFilter()
          Loops through the filter chain and calls the current servlets service method at the end (only if a current servlet is set).
 void doGet()
          Calls the current servlets doGet method.
 void doHead()
          Calls the current servlets doHead method.
 void doOptions()
          Calls the current servlets doOptions method.
 void doPost()
          Calls the current servlets doPost method.
 void doPut()
          Calls the current servlets doPut method.
 void doTrace()
          Calls the current servlets doTrace method.
 javax.servlet.ServletRequest getFilteredRequest()
          Returns the last request from the filter chain.
 javax.servlet.ServletResponse getFilteredResponse()
          Returns the last response from the filter chain.
 java.lang.String getOutput()
          Returns the servlet output as a string.
 javax.servlet.http.HttpServlet getServlet()
          Returns the current servlet.
 void init()
          Calls the current servlets init method.
 void releaseFilters()
          Deletes all filters in the filter chain.
 void service()
          Calls the current servlets service method.
 void setDoChain(boolean doChain)
          If doChain is set to true (default is false) every call of one of the servlet methods will go through the filter chain before calling the servlet method.
 void setServlet(javax.servlet.http.HttpServlet servlet)
          Sets the specified servlet as the current servlet without initializing it.
 void setServlet(javax.servlet.http.HttpServlet servlet, boolean doInit)
          Sets the specified servlet as the current servlet.
 
Methods inherited from class com.mockrunner.base.HTMLOutputModule
getOutputAsBufferedReader, getOutputAsJDOMDocument, getOutputAsW3CDocument, getOutputAsWellformedXML, setCaseSensitive, verifyOutput, verifyOutputContains, verifyOutputRegularExpression
 
Methods inherited from class com.mockrunner.base.WebTestModule
addRequestParameter, addRequestParameter, addRequestParameter, getRequestAttribute, getRequestParameter, getSessionAttribute, setRequestAttribute, setSessionAttribute
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ServletTestModule

public ServletTestModule(WebMockObjectFactory mockFactory)
Method Detail

createServlet

public javax.servlet.http.HttpServlet createServlet(java.lang.Class servletClass)
Creates a servlet and initializes it. servletClass must be of the type HttpServlet, otherwise a RuntimeException will be thrown. Sets the specified servlet as the current servlet and initializes the filter chain with it.

Parameters:
servletClass - the class of the servlet
Returns:
instance of HttpServlet
Throws:
java.lang.RuntimeException - if servletClass is not an instance of HttpServlet

setServlet

public void setServlet(javax.servlet.http.HttpServlet servlet)
Sets the specified servlet as the current servlet without initializing it. You have to set the ServletConfig on your own. Usually you can use WebMockObjectFactory.getMockServletConfig().

Parameters:
servlet - the servlet

setServlet

public void setServlet(javax.servlet.http.HttpServlet servlet,
                       boolean doInit)
Sets the specified servlet as the current servlet. Initializes it, if doInit is true.

Parameters:
servlet - the servlet
doInit - should init be called

getServlet

public javax.servlet.http.HttpServlet getServlet()
Returns the current servlet.

Returns:
the servlet

createFilter

public javax.servlet.Filter createFilter(java.lang.Class filterClass)
Creates a filter, initializes it and adds it to the filter chain. filterClass must be of the type Filter, otherwise a RuntimeException will be thrown. You can loop through the filter chain with doFilter(). If you set doChain to true every call of one of the servlet methods will go through the filter chain before calling the servlet method.

Parameters:
filterClass - the class of the filter
Returns:
instance of Filter
Throws:
java.lang.RuntimeException - if filterClass is not an instance of Filter

addFilter

public void addFilter(javax.servlet.Filter filter)
Adds the specified filter to the filter chain without initializing it. You have to set the FilterConfig on your own. Usually you can use WebMockObjectFactory.getMockFilterConfig().

Parameters:
filter - the filter

addFilter

public void addFilter(javax.servlet.Filter filter,
                      boolean doInit)
Adds the specified filter it to the filter chain. Initializes it, if doInit is true.

Parameters:
filter - the filter
doInit - should init be called

releaseFilters

public void releaseFilters()
Deletes all filters in the filter chain.


setDoChain

public void setDoChain(boolean doChain)
If doChain is set to true (default is false) every call of one of the servlet methods will go through the filter chain before calling the servlet method.

Parameters:
doChain - true if the chain should be called

doFilter

public void doFilter()
Loops through the filter chain and calls the current servlets service method at the end (only if a current servlet is set). You can use it to test single filters or the interaction of filters and servlets. If you set doChain to true (use setDoChain(boolean)), this method is called before any call of a servlet method. If a filter does not call it's chains doFilter method, the chain breaks and the servlet will not be called (just like it in the real container).


init

public void init()
Calls the current servlets init method. Is automatically done when calling createServlet(java.lang.Class).


doDelete

public void doDelete()
Calls the current servlets doDelete method. If you set doChain to true (use setDoChain(boolean)), the filter chain will be called before doDelete.


doGet

public void doGet()
Calls the current servlets doGet method. If you set doChain to true (use setDoChain(boolean)), the filter chain will be called before doGet.


doOptions

public void doOptions()
Calls the current servlets doOptions method. If you set doChain to true (use setDoChain(boolean)), the filter chain will be called before doOptions.


doPost

public void doPost()
Calls the current servlets doPost method. If you set doChain to true (use setDoChain(boolean)), the filter chain will be called before doPost.


doPut

public void doPut()
Calls the current servlets doPut method. If you set doChain to true (use setDoChain(boolean)), the filter chain will be called before doPut.


doTrace

public void doTrace()
Calls the current servlets doTrace method. If you set doChain to true (use setDoChain(boolean)), the filter chain will be called before doTrace.


doHead

public void doHead()
Calls the current servlets doHead method. If you set doChain to true (use setDoChain(boolean)), the filter chain will be called before doHead.


service

public void service()
Calls the current servlets service method. If you set doChain to true (use setDoChain(boolean)), the filter chain will be called before service.


getFilteredRequest

public javax.servlet.ServletRequest getFilteredRequest()
Returns the last request from the filter chain. Since filters can replace the request with a request wrapper, this method makes only sense after calling at least one filter, i.e. after calling doFilter() or after calling one servlet method with doChain set to true.

Returns:
the filtered request

getFilteredResponse

public javax.servlet.ServletResponse getFilteredResponse()
Returns the last response from the filter chain. Since filters can replace the response with a response wrapper, this method makes only sense after calling at least one filter, i.e. after calling doFilter() or after calling one servlet method with doChain set to true.

Returns:
the filtered response

getOutput

public java.lang.String getOutput()
Returns the servlet output as a string. Flushes the output before returning it.

Specified by:
getOutput in class HTMLOutputModule
Returns:
the servlet output

clearOutput

public void clearOutput()
Clears the output content