|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object com.mockrunner.base.WebTestModule com.mockrunner.base.HTMLOutputModule com.mockrunner.struts.ActionTestModule
public class ActionTestModule
Module for Struts action tests. Simulates Struts
without reading the struts-config.xml file.
Per default this class does everything like Struts
when calling an action but you can change the behaviour
(e.g. disable form population).
Please note: If your action throws an exception and an
exception handler is registered (use addExceptionHandler(com.mockrunner.struts.ExceptionHandlerConfig)
),
the handler will be called to handle the exception.
Otherwise the exception will be rethrown as NestedApplicationException
.
Constructor Summary | |
---|---|
ActionTestModule(ActionMockObjectFactory mockFactory)
|
Method Summary | |
---|---|
org.apache.struts.action.ActionForward |
actionPerform(org.apache.struts.action.Action action)
Calls the specified action using no ActionForm . |
org.apache.struts.action.ActionForward |
actionPerform(org.apache.struts.action.Action action,
org.apache.struts.action.ActionForm form)
Calls the specified action using the specified ActionForm object. |
org.apache.struts.action.ActionForward |
actionPerform(org.apache.struts.action.Action action,
java.lang.Class form)
Calls the specified action using the ActionForm of the specified type. |
org.apache.struts.action.ActionForward |
actionPerform(java.lang.Class action)
Calls the action of the specified type using no ActionForm . |
org.apache.struts.action.ActionForward |
actionPerform(java.lang.Class action,
org.apache.struts.action.ActionForm form)
Calls the action of the specified type using the specified ActionForm object. |
org.apache.struts.action.ActionForward |
actionPerform(java.lang.Class action,
java.lang.Class form)
Calls the action of the specified type using the ActionForm of the specified type. |
void |
addExceptionHandler(ExceptionHandlerConfig handler)
Registers an exception handler. |
java.lang.String |
addMappedPropertyRequestPrefix(java.lang.String property)
Convenience method for map backed properties. |
org.apache.struts.action.ActionForm |
createActionForm(java.lang.Class form)
Creates a new ActionForm object of the specified
type and sets it as the current ActionForm . |
org.apache.struts.action.DynaActionForm |
createDynaActionForm(org.apache.struts.config.FormBeanConfig formConfig)
Creates a new DynaActionForm based on the specified
form config and sets it as the current ActionForm . |
org.apache.commons.validator.ValidatorResources |
createValidatorResources(java.lang.String[] resourcesFiles)
Creates a valid ValidatorResources object based
on the specified config files. |
void |
generateValidToken()
Generates a token and sets it to the session and the request. |
org.apache.struts.action.ActionMessage |
getActionErrorByKey(java.lang.String errorKey)
Returns the action error with the specified key or null if such an error does not exist. |
org.apache.struts.action.ActionMessages |
getActionErrors()
Get the currently present action errors. |
org.apache.struts.action.ActionMessages |
getActionErrorsFromRequest()
Get the currently present action errors from the request. |
org.apache.struts.action.ActionMessages |
getActionErrorsFromSession()
Get the currently present action errors from the session. |
org.apache.struts.action.ActionForm |
getActionForm()
Returns the current ActionForm . |
MockActionForward |
getActionForward()
Returns the current ActionForward . |
org.apache.struts.action.ActionMapping |
getActionMapping()
Delegates to ActionMockObjectFactory.getActionMapping() . |
org.apache.struts.action.ActionMessage |
getActionMessageByKey(java.lang.String messageKey)
Returns the action message with the specified key or null if such a message does not exist. |
org.apache.struts.action.ActionMessages |
getActionMessages()
Get the currently present action messages. |
org.apache.struts.action.ActionMessages |
getActionMessagesFromRequest()
Get the currently present action messages from the request. |
org.apache.struts.action.ActionMessages |
getActionMessagesFromSession()
Get the currently present action messages from the session. |
org.apache.struts.action.Action |
getLastAction()
Returns the last tested Action object. |
MockActionMapping |
getMockActionMapping()
Delegates to ActionMockObjectFactory.getMockActionMapping() . |
MockPageContext |
getMockPageContext()
Returns the MockPageContext object. |
java.lang.String |
getOutput()
Returns the HTML output as a string (if the action creates HTML output). |
boolean |
hasActionErrors()
Returns if action errors are present. |
boolean |
hasActionMessages()
Returns if action messages are present. |
void |
populateRequestToForm()
Populates the current request parameters to the ActionForm . |
void |
setActionErrors(org.apache.struts.action.ActionMessages errors)
Sets the specified ActionErrors object
as the currently present errors to the request. |
void |
setActionErrorsToSession(org.apache.struts.action.ActionMessages errors)
Sets the specified ActionErrors object
as the currently present errors to the session. |
void |
setActionForm(org.apache.struts.action.ActionForm formObj)
Sets the specified ActionForm object as the
current ActionForm . |
void |
setActionMessages(org.apache.struts.action.ActionMessages messages)
Sets the specified ActionMessages object
as the currently present messages to the request. |
void |
setActionMessagesToSession(org.apache.struts.action.ActionMessages messages)
Sets the specified ActionMessages object
as the currently present messages to the session. |
void |
setDataSource(javax.sql.DataSource dataSource)
Sets the specified DataSource . |
void |
setDataSource(java.lang.String key,
javax.sql.DataSource dataSource)
Sets the specified DataSource . |
void |
setDoPopulate(boolean doPopulate)
Set if the form should be populated with the request parameters before calling the action. |
void |
setErrorAttributeKey(java.lang.String errorAttributeKey)
Name of the key under which errors are stored. |
void |
setInput(java.lang.String input)
Sets the input attribute. |
void |
setLocale(java.util.Locale locale)
Sets the specified locale as a session attribute using Globals.LOCALE_KEY as the key. |
void |
setMessageAttributeKey(java.lang.String messageAttributeKey)
Name of the key under which messages are stored. |
void |
setParameter(java.lang.String parameter)
Sets the parameter by calling ActionMapping.setParameter
on the action mapping returned by getActionMapping() . |
void |
setRecognizeMessagesInSession(boolean recognizeInSession)
Set if messages that are saved to the session (instead of the request) should be recognized. |
void |
setReset(boolean reset)
Set if the reset method should be called before populating a form with populateRequestToForm() . |
void |
setResources(org.apache.struts.util.MessageResources resources)
Sets the specified messages resources as a request attribute using Globals.MESSAGES_KEY as the key. |
void |
setResources(java.lang.String key,
org.apache.struts.util.MessageResources resources)
Sets the specified messages resources as a servlet context attribute using the specified key and the module config prefix. |
void |
setValidate(boolean validate)
Sets if form validation should be performed before calling the action. |
void |
setValidatorResources(org.apache.commons.validator.ValidatorResources validatorResources)
Sets the specified ValidatorResources . |
void |
verifyActionErrorNotPresent(java.lang.String errorKey)
Verifies that an action error with the specified key is not present. |
void |
verifyActionErrorPresent(java.lang.String errorKey)
Verifies that an action error with the specified key is present. |
void |
verifyActionErrorProperty(java.lang.String errorKey,
java.lang.String property)
Verifies that the specified error is stored for the specified property. |
void |
verifyActionErrors(java.lang.String[] errorKeys)
Verifies that the specified action errors are present. |
void |
verifyActionErrorValue(java.lang.String errorKey,
java.lang.Object value)
Verifies the value of the action error with the specified key. |
void |
verifyActionErrorValues(java.lang.String errorKey,
java.lang.Object[] values)
Verifies the values of the action error with the specified key. |
void |
verifyActionMessageNotPresent(java.lang.String messageKey)
Verifies that an action message with the specified key is not present. |
void |
verifyActionMessagePresent(java.lang.String messageKey)
Verifies that an action message with the specified key is present. |
void |
verifyActionMessageProperty(java.lang.String messageKey,
java.lang.String property)
Verifies that the specified message is stored for the specified property. |
void |
verifyActionMessages(java.lang.String[] messageKeys)
Verifies that the specified action messages are present. |
void |
verifyActionMessageValue(java.lang.String messageKey,
java.lang.Object value)
Verifies the value of the action message with the specified key. |
void |
verifyActionMessageValues(java.lang.String messageKey,
java.lang.Object[] values)
Verifies the values of the action message with the specified key. |
void |
verifyForward(java.lang.String path)
Verifies the forward path returned by the action. |
void |
verifyForwardName(java.lang.String name)
Verifies the forward name returned by the action. |
void |
verifyHasActionErrors()
Verifies that there are action errors present. |
void |
verifyHasActionMessages()
Verifies that there are action messages present. |
void |
verifyNoActionErrors()
Verifies that there are no action errors present. |
void |
verifyNoActionMessages()
Verifies that there are no action messages present. |
void |
verifyNumberActionErrors(int number)
Verifies the number of action errors. |
void |
verifyNumberActionMessages(int number)
Verifies the number of action messages. |
void |
verifyRedirect(boolean redirect)
Verifies the redirect attribute. |
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 |
---|
public ActionTestModule(ActionMockObjectFactory mockFactory)
Method Detail |
---|
public void setReset(boolean reset)
populateRequestToForm()
.
Default is true
which is the standard Struts
behaviour.
reset
- should reset be calledpublic void setDoPopulate(boolean doPopulate)
true
which is the standard Struts
behaviour.
doPopulate
- should population be performedpublic void setRecognizeMessagesInSession(boolean recognizeInSession)
true
.
recognizeInSession
- should messages in the session be recognizedpublic void setMessageAttributeKey(java.lang.String messageAttributeKey)
Globals.MESSAGE_KEY
.
messageAttributeKey
- the message keypublic void setErrorAttributeKey(java.lang.String errorAttributeKey)
Globals.ERROR_KEY
.
errorAttributeKey
- the message keypublic java.lang.String addMappedPropertyRequestPrefix(java.lang.String property)
property
- the property
public void setParameter(java.lang.String parameter)
ActionMapping.setParameter
on the action mapping returned by getActionMapping()
.
You can test your Actions with different parameter settings in the
same test method.
parameter
- the parameterpublic void setValidate(boolean validate)
ActionMapping.setValidate
on the action mapping returned
by getActionMapping()
. Default is false
.
validate
- should validation be performedpublic void setInput(java.lang.String input)
verifyForward(java.lang.String)
.
Calls ActionMapping.setInput
on the action mapping returned
by getActionMapping()
.
input
- the input attributepublic void addExceptionHandler(ExceptionHandlerConfig handler)
DefaultExceptionHandlerConfig
to this method. DefaultExceptionHandlerConfig
relies on Struts ExceptionHandler
classes.
In special cases, you may add own implementations of
ExceptionHandlerConfig
, that may be independent from
the Struts exception handling mechanism.
If no matching handler is registered, the exception will be rethrown
as NestedApplicationException
.
handler
- the exception handlerpublic void setResources(org.apache.struts.util.MessageResources resources)
Globals.MESSAGES_KEY
as the key. You can
use this method, if your action calls
Action.getResources(HttpServletRequest)
.
The deprecated method Action.getResources()
takes the resources from the servlet context with the same key.
If your action uses this method, you have to set the resources
manually to the servlet context.
resources
- the messages resourcespublic void setResources(java.lang.String key, org.apache.struts.util.MessageResources resources)
Action.getResources(HttpServletRequest, String)
.
Please note that the MockModuleConfig
is set by Mockrunner as the current module. It has the name testmodule.
This can be changed with ModuleConfig.setPrefix
.
key
- the key of the messages resourcesresources
- the messages resourcespublic void setDataSource(javax.sql.DataSource dataSource)
DataSource
.
You can use this method, if your action calls
Action.getDataSource(HttpServletRequest)
.
dataSource
- DataSource
public void setDataSource(java.lang.String key, javax.sql.DataSource dataSource)
DataSource
.
You can use this method, if your action calls
Action.getDataSource(HttpServletRequest, String)
.
key
- the key of the DataSource
dataSource
- DataSource
public void setLocale(java.util.Locale locale)
Globals.LOCALE_KEY
as the key. You can
use this method, if your action calls
Action.getLocale(HttpServletRequest)
.
locale
- the localepublic org.apache.commons.validator.ValidatorResources createValidatorResources(java.lang.String[] resourcesFiles)
ValidatorResources
object based
on the specified config files. Since the parsing of the files
is time consuming, it makes sense to cache the result.
You can set the returned ValidatorResources
object
with setValidatorResources(org.apache.commons.validator.ValidatorResources)
. It is then used in
all validations.
resourcesFiles
- the array of config filespublic void setValidatorResources(org.apache.commons.validator.ValidatorResources validatorResources)
ValidatorResources
. The easiest
way to create ValidatorResources
is the method
createValidatorResources(java.lang.String[])
.
validatorResources
- the ValidatorResources
public void verifyForward(java.lang.String path)
mapping.findForward("success")
to find the forward, you can use this method or
verifyForwardName(java.lang.String)
to test the success
forward
name. If your action creates an ActionForward
on its
own you can use this method to verify the forward path
.
path
- the expected path
VerifyFailedException
- if verification failspublic void verifyForwardName(java.lang.String name)
mapping.findForward("success")
to find the forward, you can use this method or
verifyForward(java.lang.String)
to test the success
forward
name. If your action creates an ActionForward
on its
own you can use this method to verify the forward name
.
name
- the expected name
VerifyFailedException
- if verification failspublic void verifyRedirect(boolean redirect)
redirect
- the expected redirect attribute
VerifyFailedException
- if verification failspublic void verifyNoActionErrors()
VerifyFailedException
- if verification failspublic void verifyNoActionMessages()
VerifyFailedException
- if verification failspublic void verifyHasActionErrors()
VerifyFailedException
- if verification failspublic void verifyHasActionMessages()
VerifyFailedException
- if verification failspublic void verifyActionErrorPresent(java.lang.String errorKey)
errorKey
- the expected error key
VerifyFailedException
- if verification failspublic void verifyActionMessagePresent(java.lang.String messageKey)
messageKey
- the expected message key
VerifyFailedException
- if verification failspublic void verifyActionErrorNotPresent(java.lang.String errorKey)
errorKey
- the error key
VerifyFailedException
- if verification failspublic void verifyActionMessageNotPresent(java.lang.String messageKey)
messageKey
- the message key
VerifyFailedException
- if verification failspublic void verifyActionErrors(java.lang.String[] errorKeys)
errorKeys
- the array of expected error keys
VerifyFailedException
- if verification failspublic void verifyActionMessages(java.lang.String[] messageKeys)
messageKeys
- the array of expected message keys
VerifyFailedException
- if verification failspublic void verifyActionErrorValues(java.lang.String errorKey, java.lang.Object[] values)
errorKey
- the error keyvalues
- the exepcted values
VerifyFailedException
- if verification failspublic void verifyActionMessageValues(java.lang.String messageKey, java.lang.Object[] values)
messageKey
- the message keyvalues
- the exepcted values
VerifyFailedException
- if verification failspublic void verifyActionErrorValue(java.lang.String errorKey, java.lang.Object value)
errorKey
- the error keyvalue
- the exepcted value
VerifyFailedException
- if verification failspublic void verifyActionMessageValue(java.lang.String messageKey, java.lang.Object value)
messageKey
- the message keyvalue
- the exepcted value
VerifyFailedException
- if verification failspublic void verifyActionErrorProperty(java.lang.String errorKey, java.lang.String property)
errorKey
- the error keyproperty
- the exepcted value
VerifyFailedException
- if verification failspublic void verifyActionMessageProperty(java.lang.String messageKey, java.lang.String property)
messageKey
- the message keyproperty
- the exepcted value
VerifyFailedException
- if verification failspublic void verifyNumberActionErrors(int number)
number
- the expected number of errors
VerifyFailedException
- if verification failspublic void verifyNumberActionMessages(int number)
number
- the expected number of messages
VerifyFailedException
- if verification failspublic org.apache.struts.action.ActionMessage getActionErrorByKey(java.lang.String errorKey)
errorKey
- the error key
public org.apache.struts.action.ActionMessage getActionMessageByKey(java.lang.String messageKey)
messageKey
- the message key
public void setActionMessages(org.apache.struts.action.ActionMessages messages)
ActionMessages
object
as the currently present messages to the request.
messages
- the ActionMessages objectpublic void setActionMessagesToSession(org.apache.struts.action.ActionMessages messages)
ActionMessages
object
as the currently present messages to the session.
messages
- the ActionMessages objectpublic org.apache.struts.action.ActionMessages getActionMessages()
actionPerform(java.lang.Class)
to get the messages the action
has set. If messages in the session are recognized
(use setRecognizeMessagesInSession(boolean)
), this method
returns the union of request and session messages. Otherwise,
it only returns the request messages.
public org.apache.struts.action.ActionMessages getActionMessagesFromRequest()
public org.apache.struts.action.ActionMessages getActionMessagesFromSession()
public boolean hasActionMessages()
public void setActionErrors(org.apache.struts.action.ActionMessages errors)
ActionErrors
object
as the currently present errors to the request.
errors
- the ActionErrors objectpublic void setActionErrorsToSession(org.apache.struts.action.ActionMessages errors)
ActionErrors
object
as the currently present errors to the session.
errors
- the ActionErrors objectpublic org.apache.struts.action.ActionMessages getActionErrors()
actionPerform(java.lang.Class)
to get the errors the action
has set. If messages in the session are recognized
(use setRecognizeMessagesInSession(boolean)
), this method
returns the union of request and session errors. Otherwise,
it only returns the request errors.
public org.apache.struts.action.ActionMessages getActionErrorsFromRequest()
public org.apache.struts.action.ActionMessages getActionErrorsFromSession()
public boolean hasActionErrors()
public MockActionMapping getMockActionMapping()
ActionMockObjectFactory.getMockActionMapping()
.
public org.apache.struts.action.ActionMapping getActionMapping()
ActionMockObjectFactory.getActionMapping()
.
public MockPageContext getMockPageContext()
MockPageContext
object.
Delegates to WebMockObjectFactory.getMockPageContext()
.
public MockActionForward getActionForward()
ActionForward
.
Can be called after actionPerform(java.lang.Class)
to get
the ActionForward
the action
has returned.
public org.apache.struts.action.Action getLastAction()
Action
object.
Action
objectpublic void generateValidToken()
public org.apache.struts.action.ActionForm getActionForm()
ActionForm
.
ActionForm
objectpublic void setActionForm(org.apache.struts.action.ActionForm formObj)
ActionForm
object as the
current ActionForm
.
formObj
- the ActionForm
objectpublic org.apache.struts.action.ActionForm createActionForm(java.lang.Class form)
ActionForm
object of the specified
type and sets it as the current ActionForm
.
form
- the Class
of the formpublic org.apache.struts.action.DynaActionForm createDynaActionForm(org.apache.struts.config.FormBeanConfig formConfig)
DynaActionForm
based on the specified
form config and sets it as the current ActionForm
.
formConfig
- the FormBeanConfig
public void populateRequestToForm()
ActionForm
. The form will be reset
before populating if reset is enabled (setReset(boolean)
.
If form validation is enabled (use setValidate(boolean)
) the
form will be validated after populating it and the
appropriate ActionErrors
will be set.
public org.apache.struts.action.ActionForward actionPerform(java.lang.Class action)
ActionForm
. Sets the current action
form to null
.
action
- the Class
of the action
ActionForward
public org.apache.struts.action.ActionForward actionPerform(org.apache.struts.action.Action action)
ActionForm
. Sets the current ActionForm
to null
.
action
- the Action
ActionForward
public org.apache.struts.action.ActionForward actionPerform(java.lang.Class action, java.lang.Class form)
ActionForm
of the specified type.
Creates the appropriate ActionForm
, sets it as the
current ActionForm
and populates it before calling the action
(if populating is disabled, the form will not be populated, use
setDoPopulate(boolean)
).
If form validation is enabled (use setValidate(boolean)
) and
fails, the action will not be called. In this case,
the returned ActionForward
is based on the
input attribute. (Set it with setInput(java.lang.String)
).
action
- the Class
of the actionform
- the Class
of the form
ActionForward
public org.apache.struts.action.ActionForward actionPerform(org.apache.struts.action.Action action, java.lang.Class form)
ActionForm
of the specified type.
Creates the appropriate ActionForm
, sets it as the
current ActionForm
and populates it before calling the action
(if populating is disabled, the form will not be populated, use
setDoPopulate(boolean)
).
If form validation is enabled (use setValidate(boolean)
) and
fails, the action will not be called. In this case,
the returned ActionForward
is based on the
input attribute. (Set it with setInput(java.lang.String)
).
action
- the Action
form
- the Class
of the form
ActionForward
public org.apache.struts.action.ActionForward actionPerform(java.lang.Class action, org.apache.struts.action.ActionForm form)
ActionForm
object. The form will
be set as the current ActionForm
and
will be populated before the action is called (if populating is
disabled, the form will not be populated, use setDoPopulate(boolean)
).
Please note that request parameters will eventually overwrite
form values. Furthermore the form will be reset
before populating it. If you do not want that, disable reset
using setReset(boolean)
. If form validation is enabled
(use setValidate(boolean)
) and fails, the action will not be
called. In this case, the returned ActionForward
is based on the input attribute. (Set it with setInput(java.lang.String)
).
action
- the Class
of the actionform
- the ActionForm
object
ActionForward
public org.apache.struts.action.ActionForward actionPerform(org.apache.struts.action.Action action, org.apache.struts.action.ActionForm form)
ActionForm
object. The form will
be set as the current ActionForm
and
will be populated before the action is called (if populating is
disabled, the form will not be populated, use setDoPopulate(boolean)
).
Please note that request parameters will eventually overwrite
form values. Furthermore the form will be reset
before populating it. If you do not want that, disable reset
using setReset(boolean)
. If form validation is enabled
(use setValidate(boolean)
) and fails, the action will not be
called. In this case, the returned ActionForward
is based on the input attribute. (Set it with setInput(java.lang.String)
).
action
- the Action
form
- the ActionForm
object
ActionForward
public java.lang.String getOutput()
getOutput
in class HTMLOutputModule
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |