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