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

Quick Search    Search Deep

servletunit.struts
Class CactusStrutsTestCase  view CactusStrutsTestCase download CactusStrutsTestCase.java

java.lang.Object
  extended byjunit.framework.Assert
      extended byjunit.framework.TestCase
          extended byorg.apache.cactus.internal.AbstractCactusTestCase
              extended byorg.apache.cactus.ServletTestCase
                  extended byservletunit.struts.CactusStrutsTestCase
All Implemented Interfaces:
org.apache.cactus.internal.CactusTestCase, junit.framework.Test

public class CactusStrutsTestCase
extends org.apache.cactus.ServletTestCase

CactusStrutsTestCase is an extension of the Cactus ServletTestCase base class that provides additional methods to aid in testing Struts Action objects. It uses an in-container approach to run the servlet container, and tests the execution of Action objects as they are actually run through the Struts ActionServlet. CactusStrutsTestCase 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.

Please note that this class is meant to run in the Cactus framework, and you must configure your test environment accordingly. Please see http://jakarta.apache.org/cactus for more details.


Field Summary
protected  org.apache.struts.action.ActionServlet actionServlet
           
protected  boolean actionServletIsInitialized
           
protected  boolean isInitialized
           
protected static org.apache.commons.logging.Log logger
           
protected  java.lang.String moduleName
           
protected  boolean requestPathIsSet
           
protected  javax.servlet.http.HttpServletRequestWrapper requestWrapper
           
protected  javax.servlet.http.HttpServletResponseWrapper responseWrapper
           
protected  java.lang.String servletMapping
           
 
Fields inherited from class org.apache.cactus.ServletTestCase
config, request, response, session
 
Fields inherited from class org.apache.cactus.internal.AbstractCactusTestCase
 
Fields inherited from class junit.framework.TestCase
 
Constructor Summary
CactusStrutsTestCase()
          Default constructor.
CactusStrutsTestCase(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.
 org.apache.struts.action.ActionForm getActionForm()
          Returns the ActionForm instance stored in either the request or session.
 org.apache.struts.action.ActionServlet getActionServlet()
          Returns the ActionServlet controller used in this test.
protected  java.lang.String getActualForward()
          Returns the forward sent to RequestDispatcher.
 javax.servlet.http.HttpServletRequest getRequest()
          Returns an HttpServletRequest object that can be used in this test.
 javax.servlet.http.HttpServletRequestWrapper getRequestWrapper()
          Returns a HttpServletRequestWrapper object that can be used in this test.
 javax.servlet.http.HttpServletResponse getResponse()
          Returns an HttpServletResponse object that can be used in this test.
 javax.servlet.http.HttpServletResponseWrapper getResponseWrapper()
          Returns an HttpServletResponseWrapper object that can be used in this test.
 javax.servlet.http.HttpSession getSession()
          Returns an HttpSession object that can be used in this test.
private  void init()
          A check that every method should run to ensure that the base class setUp method has been called.
 void processRequest(boolean flag)
          Instructs StrutsTestCase to fully process a forward request.
 void setActionForm(org.apache.struts.action.ActionForm form)
          Sets an ActionForm instance to be used in this test.
 void setActionServlet(org.apache.struts.action.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 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(javax.servlet.http.HttpServletRequestWrapper wrapper)
          Set this TestCase to use a given HttpServletRequestWrapper class when calling Action.execute().
 void setResponseWrapper(javax.servlet.http.HttpServletResponseWrapper wrapper)
          Set this TestCase to use a given HttpServletResponseWrapper class when calling Action.execute().
 void setServletMapping(java.lang.String servletMapping)
          Sets the servlet mapping used to map requests to the Struts controller.
protected  void setUp()
          Sets up the test fixture for this test.
protected  void tearDown()
          Tears down the fixture, for example, close a network connection.
 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 org.apache.cactus.ServletTestCase
createProtocolHandler
 
Methods inherited from class org.apache.cactus.internal.AbstractCactusTestCase
runBare, runBareServer
 
Methods inherited from class junit.framework.TestCase
countTestCases, createResult, getName, run, run, 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
 
Methods inherited from interface org.apache.cactus.internal.CactusTestCase
runBareServer
 

Field Detail

actionServlet

protected org.apache.struts.action.ActionServlet actionServlet

requestWrapper

protected javax.servlet.http.HttpServletRequestWrapper requestWrapper

responseWrapper

protected javax.servlet.http.HttpServletResponseWrapper responseWrapper

isInitialized

protected boolean isInitialized

actionServletIsInitialized

protected boolean actionServletIsInitialized

requestPathIsSet

protected boolean requestPathIsSet

moduleName

protected java.lang.String moduleName

servletMapping

protected java.lang.String servletMapping

logger

protected static org.apache.commons.logging.Log logger
Constructor Detail

CactusStrutsTestCase

public CactusStrutsTestCase()
Default constructor.


CactusStrutsTestCase

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

Method Detail

init

private void init()
A check that every method should run to ensure that the base class setUp method has been called.


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 clears all other parameters.


setServletMapping

public void setServletMapping(java.lang.String servletMapping)
Sets the servlet mapping used to map requests to the Struts controller. This is used to restore proper setting after the test has been executed. By default, the stanard "*.do" mapping will be restored, so only use this method for non-standard servlet mappings.


tearDown

protected void tearDown()
                 throws java.lang.Exception
Description copied from class: junit.framework.TestCase
Tears down the fixture, for example, close a network connection. This method is called after a test is executed.


getRequest

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


getRequestWrapper

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


setRequestWrapper

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


getResponse

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


getResponseWrapper

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


setResponseWrapper

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


getSession

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


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.


clearRequestParameters

public void clearRequestParameters()
Clears all request parameters previously set. NOTE: This will not clear parameters set using Cactus beginXXX methods!


setRequestPathInfo

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


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.


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.


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.


getActionServlet

public org.apache.struts.action.ActionServlet getActionServlet()
Returns the ActionServlet controller used in this test.


setActionServlet

public void setActionServlet(org.apache.struts.action.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 initializes the ActionServlet, sets up and optionally validates the ActionForm bean associated with the Action to be tested, and then calls the Action.execute() method. Results are stored for further validation.


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.


verifyForwardPath

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


verifyInputForward

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


verifyTilesForward

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


verifyInputTilesForward

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


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.


verifyNoActionErrors

public void verifyNoActionErrors()
Verifies that the ActionServlet controller sent no error messages upon executing an 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.


verifyNoActionMessages

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


getActionForm

public org.apache.struts.action.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(org.apache.struts.action.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.


processRequest

public void processRequest(boolean flag)
Instructs StrutsTestCase to fully process a forward request. By default, StrutsTestCase stops processing a request as soon as the forward path has been collected, in order to avoid side effects; calling this method overrides this behavior.