Skip to content

Commit

Permalink
Issue #2469 (#2470)
Browse files Browse the repository at this point in the history
Fix for "Parameters added in XmlTest during AlterSuiteListener not available in SuiteListener" 

Closes #2469
  • Loading branch information
aditya-qapitol authored Feb 3, 2021
1 parent d882ce7 commit 7fed142
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 11 deletions.
1 change: 1 addition & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
Current
Fixed: GITHUB-2467: XmlTest does not copy the xmlClasses during clone (C.V.Aditya)
Fixed: GITHUB-2469: Parameters added in XmlTest during AlterSuiteListener not available in SuiteListener (C.V.Aditya)
Fixed: GITHUB-2296: Fix for assertEquals not working for sets as order is not guaranteed. (Prashant Maroti)
Fixed: GITHUB-2465: Fix bux where Strings.join returns empty String
Fixed: GITHUB-1632: throwing SkipException sets iTestResult status to Failure instead of Skip (Julien Herr & Krishnan Mahadevan)
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/org/testng/xml/XmlTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,9 @@ public Object clone() {
result.setJUnit(isJUnit());
result.setParallel(getParallel());
result.setVerbose(getVerbose());
result.setParameters(getLocalParameters());
Map<String, String> localParameters = new HashMap<>();
localParameters.putAll(getLocalParameters());
result.setParameters(localParameters);
result.setXmlPackages(getXmlPackages());
result.setTimeOut(getTimeOut());
result.setXmlClasses(getXmlClasses());
Expand Down
80 changes: 70 additions & 10 deletions src/test/java/test/listeners/AlterSuiteListenerTest.java
Original file line number Diff line number Diff line change
@@ -1,51 +1,78 @@
package test.listeners;

import org.testng.Assert;
import org.testng.IAlterSuiteListener;
import org.testng.TestNG;
import org.testng.*;
import org.testng.annotations.Test;
import org.testng.internal.collections.Pair;
import org.testng.xml.XmlSuite;
import org.testng.xml.XmlTest;
import test.SimpleBaseTest;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;

public class AlterSuiteListenerTest extends SimpleBaseTest {

public static final String ALTER_SUITE_LISTENER = "AlterSuiteListener";

@Test
public void executionListenerWithXml() {
XmlSuite suite = runTest(AlterSuiteListener1SampleTest.class, AlterSuiteNameListener.class.getName());
XmlSuite suite = runTest(AlterSuiteListener1SampleTest.class, AlterSuiteNameListener.class.getName()).second();
Assert.assertEquals(suite.getName(), AlterSuiteNameListener.class.getSimpleName());
}

@Test
public void executionListenerWithoutListener() {
XmlSuite suite = runTest(AlterSuiteListener1SampleTest.class, null/*Donot add the listener*/);
XmlSuite suite = runTest(AlterSuiteListener1SampleTest.class).second();
Assert.assertEquals(suite.getName(), ALTER_SUITE_LISTENER);
}

@Test
public void executionListenerWithXml2() {
XmlSuite suite = runTest(AlterSuiteListener1SampleTest.class, AlterXmlTestsInSuiteListener.class.getName());
XmlSuite suite = runTest(AlterSuiteListener1SampleTest.class, AlterXmlTestsInSuiteListener.class.getName()).second();
Assert.assertEquals(suite.getTests().size(), 2);
}

@Test(description = "GITHUB-2469")
public void executionListenerWithXml3() {
Pair<TestNG, XmlSuite> retObjects = runTest(AlterSuiteListener1SampleTest.class,
AlterXmlTestWithParameterInSuiteListener.class.getName(), AlteredXmlSuiteReadListener.class.getName());
TestNG tng = retObjects.first();
XmlSuite suite = retObjects.second();
Assert.assertEquals(suite.getTests().size(), 2);
List<ISuiteListener> listeners = Optional.ofNullable(tng.getSuiteListeners()).orElse(new ArrayList<>());
Assert.assertFalse(listeners.isEmpty());
for (ISuiteListener iSuiteListener :
listeners) {
if (iSuiteListener instanceof AlteredXmlSuiteReadListener) {
AlteredXmlSuiteReadListener alteredXmlSuiteReadListener = (AlteredXmlSuiteReadListener) iSuiteListener;
XmlSuite xmlSuite = alteredXmlSuiteReadListener.currentSuiteOnStart.getXmlSuite();
List<XmlTest> tests = xmlSuite.getTests();
int i = 1;
for (XmlTest xmlTest:
tests) {
Assert.assertEquals(xmlTest.getParameter("param"), String.valueOf(i));
i++;
}
}
}
}

private XmlSuite runTest(Class<?> listenerClass, String listenerName) {
private Pair<TestNG, XmlSuite> runTest(Class<?> listenerClass, String... listenerNames) {
XmlSuite s = createXmlSuite(ALTER_SUITE_LISTENER);
createXmlTest(s, "Test", listenerClass.getName());
boolean addListener = (listenerName != null);

if (addListener) {

for (String listenerName:
listenerNames) {
s.addListener(listenerName);
}
TestNG tng = create();
tng.setXmlSuites(Arrays.asList(s));
tng.run();
return s;
Pair<TestNG, XmlSuite> returnObj = new Pair<>(tng, s);
return returnObj;
}

public static class AlterSuiteListener1SampleTest {
Expand Down Expand Up @@ -78,4 +105,37 @@ public void alter(List<XmlSuite> suites) {
}
}

public static class AlterXmlTestWithParameterInSuiteListener implements IAlterSuiteListener {

@Override
public void alter(List<XmlSuite> suites) {
XmlSuite suite = suites.get(0);
List<XmlTest> tests = suite.getTests();
XmlTest test = tests.get(0);

List<XmlTest> newXmlTests = new ArrayList<>();
XmlTest newXmlTest = (XmlTest) test.clone();
newXmlTest.setName("name_1");
newXmlTest.addParameter("param", "1");
newXmlTests.add(newXmlTest);

newXmlTest = (XmlTest) test.clone();
newXmlTest.setName("name_2");
newXmlTest.addParameter("param", "2");
newXmlTests.add(newXmlTest);

suite.setTests(newXmlTests);
}
}

public static class AlteredXmlSuiteReadListener implements ISuiteListener {

public ISuite currentSuiteOnStart;

@Override
public void onStart(ISuite suite) {
currentSuiteOnStart = suite;
}
}

}
6 changes: 6 additions & 0 deletions src/test/resources/testng.xml
Original file line number Diff line number Diff line change
Expand Up @@ -995,5 +995,11 @@
<class name="test.timeout.github2440.TimeoutWithNoExecutorTest"/>
</classes>
</test>

<test name="GITHUB-2469">
<classes>
<class name="test.listeners.AlterSuiteListenerTest"/>
</classes>
</test>
</suite>

0 comments on commit 7fed142

Please sign in to comment.