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.test;
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  public class ContainerIntegrationIT extends AbstractContainerIT {
35  
36      protected final WebClient webClient = new WebClient();
37  
38      @Before
39      public void logOut() throws IOException {
40          // Make sure we are logged out
41          final HtmlPage homePage = webClient.getPage(getBaseUri());
42          try {
43              homePage.getAnchorByHref("/logout").click();
44          }
45          catch (ElementNotFoundException e) {
46              //Ignore
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          // This'll throw an expection if not logged in
59          page.getAnchorByHref("/logout");
60      }
61  }