wicket.protocol.http
Class MockWebApplication

java.lang.Object
  extended by wicket.Application
      extended by wicket.protocol.http.WebApplication
          extended by wicket.protocol.http.MockWebApplication
All Implemented Interfaces:
ISessionFactory
Direct Known Subclasses:
WicketTester

public class MockWebApplication
extends WebApplication

This class provides a mock implementation of a Wicket HTTP based application 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. 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 application 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-application 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 initialisation. The servlet request object has some handy convenience methods for initialising 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

Field Summary
 
Fields inherited from class wicket.Application
CONFIGURATION, CONTEXTPATH, DEPLOYMENT, DEVELOPMENT
 
Constructor Summary
MockWebApplication(java.lang.String path)
          Create the mock http application that can be used for testing.
 
Method Summary
 WebRequestCycle createRequestCycle()
          Create and process the request cycle using the current request and response information.
 java.lang.Class getHomePage()
          Application subclasses must specify a home page class by implementing this abstract method.
 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
 javax.servlet.ServletContext getServletContext()
          Get the context object so that we can apply configurations to it.
 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.
protected  ISessionStore newSessionStore()
          Creates a new session facade.
 void processRequestCycle()
          Create and process the request cycle using the current request and response information.
 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 setHomePage(java.lang.Class clazz)
          Sets the home page for this mock application
 void setParametersForNextRequest(java.util.Map parametersForNextRequest)
          Sets the parameters to be set on the next request.
 void setupRequestAndResponse()
          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 wicket.protocol.http.WebApplication
getApplicationKey, getDefaultRequestCycleFactory, getRequestCycleProcessor, getRequestLogger, getSessionAttributePrefix, getSessionFactory, getWicketServlet, init, internalDestroy, internalInit, logEventTarget, logResponseTarget, mount, mount, mountBookmarkablePage, mountBookmarkablePage, mountBookmarkablePage, mountSharedResource, newRequestCycleProcessor, newSession, newWebRequest, newWebResponse, sessionDestroyed, setApplicationKey, setRequestLogger, setSessionFactory, setWicketServlet, unmount
 
Methods inherited from class wicket.Application
addComponentInstantiationListener, configure, configure, configure, destroy, exists, get, get, getAjaxSettings, getApplicationSettings, getConfigurationType, getDebugSettings, getExceptionSettings, getFrameworkSettings, getMarkupCache, getMarkupSettings, getMetaData, getName, getPageSettings, getRequestCycleSettings, getResourceSettings, getSecuritySettings, getSessionSettings, getSessionStore, getSettings, getSharedResources, initializeComponents, isAttached, removeComponentInstantiationListener, set, setMetaData, unset
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

MockWebApplication

public MockWebApplication(java.lang.String path)
Create the mock http application that can be used for testing.

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

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

getServletContext

public javax.servlet.ServletContext getServletContext()
Get the context object so that we can apply configurations to it. This method always returns an instance of MockServletContext, so it is fine to cast the result to this class in order to get access to the set methods.

Returns:
The servlet context

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()
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 -

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 void 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 initialised 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

getHomePage

public java.lang.Class getHomePage()
Description copied from class: Application
Application subclasses must specify a home page class by implementing this abstract method.

Specified by:
getHomePage in class Application
Returns:
Home page class for this application
See Also:
Application.getHomePage()

setHomePage

public void setHomePage(java.lang.Class clazz)
Sets the home page for this mock application

Parameters:
clazz -

newSessionStore

protected ISessionStore newSessionStore()
Description copied from class: Application
Creates a new session facade. Is called once per application, and is typically not something clients reimplement.

Overrides:
newSessionStore in class WebApplication
Returns:
The session facade
See Also:
WebApplication.newSessionStore()


Copyright © 2004-2007 Wicket developers. All Rights Reserved.