From: Ben W. <bw...@us...> - 2004-12-31 23:16:57
|
User: bwang00 Date: 04/12/31 15:16:47 Added: src/main/org/jboss/test/cluster/test BaseTest.java SimpleTestCase.java Log: Added basic http session repl unit test Revision Changes Path 1.1 jbosstest/src/main/org/jboss/test/cluster/test/BaseTest.java Index: BaseTest.java =================================================================== /* * JBoss, the OpenSource J2EE webOS * * Distributable under LGPL license. * See terms of license at gnu.org. */ package org.jboss.test.cluster.test; import org.apache.commons.httpclient.*; import org.apache.commons.httpclient.methods.GetMethod; import org.jboss.test.JBossClusteredTestCase; import org.jboss.test.JBossRMIAdaptorHelper; import javax.management.MBeanInfo; import javax.management.ObjectName; import java.io.IOException; import java.net.HttpURLConnection; /** * Base class to test HttpSessionReplication. * * @author Ben Wang * @version $Revision: 1.0 */ public abstract class BaseTest extends JBossClusteredTestCase { protected String[] servers_ = null; public BaseTest(String name) { super(name); } public void setUp() throws Exception { servers_ = getServers(); assertEquals("Server size " , servers_.length, 2); } /** * Sleep for specified time * * @param secs * @throws Exception */ protected void sleepThread(long secs) throws Exception { Thread.sleep(1000 * secs); } /** * Makes a http call to the jsp that retrieves the attribute stored on the * session. When the attribute values mathes with the one retrieved earlier, * we have HttpSessionReplication. * Makes use of commons-httpclient library of Apache * * @param client * @param url * @return session attribute */ protected String makeGet(HttpClient client, String url) { GetMethod method = new GetMethod(url); int responseCode = 0; try { responseCode = client.executeMethod(method); } catch (IOException e) { e.printStackTrace(); fail("HttpClient executeMethod fails." +e.toString()); } assertTrue("Get OK", responseCode == HttpURLConnection.HTTP_OK); // Read the response body. byte[] responseBody = method.getResponseBody(); // Release the connection. // method.releaseConnection(); // Deal with the response. // Use caution: ensure correct character encoding and is not binary data return new String(responseBody); } /** * Makes a http call to the jsp that retrieves the attribute stored on the * session. When the attribute values mathes with the one retrieved earlier, * we have HttpSessionReplication. * Makes use of commons-httpclient library of Apache * * @param client * @param url * @return session attribute */ protected String makeGetWithState(HttpClient client, String url) { GetMethod method = new GetMethod(url); int responseCode = 0; try { HttpState state = client.getState(); responseCode = client.executeMethod(method.getHostConfiguration(), method, state); } catch (IOException e) { e.printStackTrace(); fail("HttpClient executeMethod fails." +e.toString()); } assertTrue("Get OK", responseCode == HttpURLConnection.HTTP_OK); // Read the response body. byte[] responseBody = method.getResponseBody(); // Release the connection. // method.releaseConnection(); // Deal with the response. // Use caution: ensure correct character encoding and is not binary data return new String(responseBody); } protected void setCookieDomainToThisServer(HttpClient client, String server) { // Get the state for the JSESSIONID HttpState state = client.getState(); // Get the JSESSIONID so we can reset the host Cookie[] cookies = state.getCookies(); Cookie sessionID = null; for(int c = 0; c < cookies.length; c ++) { Cookie k = cookies[c]; if( k.getName().equalsIgnoreCase("JSESSIONID") ) sessionID = k; } if(sessionID == null) { fail("setCookieDomainToThisServer(): fail to find session id. Server name: " +server); } log.info("Saw JSESSIONID="+sessionID); // Reset the domain so that the cookie will be sent to server1 sessionID.setDomain(server); state.addCookie(sessionID); } } 1.1 jbosstest/src/main/org/jboss/test/cluster/test/SimpleTestCase.java Index: SimpleTestCase.java =================================================================== /* * JBoss, the OpenSource J2EE webOS * * Distributable under LGPL license. * See terms of license at gnu.org. */ package org.jboss.test.cluster.test; import junit.framework.Test; import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.HttpMethod; import org.apache.commons.httpclient.methods.GetMethod; import org.jboss.test.JBossClusteredTestCase; /** * Simple clustering test case of get/set. * * @author Ben Wang * @version $Revision: 1.0 */ public class SimpleTestCase extends BaseTest { public SimpleTestCase(String name) { super(name); } public static Test suite() throws Exception { Test t1 = JBossClusteredTestCase.getDeploySetup(SimpleTestCase.class, "http-sr.war"); return t1; } /** * Main method that deals with the Http Session Replication Test * * @throws Exception */ public void testHttpSessionReplication() throws Exception { String attr = ""; getLog().debug("Enter testHttpSessionReplication"); String baseURL0 = System.getProperty("jbosstest.cluster.node0.http.url"); String baseURL1 = baseURL0; if( servers_.length > 1 ) { baseURL1 = System.getProperty("jbosstest.cluster.node1.http.url"); } String setURLName = "/http-sr/testsessionreplication.jsp"; String getURLName = "/http-sr/getattribute.jsp"; getLog().debug(setURLName + ":::::::" + getURLName); // Create an instance of HttpClient. HttpClient client = new HttpClient(); // Set the session attribute first makeGet(client, baseURL0 +setURLName); // Get the Attribute set by testsessionreplication.jsp attr = makeGetWithState(client, baseURL0 +getURLName); // Let's switch to server 2 to retrieve the session attribute. setCookieDomainToThisServer(client, servers_[1]); String attr2 = makeGet(client, baseURL1 +getURLName); // Check the result assertEquals("Http session replication attribtues retrieved from both servers ", attr, attr2); getLog().debug("Http Session Replication has happened"); getLog().debug("Exit testHttpSessionReplication"); } } |