Skip to content

Commit

Permalink
GITHUB- - Failsafe parameter.toString
Browse files Browse the repository at this point in the history
  • Loading branch information
seregamorph authored and krmahadevan committed Nov 10, 2022
1 parent 75698c2 commit acfac7a
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 2 deletions.
3 changes: 2 additions & 1 deletion CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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<ITestResult> 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");
}
}
}

0 comments on commit acfac7a

Please sign in to comment.