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 java.lang.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, java.lang.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.
 java.util.Map 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.
 javax.servlet.ServletContext newServletContext(java.lang.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.
 void processRequestCycle(java.lang.Class pageClass)
          Initialize a new WebRequestCycle and all its dependent objects
 void processRequestCycle(java.lang.Class 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 setParametersForNextRequest(java.util.Map 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,
                          java.lang.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(java.lang.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 void processRequestCycle(java.lang.Class pageClass)
Initialize a new WebRequestCycle and all its dependent objects

Parameters:
pageClass -

processRequestCycle

public void processRequestCycle(java.lang.Class pageClass,
                                PageParameters params)
Initialize a new WebRequestCycle and all its dependent objects

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)

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.


getParametersForNextRequest

public java.util.Map 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(java.util.Map 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



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