Author: mgrigorov
Date: Sun Nov 21 11:39:00 2010
New Revision: 1037425

URL: http://svn.apache.org/viewvc?rev=1037425&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

merge r1037420 from trunk (1.5.x)

Modified:
    
wicket/branches/wicket-1.4.x/wicket/src/main/java/org/apache/wicket/util/tester/BaseWicketTester.java
    
wicket/branches/wicket-1.4.x/wicket/src/main/java/org/apache/wicket/util/tester/WicketTester.java
    
wicket/branches/wicket-1.4.x/wicket/src/test/java/org/apache/wicket/util/tester/WicketTesterTest.java

Modified: 
wicket/branches/wicket-1.4.x/wicket/src/main/java/org/apache/wicket/util/tester/BaseWicketTester.java
URL: 
http://svn.apache.org/viewvc/wicket/branches/wicket-1.4.x/wicket/src/main/java/org/apache/wicket/util/tester/BaseWicketTester.java?rev=1037425&r1=1037424&r2=1037425&view=diff
==============================================================================
--- 
wicket/branches/wicket-1.4.x/wicket/src/main/java/org/apache/wicket/util/tester/BaseWicketTester.java
 (original)
+++ 
wicket/branches/wicket-1.4.x/wicket/src/main/java/org/apache/wicket/util/tester/BaseWicketTester.java
 Sun Nov 21 11:39:00 2010
@@ -648,6 +648,40 @@ public class BaseWicketTester extends Mo
                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/branches/wicket-1.4.x/wicket/src/main/java/org/apache/wicket/util/tester/WicketTester.java
URL: 
http://svn.apache.org/viewvc/wicket/branches/wicket-1.4.x/wicket/src/main/java/org/apache/wicket/util/tester/WicketTester.java?rev=1037425&r1=1037424&r2=1037425&view=diff
==============================================================================
--- 
wicket/branches/wicket-1.4.x/wicket/src/main/java/org/apache/wicket/util/tester/WicketTester.java
 (original)
+++ 
wicket/branches/wicket-1.4.x/wicket/src/main/java/org/apache/wicket/util/tester/WicketTester.java
 Sun Nov 21 11:39:00 2010
@@ -43,9 +43,9 @@ import org.apache.wicket.model.IModel;
 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.SecondLevelCacheSessionStore.IPageStore;
 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;
@@ -85,11 +85,11 @@ import org.slf4j.LoggerFactory;
  * 
  * public void testRenderMyPage()
  * {
- *     //start and render the test page
+ *     // start and render the test page
  *     tester.startPage(MyPage.class);
- *     //assert rendered page class
+ *     // assert rendered page class
  *     tester.assertRenderedPage(MyPage.class);
- *     //assert rendered label component
+ *     // assert rendered label component
  *     tester.assertLabel(&quot;myMessage&quot;, &quot;Hello!&quot;);
  * }
  * </pre>
@@ -108,11 +108,11 @@ import org.slf4j.LoggerFactory;
  *     }
  * }
  * 
- * //test code
+ * // test code
  * public void testLinkToYourPage()
  * {
  *     tester.startPage(MyPage.class);
- *     //click link and render
+ *     // click link and render
  *     tester.clickLink(&quot;toYourPage&quot;);
  *     tester.assertRenderedPage(YourPage.class);
  *     tester.assertLabel(&quot;yourMessage&quot;, &quot;Hi!&quot;);
@@ -124,7 +124,7 @@ import org.slf4j.LoggerFactory;
  * <code>MyPage</code> is completed. Now we test <code>YourPage</code> 
standalone:
  * 
  * <pre>
- * //test code
+ * // test code
  * public void testRenderYourPage()
  * {
  *     // provide page instance source for WicketTester
@@ -538,8 +538,9 @@ public class WicketTester extends BaseWi
        public void assertNoErrorMessage()
        {
                List<Serializable> messages = 
getMessages(FeedbackMessage.ERROR);
-               Assert.assertTrue("expect no error message, but contains\n" +
-                       WicketTesterHelper.asLined(messages), 
messages.isEmpty());
+               Assert.assertTrue(
+                       "expect no error message, but contains\n" + 
WicketTesterHelper.asLined(messages),
+                       messages.isEmpty());
        }
 
        /**
@@ -548,8 +549,9 @@ 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), 
messages.isEmpty());
+               Assert.assertTrue(
+                       "expect no info message, but contains\n" + 
WicketTesterHelper.asLined(messages),
+                       messages.isEmpty());
        }
 
        /**
@@ -651,6 +653,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/branches/wicket-1.4.x/wicket/src/test/java/org/apache/wicket/util/tester/WicketTesterTest.java
URL: 
http://svn.apache.org/viewvc/wicket/branches/wicket-1.4.x/wicket/src/test/java/org/apache/wicket/util/tester/WicketTesterTest.java?rev=1037425&r1=1037424&r2=1037425&view=diff
==============================================================================
--- 
wicket/branches/wicket-1.4.x/wicket/src/test/java/org/apache/wicket/util/tester/WicketTesterTest.java
 (original)
+++ 
wicket/branches/wicket-1.4.x/wicket/src/test/java/org/apache/wicket/util/tester/WicketTesterTest.java
 Sun Nov 21 11:39:00 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,12 +29,14 @@ 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.PackageResource.PackageResourceBlockedException;
 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;
@@ -254,6 +257,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
@@ -546,8 +633,10 @@ public class WicketTesterTest extends Te
                Pojo pojo = page.getPojo();
 
                assertEquals("Mock name", pojo.getName());
-               assertEquals("Mock name", 
((TextField<?>)tester.getComponentFromLastRenderedPage("form" +
-                       Component.PATH_SEPARATOR + "name")).getValue());
+               assertEquals(
+                       "Mock name",
+                       
((TextField<?>)tester.getComponentFromLastRenderedPage("form" +
+                               Component.PATH_SEPARATOR + "name")).getValue());
 
                assertFalse(page.isExecuted());
 
@@ -558,8 +647,10 @@ public class WicketTesterTest extends Te
                assertTrue("AjaxFormSubmitBehavior.onSubmit() has not been 
executed in " +
                        MockPageWithFormAndAjaxFormSubmitBehavior.class, 
page.isExecuted());
 
-               assertEquals("Mock name", 
((TextField<?>)tester.getComponentFromLastRenderedPage("form" +
-                       Component.PATH_SEPARATOR + "name")).getValue());
+               assertEquals(
+                       "Mock name",
+                       
((TextField<?>)tester.getComponentFromLastRenderedPage("form" +
+                               Component.PATH_SEPARATOR + "name")).getValue());
 
                // The name of the pojo should still be the same. If the
                // executeAjaxEvent weren't submitting the form the name would 
have been
@@ -627,8 +718,9 @@ public class WicketTesterTest extends Te
 
        IRequestTargetUrlCodingStrategy getRequestCodingStrategy()
        {
-               String relativePath = 
tester.getApplication().getWicketFilter().getRelativePath(
-                       tester.getServletRequest());
+               String relativePath = tester.getApplication()
+                       .getWicketFilter()
+                       .getRelativePath(tester.getServletRequest());
                return tester.getApplication()
                        .getRequestCycleProcessor()
                        .getRequestCodingStrategy()


Reply via email to