KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.IOException JavaDoc;
25 import java.net.HttpURLConnection JavaDoc;
26
27 import org.apache.commons.httpclient.Cookie;
28 import org.apache.commons.httpclient.Header;
29 import org.apache.commons.httpclient.HttpClient;
30 import org.apache.commons.httpclient.HttpState;
31 import org.apache.commons.httpclient.methods.GetMethod;
32 import org.jboss.test.JBossClusteredTestCase;
33
34 /**
35  * Base class to test HttpSessionReplication.
36  *
37  * @author Ben Wang
38  * @version $Revision: 1.0
39  */

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

47    public static final long DEFAULT_SLEEP = 300;
48    
49    protected String JavaDoc[] servers_ = null;
50    protected String JavaDoc baseURL0_;
51    protected String JavaDoc baseURL1_;
52
53    public BaseTest(String JavaDoc name)
54    {
55       super(name);
56    }
57
58    protected void setUp() throws Exception JavaDoc
59    {
60       super.setUp();
61
62       servers_ = super.getServers();
63       assertEquals("Server size " , servers_.length, 2);
64
65       String JavaDoc[] httpURLs = super.getHttpURLs();
66       assertEquals("Url size " , httpURLs.length, 2);
67       baseURL0_ = httpURLs[0];
68       baseURL1_ = baseURL0_;
69       if( servers_.length > 1 )
70       {
71         baseURL1_ = httpURLs[1];
72       }
73    }
74
75    /**
76     * Sleep for specified time
77     *
78     * @param msecs
79     */

80    protected void sleepThread(long msecs)
81    {
82       try {
83          Thread.sleep(msecs);
84       } catch (InterruptedException JavaDoc e) {
85          e.printStackTrace();
86       }
87    }
88
89
90    /**
91     * Makes a http call to the jsp that retrieves the attribute stored on the
92     * session. When the attribute values mathes with the one retrieved earlier,
93     * we have HttpSessionReplication.
94     * Makes use of commons-httpclient library of Apache
95     *
96     * @param client
97     * @param url
98     * @return session attribute
99     */

100    protected String JavaDoc makeGet(HttpClient client, String JavaDoc url)
101    {
102       getLog().debug("makeGet(): trying to get from url " +url);
103
104       GetMethod method = new GetMethod(url);
105       int responseCode = 0;
106       try
107       {
108          responseCode = client.executeMethod(method);
109       } catch (IOException JavaDoc e)
110       {
111          e.printStackTrace();
112          fail("HttpClient executeMethod fails." +e.toString());
113       }
114       assertTrue("Get OK with url: " +url + " responseCode: " +responseCode
115         , responseCode == HttpURLConnection.HTTP_OK);
116
117       // Read the response body.
118
byte[] responseBody = method.getResponseBody();
119
120       // Release the connection.
121
// method.releaseConnection();
122

123       // Deal with the response.
124
// Use caution: ensure correct character encoding and is not binary data
125
return new String JavaDoc(responseBody);
126    }
127
128    /**
129     * Makes a http call to the jsp that retrieves the attribute stored on the
130     * session. When the attribute values mathes with the one retrieved earlier,
131     * we have HttpSessionReplication.
132     * Makes use of commons-httpclient library of Apache
133     *
134     * @param client
135     * @param url
136     * @return session attribute
137     */

138    protected String JavaDoc makeGetFailed(HttpClient client, String JavaDoc url)
139    {
140       getLog().debug("makeGetFailed(): trying to get from url " +url);
141
142       GetMethod method = new GetMethod(url);
143       int responseCode = 0;
144       try
145       {
146          responseCode = client.executeMethod(method);
147       } catch (IOException JavaDoc e)
148       {
149          e.printStackTrace();
150       }
151       assertTrue("Should not be OK code with url: " +url + " responseCode: " +responseCode
152         , responseCode != HttpURLConnection.HTTP_OK);
153
154       // Read the response body.
155
byte[] responseBody = method.getResponseBody();
156
157       // Release the connection.
158
// method.releaseConnection();
159

160       // Deal with the response.
161
// Use caution: ensure correct character encoding and is not binary data
162
return new String JavaDoc(responseBody);
163    }
164
165
166    /**
167     * Makes a http call to the jsp that retrieves the attribute stored on the
168     * session. When the attribute values mathes with the one retrieved earlier,
169     * we have HttpSessionReplication.
170     * Makes use of commons-httpclient library of Apache
171     *
172     * @param client
173     * @param url
174     * @return session attribute
175     */

176    protected String JavaDoc makeGetWithState(HttpClient client, String JavaDoc url)
177    {
178       getLog().debug("makeGetWithState(): trying to get from url " +url);
179       GetMethod method = new GetMethod(url);
180       int responseCode = 0;
181       try
182       {
183          HttpState state = client.getState();
184          responseCode = client.executeMethod(method.getHostConfiguration(),
185             method, state);
186       } catch (IOException JavaDoc e)
187       {
188          e.printStackTrace();
189          fail("HttpClient executeMethod fails." +e.toString());
190       }
191       assertTrue("Get OK", responseCode == HttpURLConnection.HTTP_OK);
192
193       // Read the response body.
194
byte[] responseBody = method.getResponseBody();
195       /* Validate that the attribute was actually seen. An absence of the
196          header is treated as true since there are pages used that done't
197          add it.
198       */

199       Header hdr = method.getResponseHeader("X-SawTestHttpAttribute");
200       Boolean JavaDoc sawAttr = hdr != null ? Boolean.valueOf(hdr.getValue()) : Boolean.TRUE;
201       String JavaDoc attr = null;
202       if( sawAttr.booleanValue() )
203          attr = new String JavaDoc(responseBody);
204       // Release the connection.
205
// method.releaseConnection();
206

207       // Deal with the response.
208
// Use caution: ensure correct character encoding and is not binary data
209
return attr;
210    }
211
212    protected void setCookieDomainToThisServer(HttpClient client, String JavaDoc server)
213    {
214       // Get the session cookie
215
Cookie sessionID = getSessionCookie(client, server);
216       // Reset the domain so that the cookie will be sent to server1
217
sessionID.setDomain(server);
218       client.getState().addCookie(sessionID);
219    }
220    
221    protected String JavaDoc getSessionID(HttpClient client, String JavaDoc server)
222    {
223       Cookie sessionID = getSessionCookie(client, server);
224       return sessionID.getValue();
225    }
226    
227    protected Cookie getSessionCookie(HttpClient client, String JavaDoc server)
228    {
229       // Get the state for the JSESSIONID
230
HttpState state = client.getState();
231       // Get the JSESSIONID so we can reset the host
232
Cookie[] cookies = state.getCookies();
233       Cookie sessionID = null;
234       for(int c = 0; c < cookies.length; c ++)
235       {
236          Cookie k = cookies[c];
237          if( k.getName().equalsIgnoreCase("JSESSIONID") )
238             sessionID = k;
239       }
240       if(sessionID == null)
241       {
242          fail("setCookieDomainToThisServer(): fail to find session id. Server name: " +server);
243       }
244       log.info("Saw JSESSIONID="+sessionID);
245       return sessionID;
246    }
247    
248    protected String JavaDoc stripJvmRoute(String JavaDoc id)
249    {
250       int index = id.indexOf(".");
251       if (index > 0)
252       {
253          return id.substring(0, index);
254       }
255       else
256       {
257          return id;
258       }
259    }
260 }
261
Popular Tags