/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package bdjunit.textui;
import bdjunit.framework.AssertionFailedError;
import bdjunit.framework.Test;
import bdjunit.framework.TestListener;
import bdjunit.framework.TestResult;
import bdjunit.framework.TestSuite;
import bdjunit.util.CallStackToolBox;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Iterator;
/**
* Systemout standard output Test Runner.
*/
public class SystemOutTestRunner implements TestListener {
private int errors = 0;
private int failures = 0;
private SystemOutTestRunner() {
// Can't instance
}
static public TestResult run(Test test) throws Exception {
SystemOutTestRunner runner = new SystemOutTestRunner();
return runner.doRun(test);
}
public TestResult doRun(Test suite) {
TestResult result= new TestResult();
result.addListener(this);
suite.run(result);
// Tests Result
StringBuffer strBuf = new StringBuffer();
if (result.failureCount() == 0 && result.errorCount() == 0) {
// all tests OK
strBuf.append("OK");
strBuf.append("\n");
strBuf.append("Tests:" + suite.countTestCases());
System.out.println(strBuf.toString());
}
else {
// report failure and error
strBuf.append("Tests: " + suite.countTestCases() + " ");
strBuf.append("Failures: " + result.failureCount() + " ");
strBuf.append("Errors: " + result.errorCount() + " ");
System.out.println(strBuf.toString());
}
return result;
}
static private Test getTest(Class testClass) throws Exception {
Test testSuite = null;
try {
Method suiteMethod= testClass.getMethod("suite", new Class[0]);
testSuite = (Test) suiteMethod.invoke(null, new Class[0]);
} catch (Exception ex) {
return new TestSuite(testClass);
}
return testSuite;
}
static private Test getTest(String className) throws Exception {
return getTest(Class.forName(className));
}
public void addError(Test test, Throwable t) {
errors++;
StringBuffer strBuf = new StringBuffer("");
strBuf.append("\nError" + ":" + test.toString() +
":" + t.getMessage() + ":");
//// display 5 levels call stack
//ArrayList strings = CallStackToolBox.getCallStackStrings(t, 5);
//Iterator iter = strings.listIterator();
//while(iter.hasNext()) {
// String callstackString = (String)iter.next();
// strBuf.append(callstackString + "\n");
//}
//strBuf = strBuf.deleteCharAt(strBuf.length() - 1); // Delete last "\n"
System.out.println(strBuf.toString());
t.printStackTrace();
}
public void addFailure(Test test, AssertionFailedError t) {
failures++;
StringBuffer strBuf = new StringBuffer("");
strBuf.append("\nFail" + ":" + test.toString() +
":" + t.getMessage() + ":");
//// display 5 levels call stack
//ArrayList strings = CallStackToolBox.getCallStackStrings(t, 5);
//Iterator iter = strings.listIterator();
//while(iter.hasNext()) {
// String callstackString = (String)iter.next();
// strBuf.append(callstackString + "\n");
//}
//strBuf = strBuf.deleteCharAt(strBuf.length() - 1); // Delete last "\n"
System.out.println(strBuf.toString());
t.printStackTrace();
}
public void endTest(Test test) {
// TBD
}
public void startTest(Test test) {
System.out.print(".");
}
}