KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > cluster > test > WebSessionTestCase


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.test.cluster.test;
23
24 import java.net.HttpURLConnection JavaDoc;
25
26 import junit.framework.Test;
27
28 import org.apache.commons.httpclient.Cookie;
29 import org.apache.commons.httpclient.Header;
30 import org.apache.commons.httpclient.HttpClient;
31 import org.apache.commons.httpclient.HttpState;
32 import org.apache.commons.httpclient.methods.GetMethod;
33 import org.jboss.test.JBossClusteredTestCase;
34
35 /** Tests of http session replication
36  *
37  * @author Scott.Stark@jboss.org
38  * @version $Revision: 43886 $
39  */

40 public class WebSessionTestCase extends JBossClusteredTestCase
41 {
42    /**
43     * Standard number of ms to pause between http requests
44     * to give session time to replicate
45     */

46    public static final long DEFAULT_SLEEP = BaseTest.DEFAULT_SLEEP;
47    
48    public WebSessionTestCase(String JavaDoc name)
49    {
50       super(name);
51    }
52
53    public static Test suite() throws Exception JavaDoc
54    {
55       Test t1 = getDeploySetup(WebSessionTestCase.class, "dist-ss.war");
56       return t1;
57    }
58
59    /** This makes 2 requests to the jbosstest.cluster.node0 /dist-ss/StatefulSessionServlet
60     * followed by 2 requests to the jbosstest.cluster.node1 /dist-ss/StatefulSessionServlet
61     * using the same session ID to validate that the session is replicated
62     * with the current value and updated correctly. The session AccessCount
63     * value is returned via the X-AccessCount header which should be 4 after
64     * the last request.
65     *
66     * @throws Exception
67     */

68    public void testServletSessionFailover()
69       throws Exception JavaDoc
70    {
71       getLog().debug("+++ testServletSessionFailover");
72
73       String JavaDoc[] servers = super.getServers();
74       String JavaDoc[] httpURLs = super.getHttpURLs();
75       // Access the StatefulSessionServlet of dist-ss.war@server0 twice
76
String JavaDoc baseURL0 = httpURLs[0];
77       HttpClient httpConn = new HttpClient();
78       GetMethod servletGet0 = new GetMethod(baseURL0+"/dist-ss/StatefulSessionServlet/");
79       int responseCode = httpConn.executeMethod(servletGet0);
80       assertTrue("Get OK", responseCode == HttpURLConnection.HTTP_OK);
81       Header accessCount = servletGet0.getResponseHeader("X-AccessCount");
82       int count = Integer.parseInt(accessCount.getValue());
83       assertEquals("X-AccessCount ", 1, count);
84       // Get the state for the JSESSIONID
85
HttpState state = httpConn.getState();
86       servletGet0 = new GetMethod(baseURL0+"/dist-ss/StatefulSessionServlet/");
87       responseCode = httpConn.executeMethod(servletGet0.getHostConfiguration(),
88          servletGet0, state);
89       accessCount = servletGet0.getResponseHeader("X-AccessCount");
90       count = Integer.parseInt(accessCount.getValue());
91       assertEquals("X-AccessCount ", 2, count);
92       // Get the JSESSIONID so we can reset the host
93
Cookie[] cookies = state.getCookies();
94       Cookie sessionID = null;
95       for(int c = 0; c < cookies.length; c ++)
96       {
97          Cookie k = cookies[c];
98          if( k.getName().equalsIgnoreCase("JSESSIONID") )
99             sessionID = k;
100       }
101       log.info("Saw JSESSIONID="+sessionID);
102       // Reset the domain so that the cookie will be sent to server1
103
sessionID.setDomain(servers[1]);
104       state.addCookie(sessionID);
105       _sleep(DEFAULT_SLEEP);
106
107       // Access the StatefulSessionServlet of dist-ss.war@server1 twice
108
String JavaDoc baseURL1 = httpURLs[1];
109       GetMethod servletGet1 = new GetMethod(baseURL1+"/dist-ss/StatefulSessionServlet/");
110       responseCode = httpConn.executeMethod(servletGet1.getHostConfiguration(),
111          servletGet1, state);
112       assertTrue("Get OK", responseCode == HttpURLConnection.HTTP_OK);
113       accessCount = servletGet1.getResponseHeader("X-AccessCount");
114       count = Integer.parseInt(accessCount.getValue());
115       assertEquals("X-AccessCount ", 3, count);
116       servletGet1 = new GetMethod(baseURL1+"/dist-ss/StatefulSessionServlet/");
117       responseCode = httpConn.executeMethod(servletGet1.getHostConfiguration(),
118          servletGet1, state);
119       assertTrue("Get OK", responseCode == HttpURLConnection.HTTP_OK);
120       accessCount = servletGet1.getResponseHeader("X-AccessCount");
121       count = Integer.parseInt(accessCount.getValue());
122       assertEquals("X-AccessCount ", 4, count);
123    }
124
125    /** This makes 4 requests alternating between the jbosstest.cluster.node0
126     * /dist-ss/StatefulSessionServlet and jbosstest.cluster.node1 /dist-ss/StatefulSessionServlet
127     * using the same session ID to validate that the session is replicated
128     * with the current value and updated correctly. The session AccessCount
129     * value is returned via the X-AccessCount header which should be 4 after
130     * the last request.
131     *
132     * Note: this test is not currently working since current http session
133     * replication assumes sticky session. It does not support random load
134     * balancing.
135     * bwang.
136     *
137     * @throws Exception
138     */

139    public void testServletSessionLoadBalancing()
140       throws Exception JavaDoc
141    {
142       getLog().debug("+++ testServletSessionLoadBalancing");
143
144       String JavaDoc[] servers = getServers();
145       String JavaDoc[] httpURLs = super.getHttpURLs();
146       String JavaDoc baseURL0 = httpURLs[0];
147       String JavaDoc baseURL1 = baseURL0;
148       if( servers.length > 1 )
149       {
150         baseURL1 = httpURLs[1];
151       }
152       // Access the StatefulSessionServlet of dist-ss.war@server0 twice
153
HttpClient httpConn = new HttpClient();
154       GetMethod servletGet0 = new GetMethod(baseURL0+"/dist-ss/StatefulSessionServlet/");
155       int responseCode = httpConn.executeMethod(servletGet0);
156       assertTrue("Get OK", responseCode == HttpURLConnection.HTTP_OK);
157       Header accessCount = servletGet0.getResponseHeader("X-AccessCount");
158       int count = Integer.parseInt(accessCount.getValue());
159       assertEquals("X-AccessCount ", 1, count);
160       // Get the state for the JSESSIONID
161
HttpState state = httpConn.getState();
162       // Get the JSESSIONID so we can reset the host
163
Cookie[] cookies = state.getCookies();
164       Cookie sessionID = null;
165       for(int c = 0; c < cookies.length; c ++)
166       {
167          Cookie k = cookies[c];
168          if( k.getName().equalsIgnoreCase("JSESSIONID") )
169             sessionID = k;
170       }
171       log.info("Saw JSESSIONID="+sessionID);
172       // Reset the domain so that the cookie will be sent to server1
173
sessionID.setDomain(servers[1]);
174       state.addCookie(sessionID);
175       _sleep(DEFAULT_SLEEP);
176       // Access the StatefulSessionServlet of dist-ss.war@server1 twice
177
GetMethod servletGet1 = new GetMethod(baseURL1+"/dist-ss/StatefulSessionServlet/");
178       responseCode = httpConn.executeMethod(servletGet1.getHostConfiguration(),
179          servletGet1, state);
180       assertTrue("Get OK", responseCode == HttpURLConnection.HTTP_OK);
181       accessCount = servletGet1.getResponseHeader("X-AccessCount");
182       count = Integer.parseInt(accessCount.getValue());
183       assertEquals("X-AccessCount ", 2, count);
184
185       _sleep(DEFAULT_SLEEP);
186       // Reset the domain so that the cookie will be sent to server0
187
sessionID.setDomain(servers[0]);
188       state.addCookie(sessionID);
189       servletGet0 = new GetMethod(baseURL0+"/dist-ss/StatefulSessionServlet/");
190       responseCode = httpConn.executeMethod(servletGet0.getHostConfiguration(),
191          servletGet0, state);
192       accessCount = servletGet0.getResponseHeader("X-AccessCount");
193       count = Integer.parseInt(accessCount.getValue());
194       assertEquals("X-AccessCount ", 3, count);
195
196       _sleep(DEFAULT_SLEEP);
197       // Reset the domain so that the cookie will be sent to server1
198
sessionID.setDomain(servers[1]);
199       state.addCookie(sessionID);
200       servletGet1 = new GetMethod(baseURL1+"/dist-ss/StatefulSessionServlet/");
201       responseCode = httpConn.executeMethod(servletGet1.getHostConfiguration(),
202          servletGet1, state);
203       assertTrue("Get OK", responseCode == HttpURLConnection.HTTP_OK);
204       accessCount = servletGet1.getResponseHeader("X-AccessCount");
205       count = Integer.parseInt(accessCount.getValue());
206       assertEquals("X-AccessCount ", 4, count);
207    }
208
209
210    /**
211     * Sleep for specified time
212     *
213     * @param msecs
214     */

215    protected void _sleep(long msecs)
216    {
217       try {
218          Thread.sleep(msecs);
219       } catch (InterruptedException JavaDoc e) {
220          e.printStackTrace();
221       }
222    }
223
224 }
225
Popular Tags