All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.arjuna.qa.junit.TestRunnerServlet Maven / Gradle / Ivy

/*
 * 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 Lesser General Public License, v. 2.1.
 * 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 Lesser General Public License for more details.
 * You should have received a copy of the GNU Lesser General Public License,
 * v.2.1 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.
 *
 * TestRunnerServlet.java
 */

package com.arjuna.qa.junit;

import junit.framework.AssertionFailedError;
import junit.framework.TestResult;
import org.junit.runner.Description;
import org.junit.runner.JUnitCore;
import org.junit.runner.Result;
import org.junit.runner.notification.Failure;
import org.junit.runner.notification.RunListener;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.CharArrayWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

public class TestRunnerServlet extends HttpServlet
{
    public void init(ServletConfig config) throws ServletException
    {
        super.init(config);

        _testSuiteClassName = config.getInitParameter("TestSuiteClassName");
    }

    protected String getContentType()
    {
        return "text/html";
    }

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException
    {
        try
        {
            PrintWriter writer = response.getWriter();

            response.setContentType(getContentType());
            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(getContentType());
            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("");
        writer.println("");
        writer.println("Test Runner");
        writer.println("");
        writer.println("");
        writer.println("
 
Test Runner: Status
 
"); writer.println(""); writer.println(""); writer.println(""); writer.println(""); writer.println(""); writer.println(""); if ((_runnerThread == null) || (! _runnerThread.isAlive())) { writer.println(""); writer.println(""); writer.print(""); writer.println(""); } else { writer.println(""); writer.println(""); writer.print(""); writer.println(""); if (_currentTest != null) { writer.println(""); writer.println(""); writer.print(""); writer.println(""); } } writer.println(""); writer.println(""); writer.println(""); writer.println(""); writer.println(""); writer.println(""); writer.println(""); writer.println(""); writer.println(""); writer.println(""); writer.println(""); writer.println(""); writer.println(""); writer.println("
Status
Test suite class:" + _testSuiteClassName + "
Action:"); writer.print("
"); writer.print(""); writer.print("
"); writer.println("
Action:"); writer.print("
"); writer.print(""); writer.print("
"); writer.println("
Current test:"); encode(writer, _currentTest.toString()); writer.println("
Passed count:" + _passedTests.size() + "
Failed count:" + _failedTests.size() + "
Error count:" + _errorTests.size() + "
"); if (! _passedTests.isEmpty()) { writer.println("
"); writer.println(""); writer.println(""); writer.println(""); writer.println(""); writer.println(""); writer.println(""); Iterator passedTestsIterator = _passedTests.iterator(); while (passedTestsIterator.hasNext()) { PassedTest passedTest = (PassedTest) passedTestsIterator.next(); writer.println(""); writer.print(""); writer.print(""); writer.println(""); } writer.println("
Passes
TestDuration
"); encode(writer, passedTest.description.toString()); writer.println("" + passedTest.duration + " ms
"); } if (! _failedTests.isEmpty()) { writer.println("
"); writer.println(""); writer.println(""); writer.println(""); writer.println(""); writer.println(""); writer.println(""); writer.println(""); int failedNumber = 0; Iterator failedTestsIterator = _failedTests.iterator(); while (failedTestsIterator.hasNext()) { FailedTest failedTest = (FailedTest) failedTestsIterator.next(); writer.println(""); writer.print(""); writer.print(""); writer.print(""); writer.println(""); failedNumber++; } writer.println("
Failures
TestDurationMessage
"); writer.print(""); encode(writer, failedTest.description.toString()); writer.print(""); writer.println("" + failedTest.duration + " ms"); encode(writer, failedTest.assertionFailedError.getMessage()); writer.println("
"); writer.println("
"); } if (! _errorTests.isEmpty()) { writer.println(""); writer.println(""); writer.println(""); writer.println(""); writer.println(""); writer.println(""); writer.println(""); int errorNumber = 0; Iterator errorTestsIterator = _errorTests.iterator(); while (errorTestsIterator.hasNext()) { ErrorTest errorTest = (ErrorTest) errorTestsIterator.next(); writer.println(""); writer.print(""); writer.print(""); writer.print(""); writer.println(""); errorNumber++; } writer.println("
Errors
TestDurationException/Error
"); writer.print(""); encode(writer, errorTest.description.toString()); writer.print(""); writer.println("" + errorTest.duration + " ms"); encode(writer, errorTest.throwable.toString()); writer.println("
"); } writer.println(""); writer.println(""); } 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(""); writer.println(""); writer.println("Test Runner"); writer.println(""); writer.println(""); writer.println("
 
Test Runner: Failed Status
 
"); writer.println(""); writer.print(""); writer.print(""); writer.print(""); writer.println(""); writer.println(""); writer.print(""); writer.print(""); writer.println(""); writer.println(""); writer.print(""); writer.print(""); writer.println(""); writer.print(""); writer.print(""); writer.println(""); writer.println(""); writer.println(""); writer.println(""); writer.println("
Test:"); encode(writer, failedTest.description.toString()); writer.println("
Duration:" + failedTest.duration + " ms
Message:"); encode(writer, failedTest.assertionFailedError.getMessage()); writer.println("
Stack trace:
"); writer.println("
");
            CharArrayWriter charArrayWriter = new CharArrayWriter();
            PrintWriter     printWriter     = new PrintWriter(charArrayWriter, true);
            failedTest.assertionFailedError.printStackTrace(printWriter);
            printWriter.close();
            charArrayWriter.close();
            encode(writer, charArrayWriter.toString());
            writer.println("
"); writer.println("
"); writer.println(""); writer.println(""); writer.println(""); writer.println(""); writer.println(""); writer.println(""); writer.println("
"); if (failedIndex > 0) writer.println("previous"); else writer.print(" "); writer.println(""); writer.println("all"); writer.println(""); if (failedIndex < (_failedTests.size() - 1)) writer.println("next"); else writer.print(" "); writer.println("
"); writer.println(""); writer.println(""); } 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(""); writer.println(""); writer.println("Test Runner"); writer.println(""); writer.println(""); writer.println("
 
Test Runner: Error Status
 
"); writer.println(""); writer.print(""); writer.print(""); writer.print(""); writer.println(""); writer.println(""); writer.print(""); writer.print(""); writer.println(""); writer.println(""); writer.print(""); writer.print(""); writer.println(""); writer.print(""); writer.print(""); writer.println(""); writer.println(""); writer.println(""); writer.println(""); writer.println("
Test:"); encode(writer, errorTest.description.toString()); writer.println("
Duration:" + errorTest.duration + " ms
Exception/error:"); encode(writer, errorTest.throwable.toString()); writer.println("
Stack trace:
"); writer.println("
");
            CharArrayWriter charArrayWriter = new CharArrayWriter();
            PrintWriter     printWriter     = new PrintWriter(charArrayWriter, true);
            errorTest.throwable.printStackTrace(printWriter);
            printWriter.close();
            charArrayWriter.close();
            encode(writer, charArrayWriter.toString());
            writer.println("
"); writer.println("
"); writer.println(""); writer.println(""); writer.println(""); writer.println(""); writer.println(""); writer.println(""); writer.println("
"); if (errorIndex > 0) writer.println("previous"); else writer.print(" "); writer.println(""); writer.println("all"); writer.println(""); if (errorIndex < (_errorTests.size() - 1)) writer.println("next"); else writer.print(" "); writer.println("
"); writer.println(""); writer.println(""); } else doStatus(writer, request, response); } protected class PassedTest { public Description description; public long duration; } protected class FailedTest { public Description description; public long duration; public AssertionFailedError assertionFailedError; } protected class ErrorTest { public Description description; public long duration; public Throwable throwable; } protected class RunnerThread extends Thread { RunnerThread() { super("JUnit Runner Thread") ; } public void run() { try { _passedTests.clear(); _failedTests.clear(); _errorTests.clear(); Class testSuiteClass = Class.forName(_testSuiteClassName); org.junit.runner.notification.RunListener testListener = new BasicTestListener(); JUnitCore core = new JUnitCore(); core.addListener(testListener); core.run(testSuiteClass); } catch (Exception exception) { log("Runner Thread: run failed", exception); } catch (Error error) { log("Runner Thread: run failed", error); } } } private class BasicTestListener extends RunListener { @Override public void testRunStarted(Description description) throws Exception { super.testRunStarted(description); _running = true; } @Override public void testRunFinished(Result result) throws Exception { super.testRunFinished(result); _running = false; } @Override public void testStarted(Description description) throws Exception { super.testStarted(description); _startTime = System.currentTimeMillis(); _currentTest = description; } @Override public void testFinished(Description description) throws Exception { super.testFinished(description); if (!_fail) { PassedTest passedTest = new PassedTest(); passedTest.description = description; passedTest.duration = System.currentTimeMillis() - _startTime; _passedTests.add(passedTest); _currentTest = null; } } @Override public void testFailure(Failure failure) throws Exception { super.testFailure(failure); _fail = true; Throwable _throwable = failure.getException(); _throwable.printStackTrace(System.out); if (_throwable instanceof AssertionFailedError) { FailedTest failedTest = new FailedTest(); failedTest.description = failure.getDescription(); failedTest.duration = System.currentTimeMillis() - _startTime; failedTest.assertionFailedError = (AssertionFailedError)_throwable; _failedTests.add(failedTest); } else { ErrorTest errorTest = new ErrorTest(); errorTest.description = failure.getDescription(); errorTest.duration = System.currentTimeMillis() - _startTime; errorTest.throwable = _throwable; _errorTests.add(errorTest); } _currentTest = null; } @Override public void testAssumptionFailure(Failure failure) { super.testAssumptionFailure(failure); _fail=true; Throwable _throwable = failure.getException(); _throwable.printStackTrace(System.out); ErrorTest errorTest = new ErrorTest(); errorTest.description = failure.getDescription(); errorTest.duration = System.currentTimeMillis() - _startTime; errorTest.throwable = _throwable; _errorTests.add(errorTest); _currentTest = null; } @Override public void testIgnored(Description description) throws Exception { super.testIgnored(description); _currentTest = null; } private long _startTime = 0; private boolean _fail = false; } protected 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("<"); else if (chars[index] == '>') writer.print(">"); else if (chars[index] == '&') writer.print("&"); else writer.print(chars[index]); } else writer.print("null"); } protected List _passedTests = new LinkedList(); protected List _failedTests = new LinkedList(); protected List _errorTests = new LinkedList(); protected Description _currentTest = null; protected String _testSuiteClassName = null; protected RunnerThread _runnerThread = null; protected TestResult _testResult = null; protected boolean _running = false; }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy