You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@wicket.apache.org by jd...@apache.org on 2011/04/17 18:29:30 UTC
svn commit: r1094170 - in /wicket/trunk/wicket-core/src:
main/java/org/apache/wicket/util/tester/
test/java/org/apache/wicket/util/tester/
Author: jdonnerstag
Date: Sun Apr 17 16:29:29 2011
New Revision: 1094170
URL: http://svn.apache.org/viewvc?rev=1094170&view=rev
Log:
a little walk through wicket tester
Modified:
wicket/trunk/wicket-core/src/main/java/org/apache/wicket/util/tester/BaseWicketTester.java
wicket/trunk/wicket-core/src/main/java/org/apache/wicket/util/tester/FormTester.java
wicket/trunk/wicket-core/src/main/java/org/apache/wicket/util/tester/WicketTester.java
wicket/trunk/wicket-core/src/test/java/org/apache/wicket/util/tester/WicketTesterTest.java
Modified: wicket/trunk/wicket-core/src/main/java/org/apache/wicket/util/tester/BaseWicketTester.java
URL: http://svn.apache.org/viewvc/wicket/trunk/wicket-core/src/main/java/org/apache/wicket/util/tester/BaseWicketTester.java?rev=1094170&r1=1094169&r2=1094170&view=diff
==============================================================================
--- wicket/trunk/wicket-core/src/main/java/org/apache/wicket/util/tester/BaseWicketTester.java (original)
+++ wicket/trunk/wicket-core/src/main/java/org/apache/wicket/util/tester/BaseWicketTester.java Sun Apr 17 16:29:29 2011
@@ -16,6 +16,9 @@
*/
package org.apache.wicket.util.tester;
+import static junit.framework.Assert.assertNotNull;
+import static junit.framework.Assert.fail;
+
import java.io.IOException;
import java.io.Serializable;
import java.lang.reflect.Constructor;
@@ -38,6 +41,8 @@ import javax.servlet.ServletContext;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpSession;
+import junit.framework.AssertionFailedError;
+
import org.apache.wicket.Application;
import org.apache.wicket.Component;
import org.apache.wicket.IPageManagerProvider;
@@ -433,10 +438,11 @@ public class BaseWicketTester
*
* @param request
* request to process
+ * @return true, if process was executed successfully
*/
- public void processRequest(final MockHttpServletRequest request)
+ public boolean processRequest(final MockHttpServletRequest request)
{
- processRequest(request, null);
+ return processRequest(request, null);
}
/**
@@ -522,8 +528,7 @@ public class BaseWicketTester
{
if (redirectCount++ >= 100)
{
- throw new IllegalStateException(
- "Possible infinite redirect detected. Bailing out.");
+ fail("Possible infinite redirect detected. Bailing out.");
}
Url newUrl = Url.parse(lastResponse.getRedirectLocation(),
@@ -531,7 +536,7 @@ public class BaseWicketTester
if (newUrl.isAbsolute())
{
- throw new WicketRuntimeException("Can not follow absolute redirect URL.");
+ fail("Can not follow absolute redirect URL.");
}
// append redirect URL to current URL (what browser would do)
@@ -726,8 +731,9 @@ public class BaseWicketTester
return parser.getString().toString().split("\\\"")[1];
}
}
- throw new WicketRuntimeException(
- "Last response has no AJAX base URL set by AbstractDefaultAjaxBehavior.");
+
+ fail("Last response has no AJAX base URL set by AbstractDefaultAjaxBehavior.");
+ return null;
}
/**
@@ -939,6 +945,7 @@ public class BaseWicketTester
* the parameters to use for the class.
* @return the rendered <code>Page</code>
*/
+ @SuppressWarnings("unchecked")
public final <C extends Page> C startPage(final Class<C> pageClass,
final PageParameters parameters)
{
@@ -1055,7 +1062,7 @@ public class BaseWicketTester
Args.notNull(componentClass, "componentClass");
// Create the component instance from the class
- Component comp;
+ Component comp = null;
try
{
Constructor<? extends Component> c = componentClass.getConstructor(String.class);
@@ -1063,7 +1070,7 @@ public class BaseWicketTester
}
catch (Exception e)
{
- throw convertoUnexpect(e);
+ fail(e.getMessage());
}
// process the component
@@ -1130,17 +1137,6 @@ public class BaseWicketTester
}
/**
- * Throw "standard" WicketRuntimeException
- *
- * @param e
- * @return RuntimeException
- */
- private RuntimeException convertoUnexpect(final Exception e)
- {
- return new WicketRuntimeException("tester: unexpected", e);
- }
-
- /**
* Gets the component with the given path from last rendered page. This method fails in case the
* component couldn't be found.
*
@@ -1164,7 +1160,7 @@ public class BaseWicketTester
{
fail("path: '" + path + "' does not exist for page: " +
Classes.simpleName(getLastRenderedPage().getClass()));
- return component;
+ return null;
}
if (!wantVisibleInHierarchy || component.isVisibleInHierarchy())
@@ -1426,7 +1422,7 @@ public class BaseWicketTester
{
Component linkComponent = getComponentFromLastRenderedPage(path);
- checkUsability(linkComponent);
+ checkUsability(linkComponent, true);
// if the link is an AjaxLink, we process it differently
// than a normal link
@@ -1506,7 +1502,7 @@ public class BaseWicketTester
}
catch (Exception e)
{
- fail("Internal error in WicketTester. "
+ throw new WicketRuntimeException("Internal error in WicketTester. "
+ "Please report this in Wicket's Issue Tracker.", e);
}
}
@@ -1813,7 +1809,7 @@ public class BaseWicketTester
List<AbstractAjaxTimerBehavior> behaviors = component.getBehaviors(AbstractAjaxTimerBehavior.class);
for (Behavior b : behaviors)
{
- checkUsability(component);
+ checkUsability(component, true);
log.debug("Triggering AjaxSelfUpdatingTimerBehavior: " +
component.getClassRelativePath());
@@ -1864,13 +1860,10 @@ public class BaseWicketTester
*/
public void executeAjaxEvent(final Component component, final String event)
{
- String failMessage = "Can't execute event on a component which is null.";
- notNull(failMessage, component);
-
- failMessage = "event must not be null";
- notNull(failMessage, event);
+ Args.notNull(component, "component");
+ Args.notNull(event, "event");
- checkUsability(component);
+ checkUsability(component, true);
AjaxEventBehavior ajaxEventBehavior = WicketTesterHelper.findAjaxEventBehavior(component,
event);
@@ -1931,14 +1924,10 @@ public class BaseWicketTester
{
// The form that needs to be "submitted".
Form<?> form = behavior.getForm();
+ assertNotNull("No form attached to the submitlink.", form);
- String failMessage = "No form attached to the submitlink.";
- notNull(failMessage, form);
-
- checkUsability(form);
-
+ checkUsability(form, true);
serializeFormToRequest(form);
-
executeBehavior(behavior);
}
@@ -1987,10 +1976,7 @@ public class BaseWicketTester
public String getContentTypeFromResponseHeader()
{
String contentType = getLastResponse().getContentType();
- if (contentType == null)
- {
- throw new WicketRuntimeException("No Content-Type header found");
- }
+ assertNotNull("No Content-Type header found", contentType);
return contentType;
}
@@ -2002,10 +1988,7 @@ public class BaseWicketTester
public int getContentLengthFromResponseHeader()
{
String contentLength = getLastResponse().getHeader("Content-Length");
- if (contentLength == null)
- {
- throw new WicketRuntimeException("No Content-Length header found");
- }
+ assertNotNull("No Content-Length header found", contentLength);
return Integer.parseInt(contentLength);
}
@@ -2126,38 +2109,30 @@ public class BaseWicketTester
* Checks whether a component is visible and/or enabled before usage
*
* @param component
+ * @param throwException
+ * @return result
*/
- private void checkUsability(final Component component)
+ public final Result checkUsability(final Component component, boolean throwException)
{
+ Result res = Result.pass();
+
if (component.isVisibleInHierarchy() == false)
{
- fail("The component is currently not visible in the hierarchy and thus you can not be used." +
+ res = Result.fail("The component is currently not visible in the hierarchy and thus you can not be used." +
" Component: " + component);
}
if (component.isEnabledInHierarchy() == false)
{
- fail("The component is currently not enabled in the hierarchy and thus you can not be used." +
+ res = Result.fail("The component is currently not enabled in the hierarchy and thus you can not be used." +
" Component: " + component);
}
- }
- /**
- * @param message
- */
- protected final void fail(String message)
- {
- fail(message, null);
- }
-
- /**
- *
- * @param message
- * @param cause
- */
- protected final void fail(String message, Throwable cause)
- {
- throw new WicketRuntimeException(message, cause);
+ if (throwException && res.wasFailed())
+ {
+ throw new AssertionFailedError(res.getMessage());
+ }
+ return res;
}
/**
@@ -2193,7 +2168,6 @@ public class BaseWicketTester
}
/**
- *
* @param exposeExceptions
*/
public void setExposeExceptions(boolean exposeExceptions)
@@ -2407,6 +2381,9 @@ public class BaseWicketTester
}
}
+ /**
+ *
+ */
private static class WicketTesterServletWebResponse extends ServletWebResponse
implements
IMetaDataBufferingWebResponse
Modified: wicket/trunk/wicket-core/src/main/java/org/apache/wicket/util/tester/FormTester.java
URL: http://svn.apache.org/viewvc/wicket/trunk/wicket-core/src/main/java/org/apache/wicket/util/tester/FormTester.java?rev=1094170&r1=1094169&r2=1094170&view=diff
==============================================================================
--- wicket/trunk/wicket-core/src/main/java/org/apache/wicket/util/tester/FormTester.java (original)
+++ wicket/trunk/wicket-core/src/main/java/org/apache/wicket/util/tester/FormTester.java Sun Apr 17 16:29:29 2011
@@ -24,6 +24,8 @@ import java.util.Collection;
import java.util.HashSet;
import java.util.List;
+import junit.framework.Assert;
+
import org.apache.wicket.Component;
import org.apache.wicket.WicketRuntimeException;
import org.apache.wicket.markup.html.form.AbstractTextComponent;
@@ -626,11 +628,8 @@ public class FormTester
public FormTester setValue(final String formComponentId, final String value)
{
Component component = workingForm.get(formComponentId);
- if (component == null)
- {
- throw new IllegalArgumentException(
- "Unable to set value. Couldn't find component with name: " + formComponentId);
- }
+ Assert.assertNotNull("Unable to set value. Couldn't find component with name: " +
+ formComponentId, component);
return setValue(component, value);
}
@@ -660,8 +659,7 @@ public class FormTester
}
else
{
- throw new IllegalArgumentException("Componet with id: " + formComponent.getId() +
- " is not a FormComponent");
+ fail("Componet with id: " + formComponent.getId() + " is not a FormComponent");
}
return this;
@@ -699,7 +697,7 @@ public class FormTester
if (formComponent instanceof FileUploadField == false)
{
- throw new IllegalArgumentException("'" + formComponentId + "' is not " +
+ fail("'" + formComponentId + "' is not " +
"a FileUploadField. You can only attach a file to form " +
"component of this type.");
}
@@ -853,8 +851,8 @@ public class FormTester
{
if (closed)
{
- throw new IllegalStateException("'" + path +
- "' already submitted. Note that FormTester " + "is allowed to submit only once");
+ fail("'" + path + "' already submitted. Note that FormTester " +
+ "is allowed to submit only once");
}
}
Modified: wicket/trunk/wicket-core/src/main/java/org/apache/wicket/util/tester/WicketTester.java
URL: http://svn.apache.org/viewvc/wicket/trunk/wicket-core/src/main/java/org/apache/wicket/util/tester/WicketTester.java?rev=1094170&r1=1094169&r2=1094170&view=diff
==============================================================================
--- wicket/trunk/wicket-core/src/main/java/org/apache/wicket/util/tester/WicketTester.java (original)
+++ wicket/trunk/wicket-core/src/main/java/org/apache/wicket/util/tester/WicketTester.java Sun Apr 17 16:29:29 2011
@@ -16,6 +16,11 @@
*/
package org.apache.wicket.util.tester;
+import static junit.framework.Assert.assertEquals;
+import static junit.framework.Assert.assertNotNull;
+import static junit.framework.Assert.assertTrue;
+import static junit.framework.Assert.fail;
+
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
@@ -23,7 +28,6 @@ import java.util.List;
import javax.servlet.ServletContext;
-import junit.framework.Assert;
import junit.framework.AssertionFailedError;
import org.apache.wicket.Component;
@@ -227,8 +231,7 @@ public class WicketTester extends BaseWi
{
if (null != getLastResponse().getHeader("Location"))
{
- throw new AssertionFailedError(
- "Location header should *not* be present when using Ajax");
+ fail("Location header should *not* be present when using Ajax");
}
String ajaxLocation = getLastResponse().getHeader("Ajax-Location");
@@ -389,7 +392,7 @@ public class WicketTester extends BaseWi
public void assertLabel(String path, String expectedLabelText)
{
Label label = (Label)getComponentFromLastRenderedPage(path);
- Assert.assertEquals(expectedLabelText, label.getDefaultModelObjectAsString());
+ assertEquals(expectedLabelText, label.getDefaultModelObjectAsString());
}
/**
@@ -403,7 +406,7 @@ public class WicketTester extends BaseWi
public void assertModelValue(String path, Object expectedValue)
{
Component component = getComponentFromLastRenderedPage(path);
- Assert.assertEquals(expectedValue, component.getDefaultModelObject());
+ assertEquals(expectedValue, component.getDefaultModelObject());
}
/**
@@ -427,7 +430,7 @@ public class WicketTester extends BaseWi
public void assertNoErrorMessage()
{
List<Serializable> messages = getMessages(FeedbackMessage.ERROR);
- Assert.assertTrue(
+ assertTrue(
"expect no error message, but contains\n" + WicketTesterHelper.asLined(messages),
messages.isEmpty());
}
@@ -438,8 +441,7 @@ public class WicketTester extends BaseWi
public void assertNoInfoMessage()
{
List<Serializable> messages = getMessages(FeedbackMessage.INFO);
- Assert.assertTrue(
- "expect no info message, but contains\n" + WicketTesterHelper.asLined(messages),
+ assertTrue("expect no info message, but contains\n" + WicketTesterHelper.asLined(messages),
messages.isEmpty());
}
@@ -485,7 +487,7 @@ public class WicketTester extends BaseWi
{
// Validate the document
String document = getLastResponseAsString();
- Assert.assertEquals(expectedDocument, document);
+ assertEquals(expectedDocument, document);
}
/**
@@ -546,6 +548,16 @@ public class WicketTester extends BaseWi
}
/**
+ * Checks whether a component is visible and/or enabled before usage
+ *
+ * @param component
+ */
+ public void assertUsability(final Component component)
+ {
+ checkUsability(component, true);
+ }
+
+ /**
*
* @param link
*/
@@ -576,12 +588,11 @@ public class WicketTester extends BaseWi
" is not a BookmarkablePageLink");
}
- Assert.assertEquals("BookmarkablePageLink: " + id + " is pointing to the wrong page",
- pageClass, pageLink.getPageClass());
+ assertEquals("BookmarkablePageLink: " + id + " is pointing to the wrong page", pageClass,
+ pageLink.getPageClass());
- Assert.assertEquals(
- "One or more of the parameters associated with the BookmarkablePageLink: " + id +
- " do not match", parameters, pageLink.getPageParameters());
+ assertEquals("One or more of the parameters associated with the BookmarkablePageLink: " +
+ id + " do not match", parameters, pageLink.getPageParameters());
}
/**
@@ -672,7 +683,7 @@ public class WicketTester extends BaseWi
public void executeListener(final Class<?> testClass, final Component component,
final String filename) throws Exception
{
- Assert.assertNotNull(component);
+ assertNotNull(component);
log.info("=== " + testClass.getName() + " : " + component.getPageRelativePath() + " ===");
@@ -690,7 +701,7 @@ public class WicketTester extends BaseWi
public void executeBehavior(final Class<?> testClass, final AbstractAjaxBehavior behavior,
final String filename) throws Exception
{
- Assert.assertNotNull(behavior);
+ assertNotNull(behavior);
log.info("=== " + testClass.getName() + " : " + behavior.toString() + " ===");
Modified: wicket/trunk/wicket-core/src/test/java/org/apache/wicket/util/tester/WicketTesterTest.java
URL: http://svn.apache.org/viewvc/wicket/trunk/wicket-core/src/test/java/org/apache/wicket/util/tester/WicketTesterTest.java?rev=1094170&r1=1094169&r2=1094170&view=diff
==============================================================================
--- wicket/trunk/wicket-core/src/test/java/org/apache/wicket/util/tester/WicketTesterTest.java (original)
+++ wicket/trunk/wicket-core/src/test/java/org/apache/wicket/util/tester/WicketTesterTest.java Sun Apr 17 16:29:29 2011
@@ -31,7 +31,6 @@ import org.apache.wicket.MockPageWithLin
import org.apache.wicket.MockPageWithOneComponent;
import org.apache.wicket.Page;
import org.apache.wicket.Session;
-import org.apache.wicket.WicketRuntimeException;
import org.apache.wicket.ajax.AjaxEventBehavior;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.WicketAjaxReference;
@@ -218,9 +217,9 @@ public class WicketTesterTest extends Te
try
{
tester.clickLink("ajaxLinkWithSetResponsePageClass");
- fail("Disabled link should not be clickable.");
+ throw new RuntimeException("Disabled link should not be clickable.");
}
- catch (Exception _)
+ catch (AssertionFailedError _)
{
;
}
@@ -241,9 +240,9 @@ public class WicketTesterTest extends Te
try
{
tester.executeAjaxEvent("ajaxLinkWithSetResponsePageClass", "onclick");
- fail("Disabled link should not be clickable.");
+ throw new RuntimeException("Disabled link should not be clickable.");
}
- catch (Exception _)
+ catch (AssertionFailedError _)
{
;
}
@@ -321,13 +320,12 @@ public class WicketTesterTest extends Te
;
}
-
try
{
// test #3: "createForm" is not a FormComponent
tester.assertRequired("createForm");
}
- catch (WicketRuntimeException _)
+ catch (AssertionFailedError _)
{
;
}