FileDocCategorySizeDatePackage
PlainJUnitResultFormatter.javaAPI DocApache Ant 1.707400Wed Dec 13 06:16:22 GMT 2006org.apache.tools.ant.taskdefs.optional.junit

PlainJUnitResultFormatter

public class PlainJUnitResultFormatter extends Object implements JUnitResultFormatter
Prints plain text output of the test to a specified Writer.

Fields Summary
private NumberFormat
nf
Formatter for timings.
private Hashtable
testStarts
Timing helper.
private OutputStream
out
Where to write the log to.
private StringWriter
inner
Helper to store intermediate output.
private PrintWriter
wri
Convenience layer on top of {@link #inner inner}.
private Hashtable
failed
Suppress endTest if testcase failed.
private String
systemOutput
private String
systemError
Constructors Summary
public PlainJUnitResultFormatter()
No arg constructor


        
      
        inner = new StringWriter();
        wri = new PrintWriter(inner);
    
Methods Summary
public voidaddError(junit.framework.Test test, java.lang.Throwable t)
Interface TestListener.

An error occurred while running the test.

param
test the test.
param
t the exception.

        formatError("\tCaused an ERROR", test, t);
    
public voidaddFailure(junit.framework.Test test, junit.framework.AssertionFailedError t)
Interface TestListener for JUnit > 3.4.

A Test failed.

param
test the test.
param
t the assertion that failed.

        addFailure(test, (Throwable) t);
    
public voidaddFailure(junit.framework.Test test, java.lang.Throwable t)
Interface TestListener for JUnit <= 3.4.

A Test failed.

param
test the test.
param
t the exception.

        formatError("\tFAILED", test, t);
    
public voidendTest(junit.framework.Test test)
Interface TestListener.

A Test is finished.

param
test the test.

        if (Boolean.TRUE.equals(failed.get(test))) {
            return;
        }
        synchronized (wri) {
            wri.print("Testcase: "
                      + JUnitVersionHelper.getTestCaseName(test));
            Long l = (Long) testStarts.get(test);
            double seconds = 0;
            // can be null if an error occurred in setUp
            if (l != null) {
                seconds =
                    (System.currentTimeMillis() - l.longValue()) / 1000.0;
            }

            wri.println(" took " + nf.format(seconds) + " sec");
        }
    
public voidendTestSuite(JUnitTest suite)
The whole testsuite ended.

param
suite the test suite
throws
BuildException if unable to write the output

        StringBuffer sb = new StringBuffer("Tests run: ");
        sb.append(suite.runCount());
        sb.append(", Failures: ");
        sb.append(suite.failureCount());
        sb.append(", Errors: ");
        sb.append(suite.errorCount());
        sb.append(", Time elapsed: ");
        sb.append(nf.format(suite.getRunTime() / 1000.0));
        sb.append(" sec");
        sb.append(StringUtils.LINE_SEP);

        // append the err and output streams to the log
        if (systemOutput != null && systemOutput.length() > 0) {
            sb.append("------------- Standard Output ---------------")
                .append(StringUtils.LINE_SEP)
                .append(systemOutput)
                .append("------------- ---------------- ---------------")
                .append(StringUtils.LINE_SEP);
        }

        if (systemError != null && systemError.length() > 0) {
            sb.append("------------- Standard Error -----------------")
                .append(StringUtils.LINE_SEP)
                .append(systemError)
                .append("------------- ---------------- ---------------")
                .append(StringUtils.LINE_SEP);
        }

        sb.append(StringUtils.LINE_SEP);

        if (out != null) {
            try {
                out.write(sb.toString().getBytes());
                wri.close();
                out.write(inner.toString().getBytes());
                out.flush();
            } catch (IOException ioex) {
                throw new BuildException("Unable to write output", ioex);
            } finally {
                if (out != System.out && out != System.err) {
                    FileUtils.close(out);
                }
            }
        }
    
private voidformatError(java.lang.String type, junit.framework.Test test, java.lang.Throwable t)

        synchronized (wri) {
            if (test != null) {
                endTest(test);
                failed.put(test, Boolean.TRUE);
            }

            wri.println(type);
            wri.println(t.getMessage());
            String strace = JUnitTestRunner.getFilteredTrace(t);
            wri.print(strace);
            wri.println("");
        }
    
public voidsetOutput(java.io.OutputStream out)
{@inheritDoc}.

        this.out = out;
    
public voidsetSystemError(java.lang.String err)
{@inheritDoc}.

        systemError = err;
    
public voidsetSystemOutput(java.lang.String out)
{@inheritDoc}.

        systemOutput = out;
    
public voidstartTest(junit.framework.Test t)
Interface TestListener.

A new Test is started.

param
t the test.

        testStarts.put(t, new Long(System.currentTimeMillis()));
        failed.put(t, Boolean.FALSE);
    
public voidstartTestSuite(JUnitTest suite)
The whole testsuite started.

param
suite the test suite
throws
BuildException if unable to write the output

        if (out == null) {
            return; // Quick return - no output do nothing.
        }
        StringBuffer sb = new StringBuffer("Testsuite: ");
        sb.append(suite.getName());
        sb.append(StringUtils.LINE_SEP);
        try {
            out.write(sb.toString().getBytes());
            out.flush();
        } catch (IOException ex) {
            throw new BuildException("Unable to write output", ex);
        }