JUnit: Decode stack traces to StackTraceElement[] instead of string

This commit is contained in:
Alexey Andreev 2018-11-23 14:32:10 +03:00
parent 0376a46c06
commit 2c8b0b55bb

View File

@ -62,25 +62,29 @@ final class RhinoResultParser {
String stack = result.get("stack", result).toString(); String stack = result.get("stack", result).toString();
String[] script = getScript(new File(debugFile.getParentFile(), String[] script = getScript(new File(debugFile.getParentFile(),
debugFile.getName().substring(0, debugFile.getName().length() - 9))); debugFile.getName().substring(0, debugFile.getName().length() - 9)));
stack = decodeStack(stack, script, debugInformation); StackTraceElement[] decodedStack = decodeStack(stack, script, debugInformation);
if (decodedStack != null) {
if (className.equals("java.lang.AssertionError")) { stack = "";
callback.error(new AssertionError(message + stack));
} else {
callback.error(new RuntimeException(className + ": " + message + stack));
} }
Throwable e;
if (className.equals("java.lang.AssertionError")) {
e = new AssertionError(message + stack);
} else {
e = new RuntimeException(className + ": " + message + stack);
}
e.setStackTrace(decodedStack);
callback.error(e);
break; break;
} }
} }
} }
private static String decodeStack(String stack, String[] script, DebugInformation debugInformation) { private static StackTraceElement[] decodeStack(String stack, String[] script, DebugInformation debugInformation) {
StringBuilder sb = new StringBuilder(); List<StackTraceElement> elements = new ArrayList<>();
for (String line : lineSeparator.split(stack)) { for (String line : lineSeparator.split(stack)) {
sb.append("\n\tat ");
Matcher matcher = pattern.matcher(line); Matcher matcher = pattern.matcher(line);
if (!matcher.matches()) { if (!matcher.matches()) {
sb.append(line);
continue; continue;
} }
@ -90,26 +94,32 @@ final class RhinoResultParser {
String scriptLine = script[lineNumber]; String scriptLine = script[lineNumber];
int column = firstNonSpace(scriptLine); int column = firstNonSpace(scriptLine);
MethodReference method = debugInformation.getMethodAt(lineNumber, column); MethodReference method = debugInformation.getMethodAt(lineNumber, column);
String className;
String methodName;
if (method != null) { if (method != null) {
sb.append(method.getClassName()).append(".").append(method.getName()); className = method.getClassName();
methodName = method.getName();
} else { } else {
sb.append(functionName != null ? functionName : "<unknown_function>"); className = "<JS>";
methodName = functionName != null ? functionName : "<unknown_function>";
} }
sb.append("("); String fileName;
SourceLocation location = debugInformation.getSourceLocation(lineNumber, column); SourceLocation location = debugInformation.getSourceLocation(lineNumber, column);
if (location != null && location.getFileName() != null) { if (location != null && location.getFileName() != null) {
String fileName = location.getFileName(); fileName = location.getFileName();
fileName = fileName.substring(fileName.lastIndexOf('/') + 1); fileName = fileName.substring(fileName.lastIndexOf('/') + 1);
sb.append(fileName).append(":").append(location.getLine()); lineNumber = location.getLine();
} else { } else {
sb.append("test.js:").append(lineNumber + 1); fileName = "test.js";
lineNumber++;
} }
sb.append(")");
elements.add(new StackTraceElement(className, methodName, fileName, lineNumber));
} }
return sb.toString(); return elements.toArray(new StackTraceElement[0]);
} }
private static DebugInformation getDebugInformation(File debugFile) { private static DebugInformation getDebugInformation(File debugFile) {