KickJava   Java API By Example, From Geeks To Geeks.

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


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 junit.framework.Test;
25 import org.apache.commons.httpclient.HttpClient;
26 import org.jboss.jmx.adaptor.rmi.RMIAdaptor;
27 import org.jboss.test.JBossClusteredTestCase;
28
29 /**
30  * Simple clustering test case of get/set.
31  *
32  * @author Ben Wang
33  * @version $Revision: 1.0
34  */

35 public class SimpleTestCase
36       extends BaseTest
37 {
38
39    public SimpleTestCase(String JavaDoc name)
40    {
41       super(name);
42
43    }
44
45    public static Test suite() throws Exception JavaDoc
46    {
47       Test t1 = JBossClusteredTestCase.getDeploySetup(SimpleTestCase.class,
48             "http-sr.war");
49       return t1;
50    }
51
52    /**
53     * Main method that deals with the Http Session Replication Test
54     *
55     * @throws Exception
56     */

57    public void testHttpSessionReplication()
58          throws Exception JavaDoc
59    {
60       String JavaDoc attr = "";
61       getLog().debug("Enter testHttpSessionReplication");
62
63       String JavaDoc setURLName = "/http-sr/testsessionreplication.jsp";
64       String JavaDoc getURLName = "/http-sr/getattribute.jsp";
65
66       getLog().debug(setURLName + ":::::::" + getURLName);
67
68       // Create an instance of HttpClient.
69
HttpClient client = new HttpClient();
70
71       // Set the session attribute first
72
makeGet(client, baseURL0_ +setURLName);
73
74       // Get the Attribute set by testsessionreplication.jsp
75
attr = makeGetWithState(client, baseURL0_ +getURLName);
76
77       sleepThread(DEFAULT_SLEEP);
78
79       // Let's switch to server 2 to retrieve the session attribute.
80
setCookieDomainToThisServer(client, servers_[1]);
81       String JavaDoc attr2 = makeGet(client, baseURL1_ +getURLName);
82
83       // Check the result
84
assertEquals("Http session replication attribtues retrieved from both servers ", attr, attr2);
85
86       getLog().debug("Http Session Replication has happened");
87       getLog().debug("Exit testHttpSessionReplication");
88    }
89
90    /**
91     * Tests that sessions time out properly and that activity
92     * on one cluster node prevents timeout on another.
93     */

94    public void testSessionTimeout()
95       throws Exception JavaDoc
96    {
97       String JavaDoc attr = "";
98       String JavaDoc attr2 = "";
99       getLog().debug("Enter testSessionTimeout");
100
101       String JavaDoc setURLName = "/http-sr/testsessionreplication.jsp";
102       String JavaDoc getURLName = "/http-sr/getattribute.jsp";
103
104       getLog().debug(setURLName + ":::::::" + getURLName);
105
106       // Create a session on server0
107
HttpClient client = new HttpClient();
108       makeGet(client, baseURL0_ +setURLName);
109
110       // Create a session on server 1.
111
HttpClient client1 = new HttpClient();
112       makeGet(client1, baseURL1_ +setURLName);
113       
114       // Find out the session id and use it to build an FQN
115
String JavaDoc sessionID = getSessionID(client1, servers_[1]);
116       // Strip off the jvmRoute, if there is one
117
sessionID = stripJvmRoute(sessionID);
118       String JavaDoc sessionFqn = "/JSESSION/localhost/http-sr/" + sessionID;
119
120       sleepThread(6000);
121       
122       // Set the server0 session to ensure replication occurs
123
attr = makeGetWithState(client, baseURL0_ +setURLName);
124       // Get the Attribute set by testsessionreplication.jsp
125
attr = makeGetWithState(client, baseURL0_ +getURLName);
126       assertNotNull("Http session get", attr);
127       
128       // Sleep 15 secs. This plus the previous 6 secs is enough to expire
129
// session0 on server1 if replication failed to keep it alive
130
sleepThread(15000);
131       
132       // Switch to the other server and check if 1st session is alive
133
setCookieDomainToThisServer(client, servers_[1]);
134       attr2 = makeGetWithState(client, baseURL1_ +getURLName);
135       
136       // Check the result
137
assertEquals("Http session replication attributes retrieved from both servers ", attr, attr2);
138       
139       getLog().debug("Replication has kept the session alive");
140       
141       // sleep 6 more seconds so session0 will expire on server0
142
sleepThread(6000);
143       
144       // Confirm first session is expired on node 0
145
setCookieDomainToThisServer(client, servers_[0]);
146       attr = makeGetWithState(client, baseURL0_ +getURLName);
147       assertFalse("Original session not present", attr2.equals(attr));
148       
149       // sleep 45 more seconds so session 1 will expire on server0.
150
// need a total of 70 secs -- 60 to expire and 10 to ensure the
151
// bg thread runs. 20 secs to expire is not enough as the reduced
152
// life of this session is not available to the manager w/o
153
// deserializing the session
154
sleepThread(45000);
155       
156       // Confirm 2nd session is gone from the distributed cache on node 0
157
RMIAdaptor[] adaptors = getAdaptors();
158       assertNull("Session gone from distributed cache",
159             SessionTestUtil.getSessionVersion(adaptors[0], sessionFqn));
160       assertNull("Session gone from distributed cache",
161             SessionTestUtil.getBuddySessionVersion(adaptors[0], sessionFqn));
162       
163       getLog().debug("Exit testSessionTimeout");
164    }
165    
166    public void testMaxUnreplicatedInterval()
167    {
168       getLog().debug("Enter testMaxUnreplicatedInterval");
169
170       String JavaDoc setURLName = "/http-sr/testsessionreplication.jsp";
171       String JavaDoc getURLName = "/http-sr/getattribute.jsp";
172       String JavaDoc accessURLName = "/http-sr/access.jsp";
173
174       // Create an instance of HttpClient.
175
HttpClient client = new HttpClient();
176
177       // Set the session attribute first
178
makeGet(client, baseURL0_ +setURLName);
179       // Get the Attribute set by testsessionreplication.jsp
180
String JavaDoc attr = makeGetWithState(client, baseURL0_ +getURLName);
181       
182       // Sleep 90% of the maxInactiveInterval
183
sleepThread(18000);
184       
185       // Access the session without touching any attribute
186
makeGet(client, baseURL0_ +accessURLName);
187       
188       // Sleep some more, long enough for the session to expire
189
// if access didn't cause replication and for the bg thread to run
190
sleepThread(17000);
191       
192       // Switch servers
193
setCookieDomainToThisServer(client, servers_[1]);
194       // Get the Attribute set by testsessionreplication.jsp
195
String JavaDoc attr1 = makeGetWithState(client, baseURL1_ +getURLName);
196       
197       assertEquals("Session still present", attr, attr1);
198    }
199
200 }
201
Popular Tags