View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.shiro.samples;
20  
21  import com.gargoylesoftware.htmlunit.ElementNotFoundException;
22  import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException;
23  import com.gargoylesoftware.htmlunit.WebClient;
24  import com.gargoylesoftware.htmlunit.html.HtmlForm;
25  import com.gargoylesoftware.htmlunit.html.HtmlInput;
26  import com.gargoylesoftware.htmlunit.html.HtmlPage;
27  import org.apache.shiro.testing.web.AbstractContainerIT;
28  import org.junit.Before;
29  import org.junit.Test;
30  
31  import java.io.IOException;
32  import java.net.MalformedURLException;
33  
34  
35  public class ContainerIntegrationIT extends AbstractContainerIT {
36  
37      protected final WebClient webClient = new WebClient();
38  
39      @Before
40      public void logOut() throws IOException {
41          // Make sure we are logged out
42          final HtmlPage homePage = webClient.getPage(getBaseUri());
43          try {
44              homePage.getAnchorByHref("/s/logout").click();
45          }
46          catch (ElementNotFoundException e) {
47              //Ignore
48          }
49      }
50  
51      @Test
52      public void logIn() throws FailingHttpStatusCodeException, IOException, InterruptedException {
53  
54          HtmlPage page = webClient.getPage(getBaseUri() + "s/login");
55          HtmlForm form = page.getFormByName("loginForm");
56          form.<HtmlInput>getInputByName("username").setValueAttribute("admin");
57          form.<HtmlInput>getInputByName("password").setValueAttribute("admin");
58          page = form.<HtmlInput>getInputByValue("Login").click();
59          // This'll throw an expection if not logged in
60          page.getAnchorByHref("/s/logout");
61      }
62  }