Search in sources :

Example 1 with ExecuteWatchdog

use of org.apache.tools.ant.taskdefs.ExecuteWatchdog in project ant by apache.

the class JUnitTask method execute.

/**
 * Execute a list of tests in a single forked Java VM.
 * @param testList the list of tests to execute.
 * @param thread Identifies which thread is test running in (0 for single-threaded runs)
 * @throws BuildException on error.
 */
protected void execute(final List<JUnitTest> testList, final int thread) throws BuildException {
    // Create a temporary file to pass the test cases to run to
    // the runner (one test case per line)
    final File casesFile = createTempPropertiesFile("junittestcases");
    try (BufferedWriter writer = new BufferedWriter(new FileWriter(casesFile))) {
        log("Creating casesfile '" + casesFile.getAbsolutePath() + "' with content: ", Project.MSG_VERBOSE);
        final PrintStream logWriter = new PrintStream(new LogOutputStream(this, Project.MSG_VERBOSE));
        JUnitTest test = null;
        for (JUnitTest t : testList) {
            test = t;
            test.setThread(thread);
            printDual(writer, logWriter, test.getName());
            if (test.getMethods() != null) {
                printDual(writer, logWriter, ":" + test.getMethodsString().replace(',', '+'));
            }
            if (test.getTodir() == null) {
                printDual(writer, logWriter, "," + getProject().resolveFile("."));
            } else {
                printDual(writer, logWriter, "," + test.getTodir());
            }
            if (test.getOutfile() == null) {
                printlnDual(writer, logWriter, "," + "TEST-" + test.getName());
            } else {
                printlnDual(writer, logWriter, "," + test.getOutfile());
            }
        }
        writer.flush();
        // execute the test and get the return code
        final ExecuteWatchdog watchdog = createWatchdog();
        final TestResultHolder result = executeAsForked(test, watchdog, casesFile);
        actOnTestResult(result, test, "Tests");
    } catch (final IOException e) {
        log(e.toString(), Project.MSG_ERR);
        throw new BuildException(e);
    } finally {
        try {
            FILE_UTILS.tryHardToDelete(casesFile);
        } catch (final Exception e) {
            log(e.toString(), Project.MSG_ERR);
        }
    }
}
Also used : PrintStream(java.io.PrintStream) ExecuteWatchdog(org.apache.tools.ant.taskdefs.ExecuteWatchdog) FileWriter(java.io.FileWriter) IOException(java.io.IOException) BuildException(org.apache.tools.ant.BuildException) File(java.io.File) LogOutputStream(org.apache.tools.ant.taskdefs.LogOutputStream) IOException(java.io.IOException) BuildException(org.apache.tools.ant.BuildException) BufferedWriter(java.io.BufferedWriter)

Example 2 with ExecuteWatchdog

use of org.apache.tools.ant.taskdefs.ExecuteWatchdog in project ant by apache.

the class JDependTask method execute.

/**
 * execute the task
 *
 * @exception BuildException if an error occurs
 */
@Override
public void execute() throws BuildException {
    CommandlineJava commandline = new CommandlineJava();
    if ("text".equals(format)) {
        commandline.setClassname("jdepend.textui.JDepend");
    } else if ("xml".equals(format)) {
        commandline.setClassname("jdepend.xmlui.JDepend");
    }
    if (jvm != null) {
        commandline.setVm(jvm);
    }
    if (getSourcespath() == null && getClassespath() == null) {
        throw new BuildException("Missing classespath required argument");
    }
    if (getClassespath() == null) {
        log("sourcespath is deprecated in JDepend >= 2.5 - please convert to classespath");
    }
    // execute the test and get the return code
    int exitValue;
    boolean wasKilled = false;
    if (!getFork()) {
        exitValue = executeInVM(commandline);
    } else {
        ExecuteWatchdog watchdog = createWatchdog();
        exitValue = executeAsForked(commandline, watchdog);
        // null watchdog means no timeout, you'd better not check with null
        if (watchdog != null) {
            wasKilled = watchdog.killedProcess();
        }
    }
    // if there is an error/failure and that it should halt, stop
    // everything otherwise just log a statement
    boolean errorOccurred = exitValue == JDependTask.ERRORS || wasKilled;
    if (errorOccurred) {
        String errorMessage = "JDepend FAILED" + (wasKilled ? " - Timed out" : "");
        if (getHaltonerror()) {
            throw new BuildException(errorMessage, getLocation());
        }
        log(errorMessage, Project.MSG_ERR);
    }
}
Also used : ExecuteWatchdog(org.apache.tools.ant.taskdefs.ExecuteWatchdog) CommandlineJava(org.apache.tools.ant.types.CommandlineJava) BuildException(org.apache.tools.ant.BuildException)

Example 3 with ExecuteWatchdog

use of org.apache.tools.ant.taskdefs.ExecuteWatchdog in project ant by apache.

the class JUnitTask method execute.

/**
 * Run the tests.
 * @param arg one JUnitTest
 * @param thread Identifies which thread is test running in (0 for single-threaded runs)
 * @throws BuildException in case of test failures or errors
 */
protected void execute(final JUnitTest arg, final int thread) throws BuildException {
    validateTestName(arg.getName());
    final JUnitTest test = (JUnitTest) arg.clone();
    test.setThread(thread);
    // @todo should be moved to the test class instead.
    if (test.getTodir() == null) {
        test.setTodir(getProject().resolveFile("."));
    }
    if (test.getOutfile() == null) {
        test.setOutfile("TEST-" + test.getName());
    }
    // execute the test and get the return code
    TestResultHolder result;
    if (test.getFork()) {
        final ExecuteWatchdog watchdog = createWatchdog();
        result = executeAsForked(test, watchdog, null);
    // null watchdog means no timeout, you'd better not check with null
    } else {
        result = executeInVM(test);
    }
    actOnTestResult(result, test, "Test " + test.getName());
}
Also used : ExecuteWatchdog(org.apache.tools.ant.taskdefs.ExecuteWatchdog)

Aggregations

ExecuteWatchdog (org.apache.tools.ant.taskdefs.ExecuteWatchdog)3 BuildException (org.apache.tools.ant.BuildException)2 BufferedWriter (java.io.BufferedWriter)1 File (java.io.File)1 FileWriter (java.io.FileWriter)1 IOException (java.io.IOException)1 PrintStream (java.io.PrintStream)1 LogOutputStream (org.apache.tools.ant.taskdefs.LogOutputStream)1 CommandlineJava (org.apache.tools.ant.types.CommandlineJava)1