Skip to content

Commit

Permalink
Merge pull request #2808 from seregamorph/GITHUB-2807
Browse files Browse the repository at this point in the history
GITHUB-2807 - Failsafe buildStackTrace
  • Loading branch information
juherr committed Oct 6, 2022
2 parents 9150736 + 64676aa commit fba0723
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
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
8 changes: 7 additions & 1 deletion testng-core-api/src/main/java/org/testng/internal/Utils.java
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());
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");
}
}
}

0 comments on commit fba0723

Please sign in to comment.