Copilot commented on code in PR #3645:
URL: https://github.com/apache/maven-surefire/pull/3645#discussion_r4731141215
##########
surefire-providers/common-java5/src/main/java/org/apache/maven/surefire/report/SmartStackTraceParser.java:
##########
@@ -202,27 +204,44 @@ 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, and an overridden
+ * {@code getStackTrace()} can even give back null frames, which we leave
out.
+ */
+ public static StackTraceElement[] stackTraceOf(Throwable t) {
+ try {
+ StackTraceElement[] stackTrace = t.getStackTrace();
+ return stackTrace == null
+ ? new StackTraceElement[0]
+ :
Arrays.stream(stackTrace).filter(Objects::nonNull).toArray(StackTraceElement[]::new);
+ } catch (Throwable e) {
+ return new StackTraceElement[0];
+ }
+ }
+
private static String toString(Throwable t, Iterable<StackTraceElement>
elements, StackTraceFilter filter) {
StringBuilder result = new StringBuilder();
if (t != null) {
result.append(t.getClass().getName());
- String msg;
- // SUREFIRE-818
- try {
- msg = t.getMessage();
- } catch (NullPointerException npe) {
- msg = "";
- }
+ // SUREFIRE-818: a test's exception can throw from getMessage().
Like toString(), we take the localized
+ // message, which is also the one the XML report gets, so both
read the same error type.
+ String msg = new SafeThrowable(t).getLocalizedMessage();
Review Comment:
With `forkCount=0`, a null ordinary message and non-null localized message
make the XML `type` end in `:`; reversing their nullness can include
stack-frame text in the type. Make `StatelessXmlReporter.java:593–600` extract
the class from the header independently of `getMessage()`, and cover both
combinations with full and trimmed traces in forked and in-process runs. The
same mismatch affects the fallback in
`surefire-providers/surefire-junit-platform/src/main/java/org/apache/maven/surefire/junitplatform/DefaultStackTraceWriter.java:102–104`.
--
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]