Skip to content

Commit

Permalink
Fix possible StringIndexOutOfBoundsException exception in XmlReporter
Browse files Browse the repository at this point in the history
  • Loading branch information
velma authored and krmahadevan committed Apr 21, 2022
1 parent 19010bd commit 880801e
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGES.txt
Expand Up @@ -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)
Expand Down
Expand Up @@ -373,4 +373,9 @@ public boolean equals(Object o) {
public int hashCode() {
return multiplicationFactor * testNGMethod.hashCode();
}

@Override
public String toString() {
return testNGMethod.toString();
}
}
37 changes: 36 additions & 1 deletion testng-core/src/test/java/test/reports/XmlReporterTest.java
Expand Up @@ -7,18 +7,24 @@
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;
import javax.xml.xpath.XPathConstants;
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")
Expand Down Expand Up @@ -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<TestNG> 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<TestNG> 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);
}
Expand Down

0 comments on commit 880801e

Please sign in to comment.