-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathValidationMessageFormatter.java
More file actions
76 lines (70 loc) · 2.92 KB
/
ValidationMessageFormatter.java
File metadata and controls
76 lines (70 loc) · 2.92 KB
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
package net.sf.webcat.plugins.javatddplugin;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class ValidationMessageFormatter {
public static String formatMessage(Throwable error) {
if (error == null) {
return null;
}
if (error instanceof NoSuchMethodError) {
return "Your test has called a method that does not exist in the interface. "
+ "The reference implementation is not guaranteed to implement this method "
+ "and as such cannot validate this test case. "
+ "The method is: " + error.getMessage();
}
else if (error instanceof AssertionError) {
String original = error.getMessage();
String msg;
if (original != null) {
Pattern p1 = Pattern.compile(
".*expected:<?([^>]*)>? but was:<?([^>]*)>?");
Matcher m1 = p1.matcher(original);
if (m1.matches()) {
String expected = m1.group(1).trim();
String actual = m1.group(2).trim();
msg = "Your test expected: <" + expected + "> "
+ "while the reference implementation returned: <"
+ actual + ">";
}
else if (original.contains("expected null, but was:")) {
Pattern p2 = Pattern.compile(
".*expected null, but was:<?([^>]*)>?");
Matcher m2 = p2.matcher(original);
if (m2.matches()) {
String actual = m2.group(1).trim();
msg = "Your test expected: <null> "
+ "while the reference implementation returned: <"
+ actual + ">";
}
else {
msg = original;
}
}
else if (original.contains("expected same:")) {
Pattern p3 = Pattern.compile(
".*expected same:<?([^>]*)>? was not:<?([^>]*)>?");
Matcher m3 = p3.matcher(original);
if (m3.matches()) {
String expected = m3.group(1).trim();
String actual = m3.group(2).trim();
msg = "Your test expected the *same* object: <"
+ expected + "> "
+ "while the reference implementation returned a different object: <"
+ actual + ">";
}
else {
msg = original;
}
}
else {
msg = original;
}
}
else {
msg = original;
}
return msg;
}
return null;
}
}