net.sourceforge.stripes.mock
Class MockRoundtrip

java.lang.Object
  extended by net.sourceforge.stripes.mock.MockRoundtrip

public class MockRoundtrip
extends Object

Mock object that attempts to make it easier to use the other Mock objects in this package to interact with Stripes and to interrogate the results. Everything that is done in this class is do-able without this class! It simply exists to make things a bit easier. As a result all the methods in this class simply manipulate one or more of the underlying Mock objects. If some needed capability is not exposed through the MockRoundtrip it is always possible to fetch the underlying request, response and context and interact with them directly.

It is worth noting that the Mock system does not process forwards, includes and redirects. When an ActionBean (or other object) invokes the servlet APIs for any of these actions it is recorded so that it can be reported and verified later. In the majority of cases it should be sufficient to test ActionBeans in isolation and verify that they produced the expected output data and/or forward/redirect. If your ActionBeans depend on being able to include other resources before continuing, sorry - you're on your own!

An example usage of this class might look like:

 MockServletContext context = ...;
 MockRoundtrip trip = new MockRoundtrip(context, CalculatorActionBean.class);
 trip.setParameter("numberOne", "2");
 trip.setParameter("numberTwo", "2");
 trip.execute();
 CalculatorActionBean bean = trip.getActionBean(CalculatorActionBean.class);
 Assert.assertEquals(bean.getResult(), 4, "two plus two should equal four");
 Assert.assertEquals(trip.getDestination(), ""/quickstart/index.jsp");
 

Since:
Stripes 1.1.1
Author:
Tim Fennell

Field Summary
static String DEFAULT_SOURCE_PAGE
          Default value for the source page that generated this round trip request.
 
Constructor Summary
MockRoundtrip(MockServletContext context, Class<? extends ActionBean> beanType)
          Preferred constructor that will manufacture a request.
MockRoundtrip(MockServletContext context, Class<? extends ActionBean> beanType, MockHttpSession session)
          Preferred constructor that will manufacture a request.
MockRoundtrip(MockServletContext context, String actionBeanUrl)
          Constructor that will create a requeset suitable for the provided servlet context and URL.
MockRoundtrip(MockServletContext context, String actionBeanUrl, MockHttpSession session)
          Constructor that will create a requeset suitable for the provided servlet context and URL.
 
Method Summary
 void addParameter(String name, String... value)
          Adds the value provided to the set of values for the named request parameter.
 void execute()
          Executes the request in the servlet context that was provided in the constructor.
 void execute(String event)
          Executes the request in the servlet context that was provided in the constructor.
<A extends ActionBean>
A
getActionBean(Class<A> type)
          Gets the instance of the ActionBean type provided that was instantiated by Stripes to handle the request.
 MockServletContext getContext()
          Get the ActionBean context to be used by this round trip
 String getDestination()
          Gets the URL to which Stripes was directed after invoking the ActionBean.
 String getForwardUrl()
          If the request resulted in a forward, returns the URL that was forwarded to.
 byte[] getOutputBytes()
          Gets, as bytes, any data that was written to the output stream associated with the request.
 String getOutputString()
          Gets, as a String, any data that was written to the output stream associated with the request.
 String getRedirectUrl()
          If the request resulted in a redirect, returns the URL that was redirected to.
 MockHttpServletRequest getRequest()
          Get the servlet request object to be used by this round trip
 MockHttpServletResponse getResponse()
          Get the servlet response object to be used by this round trip
 ValidationErrors getValidationErrors()
          Gets the (potentially empty) set of Validation Errors that were produced by the request.
protected  void setContext(MockServletContext context)
          Set the ActionBean context to be used by this round trip
 void setParameter(String name, String... value)
          Sets the named request parameter to the value or values provided.
protected  void setRequest(MockHttpServletRequest request)
          Set the servlet request object to be used by this round trip
protected  void setResponse(MockHttpServletResponse response)
          Set the servlet response object to be used by this round trip
 void setSourcePage(String url)
          All requests to Stripes that can generate validation errors are required to supply a request parameter telling Stripes where the request came from.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_SOURCE_PAGE

public static final String DEFAULT_SOURCE_PAGE
Default value for the source page that generated this round trip request.

See Also:
Constant Field Values
Constructor Detail

MockRoundtrip

public MockRoundtrip(MockServletContext context,
                     Class<? extends ActionBean> beanType)
Preferred constructor that will manufacture a request. Uses the ServletContext to ensure that the request's context path matches. Pulls the UrlBinding of the ActionBean and uses that as the requst URL. Constructs a new session for the request.

Parameters:
context - the MockServletContext that will receive this request
beanType - a Class object representing the ActionBean that should receive the request

MockRoundtrip

public MockRoundtrip(MockServletContext context,
                     Class<? extends ActionBean> beanType,
                     MockHttpSession session)
Preferred constructor that will manufacture a request. Uses the ServletContext to ensure that the request's context path matches. Pulls the UrlBinding of the ActionBean and uses that as the requst URL. Constructs a new session for the request.

Parameters:
context - the MockServletContext that will receive this request
beanType - a Class object representing the ActionBean that should receive the request

MockRoundtrip

public MockRoundtrip(MockServletContext context,
                     String actionBeanUrl)
Constructor that will create a requeset suitable for the provided servlet context and URL. Note that in general the contructors that take an ActionBean Class object are preferred over those that take a URL. Constructs a new session for the request.

Parameters:
context - the MockServletContext that will receive this request
actionBeanUrl - the url binding of the action bean

MockRoundtrip

public MockRoundtrip(MockServletContext context,
                     String actionBeanUrl,
                     MockHttpSession session)
Constructor that will create a requeset suitable for the provided servlet context and URL. Note that in general the contructors that take an ActionBean Class object are preferred over those that take a URL. The request will use the provided session instead of creating a new one.

Parameters:
context - the MockServletContext that will receive this request
actionBeanUrl - the url binding of the action bean
session - an instance of MockHttpSession to use for the request
Method Detail

getRequest

public MockHttpServletRequest getRequest()
Get the servlet request object to be used by this round trip


setRequest

protected void setRequest(MockHttpServletRequest request)
Set the servlet request object to be used by this round trip


getResponse

public MockHttpServletResponse getResponse()
Get the servlet response object to be used by this round trip


setResponse

protected void setResponse(MockHttpServletResponse response)
Set the servlet response object to be used by this round trip


getContext

public MockServletContext getContext()
Get the ActionBean context to be used by this round trip


setContext

protected void setContext(MockServletContext context)
Set the ActionBean context to be used by this round trip


setParameter

public void setParameter(String name,
                         String... value)
Sets the named request parameter to the value or values provided. Any existing values are wiped out and replaced with the value(s) provided.


addParameter

public void addParameter(String name,
                         String... value)
Adds the value provided to the set of values for the named request parameter. If one or more values already exist they will be retained, and the new value will be appended to the set of values.


setSourcePage

public void setSourcePage(String url)
All requests to Stripes that can generate validation errors are required to supply a request parameter telling Stripes where the request came from. If you do not supply a value for this parameter then the value of MockRoundTrip.DEFAULT_SOURCE_PAGE will be used.


execute

public void execute()
             throws Exception
Executes the request in the servlet context that was provided in the constructor. If the request throws an Exception then that will be thrown from this method. Otherwise, once the execution has completed you can use the other methods on this class to examine the outcome.

Throws:
Exception

execute

public void execute(String event)
             throws Exception
Executes the request in the servlet context that was provided in the constructor. Sets up the request so that it mimics the submission of a specific event, named by the 'event' parameter to this method. If the request throws an Exception then that will be thrown from this method. Otherwise, once the execution has completed you can use the other methods on this class to examine the outcome.

Throws:
Exception

getActionBean

public <A extends ActionBean> A getActionBean(Class<A> type)
Gets the instance of the ActionBean type provided that was instantiated by Stripes to handle the request. If a bean of this type was not instantiated, this method will return null.

Parameters:
type - the Class object representing the ActionBean type expected
Returns:
the instance of the ActionBean that was created by Stripes

getValidationErrors

public ValidationErrors getValidationErrors()
Gets the (potentially empty) set of Validation Errors that were produced by the request.


getOutputBytes

public byte[] getOutputBytes()
Gets, as bytes, any data that was written to the output stream associated with the request. Note that since the Mock system does not write standard HTTP response information (headers etc.) to the output stream, this will be exactly what was written by the ActionBean.


getOutputString

public String getOutputString()
Gets, as a String, any data that was written to the output stream associated with the request. Note that since the Mock system does not write standard HTTP response information (headers etc.) to the output stream, this will be exactly what was written by the ActionBean.


getDestination

public String getDestination()
Gets the URL to which Stripes was directed after invoking the ActionBean. Assumes that the request was either forwarded or redirected exactly once. If the request was forwarded then the forwarded URL will be returned verbatim. If the response was redirected and the redirect URL was within the same web application, then the URL returned will exclude the context path. I.e. the URL returned will be the same regardless of whether the page was forwarded to or redirected to.


getForwardUrl

public String getForwardUrl()
If the request resulted in a forward, returns the URL that was forwarded to.


getRedirectUrl

public String getRedirectUrl()
If the request resulted in a redirect, returns the URL that was redirected to. Unlike getDestination(), the URL in this case will be the exact URL that would have been sent to the browser (i.e. including the servlet context).



? Copyright 2005-2006, Stripes Development Team.