Author: freemant
Date: Sat Jan 20 02:33:55 2007
New Revision: 498098
URL: http://svn.apache.org/viewvc?view=rev&rev=498098
Log:
Supports submit buttons in PageTester.
Added:
tapestry/tapestry5/tapestry-core/trunk/src/test/java/org/apache/tapestry/integration/app2/pages/TestPageForSubmit.java
(with props)
tapestry/tapestry5/tapestry-core/trunk/src/test/java/org/apache/tapestry/integration/pagelevel/SubmitTest.java
(with props)
tapestry/tapestry5/tapestry-core/trunk/src/test/resources/org/apache/tapestry/integration/app2/pages/TestPageForSubmit.html
(with props)
Modified:
tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/test/pagelevel/PageTester.java
tapestry/tapestry5/tapestry-core/trunk/src/site/apt/guide/unit-testing-pages.apt
tapestry/tapestry5/tapestry-core/trunk/src/test/java/org/apache/tapestry/integration/app2/pages/TestPageForForm.java
Modified:
tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/test/pagelevel/PageTester.java
URL:
http://svn.apache.org/viewvc/tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/test/pagelevel/PageTester.java?view=diff&rev=498098&r1=498097&r2=498098
==============================================================================
---
tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/test/pagelevel/PageTester.java
(original)
+++
tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/test/pagelevel/PageTester.java
Sat Jan 20 02:33:55 2007
@@ -243,6 +243,60 @@
return invoke(invocation);
}
+ /**
+ * Simulates a submission of the form by clicking the specified submit
button. The caller can
+ * specify values for the form fields.
+ *
+ * @param submitButton
+ * the submit button to be clicked.
+ * @param fieldValues
+ * the field values keyed on field names.
+ * @return The DOM created. Typically you will assert against it.
+ */
+ public Document clickSubmit(Element submitButton, Map<String, String>
fieldValues)
+ {
+ final String DEFAULT_SUBMIT_VALUE_ATTRIBUTE = "Submit Query";
+ Defense.notNull(submitButton, "submitButton");
+ assertIsSubmit(submitButton);
+ Element form = getFormAncestor(submitButton);
+ String value = submitButton.getAttribute("value");
+ if (value == null)
+ {
+ value = DEFAULT_SUBMIT_VALUE_ATTRIBUTE;
+ }
+ fieldValues.put(submitButton.getAttribute("name"), value);
+ return submitForm(form, fieldValues);
+ }
+
+ private void assertIsSubmit(Element element)
+ {
+ if (element.getName().equals("input"))
+ {
+ String type = element.getAttribute("type");
+ if (type != null && type.equals("submit"))
+ {
+ return;
+ }
+ }
+ throw new IllegalArgumentException("The specified element is not a
submit button");
+ }
+
+ private Element getFormAncestor(Element element)
+ {
+ while (true)
+ {
+ if (element == null)
+ {
+ throw new IllegalArgumentException("The given element is not
contained in a form");
+ }
+ if (element.getName().equalsIgnoreCase("form"))
+ {
+ return element;
+ }
+ element = element.getParent();
+ }
+ }
+
private void addHiddenFormFields(Element element)
{
if (isHiddenFormField(element))
@@ -269,4 +323,5 @@
{
_preferedLanguage = preferedLanguage;
}
+
}
Modified:
tapestry/tapestry5/tapestry-core/trunk/src/site/apt/guide/unit-testing-pages.apt
URL:
http://svn.apache.org/viewvc/tapestry/tapestry5/tapestry-core/trunk/src/site/apt/guide/unit-testing-pages.apt?view=diff&rev=498098&r1=498097&r2=498098
==============================================================================
---
tapestry/tapestry5/tapestry-core/trunk/src/site/apt/guide/unit-testing-pages.apt
(original)
+++
tapestry/tapestry5/tapestry-core/trunk/src/site/apt/guide/unit-testing-pages.apt
Sat Jan 20 02:33:55 2007
@@ -128,6 +128,10 @@
}
+---+
+ To submit a form by clicking a submit button, call the
+
{{{../apidocs/org/apache/tapestry/test/pagelevel/PageTester.html#clickSubmit(org.apache.tapestry.dom.Element,
java.util.Map)}clickSubmit()}}
+ method instead.
+
* Unit testing a component
To unit test a component, just create a test page containing that component.
Then
Modified:
tapestry/tapestry5/tapestry-core/trunk/src/test/java/org/apache/tapestry/integration/app2/pages/TestPageForForm.java
URL:
http://svn.apache.org/viewvc/tapestry/tapestry5/tapestry-core/trunk/src/test/java/org/apache/tapestry/integration/app2/pages/TestPageForForm.java?view=diff&rev=498098&r1=498097&r2=498098
==============================================================================
---
tapestry/tapestry5/tapestry-core/trunk/src/test/java/org/apache/tapestry/integration/app2/pages/TestPageForForm.java
(original)
+++
tapestry/tapestry5/tapestry-core/trunk/src/test/java/org/apache/tapestry/integration/app2/pages/TestPageForForm.java
Sat Jan 20 02:33:55 2007
@@ -12,17 +12,17 @@
// See the License for the specific language governing permissions and
// limitations under the License.
-package org.apache.tapestry.integration.app2.pages;
-
+package org.apache.tapestry.integration.app2.pages;
+
import org.apache.tapestry.annotations.ComponentClass;
import org.apache.tapestry.annotations.Persist;
-
[EMAIL PROTECTED]
-public class TestPageForForm
+
[EMAIL PROTECTED]
+public class TestPageForForm
{
@Persist
private String value;
-
+
public String getValue()
{
return value;
@@ -31,5 +31,6 @@
public void setValue(String value)
{
this.value = value;
- }
-}
+ }
+
+}
Added:
tapestry/tapestry5/tapestry-core/trunk/src/test/java/org/apache/tapestry/integration/app2/pages/TestPageForSubmit.java
URL:
http://svn.apache.org/viewvc/tapestry/tapestry5/tapestry-core/trunk/src/test/java/org/apache/tapestry/integration/app2/pages/TestPageForSubmit.java?view=auto&rev=498098
==============================================================================
---
tapestry/tapestry5/tapestry-core/trunk/src/test/java/org/apache/tapestry/integration/app2/pages/TestPageForSubmit.java
(added)
+++
tapestry/tapestry5/tapestry-core/trunk/src/test/java/org/apache/tapestry/integration/app2/pages/TestPageForSubmit.java
Sat Jan 20 02:33:55 2007
@@ -0,0 +1,89 @@
+// Copyright 2007 The Apache Software Foundation
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package org.apache.tapestry.integration.app2.pages;
+
+import org.apache.tapestry.annotations.Component;
+import org.apache.tapestry.annotations.ComponentClass;
+import org.apache.tapestry.annotations.OnEvent;
+import org.apache.tapestry.annotations.Persist;
+import org.apache.tapestry.corelib.components.Form;
+import org.apache.tapestry.corelib.components.Submit;
+import org.apache.tapestry.corelib.components.TextField;
+
[EMAIL PROTECTED]
+public class TestPageForSubmit
+{
+ @SuppressWarnings("unused")
+ @Component
+ private Form form1;
+
+ @SuppressWarnings("unused")
+ @Component
+ private Form form2;
+
+ @SuppressWarnings("unused")
+ @Component
+ private Submit capitalize1;
+
+ @SuppressWarnings("unused")
+ @Component
+ private Submit capitalize2;
+
+ @SuppressWarnings("unused")
+ @Component(parameters = "value=value1")
+ private TextField t1;
+
+ @SuppressWarnings("unused")
+ @Component(parameters = "value=value2")
+ private TextField t2;
+
+ @Persist
+ private String value1;
+
+ @Persist
+ private String value2;
+
+ public String getValue1()
+ {
+ return value1;
+ }
+
+ public void setValue1(String value)
+ {
+ this.value1 = value;
+ }
+
+ @OnEvent(component = "capitalize1", value = "selected")
+ public void onCapitalize1()
+ {
+ value1 = value1.toUpperCase();
+ }
+
+ @OnEvent(component = "capitalize2", value = "selected")
+ public void onCapitalize2()
+ {
+ value2 = value2.toUpperCase();
+ }
+
+ public String getValue2()
+ {
+ return value2;
+ }
+
+ public void setValue2(String value2)
+ {
+ this.value2 = value2;
+ }
+}
Propchange:
tapestry/tapestry5/tapestry-core/trunk/src/test/java/org/apache/tapestry/integration/app2/pages/TestPageForSubmit.java
------------------------------------------------------------------------------
svn:eol-style = native
Added:
tapestry/tapestry5/tapestry-core/trunk/src/test/java/org/apache/tapestry/integration/pagelevel/SubmitTest.java
URL:
http://svn.apache.org/viewvc/tapestry/tapestry5/tapestry-core/trunk/src/test/java/org/apache/tapestry/integration/pagelevel/SubmitTest.java?view=auto&rev=498098
==============================================================================
---
tapestry/tapestry5/tapestry-core/trunk/src/test/java/org/apache/tapestry/integration/pagelevel/SubmitTest.java
(added)
+++
tapestry/tapestry5/tapestry-core/trunk/src/test/java/org/apache/tapestry/integration/pagelevel/SubmitTest.java
Sat Jan 20 02:33:55 2007
@@ -0,0 +1,86 @@
+// Copyright 2007 The Apache Software Foundation
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package org.apache.tapestry.integration.pagelevel;
+
+import java.util.Map;
+
+import org.apache.tapestry.dom.Document;
+import org.apache.tapestry.dom.Element;
+import org.apache.tapestry.ioc.internal.util.CollectionFactory;
+import org.apache.tapestry.test.pagelevel.PageTester;
+import org.testng.Assert;
+import org.testng.annotations.AfterMethod;
+import org.testng.annotations.BeforeMethod;
+import org.testng.annotations.Test;
+
+public class SubmitTest extends Assert
+{
+ private PageTester _tester;
+
+ private Document _doc;
+
+ private Map<String, String> _fieldValues;
+
+ @Test
+ public void submit_form()
+ {
+ Element submitButton = _doc.getElementById("capitalize1");
+ _fieldValues.put("t1", "hello");
+ _doc = _tester.clickSubmit(submitButton, _fieldValues);
+ assertTrue(_doc.toString().contains("t1 is: HELLO"));
+ }
+
+ @Test
+ public void access_following_fields()
+ {
+ Element submitButton = _doc.getElementById("capitalize2");
+ _fieldValues.put("t2", "world");
+ _doc = _tester.clickSubmit(submitButton, _fieldValues);
+ assertTrue(_doc.toString().contains("t2 is: WORLD"));
+ }
+
+ @Test(expectedExceptions = IllegalArgumentException.class)
+ public void not_a_submit()
+ {
+ Element submitButton = _doc.getElementById("t1");
+ _tester.clickSubmit(submitButton, _fieldValues);
+ }
+
+ @Test(expectedExceptions = IllegalArgumentException.class)
+ public void not_in_form()
+ {
+ Element submitButton = _doc.getElementById("orphanedSubmit");
+ _tester.clickSubmit(submitButton, _fieldValues);
+ }
+
+ @BeforeMethod
+ public void before()
+ {
+ String appPackage = "org.apache.tapestry.integration.app2";
+ String appName = "";
+ _tester = new PageTester(appPackage, appName);
+ _doc = _tester.renderPage("TestPageForSubmit");
+ _fieldValues = CollectionFactory.newMap();
+ }
+
+ @AfterMethod
+ public void after()
+ {
+ if (_tester != null)
+ {
+ _tester.shutdown();
+ }
+ }
+}
Propchange:
tapestry/tapestry5/tapestry-core/trunk/src/test/java/org/apache/tapestry/integration/pagelevel/SubmitTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Added:
tapestry/tapestry5/tapestry-core/trunk/src/test/resources/org/apache/tapestry/integration/app2/pages/TestPageForSubmit.html
URL:
http://svn.apache.org/viewvc/tapestry/tapestry5/tapestry-core/trunk/src/test/resources/org/apache/tapestry/integration/app2/pages/TestPageForSubmit.html?view=auto&rev=498098
==============================================================================
---
tapestry/tapestry5/tapestry-core/trunk/src/test/resources/org/apache/tapestry/integration/app2/pages/TestPageForSubmit.html
(added)
+++
tapestry/tapestry5/tapestry-core/trunk/src/test/resources/org/apache/tapestry/integration/app2/pages/TestPageForSubmit.html
Sat Jan 20 02:33:55 2007
@@ -0,0 +1,16 @@
+<html xmlns:t="http://tapestry.apache.org/schema/tapestry_5_0_0.xsd">
+<p>
+<form t:id="form1">
+ <t:comp id="t1" type="TextField"/>
+ <t:comp id="capitalize1"/>
+</form>
+<form t:id="form2">
+ <t:comp id="capitalize2"/>
+ <t:comp id="t2" type="TextField"/>
+</form></p>
+<input type="submit" id="orphanedSubmit"/>
+<p>
+t1 is: ${value1}.
+t2 is: ${value2}.
+</p>
+</html>
Propchange:
tapestry/tapestry5/tapestry-core/trunk/src/test/resources/org/apache/tapestry/integration/app2/pages/TestPageForSubmit.html
------------------------------------------------------------------------------
svn:eol-style = native