servletunit.struts
Class MockStrutsTestCase

java.lang.Object
  extended byjunit.framework.Assert
      extended byjunit.framework.TestCase
          extended byservletunit.struts.MockStrutsTestCase
All Implemented Interfaces:
junit.framework.Test

public class MockStrutsTestCase
extends junit.framework.TestCase

MockStrutsTestCase is an extension of the base JUnit testcase that provides additional methods to aid in testing Struts Action objects. It uses a mock object approach to simulate a servlet container, and tests the execution of Action objects as they are actually run through the Struts ActionServlet. MockStrutsTestCase provides methods that set up the request path, request parameters for ActionForm subclasses, as well as methods that can verify that the correct ActionForward was used and that the proper ActionError messages were supplied.

NOTE: By default, the Struts ActionServlet will look for the file WEB-INF/struts-config.xml, so you must place the directory that contains WEB-INF in your CLASSPATH. If you would like to use an alternate configuration file, please see the setConfigFile() method for details on how this file is located.


Field Summary
protected  java.lang.String actionPath
           
protected  ActionServlet actionServlet
           
protected  boolean actionServletIsInitialized
           
protected  ServletConfigSimulator config
           
protected  ServletContextSimulator context
           
protected  boolean isInitialized
           
protected static Log logger
           
protected  java.lang.String[] registrations
          The set of public identifiers, and corresponding resource names, for the versions of the configuration file DTDs that we know about.
protected  HttpServletRequestSimulator request
           
protected  boolean requestPathSet
           
protected  HttpServletRequestWrapper requestWrapper
           
protected  HttpServletResponseSimulator response
           
protected  HttpServletResponseWrapper responseWrapper
           
 
Constructor Summary
MockStrutsTestCase()
          Default constructor.
MockStrutsTestCase(java.lang.String testName)
          Constructor that takes test name parameter, for backwards compatibility with older versions on JUnit.
 
Method Summary
 void actionPerform()
          Executes the Action instance to be tested.
 void addRequestParameter(java.lang.String parameterName, java.lang.String parameterValue)
          Adds an HttpServletRequest parameter to be used in setting up the ActionForm instance to be used in this test.
 void addRequestParameter(java.lang.String parameterName, java.lang.String[] parameterValues)
          Adds an HttpServletRequest parameter that is an array of String values to be used in setting up the ActionForm instance to be used in this test.
 void clearRequestParameters()
          Clears all request parameters previously set.
 ActionForm getActionForm()
          Returns the ActionForm instance stored in either the request or session.
 ActionServlet getActionServlet()
          Returns the ActionServlet controller used in this test.
protected  java.lang.String getActualForward()
          Returns the forward sent to RequestDispatcher.
 HttpServletRequestSimulator getMockRequest()
          Returns the mock HttpServletRequest object used in this test.
 HttpServletResponseSimulator getMockResponse()
          Returns the mock HttpServletResponse object used in this test.
 HttpServletRequest getRequest()
          Returns an HttpServletRequest object that can be used in this test.
 HttpServletRequestWrapper getRequestWrapper()
          Returns a HttpServletRequestWrapper object that can be used in this test.
 HttpServletResponse getResponse()
          Returns an HttpServletResponse object that can be used in this test.
 HttpServletResponseWrapper getResponseWrapper()
          Returns an HttpServletResponseWrapper object that can be used in this test.
 HttpSession getSession()
          Returns an HttpSession object that can be used in this test.
 void setActionForm(ActionForm form)
          Sets an ActionForm instance to be used in this test.
 void setActionServlet(ActionServlet servlet)
          Sets the ActionServlet to be used in this test execution.
 void setConfigFile(java.lang.String pathname)
          Sets the location of the Struts configuration file for the default module.
 void setConfigFile(java.lang.String moduleName, java.lang.String pathname)
          Sets the struts configuration file for a given sub-application.
 void setContextDirectory(java.io.File contextDirectory)
          Sets the context directory to be used with the getRealPath() methods in the ServletContext and HttpServletRequest API.
 void setInitParameter(java.lang.String key, java.lang.String value)
          Sets an initialization parameter on the ActionServlet.
 void setRequestPathInfo(java.lang.String pathInfo)
          Sets the request path instructing the ActionServlet to used a particual ActionMapping.
 void setRequestPathInfo(java.lang.String moduleName, java.lang.String pathInfo)
          Sets the request path instructing the ActionServlet to used a particual ActionMapping.
 void setRequestWrapper(HttpServletRequestWrapper wrapper)
          Set this TestCase to use a given HttpServletRequestWrapper class when calling Action.execute().
 void setResponseWrapper(HttpServletResponseWrapper wrapper)
          Set this TestCase to use a given HttpServletResponseWrapper class when calling Action.execute().
 void setServletConfigFile(java.lang.String pathname)
          Sets the location of the web.xml configuration file to be used to set up the servlet context and configuration for this test.
protected  void setUp()
          Sets up the test fixture for this test.
protected  void tearDown()
           
 void verifyActionErrors(java.lang.String[] errorNames)
          Verifies if the ActionServlet controller sent these error messages.
 void verifyActionMessages(java.lang.String[] messageNames)
          Verifies if the ActionServlet controller sent these action messages.
 void verifyForward(java.lang.String forwardName)
          Verifies if the ActionServlet controller used this forward.
 void verifyForwardPath(java.lang.String forwardPath)
          Verifies if the ActionServlet controller used this actual path as a forward.
 void verifyInputForward()
          Verifies if the ActionServlet controller forwarded to the defined input path.
 void verifyInputTilesForward(java.lang.String definitionName)
          Verifies that the ActionServlet controller forwarded to the defined input Tiles definition.
 void verifyNoActionErrors()
          Verifies that the ActionServlet controller sent no error messages upon executing an Action object.
 void verifyNoActionMessages()
          Verifies that the ActionServlet controller sent no action messages upon executing an Action object.
 void verifyTilesForward(java.lang.String forwardName, java.lang.String definitionName)
          Verifies that the ActionServlet controller used this forward and Tiles definition.
 
Methods inherited from class junit.framework.TestCase
countTestCases, createResult, getName, run, run, runBare, runTest, setName, toString
 
Methods inherited from class junit.framework.Assert
assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertFalse, assertFalse, assertNotNull, assertNotNull, assertNotSame, assertNotSame, assertNull, assertNull, assertSame, assertSame, assertTrue, assertTrue, fail, fail
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

actionServlet

protected ActionServlet actionServlet

request

protected HttpServletRequestSimulator request

response

protected HttpServletResponseSimulator response

requestWrapper

protected HttpServletRequestWrapper requestWrapper

responseWrapper

protected HttpServletResponseWrapper responseWrapper

context

protected ServletContextSimulator context

config

protected ServletConfigSimulator config

actionPath

protected java.lang.String actionPath

isInitialized

protected boolean isInitialized

actionServletIsInitialized

protected boolean actionServletIsInitialized

requestPathSet

protected boolean requestPathSet

registrations

protected java.lang.String[] registrations
The set of public identifiers, and corresponding resource names, for the versions of the configuration file DTDs that we know about. There MUST be an even number of Strings in this list!


logger

protected static Log logger
Constructor Detail

MockStrutsTestCase

public MockStrutsTestCase()
Default constructor.


MockStrutsTestCase

public MockStrutsTestCase(java.lang.String testName)
Constructor that takes test name parameter, for backwards compatibility with older versions on JUnit.

Method Detail

setUp

protected void setUp()
              throws java.lang.Exception
Sets up the test fixture for this test. This method creates an instance of the ActionServlet, initializes it to validate forms and turn off debugging, and creates a mock HttpServletRequest and HttpServletResponse object to use in this test.

Throws:
java.lang.Exception

tearDown

protected void tearDown()
                 throws java.lang.Exception
Throws:
java.lang.Exception

getRequest

public HttpServletRequest getRequest()
Returns an HttpServletRequest object that can be used in this test.


getRequestWrapper

public HttpServletRequestWrapper getRequestWrapper()
Returns a HttpServletRequestWrapper object that can be used in this test. Note that if setRequestWrapper(HttpServletRequestWrapper) has not been called, this method will return an instance of javax.servlet.http.HttpServletRequestWrapper.


setRequestWrapper

public void setRequestWrapper(HttpServletRequestWrapper wrapper)
Set this TestCase to use a given HttpServletRequestWrapper class when calling Action.execute(). Note that if this method is not called, then the normal HttpServletRequest object is used.

Parameters:
wrapper - an HttpServletRequestWrapper object to be used when calling Action.execute().

clearRequestParameters

public void clearRequestParameters()
Clears all request parameters previously set.


getResponse

public HttpServletResponse getResponse()
Returns an HttpServletResponse object that can be used in this test.


getResponseWrapper

public HttpServletResponseWrapper getResponseWrapper()
Returns an HttpServletResponseWrapper object that can be used in this test. Note that if setResponseWrapper(HttpServletResponseWrapper) has not been called, this method will return an instance of javax.servlet.http.HttpServletResponseWrapper.


setResponseWrapper

public void setResponseWrapper(HttpServletResponseWrapper wrapper)
Set this TestCase to use a given HttpServletResponseWrapper class when calling Action.execute(). Note that if this method is not called, then the normal HttpServletResponse object is used.

Parameters:
wrapper - an HttpServletResponseWrapper object to be used when calling Action.execute().

getMockRequest

public HttpServletRequestSimulator getMockRequest()
Returns the mock HttpServletRequest object used in this test. This allows access to methods for setting up test preconditions that are otherwise unavailable through the normal Servlet API.


getMockResponse

public HttpServletResponseSimulator getMockResponse()
Returns the mock HttpServletResponse object used in this test. This allows access to methods for setting up test preconditions that are otherwise unavailable through the normal Servlet API.


getSession

public HttpSession getSession()
Returns an HttpSession object that can be used in this test.


getActionServlet

public ActionServlet getActionServlet()
Returns the ActionServlet controller used in this test.


setActionServlet

public void setActionServlet(ActionServlet servlet)
Sets the ActionServlet to be used in this test execution. This method should only be used if you plan to use a customized version different from that provided in the Struts distribution.


actionPerform

public void actionPerform()
Executes the Action instance to be tested. This method calls the ActionServlet.doPost() method to execute the Action instance to be tested, passing along any parameters set in the HttpServletRequest object. It stores any results for further validation.

Throws:
junit.framework.AssertionFailedError - if there are any execution errors while calling Action.execute()

addRequestParameter

public void addRequestParameter(java.lang.String parameterName,
                                java.lang.String parameterValue)
Adds an HttpServletRequest parameter to be used in setting up the ActionForm instance to be used in this test. Each parameter added should correspond to an attribute in the ActionForm instance used by the Action instance being tested.


addRequestParameter

public void addRequestParameter(java.lang.String parameterName,
                                java.lang.String[] parameterValues)
Adds an HttpServletRequest parameter that is an array of String values to be used in setting up the ActionForm instance to be used in this test. Each parameter added should correspond to an attribute in the ActionForm instance used by the Action instance being tested.


setRequestPathInfo

public void setRequestPathInfo(java.lang.String pathInfo)
Sets the request path instructing the ActionServlet to used a particual ActionMapping.

Parameters:
pathInfo - the request path to be processed. This should correspond to a particular action mapping, as would normally appear in an HTML or JSP source file.

setRequestPathInfo

public void setRequestPathInfo(java.lang.String moduleName,
                               java.lang.String pathInfo)
Sets the request path instructing the ActionServlet to used a particual ActionMapping. Also sets the ServletPath property on the request.

Parameters:
moduleName - the name of the Struts sub-application with which this request is associated, or null if it is the default application.
pathInfo - the request path to be processed. This should correspond to a particular action mapping, as would normally appear in an HTML or JSP source file. If this request is part of a sub-application, the module name should not appear in the request path.

setInitParameter

public void setInitParameter(java.lang.String key,
                             java.lang.String value)
Sets an initialization parameter on the ActionServlet. Allows you to simulate an init parameter that would normally have been found in web.xml, but is not available while testing with mock objects.

Parameters:
key - the name of the initialization parameter
value - the value of the intialization parameter

setContextDirectory

public void setContextDirectory(java.io.File contextDirectory)
Sets the context directory to be used with the getRealPath() methods in the ServletContext and HttpServletRequest API.

Parameters:
contextDirectory - a File object representing the root context directory for this application.

setConfigFile

public void setConfigFile(java.lang.String pathname)
Sets the location of the Struts configuration file for the default module. This method can take either an absolute path, or a relative path. If an absolute path is supplied, the configuration file will be loaded from the underlying filesystem; otherwise, the ServletContext loader will be used.


setConfigFile

public void setConfigFile(java.lang.String moduleName,
                          java.lang.String pathname)
Sets the struts configuration file for a given sub-application. This method can take either an absolute path, or a relative path. If an absolute path is supplied, the configuration file will be loaded from the underlying filesystem; otherwise, the ServletContext loader will be used.

Parameters:
moduleName - the name of the sub-application, or null if this is the default application
pathname - the location of the configuration file for this sub-application

setServletConfigFile

public void setServletConfigFile(java.lang.String pathname)
Sets the location of the web.xml configuration file to be used to set up the servlet context and configuration for this test. This method supports both init-param and context-param tags, setting the ServletConfig and ServletContext appropriately. This method can take either an absolute path, or a relative path. If an absolute path is supplied, the configuration file will be loaded from the underlying filesystem; otherwise, the ServletContext loader will be used.


getActualForward

protected java.lang.String getActualForward()
Returns the forward sent to RequestDispatcher.


verifyForward

public void verifyForward(java.lang.String forwardName)
                   throws junit.framework.AssertionFailedError
Verifies if the ActionServlet controller used this forward.

Parameters:
forwardName - the logical name of a forward, as defined in the Struts configuration file. This can either refer to a global forward, or one local to the ActionMapping.
Throws:
junit.framework.AssertionFailedError - if the ActionServlet controller used a different forward than forwardName after executing an Action object.

verifyForwardPath

public void verifyForwardPath(java.lang.String forwardPath)
                       throws junit.framework.AssertionFailedError
Verifies if the ActionServlet controller used this actual path as a forward.

Parameters:
forwardPath - an absolute pathname to which the request is to be forwarded.
Throws:
junit.framework.AssertionFailedError - if the ActionServlet controller used a different forward path than forwardPath after executing an Action object.

verifyInputForward

public void verifyInputForward()
Verifies if the ActionServlet controller forwarded to the defined input path.

Throws:
junit.framework.AssertionFailedError - if the ActionServlet controller used a different forward than the defined input path after executing an Action object.

verifyTilesForward

public void verifyTilesForward(java.lang.String forwardName,
                               java.lang.String definitionName)
Verifies that the ActionServlet controller used this forward and Tiles definition.

Parameters:
forwardName - the logical name of a forward, as defined in the Struts configuration file. This can either refer to a global forward, or one local to the ActionMapping.
definitionName - the name of a Tiles definition, as defined in the Tiles configuration file.
Throws:
junit.framework.AssertionFailedError - if the ActionServlet controller used a different forward or tiles definition than those given after executing an Action object.

verifyInputTilesForward

public void verifyInputTilesForward(java.lang.String definitionName)
Verifies that the ActionServlet controller forwarded to the defined input Tiles definition.

Parameters:
definitionName - the name of a Tiles definition, as defined in the Tiles configuration file.
Throws:
junit.framework.AssertionFailedError - if the ActionServlet controller used a different forward than the defined input path after executing an Action object.

verifyActionErrors

public void verifyActionErrors(java.lang.String[] errorNames)
Verifies if the ActionServlet controller sent these error messages. There must be an exact match between the provided error messages, and those sent by the controller, in both name and number.

Parameters:
errorNames - a String array containing the error message keys to be verified, as defined in the application resource properties file.
Throws:
junit.framework.AssertionFailedError - if the ActionServlet controller sent different error messages than those in errorNames after executing an Action object.

verifyNoActionErrors

public void verifyNoActionErrors()
Verifies that the ActionServlet controller sent no error messages upon executing an Action object.

Throws:
junit.framework.AssertionFailedError - if the ActionServlet controller sent any error messages after excecuting and Action object.

verifyActionMessages

public void verifyActionMessages(java.lang.String[] messageNames)
Verifies if the ActionServlet controller sent these action messages. There must be an exact match between the provided action messages, and those sent by the controller, in both name and number.

Parameters:
messageNames - a String array containing the action message keys to be verified, as defined in the application resource properties file.
Throws:
junit.framework.AssertionFailedError - if the ActionServlet controller sent different action messages than those in messageNames after executing an Action object.

verifyNoActionMessages

public void verifyNoActionMessages()
Verifies that the ActionServlet controller sent no action messages upon executing an Action object.

Throws:
junit.framework.AssertionFailedError - if the ActionServlet controller sent any action messages after excecuting and Action object.

getActionForm

public ActionForm getActionForm()
Returns the ActionForm instance stored in either the request or session. Note that no form will be returned if the Action being tested cleans up the form instance.


setActionForm

public void setActionForm(ActionForm form)
Sets an ActionForm instance to be used in this test. The given ActionForm instance will be stored in the scope specified in the Struts configuration file (ie: request or session). Note that while this ActionForm instance is passed to the test, Struts will still control how it is used. In particular, it will call the ActionForm.reset() method, so if you override this method in your ActionForm subclass, you could potentially reset attributes in the form passed through this method.

Parameters:
form - the ActionForm instance to be used in this test.


Copyright Deryl Seale All Rights Reserved.