1 22 package org.jboss.test.cluster.httpsessionreplication; 23 24 import java.io.BufferedReader ; 25 import java.io.IOException ; 26 import java.io.InputStreamReader ; 27 import java.net.URL ; 28 import java.net.URLConnection ; 29 import java.util.Properties ; 30 31 import junit.framework.Test; 32 33 import org.apache.commons.httpclient.HttpClient; 34 import org.apache.commons.httpclient.HttpMethod; 35 import org.apache.commons.httpclient.HttpRecoverableException; 36 import org.apache.commons.httpclient.methods.GetMethod; 37 import org.jboss.test.JBossClusteredTestCase; 38 39 46 public class HttpSessionReplicationUnitTestCase 47 extends JBossClusteredTestCase { 48 51 private String [] servernames= {"jnp://localhost:1099", "jnp://localhost:1199"}; 52 53 56 private Properties prop = null; 57 58 61 private int numInstances = 0; 62 63 public HttpSessionReplicationUnitTestCase (String name) { 64 super(name); 65 try{ 66 this.getPropertiesFile(); 67 String numin = prop.getProperty("NumOfInstances"); 68 numInstances = Integer.parseInt( numin ); 69 if( numInstances < 2 ) fail( "Atleast two nodes needed"); 70 71 this.setServerNames(servernames); 73 }catch( Exception e){ 74 fail( e.getMessage()); 75 } 76 } 77 78 public static Test suite() throws Exception 79 { 80 Test t1 = JBossClusteredTestCase.getDeploySetup(HttpSessionReplicationUnitTestCase.class, 82 "httpsessionreplication.jar"); 83 return t1; 84 } 85 86 92 public void testApacheConnection() 93 throws Exception 94 { 95 getLog().debug("Enter testApacheConnection"); 96 try { 97 this.makeConnection(prop.getProperty("ApacheUrl")); 99 } catch (Exception e) { 100 } 101 getLog().debug("Exit testApacheConnection"); 102 } 103 104 108 public void testHttpSessionReplication() 109 throws Exception 110 { 111 String attr = ""; 112 getLog().debug("Enter testHttpSessionReplication"); 113 119 String urlname = prop.getProperty("SetAttrUrl"); 120 String geturlname = prop.getProperty("GetAttrUrl"); 121 135 136 HttpClient client = new HttpClient(); 138 139 HttpMethod method = new GetMethod(urlname); 141 String str = makeGet( client, method ); 142 143 method = new GetMethod(geturlname); 145 146 attr= makeGet( client,method ); 148 shutDownInstance( 1 ); 151 getLog().debug( "Brought down the first instance"); 152 sleepThread(30*1000); 155 method = new GetMethod(geturlname); 157 String attr2= makeGet( client, method ); 158 if( ! attr2.equals(attr)) fail("Http Session Replication Failed"); 159 getLog().debug("Http Session Replication has happened"); 160 getLog().debug("Exit testHttpSessionReplication"); 161 } 162 163 166 public void getPropertiesFile(){ 167 prop = new Properties (); 168 try{ 169 java.net.URL url = ClassLoader.getSystemResource("cluster/cluster-test.properties"); 170 prop.load( url.openStream()); 171 }catch( Exception e){ 172 fail("Need a properties file under src/resources/cluster:"+e.getMessage()); 173 } 174 } 175 176 180 private void shutDownInstance(int instancenum) 181 throws Exception 182 { 183 String command = getCommand(instancenum); 184 185 getLog().debug("Going to execute:"+command); 186 Process child = Runtime.getRuntime().exec(command); 187 sleepThread( 10*1000 ); 188 getLog().debug("Process exit value="+child.exitValue()); 189 } 190 191 196 private String getCommand( int instancenum) { 197 String command = ""; 202 try{ 203 command = prop.getProperty("jboss.location") + prop.getProperty("ShutDownScript"); 204 command += " -s " + "jnp://"+prop.getProperty("Instance"+instancenum+".host")+":"+ 205 prop.getProperty("Instance"+instancenum+".port.jndi"); 206 }catch( Exception e){ 207 fail( "getCommand Failed with:"+ e.getMessage()); 208 } 209 210 return command; 211 } 212 213 218 private void sleepThread(long millisecs) 219 throws Exception { 220 Thread.sleep(millisecs); 221 } 222 223 228 private void makeConnection( String urlname ) 229 throws Exception 230 { 231 getLog().debug("Enter makeConnection"); 232 try { 233 URL url = new URL (urlname); 235 URLConnection conn = url.openConnection(); 236 237 for (int i=0; ; i++) { 239 String hname = conn.getHeaderFieldKey(i); 240 String hvalue = conn.getHeaderField(i); 241 242 getLog().debug("hname="+hname+"::"+"value="+hvalue); 243 if (hname == null && hvalue == null) { 244 break; 246 } 247 if (hname == null) { 248 getLog().debug("Response from Apache="+hvalue); 249 if( hvalue.indexOf("200") < 0 && hvalue.indexOf("301") < 0 251 && hvalue.indexOf("302") < 0) 252 fail(urlname+" Down"); 253 break; 254 } 255 } 256 } catch (Exception e) { 257 getLog().debug(e); 258 } 259 } 260 261 265 private void getHttpText( String urlname ){ 266 getLog().debug( getAttribute(urlname)); 267 } 269 275 private String getAttribute( String urlname){ 276 BufferedReader in = null; 277 StringBuffer sb = new StringBuffer (); 278 try{ 279 URL url = new URL (urlname); 280 281 in = new BufferedReader (new InputStreamReader (url.openStream())); 283 String str; 284 while ((str = in.readLine()) != null) { 285 sb.append(str); 287 } 288 getLog().debug(sb.toString()); 289 }catch( Exception e){ 290 getLog().debug( e); 291 }finally{ 292 try{ 293 in.close(); 294 }catch(Exception y){} 295 } 296 return sb.toString(); 297 } 298 299 309 private String makeGet( HttpClient client, HttpMethod method) throws IOException { 310 int statusCode = -1; 312 313 try { 314 statusCode = client.executeMethod(method); 316 } catch (HttpRecoverableException e) { 317 System.err.println( 318 "A recoverable exception occurred, retrying." + 319 e.getMessage()); 320 } catch (IOException e) { 321 System.err.println("Failed to download file."); 322 e.printStackTrace(); 323 System.exit(-1); 324 } 325 326 byte[] responseBody = method.getResponseBody(); 328 329 method.releaseConnection(); 331 332 return new String (responseBody); 335 } 336 337 342 public void testServerFound() throws Exception 343 { 344 } 345 346 } 347 | Popular Tags |