[jboss-svn-commits] JBL Code SVN: r19346 - in labs/jbosstm/workspace/adinn: txtest and 10 other directories.

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Mon Mar 31 12:15:38 EDT 2008


Author: adinn
Date: 2008-03-31 12:15:38 -0400 (Mon, 31 Mar 2008)
New Revision: 19346

Added:
   labs/jbosstm/workspace/adinn/txtest/
   labs/jbosstm/workspace/adinn/txtest/build.xml
   labs/jbosstm/workspace/adinn/txtest/dd/
   labs/jbosstm/workspace/adinn/txtest/dd/tests_application.xml
   labs/jbosstm/workspace/adinn/txtest/dd/tests_web-app.xml
   labs/jbosstm/workspace/adinn/txtest/ext/
   labs/jbosstm/workspace/adinn/txtest/ext/junit.jar
   labs/jbosstm/workspace/adinn/txtest/src/
   labs/jbosstm/workspace/adinn/txtest/src/org/
   labs/jbosstm/workspace/adinn/txtest/src/org/jboss/
   labs/jbosstm/workspace/adinn/txtest/src/org/jboss/jbossts/
   labs/jbosstm/workspace/adinn/txtest/src/org/jboss/jbossts/test/
   labs/jbosstm/workspace/adinn/txtest/src/org/jboss/jbossts/test/jts/
   labs/jbosstm/workspace/adinn/txtest/src/org/jboss/jbossts/test/jts/TxMultiCreate.java
   labs/jbosstm/workspace/adinn/txtest/src/org/jboss/jbossts/test/jts/TxTestSuite.java
   labs/jbosstm/workspace/adinn/txtest/src/org/jboss/jbossts/test/qa/
   labs/jbosstm/workspace/adinn/txtest/src/org/jboss/jbossts/test/qa/junit/
   labs/jbosstm/workspace/adinn/txtest/src/org/jboss/jbossts/test/qa/junit/TestRunnerServlet.java
Log:
simple test of JTS transaction multiple create/commit/rollback

Added: labs/jbosstm/workspace/adinn/txtest/build.xml
===================================================================
--- labs/jbosstm/workspace/adinn/txtest/build.xml	                        (rev 0)
+++ labs/jbosstm/workspace/adinn/txtest/build.xml	2008-03-31 16:15:38 UTC (rev 19346)
@@ -0,0 +1,123 @@
+<!--
+  JBoss, Home of Professional Open Source
+  Copyright 2008, Red Hat Middleware LLC, and individual contributors
+  as indicated by the @author tags.
+  See the copyright.txt in the distribution for a full listing
+  of individual contributors.
+  This copyrighted material is made available to anyone wishing to use,
+  modify, copy, or redistribute it subject to the terms and conditions
+  of the GNU General Public License, v. 2.0.
+  This program is distributed in the hope that it will be useful, but WITHOUT A
+  WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
+  PARTICULAR PURPOSE.  See the GNU General Public License for more details.
+  You should have received a copy of the GNU General Public License,
+  v. 2.0 along with this distribution; if not, write to the Free Software
+  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
+  MA  02110-1301, USA.
+
+  (C) 2008,
+  @author JBoss Inc.
+-->
+
+<project name="TxTest" default="webapp" basedir=".">
+
+    <!-- ================================================================== -->
+
+    <property environment="env"/>
+    <property name="jboss.dir" value="${env.JBOSS_HOME}"/>
+    <property name="deploy.dir" value="${jboss.dir}/server/all/deploy"/>
+    <property name="hostname" value="localhost"/>
+    <property name="port" value="8080"/>
+
+    <!-- ================================================================== -->
+
+    <property name="build.dir"               value="build"/>
+    <property name="tests.src.dir"           value="src"/>
+    <property name="tests.dd.dir"            value="dd"/>
+    <property name="build.tests.classes.dir" value="${build.dir}/tests/classes"/>
+    <property name="build.tests.lib.dir"     value="${build.dir}/tests/lib"/>
+    <property name="build.tests.dd.dir"      value="${build.dir}/tests/dd"/>
+    <property name="build.tests.webapps.dir" value="${build.dir}/tests/webapps"/>
+
+    <property name="tests.libs.dir" value="ext"/>
+    <property name="tests.libs" value="junit.jar"/>
+
+    <property name="jboss.libs.dir" value="${jboss.dir}/client"/>
+    <property name="jboss.libs" value="jbossall-client.jar"/>
+
+    <property name="jboss.server.libs.dir" value="${jboss.dir}/server/all/lib"/>
+    <property name="jboss.server.libs" value="jbossjts.jar"/>
+
+    <path id="tests.lib.path">
+        <fileset dir="${tests.libs.dir}" includes="${tests.libs}"/>
+    </path>
+
+    <path id="jboss.lib.path">
+        <fileset dir="${jboss.libs.dir}" includes="${jboss.libs}"/>
+    </path>
+
+    <path id="jboss.server.lib.path">
+        <fileset dir="${jboss.server.libs.dir}" includes="${jboss.server.libs}"/>
+    </path>
+
+    <property name="javac.debug"             value="on"/>
+    <property name="javac.optimize"          value="off"/>
+
+    <target name="clean">
+        <delete dir="${build.dir}"/>
+        <delete>
+            <fileset dir="." includes="**/*.bak" defaultexcludes="no"/>
+            <fileset dir="." includes="**/*~"    defaultexcludes="no"/>
+            <fileset dir="." includes="**/#*#"   defaultexcludes="no"/>
+            <fileset dir="." includes="**/.#*"   defaultexcludes="no"/>
+        </delete>
+    </target>
+
+    <target name="init" depends="clean,init-dir"/>
+
+    <target name="init-dir">
+        <mkdir dir="${build.dir}"/>
+        <mkdir dir="${build.tests.classes.dir}"/>
+        <mkdir dir="${build.tests.lib.dir}"/>
+        <mkdir dir="${build.tests.dd.dir}"/>
+        <mkdir dir="${build.tests.webapps.dir}"/>
+    </target>
+
+    <target name="compile" depends="init">
+        <javac destdir="${build.tests.classes.dir}" debug="${javac.debug}" optimize="${javac.optimize}">
+            <classpath>
+                <path refid="tests.lib.path"/>
+                <path refid="jboss.lib.path"/>
+                <path refid="jboss.server.lib.path"/>
+            </classpath>
+            <src path="${tests.src.dir}"/>
+        </javac>
+    </target>
+
+    <target name="webapps" depends="compile" >
+        <copy file="${tests.dd.dir}/tests_web-app.xml" tofile="${build.tests.dd.dir}/web-app.xml">
+            <filterset>
+                <filter token="hostname" value="${hostname}"/>
+                <filter token="port" value="${port}"/>
+            </filterset>
+        </copy>
+        <war warfile="${build.tests.webapps.dir}/txtests.war" webxml="${build.tests.dd.dir}/web-app.xml">
+            <manifest>
+                <attribute name="Class-Path" value="${tests.libs}"/>
+            </manifest>
+            <classes dir="${build.tests.classes.dir}"/>
+        </war>
+        <ear earfile="${build.tests.webapps.dir}/txtests.ear" appxml="${tests.dd.dir}/tests_application.xml">
+            <fileset dir="${tests.libs.dir}" includes="${tests.libs}"/>
+            <fileset dir="${build.tests.webapps.dir}" includes="txtests.war"/>
+        </ear>
+    </target>
+
+    <target name="deploy" depends="webapps, undeploy">
+        <copy file="${build.tests.webapps.dir}/txtests.ear" tofile="${deploy.dir}/txtests.ear"/>
+    </target>
+
+    <target name="undeploy">
+        <delete file="${deploy.dir}/txtests.ear"/>
+    </target>
+</project>

Added: labs/jbosstm/workspace/adinn/txtest/dd/tests_application.xml
===================================================================
--- labs/jbosstm/workspace/adinn/txtest/dd/tests_application.xml	                        (rev 0)
+++ labs/jbosstm/workspace/adinn/txtest/dd/tests_application.xml	2008-03-31 16:15:38 UTC (rev 19346)
@@ -0,0 +1,35 @@
+<!--
+  JBoss, Home of Professional Open Source
+  Copyright 2008, Red Hat Middleware LLC, and individual contributors
+  as indicated by the @author tags. 
+  See the copyright.txt in the distribution for a full listing 
+  of individual contributors.
+  This copyrighted material is made available to anyone wishing to use,
+  modify, copy, or redistribute it subject to the terms and conditions
+  of the GNU General Public License, v. 2.0.
+  This program is distributed in the hope that it will be useful, but WITHOUT A 
+  WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A 
+  PARTICULAR PURPOSE.  See the GNU General Public License for more details.
+  You should have received a copy of the GNU General Public License,
+  v. 2.0 along with this distribution; if not, write to the Free Software
+  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 
+  MA  02110-1301, USA.
+  
+  (C) 2008,
+  @author JBoss Inc.
+-->
+
+<!DOCTYPE application PUBLIC "-//Sun Microsystems, Inc.//DTD J2EE Application 1.3//EN" "http://java.sun.com/dtd/application_1_3.dtd">
+
+<application>
+    <display-name>JTS Tx Tests</display-name>
+
+    <description>JTS Tx Tests</description>
+
+    <module>
+        <web>
+            <web-uri>txtests.war</web-uri>
+            <context-root>/txtests</context-root>
+        </web>
+    </module>
+</application>

Added: labs/jbosstm/workspace/adinn/txtest/dd/tests_web-app.xml
===================================================================
--- labs/jbosstm/workspace/adinn/txtest/dd/tests_web-app.xml	                        (rev 0)
+++ labs/jbosstm/workspace/adinn/txtest/dd/tests_web-app.xml	2008-03-31 16:15:38 UTC (rev 19346)
@@ -0,0 +1,47 @@
+<!--
+  JBoss, Home of Professional Open Source
+  Copyright 2008, Red Hat Middleware LLC, and individual contributors
+  as indicated by the @author tags. 
+  See the copyright.txt in the distribution for a full listing 
+  of individual contributors.
+  This copyrighted material is made available to anyone wishing to use,
+  modify, copy, or redistribute it subject to the terms and conditions
+  of the GNU General Public License, v. 2.0.
+  This program is distributed in the hope that it will be useful, but WITHOUT A 
+  WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A 
+  PARTICULAR PURPOSE.  See the GNU General Public License for more details.
+  You should have received a copy of the GNU General Public License,
+  v. 2.0 along with this distribution; if not, write to the Free Software
+  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 
+  MA  02110-1301, USA.
+  
+  (C) 2008,
+  @author JBoss Inc.
+-->
+
+<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/j2ee/dtds/web-app_2_3.dtd">
+
+<web-app>
+    <display-name>JTS Tx Tests</display-name>
+
+    <description>JTS Tx Tests Tests</description>
+
+    <servlet>
+        <servlet-name>JUnit Tests Servlet</servlet-name>
+        <servlet-class>org.jboss.jbossts.test.qa.junit.TestRunnerServlet</servlet-class>
+        <init-param>
+            <param-name>TestSuiteClassName</param-name>
+            <param-value>org.jboss.jbossts.test.jts.TxTestSuite</param-value>
+        </init-param>
+        <load-on-startup>4</load-on-startup>
+    </servlet>
+
+    <servlet-mapping>
+        <servlet-name>JUnit Tests Servlet</servlet-name>
+        <url-pattern>/index.html</url-pattern>
+    </servlet-mapping>
+
+    <welcome-file-list>
+        <welcome-file>index.html</welcome-file>
+    </welcome-file-list>
+</web-app>

Added: labs/jbosstm/workspace/adinn/txtest/ext/junit.jar
===================================================================
(Binary files differ)


Property changes on: labs/jbosstm/workspace/adinn/txtest/ext/junit.jar
___________________________________________________________________
Name: svn:mime-type
   + application/octet-stream

Added: labs/jbosstm/workspace/adinn/txtest/src/org/jboss/jbossts/test/jts/TxMultiCreate.java
===================================================================
--- labs/jbosstm/workspace/adinn/txtest/src/org/jboss/jbossts/test/jts/TxMultiCreate.java	                        (rev 0)
+++ labs/jbosstm/workspace/adinn/txtest/src/org/jboss/jbossts/test/jts/TxMultiCreate.java	2008-03-31 16:15:38 UTC (rev 19346)
@@ -0,0 +1,150 @@
+package org.jboss.jbossts.test.jts;
+
+import junit.framework.TestCase;
+
+import javax.transaction.UserTransaction;
+import javax.transaction.TransactionManager;
+import javax.naming.InitialContext;
+import javax.naming.NamingException;
+import java.io.Writer;
+import java.io.PrintStream;
+import java.io.PrintWriter;
+
+import com.arjuna.ats.arjuna.coordinator.TransactionReaper;
+
+/**
+ * Test JTS handling of multiple transaction start and commit/rollback requests
+ * User: adinn
+ * Date: Mar 31, 2008
+ * Time: 11:25:13 AM
+ * 
+ */
+public class TxMultiCreate extends TestCase
+{
+
+    /******* public API **************************************************************************************/
+
+    protected void setUp() throws Exception
+    {
+        System.out.println("set up");
+    }
+    protected void tearDown() throws Exception
+    {
+        System.out.println("tear down");
+    }
+    
+    /**
+     * test transaction creation and clean up by creating lots of threads each of which starts lots of parallel
+     * transactions none of which do any actual work
+     */
+    public void testTxMultiCreate()
+            throws Exception
+    {
+        String jndiName =  "java:TransactionManager";
+        try {
+            InitialContext ic = new InitialContext();
+            txManager = (TransactionManager) ic.lookup(jndiName);
+        } catch (NamingException ex) {
+            System.out.println("Could not get transaction manager : " + ex);
+            fail("Could not get transaction manager : " + ex);
+        }
+
+        Thread[] threads = new Thread[numThreads];
+        boolean[] ok = new boolean[numThreads];
+        boolean allOk = true;
+        int i;
+
+        for (i = 0; i < numThreads; i++) {
+            threads[i] = new TxMultiCreate.TxThread(this, i, ok);
+        }
+
+        long timeStart = System.currentTimeMillis();
+
+        for (i = 0; i < numThreads; i++) {
+            threads[i].start();
+        }
+
+        for (i = 0; i < numThreads;) {
+            try {
+                threads[i].join();
+            } catch (InterruptedException ie) {
+                continue;
+            }
+            allOk |= ok[i];
+            i++;
+        }
+
+        long timeFinish = System.currentTimeMillis();
+
+        if (allOk) {
+            System.out.format("Execution of %d transactions in %d threads took %f msecs\n",
+                    numTxs, numThreads, ((timeFinish - timeStart) / 1000.0));
+        } else {
+            fail("Error in transaction thread");
+        }
+
+        // make sure the transaction reaper has is not hanging on to any of the transactions
+        
+        assertTrue(TransactionReaper.transactionReaper().numberOfTransactions() != 0);
+    }
+
+    /******* private implementation **************************************************************************/
+
+    /**
+     *  The transaction manager obtained from JNDI
+     */
+    private TransactionManager txManager;
+    /**
+     * default for numThreads
+     */
+    private final int NUM_THREADS_DEFAULT = 16;
+    /**
+     * default for numTxs
+     */
+    private final int NUM_TXS_DEFAULT = 1024;
+    /**
+     * how many threads we should spawn to create transactions in parallel
+     */
+    private int numThreads = NUM_THREADS_DEFAULT;
+    /**
+     * how many transactions each thread should create
+     */
+    private int numTxs = NUM_TXS_DEFAULT;
+
+    private static class TxThread extends Thread
+
+    {
+        TxMultiCreate txMultiCreate;
+        boolean[] ok;
+        int index;
+
+        public TxThread(TxMultiCreate txMultiCreate, int index, boolean[] ok)
+        {
+            this.txMultiCreate = txMultiCreate;
+            this.index = index;
+            this.ok = ok;
+        }
+
+        public void run()
+        {
+            TransactionManager tm = txMultiCreate.txManager;
+            int max = txMultiCreate.numTxs;
+            int mod = txMultiCreate.numThreads;
+            int i;
+
+            for (i = 0; i < max; i++) {
+                try {
+                    tm.begin();
+                    // rollback every now and then
+                    if (i % mod == index) {
+                        tm.rollback();
+                    } else {
+                        tm.commit();
+                    }
+                } catch (Exception e) {
+                    System.out.println("Thread[" + i + "] : exception : " + e);
+                }
+            }
+        }
+    }
+}

Added: labs/jbosstm/workspace/adinn/txtest/src/org/jboss/jbossts/test/jts/TxTestSuite.java
===================================================================
--- labs/jbosstm/workspace/adinn/txtest/src/org/jboss/jbossts/test/jts/TxTestSuite.java	                        (rev 0)
+++ labs/jbosstm/workspace/adinn/txtest/src/org/jboss/jbossts/test/jts/TxTestSuite.java	2008-03-31 16:15:38 UTC (rev 19346)
@@ -0,0 +1,17 @@
+package org.jboss.jbossts.test.jts;
+
+/**
+ * Created by IntelliJ IDEA.
+ * User: adinn
+ * Date: Mar 31, 2008
+ * Time: 1:54:09 PM
+ * To change this template use File | Settings | File Templates.
+ */
+public class TxTestSuite extends junit.framework.TestSuite
+{
+    public TxTestSuite()
+    {
+        // wsas basic tests
+        addTest(new junit.framework.TestSuite(org.jboss.jbossts.test.jts.TxMultiCreate.class));
+    }
+}

Added: labs/jbosstm/workspace/adinn/txtest/src/org/jboss/jbossts/test/qa/junit/TestRunnerServlet.java
===================================================================
--- labs/jbosstm/workspace/adinn/txtest/src/org/jboss/jbossts/test/qa/junit/TestRunnerServlet.java	                        (rev 0)
+++ labs/jbosstm/workspace/adinn/txtest/src/org/jboss/jbossts/test/qa/junit/TestRunnerServlet.java	2008-03-31 16:15:38 UTC (rev 19346)
@@ -0,0 +1,607 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2006, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. 
+ * See the copyright.txt in the distribution for a full listing 
+ * of individual contributors.
+ * This copyrighted material is made available to anyone wishing to use,
+ * modify, copy, or redistribute it subject to the terms and conditions
+ * of the GNU General Public License, v. 2.0.
+ * This program is distributed in the hope that it will be useful, but WITHOUT A 
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A 
+ * PARTICULAR PURPOSE.  See the GNU General Public License for more details.
+ * You should have received a copy of the GNU General Public License,
+ * v. 2.0 along with this distribution; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 
+ * MA  02110-1301, USA.
+ * 
+ * (C) 2005-2006,
+ * @author JBoss Inc.
+ */
+/*
+ * Copyright (c) 2002, 2003, Arjuna Technologies Limited.
+ *
+ * WSTXTestRunnerServlet.java
+ */
+
+package org.jboss.jbossts.test.qa.junit;
+
+import java.io.*;
+import java.util.*;
+import javax.servlet.*;
+import javax.servlet.http.*;
+import junit.framework.*;
+
+public class TestRunnerServlet extends HttpServlet
+{
+    public void init(ServletConfig config) throws ServletException
+    {
+        super.init(config);
+
+        _testSuiteClassName = config.getInitParameter("TestSuiteClassName");
+    }
+
+    public void doGet(HttpServletRequest request, HttpServletResponse response)
+        throws ServletException, IOException
+    {
+        try
+        {
+            PrintWriter writer = response.getWriter();
+
+            response.setContentType("text/html");
+            response.setHeader("Cache-Control", "no-cache");
+
+            if (request.getParameter("failednumber") != null)
+                doStatusFailed(writer, request, response);
+            else if (request.getParameter("errornumber") != null)
+                doStatusError(writer, request, response);
+            else
+                doStatus(writer, request, response);
+        }
+        catch (Exception exception)
+        {
+            log("Test Runner: doGet failed", exception);
+
+            response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, exception.toString());
+        }
+        catch (Error error)
+        {
+            log("Test Runner: doGet failed", error);
+
+            response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, error.toString());
+        }
+    }
+
+    public void doPost(HttpServletRequest request, HttpServletResponse response)
+        throws ServletException, IOException
+    {
+        try
+        {
+            PrintWriter writer = response.getWriter();
+
+            response.setContentType("text/html");
+            response.setHeader("Cache-Control", "no-cache");
+
+	    if ((_runnerThread == null) || (! _runnerThread.isAlive()))
+	    {
+                _runnerThread = new RunnerThread();
+                _runnerThread.start();
+            }
+
+            if (request.getParameter("failednumber") != null)
+                doStatusFailed(writer, request, response);
+            else if (request.getParameter("errornumber") != null)
+                doStatusError(writer, request, response);
+            else
+                doStatus(writer, request, response);
+        }
+        catch (Exception exception)
+        {
+            log("Test Runner: doPost failed", exception);
+
+            response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, exception.toString());
+        }
+        catch (Error error)
+        {
+            log("Test Runner: doPost failed", error);
+
+            response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, error.toString());
+        }
+    }
+
+    public void doStatus(PrintWriter writer, HttpServletRequest request, HttpServletResponse response)
+        throws ServletException
+    {
+        writer.println("<HTML>");
+        writer.println("<HEAD>");
+        writer.println("<TITLE>Test Runner</TITLE>");
+        writer.println("</HEAD>");
+        writer.println("<BODY bgcolor=\"white\" style=\"font-family: Arial, Helvetica, sans-serif\">");
+        writer.println("<DIV style=\"font-family: Arial, Helvetica, sans-serif; font-size: large\">&nbsp;<BR>Test Runner: Status<BR>&nbsp;</DIV>");
+
+        writer.println("<TABLE width=\"100%\">");
+
+        writer.println("<TR><TD colspan=\"3\" align=\"center\" style=\"font-family: Arial, Helvetica, sans-serif; font-weight: bold\">Status</TD></TR>");
+        writer.println("<TR>");
+        writer.println("<TD style=\"font-family: Arial, Helvetica, sans-serif; font-weight: bold\">Test suite class:</TD>");
+        writer.println("<TD style=\"font-family: Arial, Helvetica, sans-serif\">" + _testSuiteClassName + "</TD>");
+        writer.println("</TR>");
+
+        if ((_runnerThread == null) || (! _runnerThread.isAlive()))
+        {
+            writer.println("<TR>");
+            writer.println("<TD style=\"font-family: Arial, Helvetica, sans-serif; font-weight: bold\">Action:</TD>");
+            writer.print("<TD style=\"font-family: Arial, Helvetica, sans-serif\">");
+            writer.print("<FORM method=\"POST\" action=\"" + request.getRequestURL() + "\">");
+            writer.print("<INPUT type=\"button\" value=\"run\" onclick=\"this.form.submit()\">");
+            writer.print("</FORM>");
+            writer.println("</TD>");
+            writer.println("</TR>");
+        }
+        else
+        {
+            writer.println("<TR>");
+            writer.println("<TD style=\"font-family: Arial, Helvetica, sans-serif; font-weight: bold\">Action:</TD>");
+            writer.print("<TD style=\"font-family: Arial, Helvetica, sans-serif\">");
+            writer.print("<FORM method=\"GET\" action=\"" + request.getRequestURL() + "\">");
+            writer.print("<INPUT type=\"button\" value=\"reload\" onclick=\"this.form.submit()\">");
+            writer.print("</FORM>");
+            writer.println("</TD>");
+            writer.println("</TR>");
+
+            if (_currentTest != null)
+	    {
+                writer.println("<TR>");
+                writer.println("<TD style=\"font-family: Arial, Helvetica, sans-serif; font-weight: bold\">Current test:</TD>");
+                writer.print("<TD style=\"font-family: Arial, Helvetica, sans-serif\">");
+                encode(writer, _currentTest.toString());
+                writer.println("</TD>");
+                writer.println("</TR>");
+            }
+        }
+        writer.println("</TR>");
+        writer.println("<TR>");
+        writer.println("<TD style=\"font-family: Arial, Helvetica, sans-serif; font-weight: bold\">Passed count:</TD>");
+        writer.println("<TD style=\"font-family: Arial, Helvetica, sans-serif\">" + _passedTests.size() + "</TD>");
+        writer.println("</TR>");
+        writer.println("<TR>");
+        writer.println("<TD style=\"font-family: Arial, Helvetica, sans-serif; font-weight: bold\">Failed count:</TD>");
+        writer.println("<TD style=\"font-family: Arial, Helvetica, sans-serif\">" + _failedTests.size() + "</TD>");
+        writer.println("</TR>");
+        writer.println("<TR>");
+        writer.println("<TD style=\"font-family: Arial, Helvetica, sans-serif; font-weight: bold\">Error count:</TD>");
+        writer.println("<TD style=\"font-family: Arial, Helvetica, sans-serif\">" + _errorTests.size() + "</TD>");
+        writer.println("</TR>");
+
+        writer.println("</TABLE>");
+
+        if (! _passedTests.isEmpty())
+        {
+            writer.println("<BR>");
+
+            writer.println("<TABLE width=\"100%\">");
+
+            writer.println("<TR><TD colspan=\"3\" align=\"center\" style=\"font-family: Arial, Helvetica, sans-serif; font-weight: bold\">Passes</TD></TR>");
+
+            writer.println("<TR>");
+            writer.println("<TD align=\"center\" style=\"font-family: Arial, Helvetica, sans-serif; font-weight: bold\">Test</TD>");
+            writer.println("<TD align=\"center\" style=\"font-family: Arial, Helvetica, sans-serif; font-weight: bold\">Duration</TD>");
+            writer.println("</TR>");
+
+            Iterator passedTestsIterator = _passedTests.iterator();
+            while (passedTestsIterator.hasNext())
+            {
+                PassedTest passedTest = (PassedTest) passedTestsIterator.next();
+                writer.println("<TR>");
+                writer.print("<TD style=\"font-family: Arial, Helvetica, sans-serif\">");
+                encode(writer, passedTest.test.toString());
+                writer.println("</TD>");
+                writer.print("<TD align=\"center\" style=\"font-family: Arial, Helvetica, sans-serif\">" + passedTest.duration + " ms</TD>");
+                writer.println("</TR>");
+            }
+
+            writer.println("</TABLE>");
+        }
+
+        if (! _failedTests.isEmpty())
+        {
+            writer.println("<BR>");
+
+            writer.println("<TABLE width=\"100%\">");
+
+            writer.println("<TR><TD colspan=\"3\" align=\"center\" style=\"font-family: Arial, Helvetica, sans-serif; font-weight: bold\">Failures</TD></TR>");
+            writer.println("<TR>");
+            writer.println("<TD align=\"center\" style=\"font-family: Arial, Helvetica, sans-serif; font-weight: bold\">Test</TD>");
+            writer.println("<TD align=\"center\" style=\"font-family: Arial, Helvetica, sans-serif; font-weight: bold\">Duration</TD>");
+            writer.println("<TD align=\"center\" style=\"font-family: Arial, Helvetica, sans-serif; font-weight: bold\">Message</TD>");
+            writer.println("</TR>");
+
+            int failedNumber = 0;
+            Iterator failedTestsIterator = _failedTests.iterator();
+            while (failedTestsIterator.hasNext())
+            {
+                FailedTest failedTest = (FailedTest) failedTestsIterator.next();
+                writer.println("<TR>");
+                writer.print("<TD style=\"font-family: Arial, Helvetica, sans-serif\">");
+                writer.print("<A href=\"" + request.getRequestURL() + "?failednumber=" + failedNumber + "\">");
+                encode(writer, failedTest.test.toString());
+                writer.print("</A>");
+                writer.println("</TD>");
+                writer.print("<TD align=\"center\" style=\"font-family: Arial, Helvetica, sans-serif\">" + failedTest.duration + " ms</TD>");
+                writer.print("<TD style=\"font-family: Arial, Helvetica, sans-serif\">");
+                encode(writer, failedTest.assertionFailedError.getMessage());
+                writer.println("</TD>");
+                writer.println("</TR>");
+                failedNumber++;
+            }
+
+            writer.println("</TABLE>");
+
+            writer.println("<BR>");
+        }
+
+        if (! _errorTests.isEmpty())
+        {
+            writer.println("<TABLE width=\"100%\">");
+            writer.println("<TR>");
+            writer.println("<TR><TD colspan=\"3\" align=\"center\" style=\"font-family: Arial, Helvetica, sans-serif; font-weight: bold\">Errors</TD></TR>");
+            writer.println("<TD align=\"center\" style=\"font-family: Arial, Helvetica, sans-serif; font-weight: bold\">Test</TD>");
+            writer.println("<TD align=\"center\" style=\"font-family: Arial, Helvetica, sans-serif; font-weight: bold\">Duration</TD>");
+            writer.println("<TD align=\"center\" style=\"font-family: Arial, Helvetica, sans-serif; font-weight: bold\">Exception/Error</TD>");
+            writer.println("</TR>");
+
+            int errorNumber = 0;
+            Iterator errorTestsIterator = _errorTests.iterator();
+            while (errorTestsIterator.hasNext())
+            {
+                ErrorTest errorTest = (ErrorTest) errorTestsIterator.next();
+                writer.println("<TR>");
+                writer.print("<TD style=\"font-family: Arial, Helvetica, sans-serif\">");
+                writer.print("<A href=\"" + request.getRequestURL() + "?errornumber=" + errorNumber + "\">");
+                encode(writer, errorTest.test.toString());
+                writer.print("</A>");
+                writer.println("</TD>");
+                writer.print("<TD align=\"center\" style=\"font-family: Arial, Helvetica, sans-serif\">" + errorTest.duration + " ms</TD>");
+                writer.print("<TD style=\"font-family: Arial, Helvetica, sans-serif\">");
+                encode(writer, errorTest.throwable.toString());
+                writer.println("</TD>");
+                writer.println("</TR>");
+                errorNumber++;
+            }
+
+            writer.println("</TABLE>");
+        }
+
+        writer.println("</BODY>");
+        writer.println("</HTML>");
+    }
+
+    public void doStatusFailed(PrintWriter writer, HttpServletRequest request, HttpServletResponse response)
+        throws ServletException
+    {
+        int        failedIndex = 0;
+        FailedTest failedTest  = null;
+
+        try
+        {
+            String failedIndexString = (String) request.getParameter("failednumber");
+
+            failedIndex = Integer.parseInt(failedIndexString);
+            failedTest  = (FailedTest) _failedTests.get(failedIndex);
+        }
+        catch (Exception exception)
+        {
+            failedTest = null;
+        }
+
+        if (failedTest != null)
+        {
+            writer.println("<HTML>");
+            writer.println("<HEAD>");
+            writer.println("<TITLE>Test Runner</TITLE>");
+            writer.println("</HEAD>");
+            writer.println("<BODY bgcolor=\"white\" style=\"font-family: Arial, Helvetica, sans-serif\">");
+            writer.println("<DIV style=\"font-family: Arial, Helvetica, sans-serif; font-size: large\">&nbsp;<BR>Test Runner: Failed Status<BR>&nbsp;</DIV>");
+
+            writer.println("<TABLE>");
+
+            writer.print("<TR>");
+            writer.print("<TD style=\"font-family: Arial, Helvetica, sans-serif; font-weight: bold\">Test:</TD>");
+            writer.print("<TD style=\"font-family: Arial, Helvetica, sans-serif\">");
+            encode(writer, failedTest.test.toString());
+            writer.println("</TD>");
+            writer.println("</TR>");
+
+            writer.println("<TR>");
+            writer.print("<TD style=\"font-family: Arial, Helvetica, sans-serif; font-weight: bold\">Duration:</TD>");
+            writer.print("<TD style=\"font-family: Arial, Helvetica, sans-serif\">" + failedTest.duration + " ms</TD>");
+            writer.println("</TR>");
+
+            writer.println("<TR>");
+            writer.print("<TD style=\"font-family: Arial, Helvetica, sans-serif; font-weight: bold\">Message:</TD>");
+            writer.print("<TD style=\"font-family: Arial, Helvetica, sans-serif\">");
+            encode(writer, failedTest.assertionFailedError.getMessage());
+            writer.println("</TD>");
+            writer.println("</TR>");
+
+            writer.print("<TR>");
+            writer.print("<TD colspan=\"2\" style=\"font-family: Arial, Helvetica, sans-serif; font-weight: bold\">Stack trace:</TD>");
+            writer.println("</TR>");
+            writer.println("<TR>");
+            writer.println("<TD colspan=\"2\" style=\"font-family: Arial, Helvetica, sans-serif\">");
+            writer.println("<PRE>");
+            CharArrayWriter charArrayWriter = new CharArrayWriter();
+            PrintWriter     printWriter     = new PrintWriter(charArrayWriter, true);
+            failedTest.assertionFailedError.printStackTrace(printWriter);
+            printWriter.close();
+            charArrayWriter.close();
+            encode(writer, charArrayWriter.toString());
+            writer.println("</PRE>");
+            writer.println("</TD>");
+            writer.println("</TR>");
+
+            writer.println("</TABLE>");
+
+            writer.println("<TABLE width=\"100%\">");
+            writer.println("<TR>");
+            writer.println("<TD align=\"left\" width=\"33%\" style=\"font-family: Arial, Helvetica, sans-serif\">");
+            if (failedIndex > 0)
+                writer.println("<A href=\"" + request.getRequestURL() + "?failednumber=" + (failedIndex - 1)+ "\">previous</A>");
+            else
+                writer.print("&nbsp;");
+            writer.println("</TD>");
+            writer.println("<TD align=\"center\" width=\"33%\" style=\"font-family: Arial, Helvetica, sans-serif\">");
+            writer.println("<A href=\"" + request.getRequestURL() + "\">all</A>");
+            writer.println("</TD>");
+            writer.println("<TD align=\"right\" width=\"33%\" style=\"font-family: Arial, Helvetica, sans-serif\">");
+            if (failedIndex < (_failedTests.size() - 1))
+                writer.println("<A href=\"" + request.getRequestURL() + "?failednumber=" + (failedIndex + 1)+ "\">next</A>");
+            else
+                writer.print("&nbsp;");
+            writer.println("</TD>");
+            writer.println("</TR>");
+            writer.println("</TABLE>");
+
+            writer.println("</BODY>");
+            writer.println("</HTML>");
+        }
+        else
+            doStatus(writer, request, response);
+    }
+
+    public void doStatusError(PrintWriter writer, HttpServletRequest request, HttpServletResponse response)
+        throws ServletException
+    {
+        int       errorIndex = 0;
+        ErrorTest errorTest  = null;
+
+        try
+        {
+            String errorIndexString = (String) request.getParameter("errornumber");
+
+            errorIndex = Integer.parseInt(errorIndexString);
+            errorTest  = (ErrorTest) _errorTests.get(errorIndex);
+        }
+        catch (Exception exception)
+        {
+            errorTest = null;
+        }
+
+        if (errorTest != null)
+        {
+            writer.println("<HTML>");
+            writer.println("<HEAD>");
+            writer.println("<TITLE>Test Runner</TITLE>");
+            writer.println("</HEAD>");
+            writer.println("<BODY bgcolor=\"white\" style=\"font-family: Arial, Helvetica, sans-serif\">");
+            writer.println("<DIV style=\"font-family: Arial, Helvetica, sans-serif; font-size: large\">&nbsp;<BR>Test Runner: Error Status<BR>&nbsp;</DIV>");
+
+            writer.println("<TABLE>");
+
+            writer.print("<TR>");
+            writer.print("<TD style=\"font-family: Arial, Helvetica, sans-serif; font-weight: bold\">Test:</TD>");
+            writer.print("<TD style=\"font-family: Arial, Helvetica, sans-serif\">");
+            encode(writer, errorTest.test.toString());
+            writer.println("</TD>");
+            writer.println("</TR>");
+
+            writer.println("<TR>");
+            writer.print("<TD style=\"font-family: Arial, Helvetica, sans-serif; font-weight: bold\">Duration:</TD>");
+            writer.print("<TD style=\"font-family: Arial, Helvetica, sans-serif\">" + errorTest.duration + " ms</TD>");
+            writer.println("</TR>");
+
+            writer.println("<TR>");
+            writer.print("<TD style=\"font-family: Arial, Helvetica, sans-serif; font-weight: bold\">Exception/error:</TD>");
+            writer.print("<TD style=\"font-family: Arial, Helvetica, sans-serif\">");
+            encode(writer, errorTest.throwable.toString());
+            writer.println("</TD>");
+            writer.println("</TR>");
+
+            writer.print("<TR>");
+            writer.print("<TD colspan=\"2\" style=\"font-family: Arial, Helvetica, sans-serif; font-weight: bold\">Stack trace:</TD>");
+            writer.println("</TR>");
+            writer.println("<TR>");
+            writer.println("<TD colspan=\"2\" style=\"font-family: Arial, Helvetica, sans-serif\">");
+            writer.println("<PRE>");
+            CharArrayWriter charArrayWriter = new CharArrayWriter();
+            PrintWriter     printWriter     = new PrintWriter(charArrayWriter, true);
+            errorTest.throwable.printStackTrace(printWriter);
+            printWriter.close();
+            charArrayWriter.close();
+            encode(writer, charArrayWriter.toString());
+            writer.println("</PRE>");
+            writer.println("</TD>");
+            writer.println("</TR>");
+
+            writer.println("</TABLE>");
+
+            writer.println("<TABLE width=\"100%\">");
+            writer.println("<TR>");
+            writer.println("<TD align=\"left\" width=\"33%\" style=\"font-family: Arial, Helvetica, sans-serif\">");
+            if (errorIndex > 0)
+                writer.println("<A href=\"" + request.getRequestURL() + "?errornumber=" + (errorIndex - 1)+ "\">previous</A>");
+            else
+                writer.print("&nbsp;");
+            writer.println("</TD>");
+            writer.println("<TD align=\"center\" width=\"33%\" style=\"font-family: Arial, Helvetica, sans-serif\">");
+            writer.println("<A href=\"" + request.getRequestURL() + "\">all</A>");
+            writer.println("</TD>");
+            writer.println("<TD align=\"right\" width=\"33%\" style=\"font-family: Arial, Helvetica, sans-serif\">");
+            if (errorIndex < (_errorTests.size() - 1))
+                writer.println("<A href=\"" + request.getRequestURL() + "?errornumber=" + (errorIndex + 1)+ "\">next</A>");
+            else
+                writer.print("&nbsp;");
+            writer.println("</TD>");
+            writer.println("</TR>");
+            writer.println("</TABLE>");
+
+            writer.println("</BODY>");
+            writer.println("</HTML>");
+        }
+        else
+            doStatus(writer, request, response);
+    }
+
+    private class PassedTest
+    {
+        public Test test;
+        public long duration;
+    }
+
+    private class FailedTest
+    {
+        public Test                 test;
+        public long                 duration;
+        public AssertionFailedError assertionFailedError;
+    }
+
+    private class ErrorTest
+    {
+        public Test      test;
+        public long      duration;
+	public Throwable throwable;
+    }
+
+    private class RunnerThread extends Thread
+    {
+        public void run()
+        {
+            try
+            {
+                _passedTests.clear();
+                _failedTests.clear();
+                _errorTests.clear();
+
+                Class        testSuiteClass = Class.forName(_testSuiteClassName);
+                TestListener testListener   = new BasicTestListener();
+
+                _testResult = new TestResult();
+                _testSuite  = (TestSuite) testSuiteClass.newInstance();
+
+                _testResult.addListener(testListener);
+                _testSuite.run(_testResult);
+                _testResult.removeListener(testListener);
+            }
+            catch (Exception exception)
+            {
+                log("Runner Thread: run failed", exception);
+            }
+            catch (Error error)
+            {
+                log("Runner Thread: run failed", error);
+            }
+        }
+    }
+
+    private class BasicTestListener implements TestListener
+    {
+        public void startTest(Test test)
+        {
+            _startTime            = System.currentTimeMillis();
+            _failed               = false;
+            _error                = false;
+            _assertionFailedError = null;
+            _throwable            = null;
+            _currentTest          = test;
+        }
+
+        public void addError(Test test, Throwable throwable)
+        {
+            _error     = true;
+            _throwable = throwable;
+            throwable.printStackTrace(System.out);
+        }
+
+        public void addFailure(Test test, AssertionFailedError assertionFailedError)
+        {
+            _failed               = true;
+            _assertionFailedError = assertionFailedError;
+            assertionFailedError.printStackTrace(System.out);
+        }
+
+        public void endTest(Test test)
+        {
+            if (_failed)
+            {
+                FailedTest failedTest           = new FailedTest();
+                failedTest.test                 = test;
+                failedTest.duration             = System.currentTimeMillis() - _startTime;
+                failedTest.assertionFailedError = _assertionFailedError;
+                _failedTests.add(failedTest);
+	    }
+	    else if (_error)
+            {
+                ErrorTest errorTest = new ErrorTest();
+                errorTest.test      = test;
+                errorTest.duration  = System.currentTimeMillis() - _startTime;
+                errorTest.throwable = _throwable;
+                _errorTests.add(errorTest);
+	    }
+	    else
+            {
+                PassedTest passedTest = new PassedTest();
+                passedTest.test       = test;
+                passedTest.duration   = System.currentTimeMillis() - _startTime;
+                _passedTests.add(passedTest);
+	    }
+
+            _currentTest = null;
+        }
+
+        private long                 _startTime            = 0;
+        private boolean              _failed               = false;
+        private boolean              _error                = false;
+        private AssertionFailedError _assertionFailedError = null;
+        private Throwable            _throwable            = null;
+    }
+
+    private static void encode(PrintWriter writer, String string)
+    {
+        if (string != null)
+        {
+            char[] chars = string.toCharArray();
+
+            for (int index = 0; index < chars.length; index++)
+                if (chars[index] == '<')
+                    writer.print("&lt;");
+                else if (chars[index] == '>')
+                    writer.print("&gt;");
+                else if (chars[index] == '&')
+                    writer.print("&amp;");
+                else
+                    writer.print(chars[index]);
+        }
+        else
+            writer.print("null");
+    }
+
+    private List         _passedTests        = new LinkedList();
+    private List         _failedTests        = new LinkedList();
+    private List         _errorTests         = new LinkedList();
+    private Test         _currentTest        = null;
+    private String       _testSuiteClassName = null;
+    private RunnerThread _runnerThread       = null;
+    private TestResult   _testResult         = null;
+    private TestSuite    _testSuite          = null;
+
+}
\ No newline at end of file




More information about the jboss-svn-commits mailing list