Skip to content

Commit ea983c3

Browse files
committed
Fixed the test
1 parent 9b87775 commit ea983c3

File tree

3 files changed

+74
-3
lines changed

3 files changed

+74
-3
lines changed

servlet/security-form-based/src/main/webapp/index.jsp

+2-2
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,10 @@
4747
<html>
4848
<head>
4949
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
50-
<title>Servlet Form-Based Security - Success</title>
50+
<title>Form-based Security - Success</title>
5151
</head>
5252
<body>
53-
<h1>Servlet Form-Based Security- Success</h1>
53+
<h1>Form-based Security - Success</h1>
5454

5555
If you reached this page that means form-based security credentials are correctly configured.
5656
</body>

servlet/security-form-based/src/main/webapp/loginform.jsp

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555
<form method="POST" action="j_security_check">
5656
Username: <input type="text" name="j_username"> <p/>
5757
Password: <input type="password" name="j_password" autocomplete="off"> <p/>
58-
<input type="submit" value="Submit">
58+
<input type="submit" value="Submit" name="submitButton">
5959
<input type="reset" value="Reset">
6060
</form>
6161

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package org.javaee7.servlet.security.form.based;
2+
3+
import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException;
4+
import com.gargoylesoftware.htmlunit.WebClient;
5+
import com.gargoylesoftware.htmlunit.html.HtmlForm;
6+
import com.gargoylesoftware.htmlunit.html.HtmlPage;
7+
import com.gargoylesoftware.htmlunit.html.HtmlSubmitInput;
8+
import java.io.File;
9+
import java.io.IOException;
10+
import java.net.URL;
11+
import org.jboss.arquillian.container.test.api.Deployment;
12+
import org.jboss.arquillian.junit.Arquillian;
13+
import org.jboss.arquillian.test.api.ArquillianResource;
14+
import org.jboss.shrinkwrap.api.ShrinkWrap;
15+
import org.jboss.shrinkwrap.api.spec.WebArchive;
16+
import org.junit.Test;
17+
import static org.junit.Assert.*;
18+
import org.junit.Before;
19+
import org.junit.runner.RunWith;
20+
21+
/**
22+
* @author Arun Gupta
23+
*/
24+
@RunWith(Arquillian.class)
25+
public class FormTest {
26+
27+
private static final String WEBAPP_SRC = "src/main/webapp";
28+
29+
@ArquillianResource
30+
private URL base;
31+
32+
HtmlForm loginForm;
33+
34+
@Deployment(testable = false)
35+
public static WebArchive createDeployment() {
36+
return ShrinkWrap.create(WebArchive.class)
37+
.addAsWebResource(new File(WEBAPP_SRC, "index.jsp"))
38+
.addAsWebResource(new File(WEBAPP_SRC, "loginerror.jsp"))
39+
.addAsWebResource(new File(WEBAPP_SRC, "loginform.jsp"))
40+
.addAsWebInfResource(new File(WEBAPP_SRC + "/WEB-INF", "web.xml"))
41+
.addAsWebInfResource(new File(WEBAPP_SRC + "/WEB-INF", "glassfish-web.xml"));
42+
}
43+
44+
@Before
45+
public void setup() throws IOException {
46+
WebClient webClient = new WebClient();
47+
HtmlPage page = webClient.getPage(base + "/index.jsp");
48+
loginForm = page.getForms().get(0);
49+
50+
}
51+
52+
@Test
53+
public void testGetWithCorrectCredentials() throws Exception {
54+
loginForm.getInputByName("j_username").setValueAttribute("u1");
55+
loginForm.getInputByName("j_password").setValueAttribute("p1");
56+
HtmlSubmitInput submitButton = loginForm.getInputByName("submitButton");
57+
HtmlPage page2 = submitButton.click();
58+
59+
assertEquals("Form-based Security - Success", page2.getTitleText());
60+
}
61+
62+
@Test
63+
public void testGetWithIncorrectCredentials() throws Exception {
64+
loginForm.getInputByName("j_username").setValueAttribute("random");
65+
loginForm.getInputByName("j_password").setValueAttribute("random");
66+
HtmlSubmitInput submitButton = loginForm.getInputByName("submitButton");
67+
HtmlPage page2 = submitButton.click();
68+
69+
assertEquals("Form-Based Login Error Page", page2.getTitleText());
70+
}
71+
}

0 commit comments

Comments
 (0)