Copilot commented on code in PR #3645:
URL: https://github.com/apache/maven-surefire/pull/3645#discussion_r4730247365
##########
surefire-providers/common-java5/src/main/java/org/apache/maven/surefire/report/SmartStackTraceParser.java:
##########
@@ -202,25 +202,43 @@ private static boolean
containsClassName(StackTraceElement[] stackTrace, StackTr
return false;
}
- private static String causeToString(Throwable cause, StackTraceFilter
filter) {
+ private static String causeToString(Throwable topmost, StackTraceFilter
filter) {
StringBuilder resp = new StringBuilder();
- while (cause != null) {
+ Set<Throwable> seen = newIdentitySet();
+ seen.add(topmost);
+ for (Throwable cause = topmost.getCause(); cause != null &&
seen.add(cause); cause = cause.getCause()) {
resp.append("Caused by: ");
- resp.append(toString(cause, asList(cause.getStackTrace()),
filter));
- cause = cause.getCause();
+ resp.append(toString(cause, asList(stackTraceOf(cause)), filter));
}
return resp.toString();
}
+ // two exceptions can be the cause of each other, so remember which ones
we already walked through
+ private static Set<Throwable> newIdentitySet() {
+ return Collections.newSetFromMap(new IdentityHashMap<>());
+ }
+
+ /**
+ * The frames of {@code t}, or none. A mocked exception can return null
here, or throw.
+ */
+ public static StackTraceElement[] stackTraceOf(Throwable t) {
+ try {
+ StackTraceElement[] stackTrace = t.getStackTrace();
+ return stackTrace == null ? new StackTraceElement[0] : stackTrace;
Review Comment:
An overridden `getStackTrace()` returning `{null}` passes through unchanged
and makes `smartTrimmedStackTrace()` throw in `rootIsInclass()`. Because
`EventChannelEncoder` requests the smart trace before either protected
renderer, the test error is still lost. Filter out null frames here and add a
regression with a null first frame.
##########
surefire-providers/surefire-junit-platform/src/main/java/org/apache/maven/surefire/junitplatform/DefaultStackTraceWriter.java:
##########
@@ -72,16 +76,41 @@ public String smartTrimmedStackTrace() {
@Override
public String writeTrimmedTraceToString() {
- return t == null ? "" :
SmartStackTraceParser.stackTraceWithFocusOnClassAsString(t, testClass);
+ if (t == null) {
+ return "";
+ }
+ try {
+ return SmartStackTraceParser.stackTraceWithFocusOnClassAsString(t,
testClass);
+ } catch (Throwable e) {
+ return classNameAndFrames(t);
+ }
}
@Override
public SafeThrowable getThrowable() {
return t == null ? null : new SafeThrowable(t);
}
+ /**
+ * Used when the exception of a test throws while we print it, for example
from getMessage() or
+ * toString() (SUREFIRE-818). If we let that escape, the test error is
never reported.
+ * The first line keeps the usual "class: message" form, the XML report
reads the error type from it.
+ */
+ private static String classNameAndFrames(Throwable t) {
+ StringBuilder trace = new StringBuilder(t.getClass().getName());
+ String message = new SafeThrowable(t).getMessage();
Review Comment:
`SafeThrowable.getMessage()` can still throw if the exception raised by the
original accessor also has a throwing message accessor, so this fallback can
still lose the test result. The same gap in
`SafeThrowable.getLocalizedMessage()` affects event serialization before either
trace renderer runs. Guard the secondary accessor calls in both methods with a
terminal, non-throwing fallback and add a failure-event regression test.
##########
surefire-providers/surefire-junit-platform/src/main/java/org/apache/maven/surefire/junitplatform/DefaultStackTraceWriter.java:
##########
@@ -72,16 +76,41 @@ public String smartTrimmedStackTrace() {
@Override
public String writeTrimmedTraceToString() {
- return t == null ? "" :
SmartStackTraceParser.stackTraceWithFocusOnClassAsString(t, testClass);
+ if (t == null) {
+ return "";
+ }
+ try {
+ return SmartStackTraceParser.stackTraceWithFocusOnClassAsString(t,
testClass);
+ } catch (Throwable e) {
+ return classNameAndFrames(t);
+ }
}
@Override
public SafeThrowable getThrowable() {
return t == null ? null : new SafeThrowable(t);
}
+ /**
+ * Used when the exception of a test throws while we print it, for example
from getMessage() or
+ * toString() (SUREFIRE-818). If we let that escape, the test error is
never reported.
+ * The first line keeps the usual "class: message" form, the XML report
reads the error type from it.
+ */
+ private static String classNameAndFrames(Throwable t) {
+ StringBuilder trace = new StringBuilder(t.getClass().getName());
+ String message = new SafeThrowable(t).getMessage();
Review Comment:
When normal trace printing fails and `getMessage()` and
`getLocalizedMessage()` differ in nullness, this fallback's header disagrees
with the transported message and corrupts the XML exception `type`. The same
mismatch occurs in `SmartStackTraceParser.java:241–242` when `getMessage()`
throws an exception with a null message, producing `type="ExceptionClass:"`.
Keep both headers consistent with the safely obtained localized message, and
add XML-report tests for both cases.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]