Author: jfrederic.clere(a)jboss.com
Date: 2008-07-03 06:26:27 -0400 (Thu, 03 Jul 2008)
New Revision: 1737
Added:
trunk/mod_cluster/test/java/build.xml
trunk/mod_cluster/test/java/org/
trunk/mod_cluster/test/java/org/jboss/
trunk/mod_cluster/test/java/org/jboss/mod_cluster/
trunk/mod_cluster/test/java/org/jboss/mod_cluster/Client.java
trunk/mod_cluster/test/java/org/jboss/mod_cluster/JBossWeb.java
trunk/mod_cluster/test/java/org/jboss/mod_cluster/Maintest.java
trunk/mod_cluster/test/java/org/jboss/mod_cluster/TestBase.java
Log:
Testsuite for mod_cluster.
Added: trunk/mod_cluster/test/java/build.xml
===================================================================
--- trunk/mod_cluster/test/java/build.xml (rev 0)
+++ trunk/mod_cluster/test/java/build.xml 2008-07-03 10:26:27 UTC (rev 1737)
@@ -0,0 +1,88 @@
+<?xml version="1.0"?>
+<!--
+ * Copyright(c) 2008 Red Hat Middleware, LLC,
+ * and individual contributors as indicated by the @authors tag.
+ * See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library in the file COPYING.LIB;
+ * if not, write to the Free Software Foundation, Inc.,
+ * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
+ *
+ * @author Jean-Frederic Clere
+ * @version $Revision$
+-->
+<project name="mod_cluster" default="all"
basedir=".">
+
+ <!-- See "build.properties.sample" in the top level directory for all
-->
+ <!-- property values you must customize for successful building!!! -->
+ <property file="${user.home}/build.properties"/>
+ <property file="build.properties"/>
+
+ <property file="build.properties.default"/>
+
+ <property name="test.classes"
value="${basedir}/output/classes"/>
+
+ <property name="compile.source" value="1.5"/>
+
+ <property name="junit.jar" value="${junit.home}/junit.jar"/>
+ <property name="test.runner"
value="junit.textui.TestRunner"/>
+
+ <path id="tomcat.test.classpath">
+ <pathelement location="${test.classes}"/>
+ <pathelement location="${junit.jar}"/>
+ <pathelement location="${commons-httpclient.jar}"/>
+ <pathelement location="${commons-logging.jar}"/>
+ <pathelement location="${commons-codec.jar}"/>
+ <pathelement location="${bootstrap.jar}"/>
+ <pathelement location="${annotations-api.jar}"/>
+
+ <fileset dir="${jbosswebjar}/">
+ <include name="jbossweb.jar"/>
+ <include name="servlet-api.jar"/>
+ </fileset>
+
+<!--
+ <fileset dir="${jbossweb.build}/bin/">
+ <include name="tomcat-juli.jar"/>
+ </fileset>
+ -->
+ </path>
+
+ <target name="compile">
+
+ <mkdir dir="${test.classes}"/>
+ <mkdir dir="${test.classes}/conf"/>
+
+ <!-- Compile -->
+ <javac srcdir="." destdir="${test.classes}"
+ debug="${compile.debug}"
+ deprecation="${compile.deprecation}"
+ source="${compile.source}"
+ optimize="${compile.optimize}">
+ <classpath refid="tomcat.test.classpath" />
+ <include name="org/jboss/mod_cluster/**" />
+ </javac>
+
+ </target>
+
+ <target name="all" depends="compile">
+ <copy file="conf/web.xml" todir="${test.classes}/conf"/>
+ <java dir="${test.classes}" classname="${test.runner}"
fork="yes" failonerror="${test.failonerror}">
+ <arg value="org.jboss.mod_cluster.Maintest"/>
+ <classpath refid="tomcat.test.classpath"/>
+ </java>
+
+ </target>
+</project>
Added: trunk/mod_cluster/test/java/org/jboss/mod_cluster/Client.java
===================================================================
--- trunk/mod_cluster/test/java/org/jboss/mod_cluster/Client.java
(rev 0)
+++ trunk/mod_cluster/test/java/org/jboss/mod_cluster/Client.java 2008-07-03 10:26:27 UTC
(rev 1737)
@@ -0,0 +1,154 @@
+/*
+ * mod_cluster
+ *
+ * Copyright(c) 2008 Red Hat Middleware, LLC,
+ * and individual contributors as indicated by the @authors tag.
+ * See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library in the file COPYING.LIB;
+ * if not, write to the Free Software Foundation, Inc.,
+ * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
+ *
+ * @author Jean-Frederic Clere
+ * @version $Revision$
+ */
+
+package org.jboss.mod_cluster;
+
+import org.apache.commons.httpclient.*;
+import org.apache.commons.httpclient.methods.*;
+
+public class Client extends Thread {
+
+ private String jsessionid = null;
+ private String URL = null;
+ private String command = null;
+ private int nbtest = 10;
+ private boolean checkcookie = true;
+ private boolean success = false;
+ /*
+ *
+ * Usage:
+ * java Client
http://mywebserver:80/ test
+ *
+ * @param args command line arguments
+ * Argument 0 is a URL to a web server
+ * Argument 1 is the command to execute.
+ *
+ */
+ public static void main(String[] args) throws Exception
+ {
+ if (args.length != 2)
+ {
+ System.err.println("missing command line arguments");
+ System.exit(1);
+ }
+ Client client = new Client();
+ client.runit(args[0], args[1], 10, true);
+ client.start();
+ try {
+ client.join();
+ } catch (InterruptedException ex) {
+ ex.printStackTrace();
+ }
+ }
+
+ public int runit(String URL, String command, int nbtest, boolean checkcookie)
throws Exception
+ {
+ this.URL = URL;
+ this.command = command;
+ this.checkcookie = checkcookie;
+ this.nbtest = nbtest;
+
+ return runit();
+ }
+
+ public int runit() throws Exception
+ {
+
+ HttpClient httpClient = new HttpClient();
+ GetMethod pm = new GetMethod(URL);
+
+ System.out.println("Connecting to " + URL);
+
+ Integer connectionTimeout = 40000;
+ pm.getParams().setParameter("http.socket.timeout",
connectionTimeout);
+ pm.getParams().setParameter("http.connection.timeout",
connectionTimeout);
+ httpClient.getParams().setParameter("http.socket.timeout",
connectionTimeout);
+ httpClient.getParams().setParameter("http.connection.timeout",
connectionTimeout);
+ if (jsessionid != null) {
+ System.out.println("jsessionid: " + jsessionid);
+ pm.setRequestHeader("Cookie", "JSESSIONID=" +
jsessionid);
+ }
+
+ int httpResponseCode = 0;
+ try {
+ httpResponseCode = httpClient.executeMethod(pm);
+ System.out.println("response: " + httpResponseCode);
+ System.out.println("response: " + pm.getStatusLine());
+ if (httpResponseCode == 200) {
+ Cookie[] cookies = httpClient.getState().getCookies();
+ System.out.println( "Cookies: " + cookies);
+ if (cookies != null && cookies.length!=0) {
+ for (int i = 0; i < cookies.length; i++) {
+ Cookie cookie = cookies[i];
+ System.out.println( "Cookie: " +
cookie.getName() + ", Value: " + cookie.getValue());
+ if (cookie.getName().equals("JSESSIONID")) {
+ if (jsessionid == null) {
+ jsessionid = cookie.getValue();
+ System.out.println("cookie first time:
" + jsessionid);
+ return 0; // first time ok.
+ } else {
+ System.out.println("cookie second time:
" + jsessionid);
+ if (jsessionid.compareTo(cookie.getValue()) == 0)
{
+ System.out.println("cookie ok");
+ return 0;
+ }
+ }
+ }
+ }
+ } else {
+ // Look in the response to make sure that there is a cookie.
+ int len = (int) pm.getResponseContentLength();
+ if (pm.getResponseBodyAsString(len).indexOf(jsessionid) !=
-1)
+ return 0;
+ System.out.println("No cookies");
+ }
+ } else {
+ System.out.println("Not 200");
+ }
+ // System.out.println("response:\n" +
pm.getResponseBodyAsString(len));
+ } catch(HttpException e) {
+ e.printStackTrace();
+ }
+ System.out.println("DONE: " + httpResponseCode);
+ return httpResponseCode;
+ }
+ public void run() {
+ for (int i = 0; i < nbtest; i++) {
+ try {
+ if (runit() != 0) return;
+ sleep((int)(Math.random() * 1000));
+ } catch (InterruptedException e) {
+ } catch (Exception e) {}
+ }
+ System.out.println("DONE!");
+ success = true;
+ }
+ public boolean getresultok() {
+ return success;
+ }
+
+}
Added: trunk/mod_cluster/test/java/org/jboss/mod_cluster/JBossWeb.java
===================================================================
--- trunk/mod_cluster/test/java/org/jboss/mod_cluster/JBossWeb.java
(rev 0)
+++ trunk/mod_cluster/test/java/org/jboss/mod_cluster/JBossWeb.java 2008-07-03 10:26:27
UTC (rev 1737)
@@ -0,0 +1,206 @@
+/*
+ * mod_cluster
+ *
+ * Copyright(c) 2008 Red Hat Middleware, LLC,
+ * and individual contributors as indicated by the @authors tag.
+ * See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library in the file COPYING.LIB;
+ * if not, write to the Free Software Foundation, Inc.,
+ * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
+ *
+ * @author Jean-Frederic Clere
+ * @version $Revision$
+ */
+
+package org.jboss.mod_cluster;
+
+import java.io.File;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+
+import java.net.InetAddress;
+import java.net.*;
+
+import org.apache.catalina.startup.Embedded;
+import org.apache.catalina.Realm;
+import org.apache.catalina.Engine;
+import org.apache.catalina.Host;
+import org.apache.catalina.Context;
+import org.apache.catalina.*;
+import org.apache.catalina.connector.Connector;
+import org.apache.tomcat.util.IntrospectionUtils;
+import org.apache.catalina.realm.MemoryRealm;
+import org.apache.catalina.core.*;
+
+import org.jboss.web.cluster.ClusterListener;
+
+public class JBossWeb extends Embedded {
+
+ private void copyFile(File in, File out) throws IOException {
+ FileInputStream fis = new FileInputStream(in);
+ FileOutputStream fos = new FileOutputStream(out);
+ byte[] buf = new byte[1024];
+ int i = 0;
+ while((i=fis.read(buf))!=-1) {
+ fos.write(buf, 0, i);
+ }
+ fis.close();
+ fos.close();
+ }
+
+
+ public JBossWeb(String route, String host) throws IOException {
+
+ setCatalinaBase(route);
+ setCatalinaHome(route);
+
+ //Create an Engine
+ Engine baseEngine = createEngine();
+
+ baseEngine.setName(host + "Engine" + route);
+ baseEngine.setDefaultHost(host);
+ baseEngine.setJvmRoute(route);
+ baseEngine.setRealm(null);
+
+ // Create node1/webapps/ROOT and index.html
+ File fd = new File ( route + "/webapps/ROOT");
+ fd.mkdirs();
+ String docBase = fd.getAbsolutePath();
+ String appBase = fd.getParent();
+ fd = new File (route + "/webapps/ROOT" , "index.html");
+ FileWriter out = new FileWriter(fd);
+ out.write(route + ":This is a test\n");
+ out.close();
+
+ // Copy a small servlet for testing.
+ fd = new File ( route + "/webapps/ROOT/WEB-INF/classes");
+ fd.mkdirs();
+ fd = new File (route + "/webapps/ROOT/WEB-INF/classes" ,
"MyCount.class");
+ File fdin = new File ("MyCount.class");
+ copyFile(fdin, fd);
+
+
+ //Create Host
+ Host baseHost = createHost( host, appBase);
+ baseEngine.addChild( baseHost );
+
+ //Create default context
+ Context rootContext = createContext("/",docBase);
+ rootContext.setIgnoreAnnotations(true);
+ baseHost.addChild( rootContext );
+ addEngine( baseEngine );
+ baseEngine.setService(this);
+ }
+ public void addConnector(int port) throws IOException {
+
+
+ Connector connector = createConnector( (java.net.InetAddress) null,
+ port, "http");
+ // port, "ajp");
+
+ // Look in StandardService to see why it works ;-)
+ addConnector( connector );
+ }
+
+
+ /* Test */
+ public static void main(String[] args) {
+
+ StandardServer server = null;
+ JBossWeb service = null;
+ JBossWeb service2 = null;
+ try {
+ server = (StandardServer) ServerFactory.getServer();
+
+ service = new JBossWeb("node1", "localhost");
+ service.addConnector(8009);
+ server.addService(service);
+
+ service2 = new JBossWeb("node2", "localhost");
+ service2.addConnector(8000);
+ server.addService(service2);
+
+ ClusterListener cluster = new ClusterListener();
+ cluster.setAdvertiseGroupAddress("232.0.0.2");
+ cluster.setAdvertisePort(23364);
+ cluster.setSsl(false);
+ // SSL ?
+ server.addLifecycleListener((LifecycleListener) cluster);
+
+ Service[] services = server.findServices();
+ for (int i = 0; i < services.length; i++) {
+ System.out.println("service[" + i + "]: " +
services[i]);
+ Engine engine = (Engine) services[i].getContainer();
+ System.out.println("engine: " + engine);
+ // System.out.println("service: " + engine.getService());
+ // System.out.println("connectors: " +
engine.getService().findConnectors());
+ System.out.println("JFC connectors: " +
services[i].findConnectors());
+ Connector [] connectors = services[i].findConnectors();
+ for (int j = 0; j < connectors.length; j++) {
+ System.out.println("JFC connector: " + connectors[j]);
+ }
+ }
+
+ server.start();
+
+ } catch(IOException ex) {
+ ex.printStackTrace();
+ } catch (LifecycleException ex) {
+ ex.printStackTrace();
+ }
+
+ // Wait until httpd as received the nodes information.
+ try {
+ Thread.sleep(15000);
+ } catch (InterruptedException ex) {
+ ex.printStackTrace();
+ }
+
+ // Start the client and wait for it.
+ Client client = new Client();
+
+ // Wait for it.
+ try {
+ client.runit("http://localhost:7779/ROOT/MyCount", "cmd",
10, true);
+ client.start();
+ client.join();
+ } catch (Exception ex) {
+ ex.printStackTrace();
+ }
+ if (client.getresultok())
+ System.out.println("Test DONE");
+ else
+ System.out.println("Test FAILED");
+
+ // Stop the server or services.
+ try {
+ server.stop();
+ // service.stop();
+ // service2.stop();
+ } catch (LifecycleException ex) {
+ ex.printStackTrace();
+ }
+
+ // Wait until httpd as received the stop messages.
+ try {
+ Thread.sleep(5000);
+ } catch (InterruptedException ex) {
+ ex.printStackTrace();
+ }
+ }
+}
Added: trunk/mod_cluster/test/java/org/jboss/mod_cluster/Maintest.java
===================================================================
--- trunk/mod_cluster/test/java/org/jboss/mod_cluster/Maintest.java
(rev 0)
+++ trunk/mod_cluster/test/java/org/jboss/mod_cluster/Maintest.java 2008-07-03 10:26:27
UTC (rev 1737)
@@ -0,0 +1,47 @@
+/*
+ * mod_cluster
+ *
+ * Copyright(c) 2008 Red Hat Middleware, LLC,
+ * and individual contributors as indicated by the @authors tag.
+ * See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library in the file COPYING.LIB;
+ * if not, write to the Free Software Foundation, Inc.,
+ * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
+ *
+ * @author Jean-Frederic Clere
+ * @version $Revision$
+ */
+
+package org.jboss.mod_cluster;
+
+import junit.framework.Test;
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
+import junit.textui.TestRunner;
+
+import java.lang.Exception;
+
+
+public class Maintest extends TestCase {
+ public static void main( String args[] ) {
+ TestRunner.run(suite());
+ }
+ public static Test suite() {
+ TestSuite suite = new TestSuite();
+ suite.addTest(new TestSuite(TestBase.class));
+ return suite;
+ }
+}
Added: trunk/mod_cluster/test/java/org/jboss/mod_cluster/TestBase.java
===================================================================
--- trunk/mod_cluster/test/java/org/jboss/mod_cluster/TestBase.java
(rev 0)
+++ trunk/mod_cluster/test/java/org/jboss/mod_cluster/TestBase.java 2008-07-03 10:26:27
UTC (rev 1737)
@@ -0,0 +1,131 @@
+/*
+ * mod_cluster
+ *
+ * Copyright(c) 2008 Red Hat Middleware, LLC,
+ * and individual contributors as indicated by the @authors tag.
+ * See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library in the file COPYING.LIB;
+ * if not, write to the Free Software Foundation, Inc.,
+ * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
+ *
+ * @author Jean-Frederic Clere
+ * @version $Revision$
+ */
+
+package org.jboss.mod_cluster;
+
+import java.io.IOException;
+
+import junit.framework.TestCase;
+
+import org.apache.catalina.Engine;
+import org.apache.catalina.ServerFactory;
+import org.apache.catalina.Service;
+import org.apache.catalina.LifecycleListener;
+import org.apache.catalina.LifecycleException;
+import org.apache.catalina.connector.Connector;
+import org.apache.catalina.core.StandardServer;
+
+import org.jboss.web.cluster.ClusterListener;
+
+public class TestBase extends TestCase {
+
+ /* Test */
+ public void testBase() {
+
+ StandardServer server = null;
+ JBossWeb service = null;
+ JBossWeb service2 = null;
+ try {
+ server = (StandardServer) ServerFactory.getServer();
+
+ service = new JBossWeb("node1", "localhost");
+ service.addConnector(8009);
+ server.addService(service);
+
+ service2 = new JBossWeb("node2", "localhost");
+ service2.addConnector(8000);
+ server.addService(service2);
+
+ ClusterListener cluster = new ClusterListener();
+ cluster.setAdvertiseGroupAddress("232.0.0.2");
+ cluster.setAdvertisePort(23364);
+ cluster.setSsl(false);
+ // SSL ?
+ server.addLifecycleListener((LifecycleListener) cluster);
+
+ Service[] services = server.findServices();
+ for (int i = 0; i < services.length; i++) {
+ System.out.println("service[" + i + "]: " +
services[i]);
+ Engine engine = (Engine) services[i].getContainer();
+ System.out.println("engine: " + engine);
+ // System.out.println("service: " + engine.getService());
+ // System.out.println("connectors: " +
engine.getService().findConnectors());
+ System.out.println("JFC connectors: " +
services[i].findConnectors());
+ Connector [] connectors = services[i].findConnectors();
+ for (int j = 0; j < connectors.length; j++) {
+ System.out.println("JFC connector: " + connectors[j]);
+ }
+ }
+
+ server.start();
+
+ } catch(IOException ex) {
+ ex.printStackTrace();
+ } catch (LifecycleException ex) {
+ ex.printStackTrace();
+ }
+
+ // Wait until httpd as received the nodes information.
+ try {
+ Thread.sleep(15000);
+ } catch (InterruptedException ex) {
+ ex.printStackTrace();
+ }
+
+ // Start the client and wait for it.
+ Client client = new Client();
+
+ // Wait for it.
+ try {
+ client.runit("http://localhost:7779/ROOT/MyCount", "cmd",
10, true);
+ client.start();
+ client.join();
+ } catch (Exception ex) {
+ ex.printStackTrace();
+ }
+ if (client.getresultok())
+ System.out.println("Test DONE");
+ else
+ System.out.println("Test FAILED");
+
+ // Stop the server or services.
+ try {
+ server.stop();
+ // service.stop();
+ // service2.stop();
+ } catch (LifecycleException ex) {
+ ex.printStackTrace();
+ }
+
+ // Wait until httpd as received the stop messages.
+ try {
+ Thread.sleep(5000);
+ } catch (InterruptedException ex) {
+ ex.printStackTrace();
+ }
+ }
+}