org.apache.wicket.protocol.http
Class MockWebApplication

java.lang.Object
  extended by org.apache.wicket.protocol.http.MockWebApplication
Direct Known Subclasses:
BaseWicketTester

public class MockWebApplication
extends Object

This class provides a mock implementation of a Wicket HTTP based tester that can be used for testing. It emulates all of the functionality of an HttpServlet in a controlled, single-threaded environment. It is supported with mock objects for WebSession, HttpServletRequest, HttpServletResponse and ServletContext.

In its most basic usage you can just create a new MockWebApplication and provide your Wicket Application object. This should be sufficient to allow you to construct components and pages and so on for testing. To use certain features such as localization you must also call setupRequestAndResponse().

The tester takes an optional path attribute that defines a directory on the disk which will correspond to the root of the WAR bundle. This can then be used for locating non-tester resources.

To actually test the processing of a particular page or component you can also call processRequestCycle() to do all the normal work of a Wicket request.

Between calling setupRequestAndResponse() and processRequestCycle() you can get hold of any of the objects for initialization. The servlet request object has some handy convenience methods for Initializing the request to invoke certain types of pages and components.

After completion of processRequestCycle() you will probably just be testing component states. However, you also have full access to the response document (or binary data) and result codes via the servlet response object.

IMPORTANT NOTES

Author:
Chris Turner

Constructor Summary
MockWebApplication(WebApplication application, String path)
          Create the mock http tester that can be used for testing.
 
Method Summary
 WebRequestCycle createRequestCycle()
          Create and process the request cycle using the current request and response information.
 void destroy()
          clears this mock application
 WebApplication getApplication()
          Gets the application object.
 Page getLastRenderedPage()
          Get the page that was just rendered by the last request cycle processing.
 Map<String,String[]> getParametersForNextRequest()
          Gets the parameters to be set on the next request.
 Page getPreviousRenderedPage()
          Get the page that was previously
 MockHttpServletRequest getServletRequest()
          Get the request object so that we can apply configurations to it.
 MockHttpServletResponse getServletResponse()
          Get the response object so that we can apply configurations to it.
 MockHttpSession getServletSession()
          Get the session object so that we can apply configurations to it.
 WebRequest getWicketRequest()
          Get the wicket request object.
 WebResponse getWicketResponse()
          Get the wicket response object.
 WebSession getWicketSession()
          Get the wicket session.
 boolean initializeHttpSessionAsTemporary()
          Callback to signal the application to create temporary sessions instead of normal sessions.
 boolean isCreateAjaxRequest()
          Gets createAjaxRequest.
 javax.servlet.ServletContext newServletContext(String path)
          Used to create a new mock servlet context.
 void postProcessRequestCycle(WebRequestCycle cycle)
           
 void processRequestCycle()
          Create and process the request cycle using the current request and response information.
<C extends Page>
void
processRequestCycle(Class<C> pageClass)
          Initialize a new WebRequestCycle and all its dependent objects
<C extends Page>
void
processRequestCycle(Class<C> pageClass, PageParameters params)
          Initialize a new WebRequestCycle and all its dependent objects
 void processRequestCycle(Component component)
          Initialize a new WebRequestCycle and all its dependent objects
 void processRequestCycle(WebRequestCycle cycle)
          Create and process the request cycle using the current request and response information.
 void setCreateAjaxRequest(boolean createAjaxRequest)
          Sets createAjaxRequest.
 void setParametersForNextRequest(Map<String,String[]> parametersForNextRequest)
          Sets the parameters to be set on the next request.
 WebRequestCycle setupRequestAndResponse()
          Reset the request and the response back to a starting state and recreate the necessary wicket request, response and session objects.
 WebRequestCycle setupRequestAndResponse(boolean isAjax)
          Reset the request and the response back to a starting state and recreate the necessary wicket request, response and session objects.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

MockWebApplication

public MockWebApplication(WebApplication application,
                          String path)
Create the mock http tester that can be used for testing.

Parameters:
application - The wicket application object
path - The absolute path on disk to the web tester contents (e.g. war root) - may be null
See Also:
MockServletContext
Method Detail

initializeHttpSessionAsTemporary

public boolean initializeHttpSessionAsTemporary()
Callback to signal the application to create temporary sessions instead of normal sessions. This should only be used if you want to test stuff like Session.bind(). Default returns false.

Returns:
true if sessions should be temporary by default, otherwise false

newServletContext

public javax.servlet.ServletContext newServletContext(String path)
Used to create a new mock servlet context.

Parameters:
path - The absolute path on disk to the web tester contents (e.g. war root) - may be null
Returns:
ServletContext

getApplication

public final WebApplication getApplication()
Gets the application object.

Returns:
Wicket application

getLastRenderedPage

public Page getLastRenderedPage()
Get the page that was just rendered by the last request cycle processing.

Returns:
The last rendered page

getPreviousRenderedPage

public Page getPreviousRenderedPage()
Get the page that was previously

Returns:
The last rendered page

getServletRequest

public MockHttpServletRequest getServletRequest()
Get the request object so that we can apply configurations to it.

Returns:
The request object

getServletResponse

public MockHttpServletResponse getServletResponse()
Get the response object so that we can apply configurations to it.

Returns:
The response object

getServletSession

public MockHttpSession getServletSession()
Get the session object so that we can apply configurations to it.

Returns:
The session object

getWicketRequest

public WebRequest getWicketRequest()
Get the wicket request object.

Returns:
The wicket request object

getWicketResponse

public WebResponse getWicketResponse()
Get the wicket response object.

Returns:
The wicket response object

getWicketSession

public WebSession getWicketSession()
Get the wicket session.

Returns:
The wicket session object

processRequestCycle

public void processRequestCycle(Component component)
Initialize a new WebRequestCycle and all its dependent objects

Parameters:
component -

processRequestCycle

public <C extends Page> void processRequestCycle(Class<C> pageClass)
Initialize a new WebRequestCycle and all its dependent objects

Type Parameters:
C -
Parameters:
pageClass -

processRequestCycle

public <C extends Page> void processRequestCycle(Class<C> pageClass,
                                                 PageParameters params)
Initialize a new WebRequestCycle and all its dependent objects

Type Parameters:
C -
Parameters:
pageClass -
params -

processRequestCycle

public void processRequestCycle()
Create and process the request cycle using the current request and response information.


processRequestCycle

public void processRequestCycle(WebRequestCycle cycle)
Create and process the request cycle using the current request and response information.

Parameters:
cycle -

postProcessRequestCycle

public final void postProcessRequestCycle(WebRequestCycle cycle)
Parameters:
cycle -

createRequestCycle

public WebRequestCycle createRequestCycle()
Create and process the request cycle using the current request and response information.

Returns:
A new and initialized WebRequestCyle

setupRequestAndResponse

public WebRequestCycle setupRequestAndResponse(boolean isAjax)
Reset the request and the response back to a starting state and recreate the necessary wicket request, response and session objects. The request and response objects can be accessed and Initialized at this point.

Parameters:
isAjax - indicates whether the request should be initialized as an ajax request (ajax header "Wicket-Ajax" is set)
Returns:
the constructed WebRequestCycle

setupRequestAndResponse

public WebRequestCycle setupRequestAndResponse()
Reset the request and the response back to a starting state and recreate the necessary wicket request, response and session objects. The request and response objects can be accessed and Initialized at this point.

Returns:
the constructed WebRequestCycle

getParametersForNextRequest

public Map<String,String[]> getParametersForNextRequest()
Gets the parameters to be set on the next request.

Returns:
the parameters to be set on the next request

setParametersForNextRequest

public void setParametersForNextRequest(Map<String,String[]> parametersForNextRequest)
Sets the parameters to be set on the next request.

Parameters:
parametersForNextRequest - the parameters to be set on the next request

destroy

public void destroy()
clears this mock application


isCreateAjaxRequest

public boolean isCreateAjaxRequest()
Gets createAjaxRequest.

Returns:
createAjaxRequest

setCreateAjaxRequest

public void setCreateAjaxRequest(boolean createAjaxRequest)
Sets createAjaxRequest. Must be true to create add a wicket-ajax header to the next request. Will be immediately reset to false

Parameters:
createAjaxRequest - createAjaxRequest


Copyright © 2004-2011 Apache Software Foundation. All Rights Reserved.