Skip to content

Commit 9108267

Browse files
jboadasmaibin
authored andcommitted
Converters, Listeners and Validators (eugenp#1634)
* Converters, Listeners and Validators in Java EE 7 * Convertesrs, Listeners and validators Tests
1 parent e3a45f5 commit 9108267

5 files changed

Lines changed: 186 additions & 6 deletions

File tree

‎jee7/pom.xml‎

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,16 @@
9898
<artifactId>shrinkwrap-resolver-impl-maven-archive</artifactId>
9999
<scope>test</scope>
100100
</dependency>
101-
101+
<dependency>
102+
<groupId>org.apache.httpcomponents</groupId>
103+
<artifactId>httpclient</artifactId>
104+
<version>4.5</version>
105+
</dependency>
106+
<dependency>
107+
<groupId>commons-io</groupId>
108+
<artifactId>commons-io</artifactId>
109+
<version>2.4</version>
110+
</dependency>
102111
<dependency>
103112
<groupId>com.sun.faces</groupId>
104113
<artifactId>jsf-api</artifactId>
@@ -378,5 +387,21 @@
378387
</dependency>
379388
</dependencies>
380389
</profile>
390+
<profile>
391+
<id>webdriver-chrome</id>
392+
<activation>
393+
<activeByDefault>true</activeByDefault>
394+
</activation>
395+
<properties>
396+
<browser>chrome</browser>
397+
</properties>
398+
</profile>
399+
400+
<profile>
401+
<id>webdriver-firefox</id>
402+
<properties>
403+
<browser>firefox</browser>
404+
</properties>
405+
</profile>
381406
</profiles>
382407
</project>

‎jee7/src/main/webapp/ConvListVal.xhtml‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
</h:head>
99
<h:body>
1010
<h1>Testing converters</h1>
11-
<h:form id="myform">
11+
<h:form id="myForm">
1212
<h:outputLabel value="Age:"/>
1313
<h:inputText id="age" value="#{convListVal.age}">
1414
<f:converter converterId="javax.faces.Integer" />
@@ -19,13 +19,13 @@
1919
<h:inputText id="average" value="#{convListVal.average}">
2020
<f:converter converterId="javax.faces.Double" />
2121
</h:inputText>
22-
<h:message for="average" />
22+
<h:message id = "averageError" for="average" />
2323
<br/>
2424
<h:outputLabel value="Date:"/>
25-
<h:inputText id="MyDate" value="#{convListVal.myDate}">
25+
<h:inputText id="myDate" value="#{convListVal.myDate}">
2626
<f:convertDateTime pattern="dd/MM/yyyy" />
2727
</h:inputText>
28-
<h:message for="MyDate" />
28+
<h:message id="myDateError" for="myDate" />
2929
<br/>
3030
<h:outputText value="#{convListVal.myDate}">
3131
<f:convertDateTime dateStyle="full" locale="en"/>
@@ -41,7 +41,7 @@
4141
<h:inputText id="surname" value="#{convListVal.surname}">
4242
<f:validateLength minimum="5" maximum="10"/>
4343
</h:inputText>
44-
<h:message for="surname" errorStyle="color:red" />
44+
<h:message id="surnameError" for="surname" errorStyle="color:red" />
4545
</h:panelGroup>
4646
<br/>
4747
<h:commandButton id="send" value="submit" type="submit" />
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<faces-config
3+
xmlns="http://java.sun.com/xml/ns/javaee"
4+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5+
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
6+
http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd"
7+
version="2.0">
8+
<managed-bean>
9+
<managed-bean-name>convListVal</managed-bean-name>
10+
<managed-bean-class>com.baeldung.convListVal.ConvListVal</managed-bean-class>
11+
<managed-bean-scope>session</managed-bean-scope>
12+
</managed-bean>
13+
</faces-config>
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<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">
3+
<!-- The bare minimum needed for JSF 2.2 is a servlet 2.5 or later
4+
declaration (this uses 3.0) and the mapping for the FacesServlet.
5+
Setting PROJECT_STAGE to Development is highly recommended
6+
during initial development so that you get more helpful
7+
error messages. Whether you want server-side state saving
8+
(default) or client-side is a more complicated question:
9+
client-side uses more bandwidth but fewer server resources.
10+
Client-side also helps to avoid the dreaded view expired exceptions.
11+
12+
From JSF 2 and PrimeFaces tutorial
13+
at http://www.coreservlets.com/JSF-Tutorial/jsf2/
14+
-->
15+
<servlet>
16+
<servlet-name>Faces Servlet</servlet-name>
17+
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
18+
</servlet>
19+
<servlet-mapping>
20+
<servlet-name>Faces Servlet</servlet-name>
21+
<url-pattern>*.jsf</url-pattern>
22+
</servlet-mapping>
23+
<context-param>
24+
<param-name>javax.faces.PROJECT_STAGE</param-name>
25+
<param-value>Development</param-value>
26+
</context-param>
27+
<context-param>
28+
<description>State saving method: 'client' or 'server' (default). See JSF Specification section 2.5.2</description>
29+
<param-name>javax.faces.STATE_SAVING_METHOD</param-name>
30+
<param-value>client</param-value>
31+
</context-param>
32+
<!-- If you go to http://host/project/ (with no file name), it will
33+
try index.jsf first, welcome.jsf next, and so forth.
34+
-->
35+
<welcome-file-list>
36+
<welcome-file>index.jsf</welcome-file>
37+
<welcome-file>welcome.jsf</welcome-file>
38+
<welcome-file>index.html</welcome-file>
39+
<welcome-file>index.jsp</welcome-file>
40+
</welcome-file-list>
41+
</web-app>
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
package com.baeldung.convListVal;
2+
3+
import static org.jboss.arquillian.graphene.Graphene.guardHttp;
4+
5+
import java.io.File;
6+
import java.net.URL;
7+
8+
import org.jboss.arquillian.container.test.api.Deployment;
9+
import org.jboss.arquillian.container.test.api.RunAsClient;
10+
import org.jboss.arquillian.drone.api.annotation.Drone;
11+
import org.jboss.arquillian.junit.Arquillian;
12+
import org.jboss.arquillian.test.api.ArquillianResource;
13+
import org.jboss.shrinkwrap.api.ShrinkWrap;
14+
import org.jboss.shrinkwrap.api.asset.EmptyAsset;
15+
import org.jboss.shrinkwrap.api.spec.WebArchive;
16+
import org.junit.Assert;
17+
import org.junit.Test;
18+
import org.junit.runner.RunWith;
19+
import org.openqa.selenium.By;
20+
import org.openqa.selenium.WebDriver;
21+
import org.openqa.selenium.WebElement;
22+
import org.openqa.selenium.support.FindBy;
23+
24+
@RunWith(Arquillian.class)
25+
public class ConvListValTest {
26+
27+
@ArquillianResource
28+
private URL deploymentUrl;
29+
30+
private static final String WEBAPP_SRC = "src/main/webapp";
31+
32+
@Deployment(testable = false)
33+
public static WebArchive createDeployment() {
34+
return ( ShrinkWrap.create(
35+
WebArchive.class, "jee7.war").
36+
addClasses(ConvListVal.class, MyListener.class)).
37+
addAsWebResource(new File(WEBAPP_SRC, "ConvListVal.xhtml")).
38+
addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml");
39+
}
40+
41+
@Drone
42+
WebDriver browser;
43+
44+
@ArquillianResource
45+
URL contextPath;
46+
47+
@FindBy(id="myForm:age")
48+
private WebElement ageInput;
49+
50+
@FindBy(id="myForm:average")
51+
private WebElement averageInput;
52+
53+
@FindBy(id="myForm:send")
54+
private WebElement sendButton;
55+
56+
@Test
57+
@RunAsClient
58+
public void givenAge_whenAgeInvalid_thenErrorMessage() throws Exception {
59+
browser.get(deploymentUrl.toExternalForm() + "ConvListVal.jsf");
60+
ageInput.sendKeys("stringage");
61+
guardHttp(sendButton).click();
62+
Assert.assertTrue("Show Age error message", browser.findElements(By.id("myForm:ageError")).size() > 0);
63+
}
64+
65+
@Test
66+
@RunAsClient
67+
public void givenAverage_whenAverageInvalid_thenErrorMessage() throws Exception {
68+
browser.get(deploymentUrl.toExternalForm() + "ConvListVal.jsf");
69+
averageInput.sendKeys("stringaverage");
70+
guardHttp(sendButton).click();
71+
Assert.assertTrue("Show Average error message", browser.findElements(By.id("myForm:averageError")).size() > 0);
72+
}
73+
74+
@Test
75+
@RunAsClient
76+
public void givenDate_whenDateInvalid_thenErrorMessage() throws Exception {
77+
browser.get(deploymentUrl.toExternalForm() + "ConvListVal.jsf");
78+
averageInput.sendKeys("123");
79+
guardHttp(sendButton).click();
80+
Assert.assertTrue("Show Date error message", browser.findElements(By.id("myForm:myDateError")).size() > 0);
81+
}
82+
83+
@Test
84+
@RunAsClient
85+
public void givenSurname_whenSurnameMinLenghtInvalid_thenErrorMessage() throws Exception {
86+
browser.get(deploymentUrl.toExternalForm() + "ConvListVal.jsf");
87+
averageInput.sendKeys("aaa");
88+
guardHttp(sendButton).click();
89+
Assert.assertTrue("Show Surname error message", browser.findElements(By.id("myForm:surnameError")).size() > 0);
90+
}
91+
92+
@Test
93+
@RunAsClient
94+
public void givenSurname_whenSurnameMaxLenghtInvalid_thenErrorMessage() throws Exception {
95+
browser.get(deploymentUrl.toExternalForm() + "ConvListVal.jsf");
96+
averageInput.sendKeys("aaaaabbbbbc");
97+
guardHttp(sendButton).click();
98+
Assert.assertTrue("Show Surname error message", browser.findElements(By.id("myForm:surnameError")).size() > 0);
99+
}
100+
101+
}

0 commit comments

Comments
 (0)