Converters, Listeners and Validators (#1634)

* Converters, Listeners and Validators in Java EE 7

* Convertesrs, Listeners and validators Tests
This commit is contained in:
Jesus Boadas 2017-04-12 09:30:12 -04:00 committed by maibin
parent e3a45f50e6
commit 910826733c
5 changed files with 186 additions and 6 deletions

View File

@ -98,7 +98,16 @@
<artifactId>shrinkwrap-resolver-impl-maven-archive</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
<dependency>
<groupId>com.sun.faces</groupId>
<artifactId>jsf-api</artifactId>
@ -378,5 +387,21 @@
</dependency>
</dependencies>
</profile>
<profile>
<id>webdriver-chrome</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<browser>chrome</browser>
</properties>
</profile>
<profile>
<id>webdriver-firefox</id>
<properties>
<browser>firefox</browser>
</properties>
</profile>
</profiles>
</project>

View File

@ -8,7 +8,7 @@
</h:head>
<h:body>
<h1>Testing converters</h1>
<h:form id="myform">
<h:form id="myForm">
<h:outputLabel value="Age:"/>
<h:inputText id="age" value="#{convListVal.age}">
<f:converter converterId="javax.faces.Integer" />
@ -19,13 +19,13 @@
<h:inputText id="average" value="#{convListVal.average}">
<f:converter converterId="javax.faces.Double" />
</h:inputText>
<h:message for="average" />
<h:message id = "averageError" for="average" />
<br/>
<h:outputLabel value="Date:"/>
<h:inputText id="MyDate" value="#{convListVal.myDate}">
<h:inputText id="myDate" value="#{convListVal.myDate}">
<f:convertDateTime pattern="dd/MM/yyyy" />
</h:inputText>
<h:message for="MyDate" />
<h:message id="myDateError" for="myDate" />
<br/>
<h:outputText value="#{convListVal.myDate}">
<f:convertDateTime dateStyle="full" locale="en"/>
@ -41,7 +41,7 @@
<h:inputText id="surname" value="#{convListVal.surname}">
<f:validateLength minimum="5" maximum="10"/>
</h:inputText>
<h:message for="surname" errorStyle="color:red" />
<h:message id="surnameError" for="surname" errorStyle="color:red" />
</h:panelGroup>
<br/>
<h:commandButton id="send" value="submit" type="submit" />

View File

@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<faces-config
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd"
version="2.0">
<managed-bean>
<managed-bean-name>convListVal</managed-bean-name>
<managed-bean-class>com.baeldung.convListVal.ConvListVal</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
</managed-bean>
</faces-config>

View File

@ -0,0 +1,41 @@
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
<!-- The bare minimum needed for JSF 2.2 is a servlet 2.5 or later
declaration (this uses 3.0) and the mapping for the FacesServlet.
Setting PROJECT_STAGE to Development is highly recommended
during initial development so that you get more helpful
error messages. Whether you want server-side state saving
(default) or client-side is a more complicated question:
client-side uses more bandwidth but fewer server resources.
Client-side also helps to avoid the dreaded view expired exceptions.
From JSF 2 and PrimeFaces tutorial
at http://www.coreservlets.com/JSF-Tutorial/jsf2/
-->
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.jsf</url-pattern>
</servlet-mapping>
<context-param>
<param-name>javax.faces.PROJECT_STAGE</param-name>
<param-value>Development</param-value>
</context-param>
<context-param>
<description>State saving method: 'client' or 'server' (default). See JSF Specification section 2.5.2</description>
<param-name>javax.faces.STATE_SAVING_METHOD</param-name>
<param-value>client</param-value>
</context-param>
<!-- If you go to http://host/project/ (with no file name), it will
try index.jsf first, welcome.jsf next, and so forth.
-->
<welcome-file-list>
<welcome-file>index.jsf</welcome-file>
<welcome-file>welcome.jsf</welcome-file>
<welcome-file>index.html</welcome-file>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>

View File

@ -0,0 +1,101 @@
package com.baeldung.convListVal;
import static org.jboss.arquillian.graphene.Graphene.guardHttp;
import java.io.File;
import java.net.URL;
import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.container.test.api.RunAsClient;
import org.jboss.arquillian.drone.api.annotation.Drone;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.arquillian.test.api.ArquillianResource;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.asset.EmptyAsset;
import org.jboss.shrinkwrap.api.spec.WebArchive;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
@RunWith(Arquillian.class)
public class ConvListValTest {
@ArquillianResource
private URL deploymentUrl;
private static final String WEBAPP_SRC = "src/main/webapp";
@Deployment(testable = false)
public static WebArchive createDeployment() {
return ( ShrinkWrap.create(
WebArchive.class, "jee7.war").
addClasses(ConvListVal.class, MyListener.class)).
addAsWebResource(new File(WEBAPP_SRC, "ConvListVal.xhtml")).
addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml");
}
@Drone
WebDriver browser;
@ArquillianResource
URL contextPath;
@FindBy(id="myForm:age")
private WebElement ageInput;
@FindBy(id="myForm:average")
private WebElement averageInput;
@FindBy(id="myForm:send")
private WebElement sendButton;
@Test
@RunAsClient
public void givenAge_whenAgeInvalid_thenErrorMessage() throws Exception {
browser.get(deploymentUrl.toExternalForm() + "ConvListVal.jsf");
ageInput.sendKeys("stringage");
guardHttp(sendButton).click();
Assert.assertTrue("Show Age error message", browser.findElements(By.id("myForm:ageError")).size() > 0);
}
@Test
@RunAsClient
public void givenAverage_whenAverageInvalid_thenErrorMessage() throws Exception {
browser.get(deploymentUrl.toExternalForm() + "ConvListVal.jsf");
averageInput.sendKeys("stringaverage");
guardHttp(sendButton).click();
Assert.assertTrue("Show Average error message", browser.findElements(By.id("myForm:averageError")).size() > 0);
}
@Test
@RunAsClient
public void givenDate_whenDateInvalid_thenErrorMessage() throws Exception {
browser.get(deploymentUrl.toExternalForm() + "ConvListVal.jsf");
averageInput.sendKeys("123");
guardHttp(sendButton).click();
Assert.assertTrue("Show Date error message", browser.findElements(By.id("myForm:myDateError")).size() > 0);
}
@Test
@RunAsClient
public void givenSurname_whenSurnameMinLenghtInvalid_thenErrorMessage() throws Exception {
browser.get(deploymentUrl.toExternalForm() + "ConvListVal.jsf");
averageInput.sendKeys("aaa");
guardHttp(sendButton).click();
Assert.assertTrue("Show Surname error message", browser.findElements(By.id("myForm:surnameError")).size() > 0);
}
@Test
@RunAsClient
public void givenSurname_whenSurnameMaxLenghtInvalid_thenErrorMessage() throws Exception {
browser.get(deploymentUrl.toExternalForm() + "ConvListVal.jsf");
averageInput.sendKeys("aaaaabbbbbc");
guardHttp(sendButton).click();
Assert.assertTrue("Show Surname error message", browser.findElements(By.id("myForm:surnameError")).size() > 0);
}
}