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 callback
     form.onProcess();

     // Check that nameField value is now bound to request value
     Assert.assertEquals(requestValue, nameField.getValueObject());
 }
 


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 the processed Controls.
 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 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.
 
Methods inherited from class org.apache.click.Context
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 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
controlRegistry - the controlRegistry instance
Returns:
new Context instance

executeActionListeners

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

Returns:
true if all listeners returned true, false otherwise

fireActionEventsAndClearRegistry

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

Fire all action events that was registered by the processed Controls, and clears all registered listeners from the ControlRegistry.

Returns:
true if all listeners returned true, false otherwise