package org.jboss.test.cluster.httpsessionreplication;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.Properties;
import junit.framework.Test;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.HttpRecoverableException;
import org.apache.commons.httpclient.methods.GetMethod;
import org.jboss.test.JBossClusteredTestCase;
public class HttpSessionReplicationUnitTestCase
extends JBossClusteredTestCase {
private String[] servernames= {"jnp://localhost:1099", "jnp://localhost:1199"};
private Properties prop = null;
private int numInstances = 0;
public HttpSessionReplicationUnitTestCase (String name) {
super(name);
try{
this.getPropertiesFile();
String numin = prop.getProperty("NumOfInstances");
numInstances = Integer.parseInt( numin );
if( numInstances < 2 ) fail( "Atleast two nodes needed");
this.setServerNames(servernames);
}catch( Exception e){
fail( e.getMessage());
}
}
public static Test suite() throws Exception
{
Test t1 = JBossClusteredTestCase.getDeploySetup(HttpSessionReplicationUnitTestCase.class,
"httpsessionreplication.jar");
return t1;
}
public void testApacheConnection()
throws Exception
{
getLog().debug("Enter testApacheConnection");
try {
this.makeConnection(prop.getProperty("ApacheUrl"));
} catch (Exception e) {
}
getLog().debug("Exit testApacheConnection");
}
public void testHttpSessionReplication()
throws Exception
{
String attr = "";
getLog().debug("Enter testHttpSessionReplication");
String urlname = prop.getProperty("SetAttrUrl");
String geturlname = prop.getProperty("GetAttrUrl");
HttpClient client = new HttpClient();
HttpMethod method = new GetMethod(urlname);
String str = makeGet( client, method );
method = new GetMethod(geturlname);
attr= makeGet( client,method );
shutDownInstance( 1 );
getLog().debug( "Brought down the first instance");
sleepThread(30*1000);
method = new GetMethod(geturlname);
String attr2= makeGet( client, method );
if( ! attr2.equals(attr)) fail("Http Session Replication Failed");
getLog().debug("Http Session Replication has happened");
getLog().debug("Exit testHttpSessionReplication");
}
public void getPropertiesFile(){
prop = new Properties();
try{
java.net.URL url = ClassLoader.getSystemResource("cluster/cluster-test.properties");
prop.load( url.openStream());
}catch( Exception e){
fail("Need a properties file under src/resources/cluster:"+e.getMessage());
}
}
private void shutDownInstance(int instancenum)
throws Exception
{
String command = getCommand(instancenum);
getLog().debug("Going to execute:"+command);
Process child = Runtime.getRuntime().exec(command);
sleepThread( 10*1000 );
getLog().debug("Process exit value="+child.exitValue());
}
private String getCommand( int instancenum) {
String command = "";
try{
command = prop.getProperty("jboss.location") + prop.getProperty("ShutDownScript");
command += " -s " + "jnp://"+prop.getProperty("Instance"+instancenum+".host")+":"+
prop.getProperty("Instance"+instancenum+".port.jndi");
}catch( Exception e){
fail( "getCommand Failed with:"+ e.getMessage());
}
return command;
}
private void sleepThread(long millisecs)
throws Exception {
Thread.sleep(millisecs);
}
private void makeConnection( String urlname )
throws Exception
{
getLog().debug("Enter makeConnection");
try {
URL url = new URL(urlname);
URLConnection conn = url.openConnection();
for (int i=0; ; i++) {
String hname = conn.getHeaderFieldKey(i);
String hvalue = conn.getHeaderField(i);
getLog().debug("hname="+hname+"::"+"value="+hvalue);
if (hname == null && hvalue == null) {
break;
}
if (hname == null) {
getLog().debug("Response from Apache="+hvalue);
if( hvalue.indexOf("200") < 0 && hvalue.indexOf("301") < 0
&& hvalue.indexOf("302") < 0)
fail(urlname+" Down");
break;
}
}
} catch (Exception e) {
getLog().debug(e);
}
}
private void getHttpText( String urlname ){
getLog().debug( getAttribute(urlname));
}
private String getAttribute( String urlname){
BufferedReader in = null;
StringBuffer sb = new StringBuffer();
try{
URL url = new URL(urlname);
in = new BufferedReader(new InputStreamReader(url.openStream()));
String str;
while ((str = in.readLine()) != null) {
sb.append(str);
}
getLog().debug(sb.toString());
}catch( Exception e){
getLog().debug( e);
}finally{
try{
in.close();
}catch(Exception y){}
}
return sb.toString();
}
private String makeGet( HttpClient client, HttpMethod method) throws IOException{
int statusCode = -1;
try {
statusCode = client.executeMethod(method);
} catch (HttpRecoverableException e) {
System.err.println(
"A recoverable exception occurred, retrying." +
e.getMessage());
} catch (IOException e) {
System.err.println("Failed to download file.");
e.printStackTrace();
System.exit(-1);
}
byte[] responseBody = method.getResponseBody();
method.releaseConnection();
return new String(responseBody);
}
public void testServerFound() throws Exception
{
}
}