Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
toddawhittaker committed Sep 25, 2020
1 parent 7d65a90 commit f7799ed
Show file tree
Hide file tree
Showing 4 changed files with 155 additions and 0 deletions.
130 changes: 130 additions & 0 deletions MimirTestRunner.java
Original file line number Diff line number Diff line change
@@ -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<String, Result> results;
private int runs;
private int fails;

public MimirTestRunner() {
junitCore = new JUnitCore();
results = new HashMap<String, Result>();
}

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<Failure> 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);
}
}
25 changes: 25 additions & 0 deletions MimirTestRunner.sh
Original file line number Diff line number Diff line change
@@ -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
Binary file added hamcrest-core-1.3.jar
Binary file not shown.
Binary file added junit-4.13.jar
Binary file not shown.

0 comments on commit f7799ed

Please sign in to comment.