Author: ehillenius
Date: Fri Jul 13 10:46:59 2007
New Revision: 556077
URL: http://svn.apache.org/viewvc?view=rev&rev=556077
Log:
added dummy web application that doesn't keep any history of pages
Modified:
incubator/wicket/trunk/jdk-1.4/wicket/src/main/java/org/apache/wicket/util/tester/WicketTester.java
Modified:
incubator/wicket/trunk/jdk-1.4/wicket/src/main/java/org/apache/wicket/util/tester/WicketTester.java
URL:
http://svn.apache.org/viewvc/incubator/wicket/trunk/jdk-1.4/wicket/src/main/java/org/apache/wicket/util/tester/WicketTester.java?view=diff&rev=556077&r1=556076&r2=556077
==============================================================================
---
incubator/wicket/trunk/jdk-1.4/wicket/src/main/java/org/apache/wicket/util/tester/WicketTester.java
(original)
+++
incubator/wicket/trunk/jdk-1.4/wicket/src/main/java/org/apache/wicket/util/tester/WicketTester.java
Fri Jul 13 10:46:59 2007
@@ -27,15 +27,18 @@
import junit.framework.AssertionFailedError;
import org.apache.wicket.Component;
+import org.apache.wicket.Page;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.feedback.FeedbackMessage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.protocol.http.HttpSessionStore;
import org.apache.wicket.protocol.http.MockHttpServletResponse;
+import org.apache.wicket.protocol.http.SecondLevelCacheSessionStore;
import org.apache.wicket.protocol.http.UnitTestSettings;
import org.apache.wicket.protocol.http.WebApplication;
import org.apache.wicket.protocol.http.WebResponse;
+import org.apache.wicket.protocol.http.SecondLevelCacheSessionStore.IPageStore;
import org.apache.wicket.session.ISessionStore;
import org.apache.wicket.util.diff.DiffUtil;
import org.slf4j.Logger;
@@ -45,7 +48,7 @@
/**
* A helper to ease unit testing of Wicket applications without the need for a
* servlet container. To start a test, either use startPage() or startPanel():
- *
+ *
* <pre>
* // production page
* public class MyPage extends WebPage
@@ -63,16 +66,16 @@
* }
* }
* </pre>
- *
+ *
* <pre>
* // test code
* private WicketTester tester;
- *
+ *
* public void setUp()
* {
* tester = new WicketTester();
* }
- *
+ *
* public void testRenderMyPage()
* {
* //start and render the test page
@@ -83,10 +86,10 @@
* tester.assertLabel("myMessage", "Hello!");
* }
* </pre>
- *
+ *
* Above example is straight forward: start MyPage.class and assert Label it
* rendered. Next, we try to navigate through link:
- *
+ *
* <pre>
* // production page
* public class YourPage extends WebPage
@@ -97,7 +100,7 @@
* info("Wicket Rocks ;-)");
* }
* }
- *
+ *
* //test code
* public void testLinkToYourPage()
* {
@@ -108,12 +111,12 @@
* tester.assertLabel("yourMessage", "Hi!");
* }
* </pre>
- *
+ *
* <code>tester.clickLink(path);</code> will simulate user click on the
* component (in this case, it's a <code>Link</code>) and render the response
* page <code>YourPage</code>. Ok, unit test of <code>MyPage</code> is
* completed. Now we test <code>YourPage</code> standalone:
- *
+ *
* <pre>
* //test code
* public void testRenderYourPage()
@@ -132,7 +135,7 @@
* tester.assertInfoMessages(new String[] { "Wicket Rocks ;-)"
});
* }
* </pre>
- *
+ *
* Instead of <code>tester.startPage(pageClass)</code>, we define a
* [EMAIL PROTECTED] org.apache.wicket.util.tester.ITestPageSource} to provide
testing page
* instance for WicketTester. This is necessary because <code>YourPage</code>
@@ -140,19 +143,18 @@
* but can not be instantiated by reflection. Finally, we use
* <code>assertInfoMessages</code> to assert there is a feedback message
* "Wicket Rocks ;-)" in INFO level.
- *
+ *
* TODO General: Example usage of FormTester
- *
+ *
* @author Ingram Chen
* @author Juergen Donnerstag
* @author Frank Bille
*/
public class WicketTester extends BaseWicketTester
{
- /** log. */
- private static final Logger log =
LoggerFactory.getLogger(WicketTester.class);
-
/**
+ * Default dummy web application for testing. Uses [EMAIL PROTECTED]
HttpSessionStore}
+ * to store pages and the session.
*/
public static class DummyWebApplication extends WebApplication
{
@@ -164,6 +166,13 @@
return DummyHomePage.class;
}
+ protected ISessionStore newSessionStore()
+ {
+ // Don't use a filestore, or we spawn lots of threads,
which makes
+ // things slow.
+ return new HttpSessionStore(this);
+ }
+
/**
* @see
org.apache.wicket.protocol.http.WebApplication#newWebResponse(javax.servlet.http.HttpServletResponse)
*/
@@ -176,14 +185,51 @@
{
// do nothing
}
-
+ }
+
+ /**
+ * Dummy web application that does not support back button support but
is
+ * cheaper to use for unit tests. Uses [EMAIL PROTECTED]
SecondLevelCacheSessionStore}
+ * with a noop [EMAIL PROTECTED] IPageStore}.
+ */
+ public static class NonCachingDummyWebApplication extends
DummyWebApplication
+ {
protected ISessionStore newSessionStore()
{
- // Don't use a filestore, or we spawn lots of threads,
which makes things slow.
- return new HttpSessionStore(this);
+ return new SecondLevelCacheSessionStore(this, new
IPageStore()
+ {
+ public void destroy()
+ {
+ }
+
+ public Page getPage(String sessionId, String
pagemap, int id, int versionNumber,
+ int ajaxVersionNumber)
+ {
+ return null;
+ }
+
+ public void pageAccessed(String sessionId, Page
page)
+ {
+ }
+
+ public void removePage(String sessionId, String
pagemap, int id)
+ {
+ }
+
+ public void storePage(String sessionId, Page
page)
+ {
+ }
+
+ public void unbind(String sessionId)
+ {
+ }
+ });
}
}
+ /** log. */
+ private static final Logger log =
LoggerFactory.getLogger(WicketTester.class);
+
/**
* Create WicketTester and automatically create a WebApplication, but
the
* tester will have no home page.
@@ -195,7 +241,7 @@
/**
* Create WicketTester and automatically create a WebApplication.
- *
+ *
* @param homePage
*/
public WicketTester(final Class homePage)
@@ -210,6 +256,13 @@
return homePage;
}
+ protected ISessionStore newSessionStore()
+ {
+ // Don't use a filestore, or we spawn lots of
threads, which
+ // makes things slow.
+ return new HttpSessionStore(this);
+ }
+
protected WebResponse newWebResponse(final
HttpServletResponse servletResponse)
{
return new WebResponse(servletResponse);
@@ -219,18 +272,12 @@
{
// Do nothing.
}
-
- protected ISessionStore newSessionStore()
- {
- // Don't use a filestore, or we spawn lots of
threads, which makes things slow.
- return new HttpSessionStore(this);
- }
});
}
/**
* Create WicketTester
- *
+ *
* @param application
* The wicket tester object
*/
@@ -241,13 +288,13 @@
/**
* Create WicketTester to help unit testing
- *
+ *
* @param application
* The wicket tester object
* @param path
* The absolute path on disk to the web application contents
* (e.g. war root) - may be null
- *
+ *
* @see
org.apache.wicket.protocol.http.MockWebApplication#MockWebApplication(String)
*/
public WicketTester(final WebApplication application, final String path)
@@ -261,35 +308,35 @@
/**
- * assert the text of <code>Label</code> component.
- *
- * @param path
- * path to <code>Label</code> component
- * @param expectedLabelText
- * expected label text
+ * Assert that the ajax location header is present
*/
- public void assertLabel(String path, String expectedLabelText)
+ public void assertAjaxLocation()
{
- Label label = (Label)getComponentFromLastRenderedPage(path);
- Assert.assertEquals(expectedLabelText,
label.getModelObjectAsString());
- }
+ if (null !=
((MockHttpServletResponse)getWicketResponse().getHttpServletResponse())
+ .getRedirectLocation())
+ {
+ throw new AssertionFailedError(
+ "Location header should *not* be
present when using Ajax");
+ }
- /**
- * assert <code>PageLink</code> link to page class.
- *
- * @param path
- * path to <code>PageLink</code> component
- * @param expectedPageClass
- * expected page class to link
- */
- public void assertPageLink(String path, Class expectedPageClass)
- {
- assertResult(isPageLink(path, expectedPageClass));
+ String ajaxLocation =
((MockHttpServletResponse)getWicketResponse()
+
.getHttpServletResponse()).getHeader("Ajax-Location");
+ if (null == ajaxLocation)
+ {
+ throw new AssertionFailedError("Ajax-Location header
should be present when using Ajax");
+ }
+
+ int statusCode =
((MockHttpServletResponse)getWicketResponse().getHttpServletResponse())
+ .getStatus();
+ if (statusCode != 200)
+ {
+ throw new AssertionFailedError("Expected HTTP status
code to be 200 (OK)");
+ }
}
/**
* assert component class
- *
+ *
* @param path
* path to component
* @param expectedComponentClass
@@ -301,30 +348,44 @@
}
/**
- * assert component visible.
- *
- * @param path
- * path to component
+ * Test that a component has been added to a AjaxRequestTarget, using
+ * [EMAIL PROTECTED] AjaxRequestTarget#addComponent(Component)}. This
method actually
+ * tests that a component is on the AJAX response sent back to the
client.
+ * <p>
+ * PLEASE NOTE! This method doesn't actually insert the component in the
+ * client DOM tree, using javascript. But it shouldn't be needed
because you
+ * have to trust that the Wicket Ajax Javascript just works.
+ *
+ * @param component
+ * The component to test whether it's on the response.
*/
- public void assertVisible(String path)
+ public void assertComponentOnAjaxResponse(Component component)
{
- assertResult(isVisible(path));
+ Result result = isComponentOnAjaxResponse(component);
+ assertResult(result);
}
/**
- * assert component invisible.
- *
- * @param path
- * path to component
+ * Test that a component has been added to a AjaxRequestTarget, using
+ * [EMAIL PROTECTED] AjaxRequestTarget#addComponent(Component)}. This
method actually
+ * tests that a component is on the AJAX response sent back to the
client.
+ * <p>
+ * PLEASE NOTE! This method doesn't actually insert the component in the
+ * client DOM tree, using javascript. But it shouldn't be needed
because you
+ * have to trust that the Wicket Ajax Javascript just works.
+ *
+ * @param componentPath
+ * The component path to the component to test whether it's
on
+ * the response.
*/
- public void assertInvisible(String path)
+ public void assertComponentOnAjaxResponse(String componentPath)
{
- assertResult(isInvisible(path));
+
assertComponentOnAjaxResponse(getComponentFromLastRenderedPage(componentPath));
}
/**
* assert the content of last rendered page contains(matches) regex
pattern.
- *
+ *
* @param pattern
* reqex pattern to match
*/
@@ -334,8 +395,62 @@
}
/**
+ * assert error feedback messages
+ *
+ * @param expectedErrorMessages
+ * expected error messages
+ */
+ public void assertErrorMessages(String[] expectedErrorMessages)
+ {
+ List actualMessages = getMessages(FeedbackMessage.ERROR);
+ List msgs = new ArrayList();
+ for (Iterator iterator = actualMessages.iterator();
iterator.hasNext();)
+ {
+ msgs.add(iterator.next().toString());
+ }
+
WicketTesterHelper.assertEquals(Arrays.asList(expectedErrorMessages), msgs);
+ }
+
+ /**
+ * assert info feedback message
+ *
+ * @param expectedInfoMessages
+ * expected info messages
+ */
+ public void assertInfoMessages(String[] expectedInfoMessages)
+ {
+ List actualMessages = getMessages(FeedbackMessage.INFO);
+
WicketTesterHelper.assertEquals(Arrays.asList(expectedInfoMessages),
actualMessages);
+ }
+
+ /**
+ * assert component invisible.
+ *
+ * @param path
+ * path to component
+ */
+ public void assertInvisible(String path)
+ {
+ assertResult(isInvisible(path));
+ }
+
+ /**
+ * assert the text of <code>Label</code> component.
+ *
+ * @param path
+ * path to <code>Label</code> component
+ * @param expectedLabelText
+ * expected label text
+ */
+ public void assertLabel(String path, String expectedLabelText)
+ {
+ Label label = (Label)getComponentFromLastRenderedPage(path);
+ Assert.assertEquals(expectedLabelText,
label.getModelObjectAsString());
+ }
+
+ /**
* assert the model of [EMAIL PROTECTED] ListView} use expectedList
- *
+ *
* @param path
* path to [EMAIL PROTECTED] ListView} component
* @param expectedList
@@ -348,8 +463,41 @@
}
/**
+ * assert no error feedback messages
+ */
+ public void assertNoErrorMessage()
+ {
+ List messages = getMessages(FeedbackMessage.ERROR);
+ Assert.assertTrue("expect no error message, but contains\n" +
+ WicketTesterHelper.asLined(messages),
messages.isEmpty());
+ }
+
+ /**
+ * assert no info feedback messages
+ */
+ public void assertNoInfoMessage()
+ {
+ List messages = getMessages(FeedbackMessage.INFO);
+ Assert.assertTrue("expect no info message, but contains\n" +
+ WicketTesterHelper.asLined(messages),
messages.isEmpty());
+ }
+
+ /**
+ * assert <code>PageLink</code> link to page class.
+ *
+ * @param path
+ * path to <code>PageLink</code> component
+ * @param expectedPageClass
+ * expected page class to link
+ */
+ public void assertPageLink(String path, Class expectedPageClass)
+ {
+ assertResult(isPageLink(path, expectedPageClass));
+ }
+
+ /**
* assert last rendered Page class
- *
+ *
* @param expectedRenderedPageClass
* expected class of last renered page
*/
@@ -364,7 +512,7 @@
* Use <code>-Dwicket.replace.expected.results=true</code> to
* automatically replace the expected output file.
* </p>
- *
+ *
* @param clazz
* Used to load the file (relative to clazz package)
* @param filename
@@ -380,7 +528,7 @@
/**
* assert last rendered Page against an expected HTML document as a
String
- *
+ *
* @param expectedDocument
* Expected output
* @throws Exception
@@ -393,88 +541,14 @@
}
/**
- * assert no error feedback messages
- */
- public void assertNoErrorMessage()
- {
- List messages = getMessages(FeedbackMessage.ERROR);
- Assert.assertTrue("expect no error message, but contains\n"
- + WicketTesterHelper.asLined(messages),
messages.isEmpty());
- }
-
- /**
- * assert no info feedback messages
- */
- public void assertNoInfoMessage()
- {
- List messages = getMessages(FeedbackMessage.INFO);
- Assert.assertTrue("expect no info message, but contains\n"
- + WicketTesterHelper.asLined(messages),
messages.isEmpty());
- }
-
- /**
- * assert error feedback messages
- *
- * @param expectedErrorMessages
- * expected error messages
- */
- public void assertErrorMessages(String[] expectedErrorMessages)
- {
- List actualMessages = getMessages(FeedbackMessage.ERROR);
- List msgs = new ArrayList();
- for (Iterator iterator = actualMessages.iterator();
iterator.hasNext();)
- {
- msgs.add(iterator.next().toString());
- }
-
WicketTesterHelper.assertEquals(Arrays.asList(expectedErrorMessages), msgs);
- }
-
- /**
- * assert info feedback message
- *
- * @param expectedInfoMessages
- * expected info messages
- */
- public void assertInfoMessages(String[] expectedInfoMessages)
- {
- List actualMessages = getMessages(FeedbackMessage.INFO);
-
WicketTesterHelper.assertEquals(Arrays.asList(expectedInfoMessages),
actualMessages);
- }
-
- /**
- * Test that a component has been added to a AjaxRequestTarget, using
- * [EMAIL PROTECTED] AjaxRequestTarget#addComponent(Component)}. This
method actually
- * tests that a component is on the AJAX response sent back to the
client.
- * <p>
- * PLEASE NOTE! This method doesn't actually insert the component in the
- * client DOM tree, using javascript. But it shouldn't be needed
because you
- * have to trust that the Wicket Ajax Javascript just works.
- *
- * @param componentPath
- * The component path to the component to test whether it's
on
- * the response.
- */
- public void assertComponentOnAjaxResponse(String componentPath)
- {
-
assertComponentOnAjaxResponse(getComponentFromLastRenderedPage(componentPath));
- }
-
- /**
- * Test that a component has been added to a AjaxRequestTarget, using
- * [EMAIL PROTECTED] AjaxRequestTarget#addComponent(Component)}. This
method actually
- * tests that a component is on the AJAX response sent back to the
client.
- * <p>
- * PLEASE NOTE! This method doesn't actually insert the component in the
- * client DOM tree, using javascript. But it shouldn't be needed
because you
- * have to trust that the Wicket Ajax Javascript just works.
- *
- * @param component
- * The component to test whether it's on the response.
+ * assert component visible.
+ *
+ * @param path
+ * path to component
*/
- public void assertComponentOnAjaxResponse(Component component)
+ public void assertVisible(String path)
{
- Result result = isComponentOnAjaxResponse(component);
- assertResult(result);
+ assertResult(isVisible(path));
}
private void assertResult(Result result)
@@ -482,32 +556,6 @@
if (result.wasFailed())
{
throw new AssertionFailedError(result.getMessage());
- }
- }
-
- /**
- * Assert that the ajax location header is present
- */
- public void assertAjaxLocation()
- {
- if (null !=
((MockHttpServletResponse)getWicketResponse().getHttpServletResponse())
- .getRedirectLocation())
- {
- throw new AssertionFailedError("Location header should
*not* be present when using Ajax");
- }
-
- String ajaxLocation =
((MockHttpServletResponse)getWicketResponse()
-
.getHttpServletResponse()).getHeader("Ajax-Location");
- if (null == ajaxLocation)
- {
- throw new AssertionFailedError("Ajax-Location header
should be present when using Ajax");
- }
-
- int statusCode = ((MockHttpServletResponse)getWicketResponse()
- .getHttpServletResponse()).getStatus();
- if (statusCode != 200)
- {
- throw new AssertionFailedError("Expected HTTP status
code to be 200 (OK)");
}
}
}