/*
* TestRunner.java
*
*/
package bdjunit.haviui;
import bdjunit.framework.AssertionFailedError;
import bdjunit.framework.Test;
import bdjunit.framework.TestListener;
import bdjunit.framework.TestResult;
import bdjunit.framework.TestSuite;
import bdjunit.runner.TestRunnerBase;
import bdjunit.util.CallStackToolBox;
import java.lang.reflect.*;
import java.awt.Container;
import java.awt.Font;
import java.awt.Color;
import java.awt.Graphics;
import org.havi.ui.*;
/**
* HAVi TestRunner
*/
public class TestRunner extends TestRunnerBase implements TestListener {
private HStaticText textLabel = null;
private StringBuffer fStrBuf = new StringBuffer();
private int fErrors = 0;
private int fFails = 0;
/** Creates a new instance of TestRunner
* @param uiTarget
*/
public TestRunner(Container uiTarget) {
TestRunnerBase.uiTarget = uiTarget;
this.textLabel = new HStaticText();
this.textLabel.setBounds(500,0,1000,500);
this.textLabel.setVisible(true);
this.textLabel.setFont(new Font(null, Font.PLAIN, 16));
this.textLabel.setVerticalAlignment(HVisible.VALIGN_TOP);
this.textLabel.setHorizontalAlignment(HVisible.HALIGN_LEFT);
// [NOTE] Can't use this method in PS3
//this.textLabel.setForeground(Color.GREEN);
//textLabel.setTextContent("Init", HState.ALL_STATES);
TestRunnerBase.uiTarget.add(textLabel);
}
static public TestResult run(Test test, Container container) throws Exception {
TestRunner runner = new TestRunner(container);
return runner.doRun(test);
}
static public Container getUiTarget() {
return TestRunnerBase.uiTarget;
}
public TestResult doRun(Test suite) {
TestResult result= new TestResult();
//result.addListener(this.testListener);
result.addListener(this);
suite.run(result);
// Tests Result
if (result.failureCount() == 0 && result.errorCount() == 0)
{ // all tests OK
fStrBuf.append("\nOK");
fStrBuf.append("\n");
fStrBuf.append("Tests:" + suite.countTestCases());
setTextLabel(fStrBuf.toString());
}
else
{ // report failure and error
fStrBuf.append("\n");
fStrBuf.append("Tests: " + suite.countTestCases() + " ");
fStrBuf.append("Failures: " + result.failureCount() + " ");
fStrBuf.append("Errors: " + result.errorCount() + " ");
setTextLabel(fStrBuf.toString());
}
return result;
}
static private Test getTest(Class testClass) throws Exception {
try {
Method suiteMethod= testClass.getMethod("suite", new Class[0]);
Test testSuite = (Test) suiteMethod.invoke(null, new Class[0]);
return testSuite;
} catch (Exception ex) {
return new TestSuite(testClass);
}
}
static private Test getTest(String className) throws Exception {
return getTest(Class.forName(className));
}
private void setTextLabel(String labelStr) {
this.textLabel.setTextContent(labelStr, HState.ALL_STATES);
uiTarget.repaint(); // will update focus status
}
// interface - TestListener
public void addError(Test test, Throwable t) {
//this.textLabel.setForeground(Color.RED);
//fStrBuf.append("E");
fErrors++;
// Show stack track, only show last one
// StackTraceElement[] stElems = t.getStackTrace();
// if (stElems.length > 0)
// {
fStrBuf.append("\nError"+ ":" + test.toString() + ":" + t.getMessage() + ":" + CallStackToolBox.getCallerInfo(t) + "\n");
setTextLabel(fStrBuf.toString());
// }
// setTextLabel(fStrBuf.toString());
}
public void addFailure(Test test, AssertionFailedError t) {
//this.textLabel.setForeground(Color.RED);
//fStrBuf.append("F");
fFails++;
// Show stack track, only show last one
// StackTraceElement[] stElems = t.getStackTrace();
// if (stElems.length > 0)
// {
fStrBuf.append("\nFail"+ ":" + test.toString() + ":" + t.getMessage() + ":" + CallStackToolBox.getCallerInfo(t) + "\n");
setTextLabel(fStrBuf.toString());
// }
}
public void endTest(Test test) {
}
public void startTest(Test test) {
fStrBuf.append("."); // A test show a dot(".")
if (fErrors + fErrors > 40) // 40 tests will change a line
{
fStrBuf.append("\n");
}
setTextLabel(fStrBuf.toString());
}
//public static void main(String args[]) {
// try {
// DVBLogTestRunner.run(getTest(args[0]));
// } catch(Exception e) {
// e.printStackTrace();
// }
//}
}