You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@wicket.apache.org by mg...@apache.org on 2010/11/21 12:27:38 UTC
svn commit: r1037420 - in /wicket/trunk/wicket/src:
main/java/org/apache/wicket/util/tester/BaseWicketTester.java
main/java/org/apache/wicket/util/tester/WicketTester.java
test/java/org/apache/wicket/util/tester/WicketTesterTest.java
Author: mgrigorov
Date: Sun Nov 21 11:27:38 2010
New Revision: 1037420
URL: http://svn.apache.org/viewvc?rev=1037420&view=rev
Log:
WICKET-3152 Wicket tester should allow testing for enabled/disabled status and for fields being required.
Add assertions for enabled/disabled component and required form component
Modified:
wicket/trunk/wicket/src/main/java/org/apache/wicket/util/tester/BaseWicketTester.java
wicket/trunk/wicket/src/main/java/org/apache/wicket/util/tester/WicketTester.java
wicket/trunk/wicket/src/test/java/org/apache/wicket/util/tester/WicketTesterTest.java
Modified: wicket/trunk/wicket/src/main/java/org/apache/wicket/util/tester/BaseWicketTester.java
URL: http://svn.apache.org/viewvc/wicket/trunk/wicket/src/main/java/org/apache/wicket/util/tester/BaseWicketTester.java?rev=1037420&r1=1037419&r2=1037420&view=diff
==============================================================================
--- wicket/trunk/wicket/src/main/java/org/apache/wicket/util/tester/BaseWicketTester.java (original)
+++ wicket/trunk/wicket/src/main/java/org/apache/wicket/util/tester/BaseWicketTester.java Sun Nov 21 11:27:38 2010
@@ -1067,6 +1067,40 @@ public class BaseWicketTester
return isFalse("component '" + path + "' is enabled", component.isEnabledInHierarchy());
}
+ /**
+ * assert component required.
+ *
+ * @param path
+ * path to component
+ * @return a <code>Result</code>
+ */
+ public Result isRequired(String path)
+ {
+ Component component = getLastRenderedPage().get(path);
+ if (component == null)
+ {
+ fail("path: '" + path + "' does no exist for page: " +
+ Classes.simpleName(getLastRenderedPage().getClass()));
+ }
+ else if (component instanceof FormComponent == false)
+ {
+ fail("path: '" + path + "' is not a form component");
+ }
+
+ return isRequired((FormComponent<?>)component);
+ }
+
+ /**
+ * assert component required.
+ *
+ * @param component
+ * a form component
+ * @return a <code>Result</code>
+ */
+ public Result isRequired(FormComponent<?> component)
+ {
+ return isTrue("component '" + component + "' is not required", component.isRequired());
+ }
/**
* assert the content of last rendered page contains(matches) regex pattern.
Modified: wicket/trunk/wicket/src/main/java/org/apache/wicket/util/tester/WicketTester.java
URL: http://svn.apache.org/viewvc/wicket/trunk/wicket/src/main/java/org/apache/wicket/util/tester/WicketTester.java?rev=1037420&r1=1037419&r2=1037420&view=diff
==============================================================================
--- wicket/trunk/wicket/src/main/java/org/apache/wicket/util/tester/WicketTester.java (original)
+++ wicket/trunk/wicket/src/main/java/org/apache/wicket/util/tester/WicketTester.java Sun Nov 21 11:27:38 2010
@@ -483,6 +483,40 @@ public class WicketTester extends BaseWi
assertResult(isVisible(path));
}
+ /**
+ * assert component is enabled.
+ *
+ * @param path
+ * path to component
+ *
+ */
+ public void assertEnabled(String path)
+ {
+ assertResult(isEnabled(path));
+ }
+
+ /**
+ * assert component is enabled.
+ *
+ * @param path
+ * path to component
+ */
+ public void assertDisabled(String path)
+ {
+ assertResult(isDisabled(path));
+ }
+
+ /**
+ * assert form component is required.
+ *
+ * @param path
+ * path to form component
+ */
+ public void assertRequired(String path)
+ {
+ assertResult(isRequired(path));
+ }
+
private void assertResult(Result result)
{
if (result.wasFailed())
Modified: wicket/trunk/wicket/src/test/java/org/apache/wicket/util/tester/WicketTesterTest.java
URL: http://svn.apache.org/viewvc/wicket/trunk/wicket/src/test/java/org/apache/wicket/util/tester/WicketTesterTest.java?rev=1037420&r1=1037419&r2=1037420&view=diff
==============================================================================
--- wicket/trunk/wicket/src/test/java/org/apache/wicket/util/tester/WicketTesterTest.java (original)
+++ wicket/trunk/wicket/src/test/java/org/apache/wicket/util/tester/WicketTesterTest.java Sun Nov 21 11:27:38 2010
@@ -21,6 +21,7 @@ import java.util.Locale;
import javax.servlet.http.Cookie;
+import junit.framework.AssertionFailedError;
import junit.framework.TestCase;
import org.apache.wicket.Component;
@@ -28,11 +29,13 @@ 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.markup.html.AjaxLink;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Button;
+import org.apache.wicket.markup.html.form.FormComponent;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.model.IModel;
@@ -252,6 +255,90 @@ public class WicketTesterTest extends Te
}
/**
+ * WICKET-3152
+ *
+ * @throws Exception
+ */
+ public void testAssertEnabled() throws Exception
+ {
+ tester.startPage(LinkPage.class);
+ tester.assertRenderedPage(LinkPage.class);
+
+ tester.getComponentFromLastRenderedPage("ajaxLinkWithSetResponsePageClass").setEnabled(
+ false);
+ try
+ {
+ tester.assertEnabled("ajaxLinkWithSetResponsePageClass");
+ fail("The link must not be enabled.");
+ }
+ catch (AssertionFailedError _)
+ {
+ ;
+ }
+ }
+
+ /**
+ * WICKET-3152
+ *
+ * @throws Exception
+ */
+ public void testAssertDisabled() throws Exception
+ {
+ tester.startPage(LinkPage.class);
+ tester.assertRenderedPage(LinkPage.class);
+
+ tester.getComponentFromLastRenderedPage("ajaxLinkWithSetResponsePageClass")
+ .setEnabled(true);
+ try
+ {
+ tester.assertDisabled("ajaxLinkWithSetResponsePageClass");
+ fail("The link must not be disabled.");
+ }
+ catch (AssertionFailedError _)
+ {
+ ;
+ }
+ }
+
+ /**
+ * WICKET-3152
+ *
+ * @throws Exception
+ */
+ public void testAssertRequired() throws Exception
+ {
+ tester.startPage(CreateBook.class);
+ tester.assertRenderedPage(CreateBook.class);
+
+ // test #1: "id" is required by default
+ tester.assertRequired("createForm:id");
+
+ FormComponent<?> bookId = (FormComponent<?>)tester.getComponentFromLastRenderedPage("createForm:id");
+ try
+ {
+ // test #2: set it manually to not required
+ bookId.setRequired(false);
+ tester.assertRequired("createForm:id");
+ fail("Book ID component must not be required anymore!");
+ }
+ catch (AssertionFailedError _)
+ {
+ ;
+ }
+
+
+ try
+ {
+ // test #3: "createForm" is not a FormComponent
+ tester.assertRequired("createForm");
+ }
+ catch (WicketRuntimeException _)
+ {
+ ;
+ }
+ }
+
+ /**
* @throws Exception
*/
public void testClickLink_ajaxLink_setResponsePage() throws Exception