Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GITHUB-2807 - Failsafe buildStackTrace #2808

Merged
merged 1 commit into from Oct 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES.txt
Expand Up @@ -2,6 +2,7 @@ Current
New: Ability to provide custom error message for assertThrows\expectThrows methods (Anatolii Yuzhakov)
Fixed: GITHUB-2780: Use SpotBugs instead of abandoned FindBugs
Fixed: GITHUB-2801: JUnitReportReporter is too slow
Fixed: GITHUB-2807: buildStackTrace should be fail-safe

7.6.1
Fixed: GITHUB-2761: Exception: ERROR java.nio.file.NoSuchFileException: /tmp/testngXmlPathInJar-15086412835569336174 (Krishnan Mahadevan)
Expand Down
Expand Up @@ -333,7 +333,13 @@ public static String shortStackTrace(Throwable t, boolean toHtml) {
private static String buildStackTrace(Throwable t, boolean toHtml, StackTraceType type) {
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
t.printStackTrace(pw);
try {
t.printStackTrace(pw);
} catch (Throwable e) {
// failsafe in case if printStackTrace throws exception
pw.println(t.getClass().getName());
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this may be not perfect exception handling, but in case if t.printStackTrace(pw) throws exception (see #2807) we do not have much options here, at least we do not rethrow it

e.printStackTrace(pw);
}
pw.flush();
String stackTrace = sw.getBuffer().toString();
if (type == StackTraceType.SHORT && !isTooVerbose()) {
Expand Down
20 changes: 20 additions & 0 deletions testng-core/src/test/java/org/testng/internal/UtilsTest.java
Expand Up @@ -2,6 +2,7 @@

import static java.util.Arrays.asList;
import static java.util.Collections.emptyList;
import static org.assertj.core.api.Assertions.assertThat;
import static org.testng.Assert.assertEquals;
import static org.testng.internal.Utils.join;

Expand Down Expand Up @@ -41,4 +42,23 @@ public void createEmptyStringWhenJoiningEmptyListWithJoinStrings() {
List<String> emptyList = emptyList();
assertEquals("", Utils.join(emptyList, ","));
}

@Test
public void buildStackTraceShouldBeFailsafe() {
// e.g. mocks of Exception classes may throw exception on exception.toString()
RuntimeException ex = new ThrowingException();
String stackTrace = Utils.longStackTrace(ex, true);

assertThat(stackTrace)
.contains("org.testng.internal.UtilsTest$ThrowingException")
.contains("java.lang.IllegalStateException: message not available");
}

// exception which cannot be printed
private static class ThrowingException extends RuntimeException {
@Override
public String getMessage() {
throw new IllegalStateException("message not available");
}
}
}