This is part of JavaUnitTestChallengeSolved. -- DonWells
import java.awt.*;
import java.awt.event.*;
public class TestGUI extends Frame {
static TestGUI testFrame;
Label scoreLabel;
List listOfTests;
Test tests [];
TestGUI(){
setLayout(new BorderLayout());
setSize(250,180);
setTitle("Unit Tests");
addWindowListener(exitListener());
add(scoreLabel(), "North");
add(listOfTests(), "Center");
add(runButton(), "South");
addTests();
showResults();}
public static void main(String arguments[]){
testFrame = new TestGUI();
testFrame.setVisible(true);}
public void addTests(){
tests = new Test[4];
tests[0] = new GoodTest();
tests[1] = new FailTest();
tests[2] = new AbortTest();
tests[3] = new DeadLockTest();}
void runTests() {
addTests();
for (int each = 0; each < tests.length; each++){
runOneTest(each);}}
void runOneTest(int anIndex) {
tests[anIndex].setResultToBeDeadlock();
tests[anIndex].setUp();
tests[anIndex].start();
waitTillTestFinishesOrDeadlock(anIndex);
tests[anIndex].stop();
tests[anIndex].tearDown();}
private void waitTillTestFinishesOrDeadlock(int anIndex){
try {
tests[anIndex].join(5000);}
catch (InterruptedException exception){
return;}}
private void showResults() {
listOfTests.removeAll();
for (int each = 0; each < tests.length; each++) {
listOfTests.add(tests[each].result);}
showScore();}
private void showScore(){
int passed = numberPassed();
float total = (float) tests.length;
int score = (int)(passed / total * 100);
scoreLabel.setText(new Integer(score).toString() + "%");
showPassFail(score);}
private int numberPassed (){
int passed = 0;
for (int each = 0; each < tests.length; each++) {
if (tests[each].success) {
passed++;}}
return passed;}
private void showPassFail (int aScore){
scoreLabel.setBackground((aScore == 100) ? Color.green : Color.red);}
private Label scoreLabel(){
return scoreLabel = new Label("Not Run", Label.CENTER);}
private List listOfTests (){
return listOfTests = new List(5);}
private Button runButton() {
Button button = new Button("Run Tests");
button.addActionListener (runButtonListener());
return button;}
private WindowAdapter exitListener(){
return new WindowAdapter() {
public void windowClosing(WindowEvent anEvent) {
System.exit(0);}};}
private ActionListener runButtonListener(){
return new ActionListener(){
public void actionPerformed(ActionEvent anEvent){
testFrame.runTests();
testFrame.showResults();}};}
}
import java.lang.*;
public class Test extends Thread {
public static String DeadlockMessage = "Deadlock : Test timed out";
public boolean success = false;
public String result = "not run";
public void setUp(){}
public void runTest()throws RuntimeException {}
public void tearDown(){}
public void should (boolean aTestPassed, String aMessage){
if (!aTestPassed) {
throw new TestFailedException(aMessage);};}
public void sleepHalfSecond(){
try {
Thread.sleep(500);}
catch (InterruptedException exception) {};}
public void setResultToBeDeadlock (){
testFailed(DeadlockMessage);}
public void run(){
runAndCaptureAborts();}
public void runAndCaptureAborts() {
try {
runAndCaptureFailures();}
catch (RuntimeException exception) {
testFailed("Aborted : " + exception.getMessage());}}
private void runAndCaptureFailures()throws RuntimeException {
try {
runAndAllowExceptions();}
catch (TestFailedException exception) {
testFailed("Failed : " + exception.getMessage());}}
private void runAndAllowExceptions()throws TestFailedException, RuntimeException {
runTest();
testPassed();}
private void testPassed(){
success = true;
result = message("Passed");}
private void testFailed(String aMessage){
success = false;
result = message(aMessage);}
private String message(String aString){
return getClass().getName() + " : " + aString;}
}
public class TestFailedException extends java.lang.RuntimeException {
TestFailedException(String aMessage){
super(aMessage);}
}