KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > security > test > mapping > RoleMappingWebTestCase


1 /*
2  * JBoss, the OpenSource J2EE webOS
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7 package org.jboss.test.security.test.mapping;
8
9 import java.net.HttpURLConnection JavaDoc;
10
11 import junit.framework.Test;
12 import junit.framework.TestSuite;
13
14 import org.apache.commons.httpclient.Cookie;
15 import org.apache.commons.httpclient.Header;
16 import org.apache.commons.httpclient.HttpClient;
17 import org.apache.commons.httpclient.HttpState;
18 import org.apache.commons.httpclient.methods.GetMethod;
19 import org.apache.commons.httpclient.methods.PostMethod;
20 import org.jboss.test.JBossTestCase;
21 import org.jboss.test.JBossTestSetup;
22
23 /**
24  * Test role mapping logic for the web layer
25  * @author <a HREF="mailto:Anil.Saldhana@jboss.org">Anil Saldhana</a>
26  * @version $Revision$
27  * @since Aug 27, 2006
28  */

29 public class RoleMappingWebTestCase extends JBossTestCase
30 {
31    private String JavaDoc baseURLNoAuth = "http://" + getServerHost()
32             + ":" + Integer.getInteger("web.port", 8080) + "/";
33    private HttpClient httpConn = new HttpClient();
34
35    public RoleMappingWebTestCase(String JavaDoc name)
36    {
37       super(name);
38    }
39    
40    /**
41     * Test a FORM auth simple webapp. A role of "testRole" will
42     * be mapped to Authorized User via the role mapping logic
43     */

44    public void testWebAccess() throws Exception JavaDoc
45    {
46       GetMethod indexGet = new GetMethod(baseURLNoAuth+"web-role-map/Secured.jsp");
47       int responseCode = httpConn.executeMethod(indexGet);
48       String JavaDoc body = indexGet.getResponseBodyAsString();
49       assertTrue("Get OK("+responseCode+")", responseCode == HttpURLConnection.HTTP_OK);
50       assertTrue("Redirected to login page", body.indexOf("j_security_check") > 0 );
51
52       HttpState state = httpConn.getState();
53       Cookie[] cookies = state.getCookies();
54       String JavaDoc sessionID = null;
55       for(int c = 0; c < cookies.length; c ++)
56       {
57          Cookie k = cookies[c];
58          if( k.getName().equalsIgnoreCase("JSESSIONID") )
59             sessionID = k.getValue();
60       }
61       getLog().debug("Saw JSESSIONID="+sessionID);
62
63       // Submit the login form
64
PostMethod formPost = new PostMethod(baseURLNoAuth+"web-role-map/j_security_check");
65       formPost.addRequestHeader("Referer", baseURLNoAuth+"web-role-map/login.html");
66       formPost.addParameter("j_username", "user");
67       formPost.addParameter("j_password", "pass");
68       responseCode = httpConn.executeMethod(formPost.getHostConfiguration(),
69          formPost, state);
70       String JavaDoc response = formPost.getStatusText();
71       log.debug("responseCode="+responseCode+", response="+response);
72       assertTrue("Saw HTTP_MOVED_TEMP", responseCode == HttpURLConnection.HTTP_MOVED_TEMP);
73
74       // Follow the redirect to the SecureServlet
75
Header location = formPost.getResponseHeader("Location");
76       String JavaDoc indexURI = location.getValue();
77       GetMethod war1Index = new GetMethod(indexURI);
78       responseCode = httpConn.executeMethod(war1Index.getHostConfiguration(),
79          war1Index, state);
80       response = war1Index.getStatusText();
81       log.debug("responseCode="+responseCode+", response="+response);
82       assertTrue("Get OK", responseCode == HttpURLConnection.HTTP_OK);
83       body = war1Index.getResponseBodyAsString();
84       if( body.indexOf("j_security_check") > 0 )
85          fail("get of "+indexURI+" redirected to login page");
86    }
87     
88    public static Test suite() throws Exception JavaDoc
89    {
90       TestSuite suite = new TestSuite();
91       suite.addTest(new TestSuite(RoleMappingWebTestCase.class));
92
93       // Create an initializer for the test suite
94
Test wrapper = new JBossTestSetup(suite)
95       {
96          protected void setUp() throws Exception JavaDoc
97          {
98             super.setUp();
99             deploy(getResourceURL("security-spi/rolemapping/rolemapping-test-service.xml"));
100             deploy("web-role-map.war");
101          }
102          protected void tearDown() throws Exception JavaDoc
103          {
104             undeploy(getResourceURL("security-spi/rolemapping/rolemapping-test-service.xml"));
105             undeploy("web-role-map.war");
106             super.tearDown();
107          }
108       };
109       return wrapper;
110    }
111 }
112
Popular Tags