Skip to content

Commit 630cb0f

Browse files
committed
Added new tests
1 parent 64c512f commit 630cb0f

File tree

2 files changed

+173
-5
lines changed

2 files changed

+173
-5
lines changed

jsf/bean-validation/src/main/webapp/index.xhtml

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,15 @@
88
<h:body>
99
<h1>JSF and Bean Validation</h1>
1010
At least 3 characters in name, 25 >= age >= 18, zip is 5 digit numeral<br/><br/>
11-
<h:form>
12-
Name: <h:inputText value="#{myBean.name}"/>
13-
Age: <h:inputText value="#{myBean.age}"/>
14-
Zip: <h:inputText value="#{myBean.zip}"/>
15-
<h:commandButton value="Submit"/>
11+
<h:form prependId="false">
12+
Name: <h:inputText id="nameInputText" value="#{myBean.name}"/>
13+
Age: <h:inputText id="ageInputText" value="#{myBean.age}"/>
14+
Zip: <h:inputText id="zipInputText" value="#{myBean.zip}"/>
15+
<h:commandButton id="submitButton" value="Submit"/>
16+
17+
<h:message for="nameInputText" id="nameMessage"/>
18+
<h:message for="ageInputText" id="ageMessage"/>
19+
<h:message for="zipInputText" id="zipMessage"/>
1620
</h:form>
1721
</h:body>
1822
</html>
Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
package org.javaee7.jsf.bean.validation;
2+
3+
import com.gargoylesoftware.htmlunit.Page;
4+
import com.gargoylesoftware.htmlunit.WebClient;
5+
import com.gargoylesoftware.htmlunit.html.DomElement;
6+
import com.gargoylesoftware.htmlunit.html.HtmlButtonInput;
7+
import com.gargoylesoftware.htmlunit.html.HtmlElement;
8+
import com.gargoylesoftware.htmlunit.html.HtmlPage;
9+
import com.gargoylesoftware.htmlunit.html.HtmlSpan;
10+
import com.gargoylesoftware.htmlunit.html.HtmlSubmitInput;
11+
import com.gargoylesoftware.htmlunit.html.HtmlTextInput;
12+
import java.io.File;
13+
import java.io.IOException;
14+
import java.net.URL;
15+
import org.jboss.arquillian.container.test.api.Deployment;
16+
import org.jboss.arquillian.junit.Arquillian;
17+
import org.jboss.arquillian.test.api.ArquillianResource;
18+
import org.jboss.shrinkwrap.api.ShrinkWrap;
19+
import org.jboss.shrinkwrap.api.spec.WebArchive;
20+
import org.junit.After;
21+
import org.junit.AfterClass;
22+
import org.junit.Before;
23+
import org.junit.BeforeClass;
24+
import org.junit.Test;
25+
import static org.junit.Assert.*;
26+
import org.junit.runner.RunWith;
27+
28+
/**
29+
* @author Arun Gupta
30+
*/
31+
@RunWith(Arquillian.class)
32+
public class MyBeanTest {
33+
34+
@ArquillianResource
35+
private URL base;
36+
37+
WebClient webClient;
38+
39+
private static final String WEBAPP_SRC = "src/main/webapp";
40+
HtmlPage page;
41+
HtmlTextInput nameInputText;
42+
HtmlTextInput ageInputText;
43+
HtmlTextInput zipInputText;
44+
HtmlSubmitInput button;
45+
46+
@Deployment(testable = false)
47+
public static WebArchive createDeployment() {
48+
return ShrinkWrap.create(WebArchive.class).
49+
addClass(MyBean.class)
50+
.addAsWebResource(new File(WEBAPP_SRC, "index.xhtml"))
51+
.addAsWebInfResource(new File(WEBAPP_SRC + "/WEB-INF", "web.xml"));
52+
}
53+
54+
@Before
55+
public void setup() throws IOException {
56+
webClient = new WebClient();
57+
page = webClient.getPage(base + "/faces/index.xhtml");
58+
nameInputText = (HtmlTextInput)page.getElementById("nameInputText");
59+
ageInputText = (HtmlTextInput)page.getElementById("ageInputText");
60+
zipInputText = (HtmlTextInput)page.getElementById("zipInputText");
61+
button = (HtmlSubmitInput)page.getElementById("submitButton");
62+
}
63+
64+
@Test
65+
public void testNameLessCharacters() throws IOException {
66+
nameInputText.setText("ab");
67+
ageInputText.setText("20");
68+
zipInputText.setText("12345");
69+
HtmlPage result = button.click();
70+
HtmlSpan span = (HtmlSpan)result.getElementById("nameMessage");
71+
assertEquals("At least 3 characters", span.asText());
72+
}
73+
74+
@Test
75+
public void testNameBoundary() throws IOException {
76+
nameInputText.setText("abc");
77+
ageInputText.setText("20");
78+
zipInputText.setText("12345");
79+
HtmlPage result = button.click();
80+
HtmlSpan span = (HtmlSpan)result.getElementById("nameMessage");
81+
assertEquals("", span.asText());
82+
}
83+
84+
@Test
85+
public void testAgeLessThan() throws IOException {
86+
nameInputText.setText("abc");
87+
ageInputText.setText("16");
88+
zipInputText.setText("12345");
89+
HtmlPage result = button.click();
90+
HtmlSpan span = (HtmlSpan)result.getElementById("ageMessage");
91+
assertEquals("must be greater than or equal to 18", span.asText());
92+
}
93+
94+
@Test
95+
public void testAgeLowBoundary() throws IOException {
96+
nameInputText.setText("abc");
97+
ageInputText.setText("18");
98+
zipInputText.setText("12345");
99+
HtmlPage result = button.click();
100+
HtmlSpan span = (HtmlSpan)result.getElementById("ageMessage");
101+
assertEquals("", span.asText());
102+
}
103+
104+
@Test
105+
public void testAgeHighBoundary() throws IOException {
106+
nameInputText.setText("abc");
107+
ageInputText.setText("25");
108+
zipInputText.setText("12345");
109+
HtmlPage result = button.click();
110+
HtmlSpan span = (HtmlSpan)result.getElementById("ageMessage");
111+
assertEquals("", span.asText());
112+
}
113+
114+
@Test
115+
public void testAgeGreaterThan() throws IOException {
116+
nameInputText.setText("abc");
117+
ageInputText.setText("26");
118+
zipInputText.setText("12345");
119+
HtmlPage result = button.click();
120+
HtmlSpan span = (HtmlSpan)result.getElementById("ageMessage");
121+
assertEquals("must be less than or equal to 25", span.asText());
122+
}
123+
124+
@Test
125+
public void testZipAlphabets() throws IOException {
126+
nameInputText.setText("abc");
127+
ageInputText.setText("20");
128+
zipInputText.setText("abcde");
129+
HtmlPage result = button.click();
130+
HtmlSpan span = (HtmlSpan)result.getElementById("zipMessage");
131+
assertEquals("must match \"[0-9]{5}\"", span.asText());
132+
}
133+
134+
@Test
135+
public void testZipLessNumbers() throws IOException {
136+
nameInputText.setText("abc");
137+
ageInputText.setText("20");
138+
zipInputText.setText("1234");
139+
HtmlPage result = button.click();
140+
HtmlSpan span = (HtmlSpan)result.getElementById("zipMessage");
141+
assertEquals("must match \"[0-9]{5}\"", span.asText());
142+
}
143+
144+
@Test
145+
public void testZipMoreNumbers() throws IOException {
146+
nameInputText.setText("abc");
147+
ageInputText.setText("20");
148+
zipInputText.setText("123456");
149+
HtmlPage result = button.click();
150+
HtmlSpan span = (HtmlSpan)result.getElementById("zipMessage");
151+
assertEquals("must match \"[0-9]{5}\"", span.asText());
152+
}
153+
154+
155+
@Test
156+
public void testZipBoundary() throws IOException {
157+
nameInputText.setText("abc");
158+
ageInputText.setText("20");
159+
zipInputText.setText("12345");
160+
HtmlPage result = button.click();
161+
HtmlSpan span = (HtmlSpan)result.getElementById("zipMessage");
162+
assertEquals("", span.asText());
163+
}
164+
}

0 commit comments

Comments
 (0)