package org.jboss.test.cluster.apache_tomcat;
import java.io.IOException;
import java.net.HttpURLConnection;
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;
import org.jboss.test.JBossRMIAdaptorHelper;
import javax.management.ObjectName;
import javax.management.MBeanInfo;
public class HttpSessionReplicationTestCase
extends JBossClusteredTestCase
{
private String apacheurl = null;
public HttpSessionReplicationTestCase(String name)
{
super(name);
}
public static Test suite() throws Exception
{
Test t1 = JBossClusteredTestCase.getDeploySetup(HttpSessionReplicationTestCase.class,
"http-sr.war");
return t1;
}
public void testApacheConnection()
throws Exception
{
getLog().debug("Enter testApacheConnection");
try
{
apacheurl = System.getProperty("apache.url");
getLog().debug(apacheurl);
assertTrue("Apache Up?", this.checkURL(apacheurl));
}
catch (Exception e)
{
getLog().debug(e.getMessage());
}
getLog().debug("Exit testApacheConnection");
}
public void testHttpSessionReplication()
throws Exception
{
String attr = "";
getLog().debug("Enter testHttpSessionReplication");
apacheurl = System.getProperty("apache.url");
String urlname = apacheurl + System.getProperty("apache.set.url");
String geturlname = apacheurl + System.getProperty("apache.get.url");
getLog().debug(urlname + ":::::::" + geturlname);
HttpClient client = new HttpClient();
HttpMethod method = new GetMethod(geturlname);
attr = makeGet(client, method);
this.shutDownTomcatInstance(1);
getLog().debug("Brought down the first tomcat instance");
String[] httpURLs = super.getHttpURLs();
String httpurl = httpURLs[0];
String tmsg = "Is 1st Tomcat really down?Tomcat Up(" + httpurl + ")=";
getLog().debug(tmsg + checkURL(httpurl));
sleepThread(30);
method = new GetMethod(geturlname);
String attr2 = makeGet(client, method);
this.sleepThread(10);
getLog().debug("Will Start the Tomcat MBean back");
this.startTomcatInstance(1);
this.sleepThread(10);
getLog().debug("Tomcat Up=" + checkURL(httpurl));
String tstr = "attr1=" + attr + " and attr2=" + attr2;
if (!attr2.equals(attr)) fail("Http Session Replication failed with " + tstr);
getLog().debug("Http Session Replication has happened");
getLog().debug("Exit testHttpSessionReplication");
}
private void startTomcatInstance(int instancenum)
throws Exception
{
String jndi = getJNDIUrl(instancenum);
getLog().debug("JNDI URL Obtained= " + jndi);
JBossRMIAdaptorHelper server = new JBossRMIAdaptorHelper(jndi);
ObjectName name = new ObjectName("jboss.web:service=WebServer");
MBeanInfo info = server.getMBeanInfo(name);
System.out.println("Tomcat MBean:" + info.getClassName());
getLog().debug("Going to start tomcat ");
server.invokeOperation(name, "start", null, null);
this.sleepThread(10);
server.invokeOperation(name, "startConnectors", null, null);
}
private void shutDownTomcatInstance(int instancenum)
throws Exception
{
String jndi = getJNDIUrl(instancenum);
getLog().debug("JNDI URL Obtained= " + jndi);
JBossRMIAdaptorHelper server = new JBossRMIAdaptorHelper(jndi);
ObjectName name = new ObjectName("jboss.web:service=WebServer");
MBeanInfo info = server.getMBeanInfo(name);
System.out.println("Tomcat MBean:" + info.getClassName());
getLog().debug("Going to stop tomcat ");
server.invokeOperation(name, "stop", null, null);
}
private String getJNDIUrl(int instancenum)
{
String jndi = "";
try
{
int num = instancenum - 1; String key = "node" + num + ".jndi.url"; jndi = System.getProperty(key);
}
catch (Exception e)
{
fail("getJNDIUrl Failed with:" + e.getMessage());
}
return jndi;
}
private void sleepThread(long secs)
throws Exception
{
Thread.sleep(1000 * secs);
}
private String makeGet(HttpClient client, HttpMethod method)
{
try
{
client.executeMethod(method);
}
catch (HttpRecoverableException e)
{
log.debug("A recoverable exception occurred, retrying." +
e.getMessage());
}
catch (IOException e)
{
log.debug(e);
e.printStackTrace();
System.exit(-1);
}
byte[] responseBody = method.getResponseBody();
method.releaseConnection();
return new String(responseBody);
}
private boolean checkURL(String url)
{
boolean ok = false;
if (url != null) url = url.trim();
try
{
HttpClient httpConn = new HttpClient();
GetMethod g = new GetMethod(url);
int responseCode = httpConn.executeMethod(g);
log.debug("Response Code for " + url + " is=" + responseCode);
ok = responseCode == HttpURLConnection.HTTP_OK;
}
catch (Exception e)
{
log.debug("Exception for checking url=" + url);
log.debug(e);
ok = false;
}
return ok;
}
}