1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.shiro.samples.guice;
20
21 import org.apache.shiro.testing.web.AbstractContainerIT;
22
23 import com.gargoylesoftware.htmlunit.ElementNotFoundException;
24 import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException;
25 import com.gargoylesoftware.htmlunit.WebAssert;
26 import com.gargoylesoftware.htmlunit.html.HtmlCheckBoxInput;
27 import com.gargoylesoftware.htmlunit.html.HtmlForm;
28 import com.gargoylesoftware.htmlunit.html.HtmlInput;
29 import com.gargoylesoftware.htmlunit.html.HtmlPage;
30 import org.junit.Before;
31 import org.junit.Test;
32
33 import java.io.IOException;
34 import java.net.MalformedURLException;
35
36 public class ContainerIntegrationIT extends AbstractContainerIT {
37
38 @Before
39 public void logOut() throws IOException {
40
41 final HtmlPage homePage = webClient.getPage(getBaseUri());
42 try {
43 homePage.getAnchorByHref("/logout").click();
44 }
45 catch (ElementNotFoundException e) {
46
47 }
48 }
49
50 @Test
51 public void logIn() throws FailingHttpStatusCodeException, MalformedURLException, IOException, InterruptedException {
52
53 HtmlPage page = webClient.getPage(getBaseUri() + "login.jsp");
54 HtmlForm form = page.getFormByName("loginform");
55 form.<HtmlInput>getInputByName("username").setValueAttribute("root");
56 form.<HtmlInput>getInputByName("password").setValueAttribute("secret");
57 page = form.<HtmlInput>getInputByName("submit").click();
58
59 page.getAnchorByHref("/logout");
60 }
61
62 @Test
63 public void logInAndRememberMe() throws Exception {
64 HtmlPage page = webClient.getPage(getBaseUri() + "login.jsp");
65 HtmlForm form = page.getFormByName("loginform");
66 form.<HtmlInput>getInputByName("username").setValueAttribute("root");
67 form.<HtmlInput>getInputByName("password").setValueAttribute("secret");
68 HtmlCheckBoxInput checkbox = form.getInputByName("rememberMe");
69 checkbox.setChecked(true);
70 page = form.<HtmlInput>getInputByName("submit").click();
71 jetty.stop();
72 jetty.start();
73 page = webClient.getPage(getBaseUri());
74
75 WebAssert.assertLinkPresentWithText(page, "Log out");
76 page = page.getAnchorByHref("/account").click();
77
78 WebAssert.assertFormPresent(page, "loginform");
79 }
80
81 }