-
Notifications
You must be signed in to change notification settings - Fork 0
/
MimirTestRunner.java
135 lines (122 loc) · 4.32 KB
/
MimirTestRunner.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
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);
}
}
int passed = runs - fails < 0 ? 0 : runs - fails;
System.out.println(String.format(
"%d of %d tests passed.",
passed, runs));
}
public int getScore() {
int score = (int)Math.round(((double)runs-fails)/runs*100);
if (score < 0) {
score = 0;
}
return score;
}
}