org.apache.click
Class MockContext

java.lang.Object
  extended by org.apache.click.Context
      extended by org.apache.click.MockContext

public class MockContext
extends Context

Provides a mock Context object for unit testing.

Note: if you want to test your Click Page instances use MockContainer instead.

This class defines a couple of helper methods to quickly create all the mock objects needed for unit testing. Please see the following methods:

To use this class in your own tests invoke one of the methods above. For example:
 public class FormTest extends TestCase {
     // Create a mock context
     MockContext context = MockContext.initContext("test-form.htm");
     MockRequest request = context.getMockRequest();

     // The request value that should be set as the textField value
     String requestValue = "one";

     // Set form name and field name parameters
     request.setParameter("form_name", "form");
     request.setParameter("name", requestValue);

     // Create form and fields
     Form form = new Form("form");
     TextField nameField = new TextField("name");
     form.add(nameField);

     // Check that nameField value is null
     Assert.assertNull(nameField.getValueObject());

     // Simulate a form onProcess event
     form.onProcess();

     // Check that nameField value is now bound to request value
     Assert.assertEquals(requestValue, nameField.getValueObject());
 } 
Please note: using MockContext to run performance tests over a large number of Controls could lead to out of memory errors. If you run into memory issues, you can either re-recreate a MockContext or invoke reset(), which removes all references to Controls, ActionListeners and Behaviors.


Field Summary
 
Fields inherited from class org.apache.click.Context
config, context, isPost, LOCALE, response
 
Method Summary
 boolean executeActionListeners()
          Execute all listeners that was registered by processed Controls.
 boolean executeBehaviors()
          Execute all behaviors that was registered by processed Controls.
 void executePreDestroy()
          Execute the preDestroy method for all registered behaviors.
 void executePreRenderHeadElements()
          Execute the preRenderHeadElements method for all registered behaviors.
 void executePreResponse()
          Execute the preResponse method for all registered behaviors.
 boolean fireActionEventsAndClearRegistry()
          Deprecated. use executeActionListeners() instead
 MockRequest getMockRequest()
          Return the MockRequest instance for this context.
 ClickServlet getServlet()
          Return the mock ClickServlet instance for this context.
static MockContext initContext()
          Creates and returns a new Context instance.
static MockContext initContext(HttpServletRequest request)
          Deprecated. use one of the other initContext methods because those will construct a complete mock stack including a MockRequest.
static MockContext initContext(Locale locale)
          Creates and returns a new Context instance for the specified locale.
static MockContext initContext(Locale locale, String servletPath)
          Creates and returns a new Context instance for the specified locale and servletPath.
static MockContext initContext(MockServletConfig servletConfig, MockRequest request, MockResponse response, ClickServlet clickServlet)
          Creates and returns a new Context instance for the specified mock objects.
static MockContext initContext(MockServletConfig servletConfig, MockRequest request, MockResponse response, ClickServlet clickServlet, ActionEventDispatcher actionEventDispatcher, ControlRegistry controlRegistry)
          Creates and returns a new Context instance for the specified mock objects.
static MockContext initContext(String servletPath)
          Creates and returns a new Context instance for the specified servletPath.
 void reset()
          Reset mock internal state.
 
Methods inherited from class org.apache.click.Context
createMessagesMap, createPage, createPage, getApplicationMode, getCharset, getCookie, getCookieValue, getFileItem, getFileItemMap, getLocale, getPageClass, getPagePath, getRequest, getRequestAttribute, getRequestParameter, getRequestParameterValues, getResourcePath, getResponse, getServletConfig, getServletContext, getSession, getSessionAttribute, getThreadLocalContext, hasRequestAttribute, hasRequestParameter, hasSession, hasSessionAttribute, hasThreadLocalContext, invalidateCookie, isAjaxRequest, isForward, isGet, isMultipartRequest, isPost, removeSessionAttribute, renderTemplate, renderTemplate, setCookie, setFlashAttribute, setLocale, setRequestAttribute, setSessionAttribute
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

getServlet

public ClickServlet getServlet()
Return the mock ClickServlet instance for this context.

Returns:
the clickServlet instance

getMockRequest

public MockRequest getMockRequest()
Return the MockRequest instance for this context.

Returns:
the MockRequest instance

initContext

public static MockContext initContext()
Creates and returns a new Context instance.

Note: servletPath will default to '/mock.htm'.

Returns:
new Context instance

initContext

public static MockContext initContext(String servletPath)
Creates and returns a new Context instance for the specified servletPath.

Parameters:
servletPath - the requests servletPath
Returns:
new Context instance

initContext

public static MockContext initContext(Locale locale)
Creates and returns a new Context instance for the specified locale. Note: servletPath will default to '/mock.htm'.

Parameters:
locale - the requests locale
Returns:
new Context instance

initContext

public static MockContext initContext(HttpServletRequest request)
Deprecated. use one of the other initContext methods because those will construct a complete mock stack including a MockRequest.

Creates and returns new Context instance for the specified request.

Parameters:
request - the mock request
Returns:
new Context instance

initContext

public static MockContext initContext(Locale locale,
                                      String servletPath)
Creates and returns a new Context instance for the specified locale and servletPath.

Parameters:
locale - the requests locale
servletPath - the requests servletPath
Returns:
new Context instance

initContext

public static MockContext initContext(MockServletConfig servletConfig,
                                      MockRequest request,
                                      MockResponse response,
                                      ClickServlet clickServlet)
Creates and returns a new Context instance for the specified mock objects.

Parameters:
servletConfig - the mock servletConfig
request - the mock request
response - the mock response
clickServlet - the mock clickServlet
Returns:
new Context instance

initContext

public static MockContext initContext(MockServletConfig servletConfig,
                                      MockRequest request,
                                      MockResponse response,
                                      ClickServlet clickServlet,
                                      ActionEventDispatcher actionEventDispatcher,
                                      ControlRegistry controlRegistry)
Creates and returns a new Context instance for the specified mock objects.

Parameters:
servletConfig - the mock servletConfig
request - the mock request
response - the mock response
clickServlet - the mock clickServlet
actionEventDispatcher - action and behavior dispatcher
controlRegistry - the control registry
Returns:
new Context instance

executeActionListeners

public boolean executeActionListeners()
Execute all listeners that was registered by processed Controls.

Returns:
true if all listeners returned true, false otherwise

executeBehaviors

public boolean executeBehaviors()
Execute all behaviors that was registered by processed Controls.

Returns:
true if all behaviors returned true, false otherwise

executePreResponse

public void executePreResponse()
Execute the preResponse method for all registered behaviors.


executePreRenderHeadElements

public void executePreRenderHeadElements()
Execute the preRenderHeadElements method for all registered behaviors.


executePreDestroy

public void executePreDestroy()
Execute the preDestroy method for all registered behaviors.


fireActionEventsAndClearRegistry

public boolean fireActionEventsAndClearRegistry()
Deprecated. use executeActionListeners() instead

Fire all action events that was registered by the processed Controls.

Returns:
true if all listeners returned true, false otherwise

reset

public void reset()
Reset mock internal state. Running a large number of tests using the same MockContext could lead to out of memory errors. Calling this method will remove any references to objects, thus freeing up memory.