aboutsummaryrefslogtreecommitdiff
path: root/src/ant/lombok/ant/SimpleTestFormatter.java
blob: e137d822522193fe2ca06a0d735226d484922e01 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package lombok.ant;

import java.io.OutputStream;
import java.io.PrintStream;
import java.util.Arrays;

import junit.framework.AssertionFailedError;
import junit.framework.Test;

import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.taskdefs.optional.junit.JUnitResultFormatter;
import org.apache.tools.ant.taskdefs.optional.junit.JUnitTest;

public class SimpleTestFormatter implements JUnitResultFormatter {
	private PrintStream out = System.out;
	private Test lastMarked = null;
	
	@Override
	public void addError(Test test, Throwable error) {
		lastMarked = test;
		logResult(test, "ERR");
		printThrowable(error, false, 2);
	}
	
	private void printThrowable(Throwable throwable, boolean cause, int indent) {
		String msg = throwable.getMessage();
		char[] prefixChars = new char[indent];
		Arrays.fill(prefixChars, ' ');
		String prefix = new String(prefixChars);
		
		if (msg == null || msg.isEmpty()) {
			out.println(prefix + (cause ? "Caused by " : "") + throwable.getClass());
		} else {
			out.println(prefix + (cause ? "Caused by " : "") + throwable.getClass() + ": " + msg);
		}
		StackTraceElement[] elems = throwable.getStackTrace();
		if (elems != null) for (StackTraceElement elem : elems) {
			out.println(prefix + "  " + elem);
		}
		
		Throwable c = throwable.getCause();
		if (c != null) printThrowable(c, true, indent + 2);
	}
	
	@Override
	public void addFailure(Test test, AssertionFailedError failure) {
		lastMarked = test;
		logResult(test, "FAIL");
		out.println(failure.getMessage());
	}
	
	@Override
	public void endTest(Test test) {
		if (test != lastMarked) logResult(test, "PASS");
	}
	
	@Override
	public void startTest(Test test) { }
	
	@Override
	public void endTestSuite(JUnitTest testSuite) throws BuildException { }
	
	@Override
	public void setOutput(OutputStream out) {
		this.out = new PrintStream(out);
	}
	
	@Override
	public void setSystemError(String msg) {
		if (msg.trim().isEmpty()) return;
		out.println(msg);
	}
	
	@Override
	public void setSystemOutput(String msg) {
		if (msg.trim().isEmpty()) return;
		out.println(msg);
	}
	
	@Override
	public void startTestSuite(JUnitTest testSuite) throws BuildException { }
	
	private void logResult(Test test, String result) {
		out.println("[" + result + "] " + String.valueOf(test));
		out.flush();
	}
}