From 7555653d9cf4fe26aad48f733d8cce1efde087d5 Mon Sep 17 00:00:00 2001 From: seregamorph Date: Thu, 10 Nov 2022 12:53:34 +0400 Subject: [PATCH] GITHUB- - Failsafe parameter.toString --- CHANGES.txt | 3 +- .../testng/reporters/TestHTMLReporter.java | 15 +++++- .../reporters/TestHTMLReporterTest.java | 49 +++++++++++++++++++ 3 files changed, 65 insertions(+), 2 deletions(-) create mode 100644 testng-core/src/test/java/org/testng/reporters/TestHTMLReporterTest.java diff --git a/CHANGES.txt b/CHANGES.txt index 3f1fed2a3c..da09ecd38e 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -9,7 +9,8 @@ Fixed: GITHUB-2800: Running Test Classes with Inherited @Factory and @DataProvid 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 +Fixed: GITHUB-2807: buildStackTrace should be fail-safe (Sergey Chernov) +Fixed: GITHUB-2830: TestHTMLReporter parameter toString should be fail-safe (Sergey Chernov) Fixed: GITHUB-2798: Parallel executions coupled with retry analyzer results in duplicate retry analyzer instances being created (Krishnan Mahadevan) 7.6.1 diff --git a/testng-core/src/main/java/org/testng/reporters/TestHTMLReporter.java b/testng-core/src/main/java/org/testng/reporters/TestHTMLReporter.java index 4d26555f02..c51244a41d 100644 --- a/testng-core/src/main/java/org/testng/reporters/TestHTMLReporter.java +++ b/testng-core/src/main/java/org/testng/reporters/TestHTMLReporter.java @@ -123,7 +123,20 @@ public static void generateTable( if (j > 0) { pw.append(", "); } - pw.append(parameters[j] == null ? "null" : parameters[j].toString()); + if (parameters[j] == null) { + pw.append("null"); + } else { + String parameterToString; + try { + parameterToString = parameters[j].toString(); + } catch (RuntimeException | Error e) { + log(e.toString()); + // failover in case parameter toString() cannot be evaluated + parameterToString = + parameters[j].getClass().getName() + "@" + System.identityHashCode(parameters[j]); + } + pw.append(parameterToString); + } } } diff --git a/testng-core/src/test/java/org/testng/reporters/TestHTMLReporterTest.java b/testng-core/src/test/java/org/testng/reporters/TestHTMLReporterTest.java new file mode 100644 index 0000000000..1193725c66 --- /dev/null +++ b/testng-core/src/test/java/org/testng/reporters/TestHTMLReporterTest.java @@ -0,0 +1,49 @@ +package org.testng.reporters; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import java.io.PrintWriter; +import java.io.StringWriter; +import java.util.Collections; +import java.util.List; +import org.testng.ITestClass; +import org.testng.ITestNGMethod; +import org.testng.ITestResult; +import org.testng.annotations.Test; +import org.testng.internal.TestResult; + +public class TestHTMLReporterTest { + + @Test(description = "GITHUB-2830") + public void generateTableParametersToStringShouldBeFailsafe() { + ITestClass testClass = mock(ITestClass.class); + when(testClass.getName()).thenReturn("testClass"); + + ITestNGMethod testNGMethod = mock(ITestNGMethod.class); + when(testNGMethod.getMethodName()).thenReturn("testMethod"); + when(testNGMethod.getTestClass()).thenReturn(testClass); + + TestResult testResult = TestResult.newEmptyTestResult(); + testResult.setMethod(testNGMethod); + + testResult.setParameters(new Object[] {new ThrowingOnToString()}); + + List tests = Collections.singletonList(testResult); + + StringWriter sw = new StringWriter(); + PrintWriter pw = new PrintWriter(sw, true); + TestHTMLReporter.generateTable(pw, "title", tests, "cssClass", (t1, t2) -> 0); + + assertThat(sw.toString()) + .contains("Parameters: org.testng.reporters.TestHTMLReporterTest$ThrowingOnToString@"); + } + + private static class ThrowingOnToString { + @Override + public String toString() { + throw new IllegalStateException("Cannot calculate toString"); + } + } +}