|
| 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