Skip to content

Commit

Permalink
Fix GITHUB-2558
Browse files Browse the repository at this point in the history
Now the listener start method and finished method worked with insertion
order, this commit changes the finished method worked with reverse
order.
  • Loading branch information
dianny chen committed Aug 16, 2021
1 parent a397e1b commit 0facded
Show file tree
Hide file tree
Showing 10 changed files with 62 additions and 23 deletions.
1 change: 1 addition & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
Current
Fixed: GITHUB-2558:Make IExecutionListener, ITestListener, IInvokedMethodListener, IConfigurationListener, ISuiteListener finish method with reverse order (dianny)
Fixed: GITHUB-2532: Apply commandline switches for suites in jar files (Dzmitry Sankouski).
Fixed: GITHUB-2558: Make IExecutionListener, ITestListener, IInvokedMethodListener, IConfigurationListener, ISuiteListener execute in the order of insertion (Krishnan Mahadevan)
Fixed: GITHUB-2611: Config Failures not included in testng-failed.xml when its part of a different test class (Krishnan Mahadevan)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,10 @@ public static <T> List<T> merge(List<T> l1, BiPredicate<T, T> condition, List<T>
});
return result;
}

public static <K> List<K> newReversedArrayList(Collection<K> c) {
List<K> list = newArrayList(c);
Collections.reverse(list);
return list;
}
}
10 changes: 7 additions & 3 deletions testng-core/src/main/java/org/testng/SuiteRunner.java
Original file line number Diff line number Diff line change
Expand Up @@ -252,10 +252,14 @@ public void setReportResults(boolean reportResults) {
}

private void invokeListeners(boolean start) {
for (ISuiteListener sl : Lists.newArrayList(listeners.values())) {
if (start) {
if (start) {
for (ISuiteListener sl : Lists.newArrayList(listeners.values())) {
sl.onStart(this);
} else {
}
} else {
List<ISuiteListener> suiteListenersReversed = new ArrayList<>(listeners.values());
Collections.reverse(suiteListenersReversed);
for (ISuiteListener sl : suiteListenersReversed) {
sl.onFinish(this);
}
}
Expand Down
11 changes: 8 additions & 3 deletions testng-core/src/main/java/org/testng/TestNG.java
Original file line number Diff line number Diff line change
Expand Up @@ -1083,10 +1083,15 @@ private void runSuiteAlterationListeners() {
}

private void runExecutionListeners(boolean start) {
for (IExecutionListener l : m_configuration.getExecutionListeners()) {
if (start) {
List<IExecutionListener> executionListeners = m_configuration.getExecutionListeners();
if (start) {
for (IExecutionListener l : executionListeners) {
l.onExecutionStart();
} else {
}
} else {
List<IExecutionListener> executionListenersReversed = new ArrayList<>(executionListeners);
Collections.reverse(executionListenersReversed);
for (IExecutionListener l : executionListenersReversed) {
l.onExecutionFinish();
}
}
Expand Down
14 changes: 10 additions & 4 deletions testng-core/src/main/java/org/testng/TestRunner.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
Expand Down Expand Up @@ -90,7 +91,7 @@ public class TestRunner
/** ITestListeners support. */
private final List<ITestListener> m_testListeners = Lists.newArrayList();

private final Set<IConfigurationListener> m_configurationListeners = Sets.newHashSet();
private final Set<IConfigurationListener> m_configurationListeners = Sets.newLinkedHashSet();
private final Set<IExecutionVisualiser> visualisers = Sets.newHashSet();

private final IConfigurationListener m_confListener = new ConfigurationListener();
Expand Down Expand Up @@ -931,10 +932,15 @@ private void logStart() {
* finish
*/
private void fireEvent(boolean isStart) {
for (ITestListener itl : m_testListeners) {
if (isStart) {
if (isStart) {
for (ITestListener itl : m_testListeners) {
itl.onStart(this);
} else {
}

} else {
List<ITestListener> testListenersReversed = new ArrayList<>(m_testListeners);
Collections.reverse(testListenersReversed);
for (ITestListener itl : testListenersReversed) {
itl.onFinish(this);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.testng.internal;

import java.util.Collections;
import java.util.List;
import org.testng.IClass;
import org.testng.IConfigurationListener;
Expand Down Expand Up @@ -35,7 +36,9 @@ public static void runPreConfigurationListeners(

public static void runPostConfigurationListeners(
ITestResult tr, ITestNGMethod tm, List<IConfigurationListener> listeners) {
for (IConfigurationListener icl : listeners) {
List<IConfigurationListener> listeners_reverted = Lists.newArrayList(listeners);
Collections.reverse(listeners_reverted);
for (IConfigurationListener icl : listeners_reverted) {
switch (tr.getStatus()) {
case ITestResult.SKIP:
icl.onConfigurationSkip(tr);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.testng.ITestResult;
import org.testng.SkipException;
import org.testng.SuiteRunState;
import org.testng.collections.Lists;
import org.testng.collections.Maps;
import org.testng.internal.IConfiguration;
import org.testng.internal.ITestResultNotifier;
Expand Down Expand Up @@ -54,7 +55,12 @@ protected void runInvokedMethodListeners(

InvokedMethodListenerInvoker invoker =
new InvokedMethodListenerInvoker(listenerMethod, testResult, testResult.getTestContext());
for (IInvokedMethodListener currentListener : m_invokedMethodListeners) {
boolean isBeforeInvocation = InvokedMethodListenerMethod.BEFORE_INVOCATION == listenerMethod;
Collection<IInvokedMethodListener> listeners =
isBeforeInvocation
? m_invokedMethodListeners
: Lists.newReversedArrayList(m_invokedMethodListeners);
for (IInvokedMethodListener currentListener : listeners) {
try {
invoker.invokeListener(currentListener, invokedMethod);
} catch (SkipException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.testng.ISuite;
import org.testng.ITestClass;
import org.testng.ITestContext;
import org.testng.ITestListener;
import org.testng.ITestNGMethod;
import org.testng.ITestResult;
import org.testng.Reporter;
Expand Down Expand Up @@ -232,7 +233,12 @@ public FailureContext retryFailed(
}

public void runTestResultListener(ITestResult tr) {
TestListenerHelper.runTestListeners(tr, m_notifier.getTestListeners());
boolean isStarted = tr.getStatus() == ITestResult.STARTED;
List<ITestListener> listeners =
isStarted
? m_notifier.getTestListeners()
: Lists.newReversedArrayList(m_notifier.getTestListeners());
TestListenerHelper.runTestListeners(tr, listeners);
}

private Collection<IDataProviderListener> dataProviderListeners() {
Expand Down
10 changes: 6 additions & 4 deletions testng-core/src/main/java/org/testng/junit/JUnitTestRunner.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@
import junit.framework.TestResult;
import junit.framework.TestSuite;
import org.testng.*;
import org.testng.ITestNGMethod;
import org.testng.ITestResult;
import org.testng.TestNGException;
import org.testng.collections.Lists;
import org.testng.internal.ITestResultNotifier;
import org.testng.internal.TestListenerHelper;
Expand Down Expand Up @@ -87,7 +84,12 @@ public void endTest(Test test) {

org.testng.internal.TestResult tr = recordResults(test, tri);

TestListenerHelper.runTestListeners(tr, m_parentRunner.getTestListeners());
boolean isStarted = tr.getStatus() == ITestResult.STARTED;
List<ITestListener> listeners =
isStarted
? m_parentRunner.getTestListeners()
: Lists.newReversedArrayList(m_parentRunner.getTestListeners());
TestListenerHelper.runTestListeners(tr, listeners);
}

public void setInvokedMethodListeners(Collection<IInvokedMethodListener> listeners) {
Expand Down
12 changes: 6 additions & 6 deletions testng-core/src/test/java/test/listeners/ListenerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -380,26 +380,26 @@ public void ensureInsertionOrderIsHonouredByListeners() {
prefix + "ClassMethodListenersHolder$ClassMethodListenerA.onBeforeClass()",
prefix + "ClassMethodListenersHolder$ClassMethodListenerB.beforeInvocation()",
prefix + "ClassMethodListenersHolder$ClassMethodListenerA.beforeInvocation()",
prefix + "ClassMethodListenersHolder$ClassMethodListenerB.afterInvocation()",
prefix + "ClassMethodListenersHolder$ClassMethodListenerA.afterInvocation()",
prefix + "ClassMethodListenersHolder$ClassMethodListenerB.afterInvocation()",
prefix + "ClassMethodListenersHolder$ClassMethodListenerB.onBeforeClass()",
prefix + "ClassMethodListenersHolder$ClassMethodListenerA.onBeforeClass()",
prefix + "ClassMethodListenersHolder$ClassMethodListenerB.onBeforeClass()",
prefix + "ClassMethodListenersHolder$ClassMethodListenerA.onBeforeClass()",
prefix + "ClassMethodListenersHolder$ClassMethodListenerB.beforeInvocation()",
prefix + "ClassMethodListenersHolder$ClassMethodListenerA.beforeInvocation()",
prefix + "ClassMethodListenersHolder$ClassMethodListenerB.afterInvocation()",
prefix + "ClassMethodListenersHolder$ClassMethodListenerA.afterInvocation()",
prefix + "ClassMethodListenersHolder$ClassMethodListenerB.afterInvocation()",
prefix + "ClassMethodListenersHolder$ClassMethodListenerB.onBeforeClass()",
prefix + "ClassMethodListenersHolder$ClassMethodListenerA.onBeforeClass()",
prefix + "TestListenersHolder$TestListenerB.onFinish(ctx)",
prefix + "TestListenersHolder$TestListenerA.onFinish(ctx)",
prefix + "SuiteListenersHolder$SuiteListenerB.onFinish()",
prefix + "TestListenersHolder$TestListenerB.onFinish(ctx)",
prefix + "SuiteListenersHolder$SuiteListenerA.onFinish()",
prefix + "SuiteListenersHolder$SuiteListenerB.onFinish()",
prefix + "ReportersHolder$ReporterB.generateReport()",
prefix + "ReportersHolder$ReporterA.generateReport()",
prefix + "ExecutionListenersHolder$ExecutionListenerB.onExecutionFinish()",
prefix + "ExecutionListenersHolder$ExecutionListenerA.onExecutionFinish()"
prefix + "ExecutionListenersHolder$ExecutionListenerA.onExecutionFinish()",
prefix + "ExecutionListenersHolder$ExecutionListenerB.onExecutionFinish()"
};
List<Class<?>> listeners =
Arrays.asList(
Expand Down

0 comments on commit 0facded

Please sign in to comment.