FileDocCategorySizeDatePackage
BriefJUnitResultFormatter.javaAPI DocApache Ant 1.707095Wed Dec 13 06:16:24 GMT 2006org.apache.tools.ant.taskdefs.optional.junit

BriefJUnitResultFormatter

public class BriefJUnitResultFormatter extends Object implements JUnitResultFormatter
Prints plain text output of the test to a specified Writer. Inspired by the PlainJUnitResultFormatter.
see
FormatterElement
see
PlainJUnitResultFormatter

Fields Summary
private OutputStream
out
Where to write the log to.
private PrintWriter
output
Used for writing the results.
private StringWriter
results
Used as part of formatting the results.
private PrintWriter
resultWriter
Used for writing formatted results to.
private NumberFormat
numberFormat
Formatter for timings.
private String
systemOutput
Output suite has written to System.out
private String
systemError
Output suite has written to System.err
Constructors Summary
public BriefJUnitResultFormatter()
Constructor for BriefJUnitResultFormatter.


            
      
        results = new StringWriter();
        resultWriter = new PrintWriter(results);
    
Methods Summary
public voidaddError(junit.framework.Test test, java.lang.Throwable error)
A test caused an error.

param
test a test
param
error the error thrown by the test

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

A Test failed.

param
test a test
param
t the assertion failed by the test

        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 a test
param
t the exception thrown by the test

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

param
test a test

    
public voidendTestSuite(JUnitTest suite)
The whole testsuite ended.

param
suite the test suite

        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(numberFormat.format(suite.getRunTime() / 1000.0));
        sb.append(" sec");
        sb.append(StringUtils.LINE_SEP);
        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);
        }

        if (output != null) {
            try {
                output.write(sb.toString());
                resultWriter.close();
                output.write(results.toString());
                output.flush();
            } finally {
                if (out != System.out && out != System.err) {
                    FileUtils.close(out);
                }
            }
        }
    
protected synchronized voidformatError(java.lang.String type, junit.framework.Test test, java.lang.Throwable error)
Format an error and print it.

param
type the type of error
param
test the test that failed
param
error the exception that the test threw

        if (test != null) {
            endTest(test);
        }

        resultWriter.println(formatTest(test) + type);
        resultWriter.println(error.getMessage());
        String strace = JUnitTestRunner.getFilteredTrace(error);
        resultWriter.println(strace);
        resultWriter.println();
    
protected java.lang.StringformatTest(junit.framework.Test test)
Format the test for printing..

param
test a test
return
the formatted testname

        if (test == null) {
            return "Null Test: ";
        } else {
            return "Testcase: " + test.toString() + ":";
        }
    
public voidsetOutput(java.io.OutputStream out)
Sets the stream the formatter is supposed to write its results to.

param
out the output stream to write to

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

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

        systemOutput = out;
    
public voidstartTest(junit.framework.Test test)
A test started.

param
test a test

    
public voidstartTestSuite(JUnitTest suite)
The whole testsuite started.

param
suite the test suite

        if (output == null) {
            return; // Quick return - no output do nothing.
        }
        StringBuffer sb = new StringBuffer("Testsuite: ");
        sb.append(suite.getName());
        sb.append(StringUtils.LINE_SEP);
        output.write(sb.toString());
        output.flush();