001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.test.cluster.test;
023:
024: import java.io.IOException;
025: import java.net.HttpURLConnection;
026:
027: import org.apache.commons.httpclient.Cookie;
028: import org.apache.commons.httpclient.Header;
029: import org.apache.commons.httpclient.HttpClient;
030: import org.apache.commons.httpclient.HttpState;
031: import org.apache.commons.httpclient.methods.GetMethod;
032: import org.jboss.test.JBossClusteredTestCase;
033:
034: /**
035: * Base class to test HttpSessionReplication.
036: *
037: * @author Ben Wang
038: * @version $Revision: 1.0
039: */
040: public abstract class BaseTest extends JBossClusteredTestCase {
041: /**
042: * Standard number of ms to pause between http requests
043: * to give session time to replicate
044: */
045: public static final long DEFAULT_SLEEP = Long.parseLong(System
046: .getProperty("jbosstest.cluster.failover.sleep", "500"));
047:
048: protected String[] servers_ = null;
049: protected String baseURL0_;
050: protected String baseURL1_;
051:
052: public BaseTest(String name) {
053: super (name);
054: }
055:
056: protected void setUp() throws Exception {
057: super .setUp();
058:
059: servers_ = super .getServers();
060: assertEquals("Server size ", servers_.length, 2);
061:
062: String[] httpURLs = super .getHttpURLs();
063: assertEquals("Url size ", httpURLs.length, 2);
064: baseURL0_ = httpURLs[0];
065: baseURL1_ = baseURL0_;
066: if (servers_.length > 1) {
067: baseURL1_ = httpURLs[1];
068: }
069: }
070:
071: /**
072: * Sleep for specified time
073: *
074: * @param msecs
075: */
076: protected void sleepThread(long msecs) {
077: try {
078: Thread.sleep(msecs);
079: } catch (InterruptedException e) {
080: e.printStackTrace();
081: }
082: }
083:
084: /**
085: * Makes a http call to the jsp that retrieves the attribute stored on the
086: * session. When the attribute values mathes with the one retrieved earlier,
087: * we have HttpSessionReplication.
088: * Makes use of commons-httpclient library of Apache
089: *
090: * @param client
091: * @param url
092: * @return session attribute
093: */
094: protected String makeGet(HttpClient client, String url) {
095: getLog().info("makeGet(): trying to get from url " + url);
096:
097: GetMethod method = new GetMethod(url);
098: int responseCode = 0;
099: try {
100: responseCode = client.executeMethod(method);
101: } catch (IOException e) {
102: e.printStackTrace();
103: fail("HttpClient executeMethod fails." + e.toString());
104: }
105: assertTrue("Get OK with url: " + url + " responseCode: "
106: + responseCode,
107: responseCode == HttpURLConnection.HTTP_OK);
108:
109: byte[] responseBody = null;
110: try {
111: // Read the response body.
112: responseBody = method.getResponseBody();
113: } catch (IOException e) {
114: e.printStackTrace();
115: fail("HttpClient getResponseBody fails." + e.toString());
116: }
117:
118: // Release the connection.
119: // method.releaseConnection();
120:
121: // Deal with the response.
122: // Use caution: ensure correct character encoding and is not binary data
123: return new String(responseBody);
124: }
125:
126: /**
127: * Makes a http call to the jsp that retrieves the attribute stored on the
128: * session. When the attribute values mathes with the one retrieved earlier,
129: * we have HttpSessionReplication.
130: * Makes use of commons-httpclient library of Apache
131: *
132: * @param client
133: * @param url
134: * @return session attribute
135: */
136: protected String makeGetFailed(HttpClient client, String url) {
137: getLog().info(
138: "makeGetailed(): Expected to failed. Trying to get from url "
139: + url);
140:
141: GetMethod method = new GetMethod(url);
142: int responseCode = 0;
143: try {
144: responseCode = client.executeMethod(method);
145: } catch (IOException e) {
146: e.printStackTrace();
147: }
148: assertTrue("Should not be OK code with url: " + url
149: + " responseCode: " + responseCode,
150: responseCode != HttpURLConnection.HTTP_OK);
151:
152: byte[] responseBody = null;
153: try {
154: // Read the response body.
155: responseBody = method.getResponseBody();
156: } catch (IOException e) {
157: e.printStackTrace();
158: fail("HttpClient getResponseBody fails." + e.toString());
159: }
160:
161: // Release the connection.
162: // method.releaseConnection();
163:
164: // Deal with the response.
165: // Use caution: ensure correct character encoding and is not binary data
166: return new String(responseBody);
167: }
168:
169: /**
170: * Makes a http call to the jsp that retrieves the attribute stored on the
171: * session. When the attribute values mathes with the one retrieved earlier,
172: * we have HttpSessionReplication.
173: * Makes use of commons-httpclient library of Apache
174: *
175: * @param client
176: * @param url
177: * @return session attribute
178: */
179: protected String makeGetWithState(HttpClient client, String url) {
180: GetMethod method = new GetMethod(url);
181: int responseCode = 0;
182: try {
183: HttpState state = client.getState();
184: responseCode = client.executeMethod(method
185: .getHostConfiguration(), method, state);
186: } catch (IOException e) {
187: e.printStackTrace();
188: fail("HttpClient executeMethod fails." + e.toString());
189: }
190: assertTrue("Get OK with url: " + url + " responseCode: "
191: + responseCode,
192: responseCode == HttpURLConnection.HTTP_OK);
193:
194: byte[] responseBody = null;
195: try {
196: // Read the response body.
197: responseBody = method.getResponseBody();
198: } catch (IOException e) {
199: e.printStackTrace();
200: fail("HttpClient getResponseBody fails." + e.toString());
201: }
202:
203: /* Validate that the attribute was actually seen. An absence of the
204: header is treated as true since there are pages used that done't
205: add it.
206: */
207: Header hdr = method.getResponseHeader("X-SawTestHttpAttribute");
208: Boolean sawAttr = hdr != null ? Boolean.valueOf(hdr.getValue())
209: : Boolean.TRUE;
210: String attr = null;
211: if (sawAttr.booleanValue())
212: attr = new String(responseBody);
213: // Release the connection.
214: // method.releaseConnection();
215:
216: // Deal with the response.
217: // Use caution: ensure correct character encoding and is not binary data
218: return attr;
219: }
220:
221: protected void setCookieDomainToThisServer(HttpClient client,
222: String server) {
223: // Get the session cookie
224: Cookie sessionID = getSessionCookie(client, server);
225: // Reset the domain so that the cookie will be sent to server1
226: sessionID.setDomain(server);
227: client.getState().addCookie(sessionID);
228: }
229:
230: protected String getSessionID(HttpClient client, String server) {
231: Cookie sessionID = getSessionCookie(client, server);
232: return sessionID.getValue();
233: }
234:
235: protected Cookie getSessionCookie(HttpClient client, String server) {
236: // Get the state for the JSESSIONID
237: HttpState state = client.getState();
238: // Get the JSESSIONID so we can reset the host
239: Cookie[] cookies = state.getCookies();
240: Cookie sessionID = null;
241: for (int c = 0; c < cookies.length; c++) {
242: Cookie k = cookies[c];
243: if (k.getName().equalsIgnoreCase("JSESSIONID"))
244: sessionID = k;
245: }
246: if (sessionID == null) {
247: fail("getSessionCookie(): fail to find session id. Server name: "
248: + server);
249: }
250: log.info("Saw JSESSIONID=" + sessionID);
251: return sessionID;
252: }
253:
254: protected String stripJvmRoute(String id) {
255: int index = id.indexOf(".");
256: if (index > 0) {
257: return id.substring(0, index);
258: } else {
259: return id;
260: }
261: }
262: }
|