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/09/22 11:24:42 UTC

svn commit: r999827 - in /wicket/trunk/wicket/src: main/java/org/apache/wicket/util/tester/ test/java/org/apache/wicket/util/tester/

Author: mgrigorov
Date: Wed Sep 22 09:24:41 2010
New Revision: 999827

URL: http://svn.apache.org/viewvc?rev=999827&view=rev
Log:
WICKET-3053 WicketTester does not preserve the form component values when submitting with AjaxSubmitLink

Serialize the form components' values in the request post parameters when clicking AjaxSubmitLink


Added:
    wicket/trunk/wicket/src/test/java/org/apache/wicket/util/tester/MockPageAjaxSubmitLinkSubmitsWholeForm.html   (with props)
    wicket/trunk/wicket/src/test/java/org/apache/wicket/util/tester/MockPageAjaxSubmitLinkSubmitsWholeForm.java   (with props)
Modified:
    wicket/trunk/wicket/src/main/java/org/apache/wicket/util/tester/BaseWicketTester.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=999827&r1=999826&r2=999827&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 Wed Sep 22 09:24:41 2010
@@ -22,11 +22,13 @@ import java.lang.reflect.Constructor;
 import java.lang.reflect.Method;
 import java.nio.charset.Charset;
 import java.util.ArrayList;
+import java.util.Collection;
 import java.util.Collections;
 import java.util.Enumeration;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
+import java.util.Set;
 import java.util.UUID;
 import java.util.regex.Pattern;
 
@@ -69,6 +71,7 @@ import org.apache.wicket.markup.html.lis
 import org.apache.wicket.markup.html.panel.Panel;
 import org.apache.wicket.mock.MockApplication;
 import org.apache.wicket.mock.MockPageManager;
+import org.apache.wicket.mock.MockRequestParameters;
 import org.apache.wicket.mock.MockSessionStore;
 import org.apache.wicket.page.IPageManager;
 import org.apache.wicket.page.IPageManagerContext;
@@ -1657,6 +1660,8 @@ public class BaseWicketTester
 		String failMessage = "No form attached to the submitlink.";
 		notNull(failMessage, form);
 
+		serializeFormToRequest(form);
+
 		Url url = Url.parse(behavior.getCallbackUrl().toString(),
 			Charset.forName(request.getCharacterEncoding()));
 		transform(url);
@@ -1667,6 +1672,44 @@ public class BaseWicketTester
 	}
 
 	/**
+	 * Puts all not already scheduled (e.g. via {@link FormTester#setValue(String, String)}) form
+	 * component values in the post parameters for the next form submit
+	 * 
+	 * @param form
+	 *            the {@link Form} which components should be submitted
+	 */
+	private void serializeFormToRequest(final Form<?> form)
+	{
+		final MockRequestParameters postParameters = request.getPostParameters();
+
+		final Set<String> currentParameterNamesSet = postParameters.getParameterNames();
+
+		form.visitFormComponents(new IVisitor<FormComponent<?>, Void>()
+		{
+			public void component(final FormComponent<?> formComponent, final IVisit<Void> visit)
+			{
+				final String inputName = formComponent.getInputName();
+				if (!currentParameterNamesSet.contains(inputName))
+				{
+					final Object modelObject = formComponent.getModelObject();
+					if (modelObject instanceof Collection<?>)
+					{
+						final Collection<?> collectionModelObject = (Collection<?>)modelObject;
+						for (Object value : collectionModelObject)
+						{
+							postParameters.addParameterValue(inputName, value.toString());
+						}
+					}
+					else
+					{
+						postParameters.addParameterValue(inputName, formComponent.getValue());
+					}
+				}
+			}
+		});
+	}
+
+	/**
 	 * Retrieves the content type from the response header.
 	 * 
 	 * @return the content type from the response header

Added: wicket/trunk/wicket/src/test/java/org/apache/wicket/util/tester/MockPageAjaxSubmitLinkSubmitsWholeForm.html
URL: http://svn.apache.org/viewvc/wicket/trunk/wicket/src/test/java/org/apache/wicket/util/tester/MockPageAjaxSubmitLinkSubmitsWholeForm.html?rev=999827&view=auto
==============================================================================
--- wicket/trunk/wicket/src/test/java/org/apache/wicket/util/tester/MockPageAjaxSubmitLinkSubmitsWholeForm.html (added)
+++ wicket/trunk/wicket/src/test/java/org/apache/wicket/util/tester/MockPageAjaxSubmitLinkSubmitsWholeForm.html Wed Sep 22 09:24:41 2010
@@ -0,0 +1,29 @@
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
+        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
+<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"
+	xmlns:wicket="http://wicket.apache.org/dtds.data/wicket-xhtml1.4-strict.dtd">
+    <head>
+        <title>LegUp: Wicket Guice 2.0</title>
+    </head>
+    <body>
+    <div>
+    <strong><a href="http://jweekend.com/dev/LegUp">LegUp</a>: <a href="http://wicket.apache.org/">Wicket</a> <a href="http://code.google.com/p/google-guice/">Guice 2.0</a></strong>
+        <br/><br/>
+        
+        <h1 wicket:id="text"></h1>
+        
+        <form wicket:id="form">
+        <fieldset>
+        	<label>Name</label><input wicket:id="name" type="text" />
+        </fieldset>
+        </form>
+        <br/><br/>
+        <a wicket:id="helloSubmit">Hello</a>
+        <br/>
+        <a wicket:id="goodbyeSubmit">Goodbye</a>
+        
+        
+        </div>
+    </body>
+</html>
+

Propchange: wicket/trunk/wicket/src/test/java/org/apache/wicket/util/tester/MockPageAjaxSubmitLinkSubmitsWholeForm.html
------------------------------------------------------------------------------
    svn:eol-style = native

Added: wicket/trunk/wicket/src/test/java/org/apache/wicket/util/tester/MockPageAjaxSubmitLinkSubmitsWholeForm.java
URL: http://svn.apache.org/viewvc/wicket/trunk/wicket/src/test/java/org/apache/wicket/util/tester/MockPageAjaxSubmitLinkSubmitsWholeForm.java?rev=999827&view=auto
==============================================================================
--- wicket/trunk/wicket/src/test/java/org/apache/wicket/util/tester/MockPageAjaxSubmitLinkSubmitsWholeForm.java (added)
+++ wicket/trunk/wicket/src/test/java/org/apache/wicket/util/tester/MockPageAjaxSubmitLinkSubmitsWholeForm.java Wed Sep 22 09:24:41 2010
@@ -0,0 +1,88 @@
+package org.apache.wicket.util.tester;
+
+import org.apache.wicket.ajax.AjaxRequestTarget;
+import org.apache.wicket.ajax.markup.html.form.AjaxSubmitLink;
+import org.apache.wicket.markup.html.WebPage;
+import org.apache.wicket.markup.html.basic.Label;
+import org.apache.wicket.markup.html.form.Form;
+import org.apache.wicket.markup.html.form.TextField;
+import org.apache.wicket.model.PropertyModel;
+import org.apache.wicket.request.mapper.parameter.PageParameters;
+
+/**
+ * WICKET-3053
+ * 
+ * Clicking AjaxSubmitLink does not preserve the values of the form components and they get
+ * null-ified
+ * 
+ * 
+ * The code is borrowed from Wicket+Guice LegUp (http://jweekend.com/dev/LegUp)
+ * 
+ * @author Richard Wilkinson - richard.wilkinson@jweekend.com
+ */
+public class MockPageAjaxSubmitLinkSubmitsWholeForm extends WebPage
+{
+	private static final long serialVersionUID = 1L;
+
+	private String text;
+
+	private String name;
+
+	/**
+	 * Constructor that is invoked when page is invoked without a em.
+	 * 
+	 * @param parameters
+	 *            Page parameters
+	 */
+	public MockPageAjaxSubmitLinkSubmitsWholeForm(final PageParameters parameters)
+	{
+		final Label label = new Label("text", new PropertyModel<String>(this, "text"));
+		label.setOutputMarkupId(true);
+
+		add(label);
+
+		Form<Void> form = new Form<Void>("form");
+		form.add(new TextField<String>("name", new PropertyModel<String>(this, "name")));
+
+		add(form);
+
+		add(new AjaxSubmitLink("helloSubmit", form)
+		{
+
+			private static final long serialVersionUID = 1L;
+
+			@Override
+			protected void onSubmit(AjaxRequestTarget target, Form<?> form)
+			{
+				text = "Hello " + name;
+				target.addComponent(label);
+			}
+
+			@Override
+			protected void onError(AjaxRequestTarget target, Form<?> form)
+			{
+				throw new RuntimeException("Unexpected error occurred.");
+			}
+		});
+
+		add(new AjaxSubmitLink("goodbyeSubmit", form)
+		{
+
+			private static final long serialVersionUID = 1L;
+
+			@Override
+			protected void onSubmit(AjaxRequestTarget target, Form<?> form)
+			{
+				text = "Goodbye " + name;
+				target.addComponent(label);
+			}
+
+			@Override
+			protected void onError(AjaxRequestTarget target, Form<?> form)
+			{
+				throw new RuntimeException("Unexpected error occurred.");
+			}
+		});
+
+	}
+}

Propchange: wicket/trunk/wicket/src/test/java/org/apache/wicket/util/tester/MockPageAjaxSubmitLinkSubmitsWholeForm.java
------------------------------------------------------------------------------
    svn:eol-style = native

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=999827&r1=999826&r2=999827&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 Wed Sep 22 09:24:41 2010
@@ -471,6 +471,43 @@ public class WicketTesterTest extends Te
 	}
 
 	/**
+	 * Test that the clickLink on AjaxSubmitLink will re-submit/preserve the values for all form
+	 * components WICKET-3053
+	 */
+	public void testClickLink_ajaxSubmitLink_preservesFormComponentValues()
+	{
+		tester.startPage(MockPageAjaxSubmitLinkSubmitsWholeForm.class);
+
+		tester.assertRenderedPage(MockPageAjaxSubmitLinkSubmitsWholeForm.class);
+
+		FormTester formTester = tester.newFormTester("form");
+
+		formTester.setValue("name", "Bob");
+
+// do not call 'formTester.submit()'. The value of 'name' will be submitted by
+// AjaxSubmitLink
+// formTester.submit();
+
+		// this will submit 'name=Bob'
+		tester.clickLink("helloSubmit", true);
+
+		tester.assertModelValue("form:name", "Bob");
+
+		tester.assertComponentOnAjaxResponse("text");
+
+		tester.assertModelValue("text", "Hello Bob");
+
+		// this should preserve the value of 'name'
+		tester.clickLink("goodbyeSubmit", true);
+
+		tester.assertModelValue("form:name", "Bob");
+
+		tester.assertComponentOnAjaxResponse("text");
+
+		tester.assertModelValue("text", "Goodbye Bob");
+	}
+
+	/**
 	 * Test that the executeAjaxEvent "submits" the form if the event is a AjaxFormSubmitBehavior.
 	 */
 	public void testExecuteAjaxEvent_ajaxFormSubmitLink()