net.sf.click
Class MockContainer

java.lang.Object
  extended bynet.sf.click.MockContainer

public class MockContainer
extends Object

Provides a mock container for testing Click Pages.

Use the start() and stop() methods to control the life cycle of the container. Each call to start / stop instantiates new mock instances for the container.

To instantiate a container you must specify a web application directory where your page templates and other resources like images, javascript and stylesheets are available.

You can set the web application root to refer to your actual live project's web directory.

For example if you are busy developing a web application located under 'c:\dev\myapp\web', you can start the MockContainerTest as follows:

 public class MyTest extends junit.framework.TestCase {
     public void testMyPage() {
         MockContainer container = new MockContainer("c:/dev/myapp/web");
         container.start();

         container.setParameter("param", "one");
         MyPage page = (MyPage) container.testPage(MyPage.class);
         Assert.assertEquals("one", page.getParam());

         container.stop();
     }
 }
 
Together with a valid web application directory you also need to have the click.xml available, either in the WEB-INF/click.xml directory or on your classpath.

Taking the above example further, if your application is developed under 'c:\dev\myapp\web', click.xml would be available at 'WEB-INF\click.xml'. The full path would be 'c:\dev\myapp\web\WEB-INF\click.xml'.

Alternatively click.xml can also be specified on the classpath. For example you can save click.xml in your src folder eg: 'c:\dev\myapp\web\src\click.xml'. Below is an example click.xml to get up and running quickly:

 <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
 <click-app charset="UTF-8">
   <pages package="com.mycorp.pages"/>
   <mode value="trace"/>
 </click-app>
 

Author:
Bob Schellink

Constructor Summary
MockContainer(String webappPath)
          Create a new container for the specified webappPath.
MockContainer(String webappPath, Locale locale)
          Create a new container for the specified webappPath and locale.
 
Method Summary
static MockRequest findMockRequest(ServletRequest request)
          Find and return the MockRequest from the request stack.
 ClickServlet getClickServlet()
          Return the container ClickServlet.
 String getForward()
          Return the path that Page forwarded to.
 String getForwardOrRedirectUrl()
          Return the forward or redirect url as set by the Page.
 Class getForwardPageClass()
          Return the Class that Page forwarded to.
 String getHtml()
          Returns the html output that was generated by HttpServletResponse.
 String getRedirect()
          Return the path that Page redirected to.
 Class getRedirectPageClass()
          Return the Class that Page redirected to.
 MockRequest getRequest()
          Return the container MockRequest.
 MockResponse getResponse()
          Return the container MockResponse.
 MockServletConfig getServletConfig()
          Return the container MockServletConfig.
 MockServletContext getServletContext()
          Return the container MockServletContext.
 MockSession getSession()
          Return the container MockSession.
 MockContainer setAttribute(String key, Object value)
          Convenience method for setting the MockRequest attribute.
 void setClickServlet(ClickServlet clickServlet)
          Set the container ClickServlet.
 MockContainer setParameter(String fieldName, File file, String contentType)
          Convenience method for setting files to be uploaded.
 MockContainer setParameter(String key, String value)
          Convenience method for setting the MockRequest parameter.
 MockContainer setParameter(String key, String[] value)
          Convenience method for setting multi-valued MockRequest parameters.
 void setRequest(MockRequest request)
          Set the container MockRequest.
 void setResponse(MockResponse response)
          Set the container MockResponse.
 void setServletConfig(MockServletConfig servletConfig)
          Set the container MockServletConfig.
 void setServletContext(MockServletContext servletContext)
          Set the container MockServletContext.
 void setSession(MockSession session)
          Set the container MockSession.
 void start()
          Starts the container and configure it for testing Page instances.
 void stop()
          Stops the container.
 Page testPage(Class pageClass)
          This method simulates a browser requesting (GET) or submitting (POST) the url associated with the specified pageClass and request parameters.
 Page testPage(Class pageClass, Map parameters)
          This method simulates a browser requesting (GET) or submitting (POST) the url associated with the specified pageClass and parameters.
 Page testPage(String path)
          This method simulates a browser requesting (GET) or submitting (POST) the specified path.
 Page testPage(String path, Map parameters)
          This method simulates a browser requesting (GET) or submitting (POST) the specified path and request parameters.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

MockContainer

public MockContainer(String webappPath)
Create a new container for the specified webappPath.

Parameters:
webappPath - specifies the web application root path where resources eg templates and images can be found.

MockContainer

public MockContainer(String webappPath,
                     Locale locale)
Create a new container for the specified webappPath and locale.

Parameters:
webappPath - specifies the web application root path where resources eg templates and images can be found.
locale - the container locale
Method Detail

getRequest

public MockRequest getRequest()
Return the container MockRequest.

Returns:
the container MockRequest

setRequest

public void setRequest(MockRequest request)
Set the container MockRequest.

Parameters:
request - the container MockRequest

getResponse

public MockResponse getResponse()
Return the container MockResponse.

Returns:
the container MockResponse

setResponse

public void setResponse(MockResponse response)
Set the container MockResponse.

Parameters:
response - the container MockResponse

getClickServlet

public ClickServlet getClickServlet()
Return the container ClickServlet.

Returns:
the container ClickServlet

setClickServlet

public void setClickServlet(ClickServlet clickServlet)
Set the container ClickServlet.

Parameters:
clickServlet - the container ClickServlet

getServletConfig

public MockServletConfig getServletConfig()
Return the container MockServletConfig.

Returns:
the container MockServletConfig

setServletConfig

public void setServletConfig(MockServletConfig servletConfig)
Set the container MockServletConfig.

Parameters:
servletConfig - the container MockServletConfig

getServletContext

public MockServletContext getServletContext()
Return the container MockServletContext.

Returns:
the container MockServletContext

setServletContext

public void setServletContext(MockServletContext servletContext)
Set the container MockServletContext.

Parameters:
servletContext - the container MockServletContext

getSession

public MockSession getSession()
Return the container MockSession.

Returns:
the container MockSession

setSession

public void setSession(MockSession session)
Set the container MockSession.

Parameters:
session - the container MockSession

start

public void start()
Starts the container and configure it for testing Page instances.

During configuration a full mock servlet stack is created consisting of:

You can provide your own Mock implementations and set them on the container using the appropriate setter method for example: setRequest(net.sf.click.servlet.MockRequest).

Please note that you must set the mock objects on the container before calling start().

You also have full access to the mock objects after starting the container by using the appropriate getter method for example: getRequest().

Below is an example of how to start the container:

 public class TestPages extends junit.framework.TestCase {

     public void testAll() {
         String webApplicationDir = "c:/dev/app/web";
         MockContainer container = new MockContainer(webApplicationDir);

         container.start();
         ...
         container.stop();
     }
 }
 

See Also:
stop()

stop

public void stop()
Stops the container. The container cannot be used until start() is called again.

Please note that after each start / stop cycle the container is reconfigured with new mock instances. The mock instances from the previous test run is discarded.

See Also:
start()

setAttribute

public MockContainer setAttribute(String key,
                                  Object value)
Convenience method for setting the MockRequest attribute.

Note this method returns this so you can easily chain calls to this method.

For example:

 container.setAttribute("id", "100").setAttribute("name", "Peter").setAttribute("amount", "555.43");
 

Parameters:
key - the attribute key
value - the attribute value
Returns:
this MockContainer instance

setParameter

public MockContainer setParameter(String key,
                                  String value)
Convenience method for setting the MockRequest parameter.

Note this method returns this so you can easily chain calls to this method.

For example:

 container.setParameter("id", "100").setParameter("name", "Peter").setParameter("amount", "555.43");
 

Parameters:
key - the parameter key
value - the parameter value
Returns:
this MockContainer instance

setParameter

public MockContainer setParameter(String key,
                                  String[] value)
Convenience method for setting multi-valued MockRequest parameters.

Note this method returns this so you can easily chain calls to this method.

For example:

 String[] array = {"one", "two", "three"};
 container.setParameter("id", "100").setParameter("name", "Peter").setParameter("amount", "555.43");
 

Parameters:
key - the parameter name
value - the parameter values
Returns:
this MockContainer instance

setParameter

public MockContainer setParameter(String fieldName,
                                  File file,
                                  String contentType)
Convenience method for setting files to be uploaded.

Note this method returns this so you can easily chain calls to this method.

For example:

 container.setParameter("helpfile", new File("c:/help.pdf"), "application/pdf").setParameter("toc", new File("c:/toc.html"),"text/html");
 

Parameters:
fieldName - the name of the upload field.
file - the file to upload
contentType - content type of the file
Returns:
this MockContainer instance

testPage

public Page testPage(Class pageClass,
                     Map parameters)
This method simulates a browser requesting (GET) or submitting (POST) the url associated with the specified pageClass and parameters.

Parameters:
pageClass - specifies the class of the Page to test
parameters - the request parameters
Returns:
the Page instance for the specified pageClass
See Also:
testPage(Class)

testPage

public Page testPage(Class pageClass)
This method simulates a browser requesting (GET) or submitting (POST) the url associated with the specified pageClass and request parameters.

The container forwards the request to ClickServlet for processing and returns the Page instance that was created.

Parameters:
pageClass - specifies the class of the Page to test
Returns:
the Page instance for the specified pageClass

testPage

public Page testPage(String path)
This method simulates a browser requesting (GET) or submitting (POST) the specified path.

Note: the path must have a leading slash '/' for example '/test.htm'. If the path does not begin with the '/' character it will automatically be added.

Parameters:
path - the page path
Returns:
a new Page instance for the specified path
See Also:
testPage(Class)

testPage

public Page testPage(String path,
                     Map parameters)
This method simulates a browser requesting (GET) or submitting (POST) the specified path and request parameters.

Note: the path must have a leading slash '/' for example '/test.htm'. If the path does not begin with the '/' character it will automatically be added.

Parameters:
path - the page path
parameters - the request parameters to set
Returns:
a new Page instance for the specified path
See Also:
testPage(Class)

getHtml

public String getHtml()
Returns the html output that was generated by HttpServletResponse.

Please note: if the Page invokes Page.setForward(Class) or Page.setRedirect(Class), this method will return blank.

The reason for this is that forward and redirect calls are only recorded, not executed.

The forward and redirect path's are only used for assertion purposes.

JSP templates is not supported by this method because a JSP template is always accessed through a Page.setForward(Class) call.

Returns:
the rendered html document

getForwardOrRedirectUrl

public String getForwardOrRedirectUrl()
Return the forward or redirect url as set by the Page.

Note: redirect url's inside this application will have their context path removed. This ensures that a forward or redirect to the same url will have the same value.

Returns:
either forward or redirect value.

getForward

public String getForward()
Return the path that Page forwarded to.

Returns:
the path that Page forwarded to

getForwardPageClass

public Class getForwardPageClass()
Return the Class that Page forwarded to.

Returns:
the class that Page forwarded to

getRedirect

public String getRedirect()
Return the path that Page redirected to.

Returns:
the path that Page redirected to

getRedirectPageClass

public Class getRedirectPageClass()
Return the Class that Page redirected to.

Returns:
the Class that Page redirected to

findMockRequest

public static MockRequest findMockRequest(ServletRequest request)
Find and return the MockRequest from the request stack.

Parameters:
request - the servlet request
Returns:
the mock request