diff --git a/MimirTestRunner.java b/MimirTestRunner.java new file mode 100644 index 0000000..56868aa --- /dev/null +++ b/MimirTestRunner.java @@ -0,0 +1,130 @@ +import org.junit.runner.JUnitCore; +import org.junit.runner.Request; +import org.junit.runner.Result; +import org.junit.internal.TextListener; +import org.junit.runner.notification.Failure; +import java.util.Map; +import java.util.HashMap; +import java.util.List; +import java.util.regex.Pattern; +import java.io.File; + +/** + * Write a description of class MyTestRunner here. + * + * @author (your name) + * @version (a version number or a date) + */ +public class MimirTestRunner { + public static void main(String [] args) { + MimirTestRunner testRunner = new MimirTestRunner(); + if (args.length == 0) { + File dir = new File("."); + File [] fileList = dir.listFiles(); + for (File file : fileList) { + String fileName = file.getName(); + if (file.isFile() + && Pattern.matches(".*Test\\.java$", fileName) + && !Pattern.matches(".*Abstract.*", fileName)) { + String className = fileName.substring(0, fileName.lastIndexOf(".java")); + testRunner.runTestClass(className); + } + } + } else { + for (String arg : args) { + testRunner.runTestClass(arg); + } + } + testRunner.printResults(); + System.exit(testRunner.getScore()); + } + + private JUnitCore junitCore; + private Map results; + private int runs; + private int fails; + + public MimirTestRunner() { + junitCore = new JUnitCore(); + results = new HashMap(); + } + + public void runTestClass(String className) { + try { + Class testClass = Class.forName(className); + Result result = junitCore.run(testClass); + runs += result.getRunCount(); + fails += result.getFailureCount(); + results.put(className, result); + } catch (ClassNotFoundException ex) { + System.out.println("Could not find class " + ex.getMessage()); + } + } + + private String niceTrace(Throwable t, String stopLine) { + final String INDENT = " "; + boolean foundStop = false; + StringBuilder builder = new StringBuilder(); + StackTraceElement [] trace = t.getStackTrace(); + + if (!t.getClass().equals(java.lang.AssertionError.class)) { + builder.append(INDENT); + builder.append(t.getClass().getName()); + builder.append("\n"); + } + + for (StackTraceElement call : trace) { + String line = call.toString(); + + if (line.contains(stopLine)) { + if (!foundStop) { + foundStop = true; + } + } else if (foundStop) { + break; + } + if (!line.contains("org.junit.Assert")) { + builder.append(INDENT).append(INDENT).append("at "); + builder.append(line); + builder.append("\n"); + } + + } + return builder.toString(); + } + + public void printResults() { + System.out.println("\nTest Results:\n"); + int count = 0; + for (String className : results.keySet()) { + Result result = results.get(className); + List failures = result.getFailures(); + for (Failure failure : failures) { + String reason = failure.getMessage(); + String trace = niceTrace(failure.getException(), className); + String errorText = "FAILED UNIT TEST"; + if (!failure.getException().getClass().equals(java.lang.AssertionError.class)) { + errorText = "ERROR IN YOUR CODE"; + } + + System.out.println(String.format( + "%d. %s - %s:", + ++count, + errorText, + failure.getTestHeader())); + if (reason != null) { + System.out.println(" " + reason); + } + System.out.println(trace); + } + } + System.out.println(String.format( + "%d of %d tests passed. Your score is %d%%", + runs-fails, runs, getScore())); + + } + + public int getScore() { + return (int)Math.round(((double)runs-fails)/runs*100); + } +} diff --git a/MimirTestRunner.sh b/MimirTestRunner.sh new file mode 100644 index 0000000..986aeb6 --- /dev/null +++ b/MimirTestRunner.sh @@ -0,0 +1,25 @@ +#!/bin/bash +wget -q http://cs.franklin.edu/~whittakt/MimirTests/hamcrest-core-1.3.jar +wget -q http://cs.franklin.edu/~whittakt/MimirTests/junit-4.13.jar +wget -q http://cs.franklin.edu/~whittakt/MimirTests/MimirTestRunner.java + +#export CLASSPATH=.:hamcrest-core-1.3.jar:junit-4.13.jar +export CLASSPATH=. +for filename in *.jar; do + export CLASSPATH=${CLASSPATH}:${filename} +done + +javac ./*.java; + +if [ $? -ne 0 ]; then + echo 0 > OUTPUT + exit 1 +fi + +if [ -f "RunMe.class" ]; then + java RunMe > DEBUG +fi + +java MimirTestRunner >> DEBUG + +echo "$?" > OUTPUT diff --git a/hamcrest-core-1.3.jar b/hamcrest-core-1.3.jar new file mode 100644 index 0000000..9d5fe16 Binary files /dev/null and b/hamcrest-core-1.3.jar differ diff --git a/junit-4.13.jar b/junit-4.13.jar new file mode 100644 index 0000000..acc3c43 Binary files /dev/null and b/junit-4.13.jar differ