introduce integration tests with minimum needed changes (#301)

* introduce integration tests with minimum needed changes

* report coverage for integration tests as well

References:
https://stackoverflow.com/a/41364462
http://www.mojohaus.org/cobertura-maven-plugin/usage.html#reports

* switch to JaCoCo as test coverage library

Cobertura latest release is old, 2015, has issues with java and an old weird issue with integration tests [1].

[1] https://github.com/cobertura/cobertura/issues/230

* Use HtmlUnit instead of JWebUnit
This commit is contained in:
Yasser Zamani
2018-12-31 19:21:49 +03:30
committed by GitHub
parent 83a0927935
commit fa90e01ba5
20 changed files with 502 additions and 241 deletions
+55 -17
View File
@@ -141,13 +141,6 @@
<artifactId>commons-fileupload</artifactId>
</dependency>
<dependency>
<groupId>net.sourceforge.jwebunit</groupId>
<artifactId>jwebunit-core</artifactId>
<version>1.4.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>commons-httpclient</groupId>
<artifactId>commons-httpclient</artifactId>
@@ -155,17 +148,17 @@
<scope>test</scope>
</dependency>
<dependency>
<groupId>net.sourceforge.jwebunit</groupId>
<artifactId>jwebunit-htmlunit-plugin</artifactId>
<version>1.4.1</version>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>net.sourceforge.htmlunit</groupId>
<artifactId>htmlunit</artifactId>
<version>2.33</version>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>xom</groupId>
<artifactId>xom</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- BeanValidation Example -->
@@ -188,6 +181,30 @@
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.22.1</version>
<configuration>
<includes>
<include>it.org.apache.struts2.showcase.*Test</include>
</includes>
</configuration>
<executions>
<execution>
<id>integration-test</id>
<goals>
<goal>integration-test</goal>
</goals>
</execution>
<execution>
<id>verify</id>
<goals>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
@@ -212,6 +229,27 @@
<descriptor>${basedir}/src/main/webapp/WEB-INF/web.xml</descriptor>
</webAppConfig>
</configuration>
<executions>
<execution>
<id>start-jetty</id>
<phase>pre-integration-test</phase>
<goals>
<!-- stop any previous instance to free up the port -->
<goal>stop</goal>
<goal>run-forked</goal>
</goals>
<configuration>
<waitForChild>false</waitForChild>
</configuration>
</execution>
<execution>
<id>stop-jetty</id>
<phase>post-integration-test</phase>
<goals>
<goal>stop</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
@@ -20,6 +20,8 @@
*/
package org.apache.struts2.showcase.chat;
import com.opensymphony.xwork2.conversion.impl.XWorkConverter;
import com.opensymphony.xwork2.inject.Inject;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;
import org.apache.struts2.util.StrutsTypeConverter;
@@ -33,6 +35,13 @@ public class DateConverter extends StrutsTypeConverter {
private static final Logger LOG = LogManager.getLogger(DateConverter.class);
private XWorkConverter fallbackConverter;
@Inject
public void setXWorkConverter(XWorkConverter fallbackConverter) {
this.fallbackConverter = fallbackConverter;
}
public Object convertFromString(Map context, String[] values, Class toClass) {
if (values.length > 0 && values[0] != null && values[0].trim().length() > 0) {
@@ -40,7 +49,8 @@ public class DateConverter extends StrutsTypeConverter {
try {
return sdf.parse(values[0]);
} catch (ParseException e) {
LOG.error("error converting value [" + values[0] + "] to Date ", e);
LOG.warn("error converting value [" + values[0] + "] to Date. Trying fallback converter.");
return this.fallbackConverter.convertValue(context, values[0], toClass);
}
}
return null;
@@ -71,7 +71,7 @@
</field>
<field name="regexValidatorField">
<field-validator type="regex">
<param name="regex"><![CDATA[ [^<>]+ ]]></param>
<param name="regex"><![CDATA[ .*\.txt ]]></param>
<message><![CDATA[ regexValidatorField must match a regexp (.*\.txt) if specified ]]></message>
</field-validator>
</field>
@@ -26,6 +26,7 @@
this.lastIndex = 0;
this.sendMessage = function(message) {
$.ajax({
async: false,
url: "sendMessage",
data: {
message: message
@@ -20,11 +20,22 @@
*/
package it.org.apache.struts2.showcase;
public class ActionChainingTest extends ITBaseTest {
public void test() {
beginAt("/actionchaining/actionChain1!input");
assertTextPresent("Action Chain 1 Property 1: Property Set In Action Chain 1");
assertTextPresent("Action Chain 2 Property 1: Property Set in Action Chain 2");
assertTextPresent("Action Chain 3 Property 1: Property set in Action Chain 3");
import org.junit.Assert;
import org.junit.Test;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
public class ActionChainingTest {
@Test
public void test() throws Exception {
try (final WebClient webClient = new WebClient()) {
final HtmlPage page = webClient.getPage(ParameterUtils.getBaseUrl() + "/actionchaining/actionChain1!input");
final String pageAsText = page.asText();
Assert.assertTrue(pageAsText.contains("Action Chain 1 Property 1: Property Set In Action Chain 1"));
Assert.assertTrue(pageAsText.contains("Action Chain 2 Property 1: Property Set in Action Chain 2"));
Assert.assertTrue(pageAsText.contains("Action Chain 3 Property 1: Property set in Action Chain 3"));
}
}
}
@@ -20,10 +20,20 @@
*/
package it.org.apache.struts2.showcase;
public class ActionTagExampleTest extends ITBaseTest {
public void test() {
beginAt("/tags/ui/actionTagExample!input.action");
assertTextPresent("This text is from the called class");
}
import org.junit.Assert;
import org.junit.Test;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
public class ActionTagExampleTest {
@Test
public void test() throws Exception {
try (final WebClient webClient = new WebClient()) {
final HtmlPage page = webClient.getPage(ParameterUtils.getBaseUrl() + "/tags/ui/actionTagExample!input.action");
final String pageAsText = page.asText();
Assert.assertTrue(pageAsText.contains("This text is from the called class"));
}
}
}
@@ -18,13 +18,35 @@
*/
package it.org.apache.struts2.showcase;
public class AsyncTest extends ITBaseTest {
public void testChatRoom() throws InterruptedException {
beginAt("/async/index.html");
import org.junit.Assert;
import org.junit.Test;
setTextField("msg", "hello");
submit();
Thread.sleep(4000);
assertTextInElement("msgs", "hello");
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.DomElement;
import com.gargoylesoftware.htmlunit.html.HtmlForm;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.html.HtmlSubmitInput;
import com.gargoylesoftware.htmlunit.html.HtmlTextInput;
public class AsyncTest {
@Test
public void testChatRoom() throws Exception {
try (final WebClient webClient = new WebClient()) {
final HtmlPage page = webClient.getPage(ParameterUtils.getBaseUrl() + "/async/index.html");
final HtmlForm form = page.getForms().get(0);
final HtmlTextInput textField = form.getInputByName("msg");
textField.type("hello");
final HtmlSubmitInput button = form.getInputByValue("Send");
final HtmlPage page2 = button.click();
Thread.sleep(4000);
final DomElement msgs = page2.getElementById("msgs");
Assert.assertEquals("hello", msgs.asText());
}
}
}
@@ -20,17 +20,34 @@
*/
package it.org.apache.struts2.showcase;
public class CRUDTest extends ITBaseTest {
public void testCreate() {
beginAt("/skill/edit.action");
import org.junit.Assert;
import org.junit.Test;
setTextField("currentSkill.name", "somename1");
setTextField("currentSkill.description", "somedescription1");
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlForm;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.html.HtmlSubmitInput;
import com.gargoylesoftware.htmlunit.html.HtmlTextInput;
submit();
public class CRUDTest {
@Test
public void testCreate() throws Exception {
try (final WebClient webClient = new WebClient()) {
final HtmlPage page = webClient.getPage(ParameterUtils.getBaseUrl() + "/skill/edit.action");
beginAt("/skill/list.action");
assertTextPresent("somename1");
assertTextPresent("somedescription1");
final HtmlForm form = page.getForms().get(0);
final HtmlTextInput textField = form.getInputByName("currentSkill.name");
textField.type("somename1");
final HtmlTextInput textField2 = form.getInputByName("currentSkill.description");
textField2.type("somedescription1");
final HtmlSubmitInput button = form.getInputByValue("Save");
final HtmlPage page2 = button.click();
final String page2Text = page2.asText();
Assert.assertTrue(page2Text.contains("somename1"));
Assert.assertTrue(page2Text.contains("somedescription1"));
}
}
}
@@ -20,12 +20,24 @@
*/
package it.org.apache.struts2.showcase;
public class ComponentTagExampleTest extends ITBaseTest {
public void test() {
beginAt("/tags/ui/componentTagExample.jsp");
assertTextPresent("Freemarker Custom Template - parameter 'paramName' - paramValue1");
assertTextPresent("Freemarker Custom Template - parameter 'paramName' - paramValue4");
assertTextPresent("JSP Custom Template - parameter 'paramName' - paramValue2");
assertTextPresent("JSP Custom Template - parameter 'paramName' - paramValue3");
import org.junit.Assert;
import org.junit.Test;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
public class ComponentTagExampleTest {
@Test
public void test() throws Exception {
try (final WebClient webClient = new WebClient()) {
final HtmlPage page = webClient
.getPage(ParameterUtils.getBaseUrl() + "/tags/ui/componentTagExample.action");
final String pageAsText = page.asText();
Assert.assertTrue(pageAsText.contains("Freemarker Custom Template - parameter 'paramName' - paramValue1"));
Assert.assertTrue(pageAsText.contains("Freemarker Custom Template - parameter 'paramName' - paramValue4"));
Assert.assertTrue(pageAsText.contains("JSP Custom Template - parameter 'paramName' - paramValue2"));
Assert.assertTrue(pageAsText.contains("JSP Custom Template - parameter 'paramName' - paramValue3"));
}
}
}
@@ -20,48 +20,82 @@
*/
package it.org.apache.struts2.showcase;
public class ConversionTest extends ITBaseTest {
public void testList() {
beginAt("/conversion/enterPersonsInfo.action");
setTextField("persons[0].name", "name0");
setTextField("persons[0].age", "0");
setTextField("persons[1].name", "name1");
setTextField("persons[1].age", "1");
setTextField("persons[2].name", "name2");
setTextField("persons[2].age", "2");
import org.junit.Assert;
import org.junit.Test;
submit();
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlForm;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.html.HtmlSubmitInput;
assertTextPresent("SET 0 Name: name0");
assertTextPresent("SET 0 Age: 0");
assertTextPresent("SET 1 Name: name1");
assertTextPresent("SET 1 Age: 1");
assertTextPresent("SET 2 Name: name2");
assertTextPresent("SET 2 Age: 2");
public class ConversionTest {
@Test
public void testList() throws Exception {
try (final WebClient webClient = new WebClient()) {
final HtmlPage page = webClient
.getPage(ParameterUtils.getBaseUrl() + "/conversion/enterPersonsInfo.action");
final HtmlForm form = page.getForms().get(0);
form.getInputByName("persons[0].name").type("name0");
form.getInputByName("persons[0].age").type("0");
form.getInputByName("persons[1].name").type("name1");
form.getInputByName("persons[1].age").type("1");
form.getInputByName("persons[2].name").type("name2");
form.getInputByName("persons[2].age").type("2");
final HtmlSubmitInput button = form.getInputByValue("Submit");
final HtmlPage page2 = button.click();
final String page2Text = page2.asText();
Assert.assertTrue(page2Text.contains("SET 0 Name: name0"));
Assert.assertTrue(page2Text.contains("SET 0 Age: 0"));
Assert.assertTrue(page2Text.contains("SET 1 Name: name1"));
Assert.assertTrue(page2Text.contains("SET 1 Age: 1"));
Assert.assertTrue(page2Text.contains("SET 2 Name: name2"));
Assert.assertTrue(page2Text.contains("SET 2 Age: 2"));
}
}
public void testSet() {
beginAt("/conversion/enterAddressesInfo.action");
setTextField("addresses('id0').address", "address0");
setTextField("addresses('id1').address", "address1");
setTextField("addresses('id2').address", "address2");
@Test
public void testSet() throws Exception {
try (final WebClient webClient = new WebClient()) {
final HtmlPage page = webClient
.getPage(ParameterUtils.getBaseUrl() + "/conversion/enterAddressesInfo.action");
submit();
final HtmlForm form = page.getForms().get(0);
assertTextPresent("id0 -> address0");
assertTextPresent("id1 -> address1");
assertTextPresent("id2 -> address2");
form.getInputByName("addresses('id0').address").type("address0");
form.getInputByName("addresses('id1').address").type("address1");
form.getInputByName("addresses('id2').address").type("address2");
final HtmlSubmitInput button = form.getInputByValue("Submit");
final HtmlPage page2 = button.click();
final String page2Text = page2.asText();
Assert.assertTrue(page2Text.contains("id0 -> address0"));
Assert.assertTrue(page2Text.contains("id1 -> address1"));
Assert.assertTrue(page2Text.contains("id2 -> address2"));
}
}
public void testEnum() {
beginAt("/conversion/enterOperationEnumInfo.action");
checkCheckbox("selectedOperations", "ADD");
checkCheckbox("selectedOperations", "MINUS");
@Test
public void testEnum() throws Exception {
try (final WebClient webClient = new WebClient()) {
final HtmlPage page = webClient
.getPage(ParameterUtils.getBaseUrl() + "/conversion/enterOperationEnumInfo.action");
submit();
final HtmlForm form = page.getForms().get(0);
assertTextPresent("ADD");
assertTextPresent("MINUS");
form.getInputByValue("ADD").setChecked(true);
form.getInputByValue("MINUS").setChecked(true);
final HtmlSubmitInput button = form.getInputByValue("Submit");
final HtmlPage page2 = button.click();
final String page2Text = page2.asText();
Assert.assertTrue(page2Text.contains("ADD"));
Assert.assertTrue(page2Text.contains("MINUS"));
}
}
}
@@ -20,16 +20,35 @@
*/
package it.org.apache.struts2.showcase;
public class ExecAndWaitTest extends ITBaseTest {
public void testNodelay() throws InterruptedException {
beginAt("/wait/example1.jsp");
import org.junit.Assert;
import org.junit.Test;
setTextField("time", "7000");
submit();
assertTextPresent("We are processing your request. Please wait.");
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlForm;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.html.HtmlSubmitInput;
import com.gargoylesoftware.htmlunit.html.HtmlTextInput;
//hit it again
beginAt("/wait/longProcess1.action?time=1000");
assertTextPresent("We are processing your request. Please wait.");
public class ExecAndWaitTest {
@Test
public void testNodelay() throws Exception {
try (final WebClient webClient = new WebClient()) {
final HtmlPage page = webClient.getPage(ParameterUtils.getBaseUrl() + "/wait/example1.action");
final HtmlForm form = page.getForms().get(0);
final HtmlTextInput textField = form.getInputByName("time");
textField.type("7000");
final HtmlSubmitInput button = form.getInputByValue("submit");
final HtmlPage page2 = button.click();
Assert.assertTrue(page2.asText().contains("We are processing your request. Please wait."));
// hit it again
final HtmlPage page3 = webClient
.getPage(ParameterUtils.getBaseUrl() + "/wait/longProcess1.action?time=1000");
Assert.assertTrue(page3.asText().contains("We are processing your request. Please wait."));
}
}
}
@@ -20,21 +20,50 @@
*/
package it.org.apache.struts2.showcase;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.MalformedURLException;
public class FileDownloadTest extends ITBaseTest {
public void testImage() throws InterruptedException, MalformedURLException {
beginAt("/filedownload/download.action");
import org.junit.Assert;
import org.junit.Test;
URL url = new URL("http://svn.apache.org/repos/asf/struts/struts2/trunk/apps/showcase/src/main/webapp/images/struts.gif");
assertDownloadedFileEquals(url);
import com.gargoylesoftware.htmlunit.Page;
import com.gargoylesoftware.htmlunit.WebClient;
public class FileDownloadTest {
@Test
public void testImage() throws Exception {
try (final WebClient webClient = new WebClient()) {
final Page page = webClient.getPage(ParameterUtils.getBaseUrl() + "/filedownload/download.action");
URL url = new URL(
"https://gitbox.apache.org/repos/asf?p=struts.git;a=blob_plain;f=apps/showcase/src/main/webapp/images/struts.gif;hb=HEAD");
Assert.assertTrue(areFilesEqual(url.openStream(), page.getWebResponse().getContentAsStream()));
}
}
public void testZip() throws InterruptedException, MalformedURLException {
beginAt("/filedownload/download2.action");
public void testZip() throws Exception {
try (final WebClient webClient = new WebClient()) {
final Page page = webClient.getPage(ParameterUtils.getBaseUrl() + "/filedownload/download2.action");
URL url = new URL("http://svn.apache.org/repos/asf/struts/struts2/trunk/apps/showcase/src/main/webapp/images/struts-gif.zip");
assertDownloadedFileEquals(url);
URL url = new URL(
"https://gitbox.apache.org/repos/asf?p=struts.git;a=blob_plain;f=apps/showcase/src/main/webapp/images/struts-gif.zip;hb=HEAD");
Assert.assertTrue(areFilesEqual(url.openStream(), page.getWebResponse().getContentAsStream()));
}
}
private boolean areFilesEqual(InputStream i1, InputStream i2) throws IOException {
// read and compare bytes pair-wise
int b1, b2;
do {
b1 = i1.read();
b2 = i2.read();
} while (b1 == b2 && b1 != -1 && b2 != -1);
i1.close();
i2.close();
// true only if end of file is reached for both
return (b1 == -1) && (b2 == -1);
}
}
@@ -20,25 +20,40 @@
*/
package it.org.apache.struts2.showcase;
import java.net.MalformedURLException;
import java.net.URL;
import org.junit.Assert;
import org.junit.Test;
public class FreeMarkerManagerTest extends ITBaseTest {
public void testCustomManager() {
beginAt("/freemarker/customFreemarkerManagerDemo.action");
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.DomElement;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
String date = getElementTextByXPath("//*[@id='todaysDate']");
assertNotNull(date);
assertTrue(date.length() > 0);
public class FreeMarkerManagerTest {
@Test
public void testCustomManager() throws Exception {
try (final WebClient webClient = new WebClient()) {
final HtmlPage page = webClient
.getPage(ParameterUtils.getBaseUrl() + "/freemarker/customFreemarkerManagerDemo.action");
String time = getElementTextByXPath("//*[@id='timeNow']");
assertNotNull(time);
assertTrue(time.length() > 0);
final DomElement date = page.getElementById("todaysDate");
Assert.assertNotNull(date);
Assert.assertTrue(date.asText().length() > 0);
final DomElement time = page.getElementById("timeNow");
Assert.assertNotNull(time);
Assert.assertTrue(time.asText().length() > 0);
}
}
public void testTags() {
beginAt("/freemarker/standardTags.action");
assertElementPresent("test_name");
assertElementPresent("test_");
@Test
public void testTags() throws Exception {
try (final WebClient webClient = new WebClient()) {
final HtmlPage page = webClient.getPage(ParameterUtils.getBaseUrl() + "/freemarker/standardTags.action");
final DomElement date = page.getElementById("test_name");
Assert.assertNotNull(date);
final DomElement time = page.getElementById("test");
Assert.assertNotNull(time);
}
}
}
@@ -1,30 +0,0 @@
/*
* $Id$
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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 it.org.apache.struts2.showcase;
import net.sourceforge.jwebunit.junit.WebTestCase;
public abstract class ITBaseTest extends WebTestCase {
public void setUp() throws Exception {
getTestContext().setBaseUrl(ParameterUtils.getBaseUrl());
}
}
@@ -16,31 +16,34 @@
* specific language governing permissions and limitations
* under the License.
*/
package it.org.apache.struts2.showcase.staticcontent;
package it.org.apache.struts2.showcase;
import it.org.apache.struts2.showcase.ITBaseTest;
import org.junit.Assert;
import org.junit.Test;
import java.io.IOException;
import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException;
import com.gargoylesoftware.htmlunit.WebClient;
import net.sourceforge.jwebunit.exception.TestingEngineResponseException;
public class StaticContentTest {
public class StaticContentTest extends ITBaseTest {
public void testInvalidRersources1() throws IOException {
try {
beginAt("/struts..");
fail("Previous request should have failed");
} catch (TestingEngineResponseException ex) {
// ok
@Test
public void testInvalidRersources1() throws Exception {
try (final WebClient webClient = new WebClient()) {
try {
webClient.getPage(ParameterUtils.getBaseUrl() + "/struts..");
Assert.fail("Previous request should have failed");
} catch (FailingHttpStatusCodeException e) {
}
}
}
public void testInvalidRersources2() throws IOException {
try {
beginAt("/struts/..%252f");
fail("Previous request should have failed");
} catch (TestingEngineResponseException ex) {
// ok
public void testInvalidRersources2() throws Exception {
try (final WebClient webClient = new WebClient()) {
try {
webClient.getPage(ParameterUtils.getBaseUrl() + "/struts/..%252f");
Assert.fail("Previous request should have failed");
} catch (FailingHttpStatusCodeException e) {
}
}
}
@@ -20,36 +20,56 @@
*/
package it.org.apache.struts2.showcase;
public class UITagExampleTest extends ITBaseTest {
public void testInputForm() {
setScriptingEnabled(false);
beginAt("/tags/ui/example!input.action");
assertFormPresent("exampleSubmit");
// text box
assertFormElementPresent("name");
// textarea
assertFormElementPresent("bio");
// select
assertFormElementPresent("favouriteColor");
// checkbox list
assertFormElementPresent("friends");
// checkbox
assertFormElementPresent("legalAge");
import org.junit.Assert;
import org.junit.Test;
// set fields
setTextField("name", "name");
setTextField("bio", "bio");
selectOption("favouriteColor", "Red");
checkCheckbox("friends", "Patrick");
checkCheckbox("friends", "Jason");
checkCheckbox("legalAge");
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlCheckBoxInput;
import com.gargoylesoftware.htmlunit.html.HtmlForm;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.html.HtmlSelect;
import com.gargoylesoftware.htmlunit.html.HtmlSubmitInput;
import com.gargoylesoftware.htmlunit.html.HtmlTextArea;
import com.gargoylesoftware.htmlunit.html.HtmlTextInput;
submit();
public class UITagExampleTest {
@Test
public void testInputForm() throws Exception {
try (final WebClient webClient = new WebClient()) {
final HtmlPage page = webClient.getPage(ParameterUtils.getBaseUrl() + "/tags/ui/example!input.action");
assertTextInElement("name", "name");
assertTextInElement("bio", "bio");
assertTextInElement("favouriteColor", "Red");
assertTextInElement("friends", "[Patrick, Jason]");
assertTextInElement("legalAge", "true");
final HtmlForm form = page.getFormByName("exampleSubmit");
Assert.assertNotNull(form);
final HtmlTextInput textField = form.getInputByName("name");
final HtmlTextArea textField2 = form.getTextAreaByName("bio");
final HtmlSelect textField3 = form.getSelectByName("favouriteColor");
final HtmlCheckBoxInput textField4 = form.getInputByValue("Patrick");
final HtmlCheckBoxInput textField41 = form.getInputByValue("Jason");
final HtmlCheckBoxInput textField5 = form.getInputByName("legalAge");
Assert.assertNotNull(textField);
Assert.assertNotNull(textField2);
Assert.assertNotNull(textField3);
Assert.assertNotNull(textField4);
Assert.assertNotNull(textField41);
Assert.assertNotNull(textField5);
textField.type("name");
textField2.type("bio");
textField3.setSelectedAttribute("Red", true);
textField4.setChecked(true);
textField41.setChecked(true);
textField5.setChecked(true);
final HtmlSubmitInput button = form.getInputByValue("Submit");
final HtmlPage page2 = button.click();
Assert.assertEquals("name", page2.getElementById("name").asText());
Assert.assertEquals("bio", page2.getElementById("bio").asText());
Assert.assertEquals("Red", page2.getElementById("favouriteColor").asText());
Assert.assertEquals("[Patrick, Jason]", page2.getElementById("friends").asText());
Assert.assertEquals("true", page2.getElementById("legalAge").asText());
}
}
}
@@ -20,27 +20,44 @@
*/
package it.org.apache.struts2.showcase;
public class ValidationTest extends ITBaseTest {
public void testFieldValidators() {
beginAt("/validation/showFieldValidatorsExamples.action");
import org.junit.Assert;
import org.junit.Test;
setTextField("integerValidatorField", "nonint");
setTextField("dateValidatorField", "nondate");
setTextField("emailValidatorField", "!@@#%");
setTextField("urlValidatorField", "!@@#%");
setTextField("stringLengthValidatorField", "a");
setTextField("regexValidatorField", "abc");
setTextField("fieldExpressionValidatorField", "abc");
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlForm;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.html.HtmlSubmitInput;
submit();
public class ValidationTest {
@Test
public void testFieldValidators() throws Exception {
try (final WebClient webClient = new WebClient()) {
final HtmlPage page = webClient
.getPage(ParameterUtils.getBaseUrl() + "/validation/showFieldValidatorsExamples.action");
assertTextPresent("Invalid field value for field \"dateValidatorField\"");
assertTextPresent("Invalid field value for field \"integerValidatorField\"");
assertTextPresent("required and must be string");
assertTextPresent("must be a valid email if supplied");
assertTextPresent("must be a valid url if supplied ");
assertTextPresent("must be a String of a specific greater than 1 less than 5 if specified ");
assertTextPresent("regexValidatorField must match a regexp (.*\\.txt) if specified ");
assertTextPresent("must be the same as the Required Validator Field if specified ");
final HtmlForm form = page.getForms().get(0);
form.getInputByName("integerValidatorField").type("nonint");
form.getInputByName("dateValidatorField").type("nondate");
form.getInputByName("emailValidatorField").type("!@@#%");
form.getInputByName("urlValidatorField").type("!@@#%");
form.getInputByName("stringLengthValidatorField").type("a");
form.getInputByName("regexValidatorField").type("abc");
form.getInputByName("fieldExpressionValidatorField").type("abc");
final HtmlSubmitInput button = form.getInputByValue("Submit");
final HtmlPage page2 = button.click();
final String page2Text = page2.asText();
Assert.assertTrue(page2Text.contains("Invalid field value for field \"dateValidatorField\""));
Assert.assertTrue(page2Text.contains("Invalid field value for field \"integerValidatorField\""));
Assert.assertTrue(page2Text.contains("required and must be string"));
Assert.assertTrue(page2Text.contains("must be a valid email if supplied"));
Assert.assertTrue(page2Text.contains("must be a valid url if supplied"));
Assert.assertTrue(
page2Text.contains("must be a String of a specific greater than 1 less than 5 if specified"));
Assert.assertTrue(page2Text.contains("regexValidatorField must match a regexp (.*\\.txt) if specified"));
Assert.assertTrue(page2Text.contains("must be the same as the Required Validator Field if specified"));
}
}
}