From 1288058fac43aa200ee2a89f3a779e5604ca7011 Mon Sep 17 00:00:00 2001 From: Krishnan Mahadevan Date: Wed, 7 Jun 2023 15:57:33 +0530 Subject: [PATCH] Dont honour params specified in suite-file tag Closes #581 TestNG does not have any logic that would honour parameter tags specified within suite-files tag. But due to a parsing bug, we end up reading parameters that were specified within `` tag. This tag by definition is NOT meant to have any child tags inside of it. Fixed this discrepancy by adding a warning when this anomaly is detected and skipping of reading the `` tag inside it as if it were specified within `` tag. --- CHANGES.txt | 1 + .../org/testng/xml/TestNGContentHandler.java | 10 + .../java/org/testng/xml/internal/Parser.java | 2 +- testng-core/src/main/resources/testng-1.1.dtd | 213 ++++++++++++++++++ .../java/test/parameters/ParameterTest.java | 20 ++ .../parameters/issue581/TestClassSample.java | 12 + .../parametertest/issue_581/parent_suite.xml | 11 + .../parametertest/issue_581/suite_one.xml | 8 + .../parametertest/issue_581/suite_two.xml | 8 + 9 files changed, 284 insertions(+), 1 deletion(-) create mode 100755 testng-core/src/main/resources/testng-1.1.dtd create mode 100644 testng-core/src/test/java/test/parameters/issue581/TestClassSample.java create mode 100644 testng-core/src/test/resources/parametertest/issue_581/parent_suite.xml create mode 100644 testng-core/src/test/resources/parametertest/issue_581/suite_one.xml create mode 100644 testng-core/src/test/resources/parametertest/issue_581/suite_two.xml diff --git a/CHANGES.txt b/CHANGES.txt index 45b2265678..e12be724ec 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -1,5 +1,6 @@ Current +Fixed: GITHUB-581: Parameters of nested test suites are overridden(Krishnan Mahadevan) Fixed: GITHUB-727 : Fixing data races (Krishnan Mahadevan) Fixed: GITHUB-2913: Maps containing nulls can be incorrectly considered equal (Alex Heneveld) diff --git a/testng-core/src/main/java/org/testng/xml/TestNGContentHandler.java b/testng-core/src/main/java/org/testng/xml/TestNGContentHandler.java index 89a9308e69..249fbf9e09 100644 --- a/testng-core/src/main/java/org/testng/xml/TestNGContentHandler.java +++ b/testng-core/src/main/java/org/testng/xml/TestNGContentHandler.java @@ -101,6 +101,7 @@ enum Location { } private final Stack m_locations = new Stack<>(); + private boolean isSuiteFileTag = false; private XmlClass m_currentClass = null; private ArrayList m_currentIncludedMethods = null; @@ -196,9 +197,11 @@ private void xmlSuiteFile(boolean start, Attributes attributes) { String path = attributes.getValue("path"); pushLocation(Location.SUITE); m_suiteFiles.add(path); + isSuiteFileTag = true; } else { m_currentSuite.setSuiteFiles(m_suiteFiles); popLocation(); + isSuiteFileTag = false; } } @@ -611,6 +614,13 @@ public void startElement(String uri, String localName, String qName, Attributes } else if ("exclude".equals(qName)) { xmlExclude(true, attributes); } else if ("parameter".equals(qName)) { + if (isSuiteFileTag) { + // do-not process a tag when it is specified inside + Logger.getLogger(getClass()) + .warn( + "Ignoring the tag because it is specified inside a tag."); + return; + } String value = expandValue(attributes.getValue("value")); Location location = m_locations.peek(); switch (location) { diff --git a/testng-core/src/main/java/org/testng/xml/internal/Parser.java b/testng-core/src/main/java/org/testng/xml/internal/Parser.java index 7c42cfb8d3..5713b66eff 100644 --- a/testng-core/src/main/java/org/testng/xml/internal/Parser.java +++ b/testng-core/src/main/java/org/testng/xml/internal/Parser.java @@ -24,7 +24,7 @@ public class Parser { /** The name of the TestNG DTD. */ - public static final String TESTNG_DTD = "testng-1.0.dtd"; + public static final String TESTNG_DTD = "testng-1.1.dtd"; /** The URL to the deprecated TestNG DTD. */ // It has to be public because its being used by TestNG eclipse plugin diff --git a/testng-core/src/main/resources/testng-1.1.dtd b/testng-core/src/main/resources/testng-1.1.dtd new file mode 100755 index 0000000000..62c6fd0cb5 --- /dev/null +++ b/testng-core/src/main/resources/testng-1.1.dtd @@ -0,0 +1,213 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/testng-core/src/test/java/test/parameters/ParameterTest.java b/testng-core/src/test/java/test/parameters/ParameterTest.java index 44c2ec93a8..8f43d40036 100644 --- a/testng-core/src/test/java/test/parameters/ParameterTest.java +++ b/testng-core/src/test/java/test/parameters/ParameterTest.java @@ -2,9 +2,12 @@ import static org.assertj.core.api.Assertions.assertThat; +import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; +import org.testng.ISuite; +import org.testng.ISuiteListener; import org.testng.ITestResult; import org.testng.TestListenerAdapter; import org.testng.TestNG; @@ -131,4 +134,21 @@ public void testNativeInjectionAndParamsInjection() { testng.run(); assertThat(listener.getPassedTests().isEmpty()).isFalse(); } + + @Test(description = "GITHUB-581") + public void ensureParametersSpecifiedInsideSuiteFilesTagAreIgnored() { + TestNG testng = create(); + testng.setTestSuites( + Collections.singletonList("src/test/resources/parametertest/issue_581/parent_suite.xml")); + Map> actualParameters = new HashMap<>(); + testng.addListener( + new ISuiteListener() { + @Override + public void onStart(ISuite suite) { + actualParameters.put(suite.getName(), suite.getXmlSuite().getAllParameters()); + } + }); + testng.run(); + actualParameters.values().forEach(each -> assertThat(each).isEmpty()); + } } diff --git a/testng-core/src/test/java/test/parameters/issue581/TestClassSample.java b/testng-core/src/test/java/test/parameters/issue581/TestClassSample.java new file mode 100644 index 0000000000..fb21ec245f --- /dev/null +++ b/testng-core/src/test/java/test/parameters/issue581/TestClassSample.java @@ -0,0 +1,12 @@ +package test.parameters.issue581; + +import org.testng.annotations.Optional; +import org.testng.annotations.Parameters; +import org.testng.annotations.Test; + +public class TestClassSample { + + @Parameters(value = "url") + @Test + public void test(@Optional String ignored) {} +} diff --git a/testng-core/src/test/resources/parametertest/issue_581/parent_suite.xml b/testng-core/src/test/resources/parametertest/issue_581/parent_suite.xml new file mode 100644 index 0000000000..268285ba28 --- /dev/null +++ b/testng-core/src/test/resources/parametertest/issue_581/parent_suite.xml @@ -0,0 +1,11 @@ + + + + + + + + + + + diff --git a/testng-core/src/test/resources/parametertest/issue_581/suite_one.xml b/testng-core/src/test/resources/parametertest/issue_581/suite_one.xml new file mode 100644 index 0000000000..e8a1c79820 --- /dev/null +++ b/testng-core/src/test/resources/parametertest/issue_581/suite_one.xml @@ -0,0 +1,8 @@ + + + + + + + + diff --git a/testng-core/src/test/resources/parametertest/issue_581/suite_two.xml b/testng-core/src/test/resources/parametertest/issue_581/suite_two.xml new file mode 100644 index 0000000000..d584d6cbdb --- /dev/null +++ b/testng-core/src/test/resources/parametertest/issue_581/suite_two.xml @@ -0,0 +1,8 @@ + + + + + + + +