|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object org.apache.click.MockContainer
public class MockContainer
Provides a mock container for testing Click Pages.
Use thestart()
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>
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<? extends Page> |
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<? extends Page> |
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. |
|
|
testPage(Class<T> pageClass)
This method simulates a browser requesting (GET) or submitting (POST) the url associated with the specified pageClass and request parameters. |
|
|
testPage(Class<T> 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 |
---|
public MockContainer(String webappPath)
webappPath
- specifies the web application root path where
resources eg templates and images can be found.public MockContainer(String webappPath, Locale locale)
webappPath
- specifies the web application root path where
resources eg templates and images can be found.locale
- the container localeMethod Detail |
---|
public MockRequest getRequest()
MockRequest
.
public void setRequest(MockRequest request)
MockRequest
.
request
- the container MockRequestpublic MockResponse getResponse()
MockResponse
.
public void setResponse(MockResponse response)
MockResponse
.
response
- the container MockResponsepublic ClickServlet getClickServlet()
ClickServlet
.
public void setClickServlet(ClickServlet clickServlet)
ClickServlet
.
clickServlet
- the container ClickServletpublic MockServletConfig getServletConfig()
MockServletConfig
.
public void setServletConfig(MockServletConfig servletConfig)
MockServletConfig
.
servletConfig
- the container MockServletConfigpublic MockServletContext getServletContext()
MockServletContext
.
public void setServletContext(MockServletContext servletContext)
MockServletContext
.
servletContext
- the container MockServletContextpublic MockSession getSession()
MockSession
.
public void setSession(MockSession session)
MockSession
.
session
- the container MockSessionpublic void start()
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(org.apache.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(); } }
stop()
public void stop()
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.
start()
public MockContainer setAttribute(String key, Object value)
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");
key
- the attribute keyvalue
- the attribute value
public MockContainer setParameter(String key, String value)
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");
key
- the parameter keyvalue
- the parameter value
public MockContainer setParameter(String key, String[] value)
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");
key
- the parameter namevalue
- the parameter values
public MockContainer setParameter(String fieldName, File file, String contentType)
container.setParameter("helpfile", new File("c:/help.pdf"), "application/pdf").setParameter("toc", new File("c:/toc.html"),"text/html");
fieldName
- the name of the upload field.file
- the file to uploadcontentType
- content type of the file
public <T extends Page> Page testPage(Class<T> pageClass, Map<?,?> parameters)
pageClass
- specifies the class of the Page to testparameters
- the request parameters
testPage(Class)
public <T extends Page> T testPage(Class<T> pageClass)
ClickServlet
for processing and returns the Page instance that was created.
pageClass
- specifies the class of the Page to test
public Page testPage(String path)
path
- the page path
testPage(Class)
public Page testPage(String path, Map<?,?> parameters)
path
- the page pathparameters
- the request parameters to set
testPage(Class)
public String getHtml()
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.
public String getForwardOrRedirectUrl()
public String getForward()
Page
forwarded to.
public Class<? extends Page> getForwardPageClass()
Page
forwarded to.
public String getRedirect()
Page
redirected to.
public Class<? extends Page> getRedirectPageClass()
Page
redirected to.
public static MockRequest findMockRequest(ServletRequest request)
request
- the servlet request
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |