From 880801efddcfbd73141aef2cda15534f529cec17 Mon Sep 17 00:00:00 2001 From: Anton Velma Date: Wed, 20 Apr 2022 15:47:52 +0300 Subject: [PATCH] Fix possible StringIndexOutOfBoundsException exception in XmlReporter --- CHANGES.txt | 1 + .../testng/internal/WrappedTestNGMethod.java | 5 +++ .../java/test/reports/XmlReporterTest.java | 37 ++++++++++++++++++- 3 files changed, 42 insertions(+), 1 deletion(-) diff --git a/CHANGES.txt b/CHANGES.txt index 03d85128e9..13fc0a7cc9 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -9,6 +9,7 @@ Fixed: GITHUB-2704: IHookable and IConfigurable callback discrepancy (Krishnan M Fixed: GITHUB-2637: Upgrade to JDK11 as the minimum JDK requirements (Krishnan Mahadevan) Fixed: GITHUB-2734: Keep the initial order of listeners (Andrei Solntsev) Fixed: GITHUB-2359: Testng @BeforeGroups is running in parallel with testcases in the group (Anton Velma) +Fixed: Possible StringIndexOutOfBoundsException in XmlReporter (Anton Velma) 7.5 Fixed: GITHUB-2701: Bump gradle version to 7.3.3 to support java17 build (ZhangJian He) diff --git a/testng-core/src/main/java/org/testng/internal/WrappedTestNGMethod.java b/testng-core/src/main/java/org/testng/internal/WrappedTestNGMethod.java index d07d335448..7fae09b771 100644 --- a/testng-core/src/main/java/org/testng/internal/WrappedTestNGMethod.java +++ b/testng-core/src/main/java/org/testng/internal/WrappedTestNGMethod.java @@ -373,4 +373,9 @@ public boolean equals(Object o) { public int hashCode() { return multiplicationFactor * testNGMethod.hashCode(); } + + @Override + public String toString() { + return testNGMethod.toString(); + } } diff --git a/testng-core/src/test/java/test/reports/XmlReporterTest.java b/testng-core/src/test/java/test/reports/XmlReporterTest.java index 4e22753113..4519582b35 100644 --- a/testng-core/src/test/java/test/reports/XmlReporterTest.java +++ b/testng-core/src/test/java/test/reports/XmlReporterTest.java @@ -7,7 +7,10 @@ import java.io.FileReader; import java.io.IOException; import java.util.UUID; +import java.util.function.Consumer; import java.util.regex.Pattern; +import java.util.stream.Collectors; +import java.util.stream.Stream; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.xpath.XPath; @@ -15,10 +18,13 @@ import javax.xml.xpath.XPathFactory; import org.testng.TestNG; import org.testng.annotations.Test; +import org.testng.internal.MethodInstance; +import org.testng.internal.WrappedTestNGMethod; import org.testng.reporters.RuntimeBehavior; import org.w3c.dom.Document; import test.SimpleBaseTest; import test.reports.issue2171.TestClassExample; +import test.simple.SimpleSample; public class XmlReporterTest extends SimpleBaseTest { @Test(description = "GITHUB-1566") @@ -64,15 +70,44 @@ public void ensureCustomisationOfReportIsSupported() throws Exception { assertThat(data.trim()).isEqualTo("issue2171.html"); } + @Test + public void ensureReportGenerationWhenTestMethodIsWrappedWithWrappedTestNGMethod() { + File file = + runTest( + SimpleSample.class, + testng -> + testng.setMethodInterceptor( + (methods, context) -> + methods.stream() + .flatMap( + t -> + Stream.of( + t, + new MethodInstance(new WrappedTestNGMethod(t.getMethod())))) + .collect(Collectors.toList()))); + assertThat(file.exists()).isTrue(); + } + private static File runTest(Class clazz) { - return runTest(clazz, RuntimeBehavior.FILE_NAME); + return runTest(clazz, RuntimeBehavior.FILE_NAME, null); + } + + private static File runTest(Class clazz, Consumer customizer) { + return runTest(clazz, RuntimeBehavior.FILE_NAME, customizer); } private static File runTest(Class clazz, String fileName) { + return runTest(clazz, fileName, null); + } + + private static File runTest(Class clazz, String fileName, Consumer customizer) { String suiteName = UUID.randomUUID().toString(); File fileLocation = createDirInTempDir(suiteName); TestNG testng = create(fileLocation.toPath(), clazz); testng.setUseDefaultListeners(true); + if (customizer != null) { + customizer.accept(testng); + } testng.run(); return new File(fileLocation, fileName); }