XMLTestListener Example
import java.io.*;
import java.lang.reflect.Method;
import org.junit.runner.*;
public class XMLTestListener implements TestListener {
private Writer out;
public XMLTestListener(OutputStream out) {
try {
this.out = new BufferedWriter(new OutputStreamWriter(out, "UTF-8"));
}
catch (UnsupportedEncodingException e) {
throw new RuntimeException("Broken VM does not support UTF-8");
}
}
public void testRunStarted(int testCount) {
try {
out.write("<?xml version='1.0' encoding='UTF-8'?>\r\n");
out.write("<testsuite>\r\n");
out.write(" <properties>\r\n");
// enumerate properties????
out.write(" </properties>\r\n");
}
catch (IOException ex) {
throw new RuntimeException(ex);
}
}
public void testRunFinished(Runner runner) {
double time = runner.getRunTime() / 1000.0; // seconds
try {
out.write("</testsuite>\r\n");
out.flush();
}
catch (IOException ex) {
throw new RuntimeException(ex);
}
}
public void testStarted(Object test, String name) {
try {
out.write(" <testcase classname='"
+ test.getClass().getName()
+ "' name='" + name + ">");
}
catch (IOException ex) {
throw new RuntimeException(ex);
}
}
public void testFinished(Object test, String name) {
try {
out.write(" </test>\r\n");
}
catch (IOException ex) {
throw new RuntimeException(ex);
}
}
public void testFailure(Failure failure) {
try {
out.write(" <failure>");
out.write(escape(failure.getMessage()));
out.write(" </failure>\r\n");
}
catch (IOException ex) {
throw new RuntimeException(ex);
}
}
private String escape(String message) {
String result = message.replaceAll("&", "&");
result = result.replaceAll("<", "<");
result.replaceAll(">", ">");
return result;
}
public void testIgnored(Method method) {
// Why a Method instead of a Ignored????
try {
out.write(" <ignored>");
out.write(escape(method.getName()));
out.write(" </ignored>\r\n");
}
catch (IOException ex) {
throw new RuntimeException(ex);
}
}
}