Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix possible StringIndexOutOfBoundsException exception in XmlReporter #2750

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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