Skip to content

Commit

Permalink
refacto: listener order determiner make the sort
Browse files Browse the repository at this point in the history
  • Loading branch information
juherr committed Feb 18, 2024
1 parent f657690 commit e5591a5
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 25 deletions.
11 changes: 6 additions & 5 deletions testng-core/src/main/java/org/testng/SuiteRunner.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.testng;

import static org.testng.ListenerComparator.sort;
import static org.testng.internal.Utils.isStringBlank;

import com.google.inject.Injector;
Expand Down Expand Up @@ -237,14 +236,16 @@ public ITestListener getExitCodeListener() {
}

private void invokeListeners(boolean start) {
Collection<ISuiteListener> original =
sort(listeners.values(), this.configuration.getListenerComparator());
if (start) {
for (ISuiteListener sl : ListenerOrderDeterminer.order(original)) {
for (ISuiteListener sl :
ListenerOrderDeterminer.order(
listeners.values(), this.configuration.getListenerComparator())) {
sl.onStart(this);
}
} else {
List<ISuiteListener> suiteListenersReversed = ListenerOrderDeterminer.reversedOrder(original);
List<ISuiteListener> suiteListenersReversed =
ListenerOrderDeterminer.reversedOrder(
listeners.values(), this.configuration.getListenerComparator());
for (ISuiteListener sl : suiteListenersReversed) {
sl.onFinish(this);
}
Expand Down
9 changes: 5 additions & 4 deletions testng-core/src/main/java/org/testng/TestNG.java
Original file line number Diff line number Diff line change
Expand Up @@ -1147,9 +1147,9 @@ private void runSuiteAlterationListeners() {
}

private void runExecutionListeners(boolean start) {
List<IExecutionListener> original =
sort(m_configuration.getExecutionListeners(), m_configuration.getListenerComparator());
List<IExecutionListener> executionListeners = ListenerOrderDeterminer.order(original);
List<IExecutionListener> executionListeners =
ListenerOrderDeterminer.order(
m_configuration.getExecutionListeners(), m_configuration.getListenerComparator());
if (start) {
for (IExecutionListener l : executionListeners) {
l.onExecutionStart();
Expand All @@ -1158,7 +1158,8 @@ private void runExecutionListeners(boolean start) {
exitCodeListener.onExecutionStart();
} else {
List<IExecutionListener> executionListenersReversed =
ListenerOrderDeterminer.reversedOrder(executionListeners);
ListenerOrderDeterminer.reversedOrder(
m_configuration.getExecutionListeners(), m_configuration.getListenerComparator());
for (IExecutionListener l : executionListenersReversed) {
l.onExecutionFinish();
}
Expand Down
8 changes: 5 additions & 3 deletions testng-core/src/main/java/org/testng/TestRunner.java
Original file line number Diff line number Diff line change
Expand Up @@ -852,15 +852,17 @@ private void logStart() {
* finish
*/
private void fireEvent(boolean isStart) {
List<ITestListener> original = sort(m_testListeners, m_configuration.getListenerComparator());
if (isStart) {
for (ITestListener itl : ListenerOrderDeterminer.order(original)) {
for (ITestListener itl :
ListenerOrderDeterminer.order(m_testListeners, m_configuration.getListenerComparator())) {
itl.onStart(this);
}
this.exitCodeListener.onStart(this);

} else {
List<ITestListener> testListenersReversed = ListenerOrderDeterminer.reversedOrder(original);
List<ITestListener> testListenersReversed =
ListenerOrderDeterminer.reversedOrder(
m_testListeners, m_configuration.getListenerComparator());
for (ITestListener itl : testListenersReversed) {
itl.onFinish(this);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package org.testng.internal;

import static org.testng.ListenerComparator.sort;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import org.testng.ITestNGListener;
import org.testng.ListenerComparator;
import org.testng.collections.Lists;
import org.testng.internal.collections.Pair;

Expand Down Expand Up @@ -43,7 +47,9 @@ private ListenerOrderDeterminer() {
* @param original - The original collection of listeners
* @return - A re-ordered collection wherein preferential listeners are added at the end
*/
public static <T> List<T> order(Collection<T> original) {
public static <T extends ITestNGListener> List<T> order(
Collection<T> original, ListenerComparator comparator) {
original = sort(original, comparator);
Pair<List<T>, List<T>> ordered = arrange(original);
List<T> ideListeners = ordered.first();
List<T> regularListeners = ordered.second();
Expand All @@ -55,7 +61,9 @@ public static <T> List<T> order(Collection<T> original) {
* @return - A reversed ordered list wherein the user listeners are found in reverse order
* followed by preferential listeners also in reverse order.
*/
public static <T> List<T> reversedOrder(Collection<T> original) {
public static <T extends ITestNGListener> List<T> reversedOrder(
Collection<T> original, ListenerComparator comparator) {
original = sort(original, comparator);
Pair<List<T>, List<T>> ordered = arrange(original);
List<T> preferentialListeners = ordered.first();
List<T> regularListeners = ordered.second();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package org.testng.internal.invokers;

import static org.testng.ListenerComparator.sort;

import java.util.Collection;
import java.util.Map;
import java.util.Set;
Expand Down Expand Up @@ -70,12 +68,12 @@ protected void runInvokedMethodListeners(
// For BEFORE_INVOCATION method, still run as insert order, but regarding AFTER_INVOCATION, it
// should be reverse order
boolean isAfterInvocation = InvokedMethodListenerMethod.AFTER_INVOCATION == listenerMethod;
Collection<IInvokedMethodListener> original =
sort(m_invokedMethodListeners, m_configuration.getListenerComparator());
Collection<IInvokedMethodListener> listeners =
isAfterInvocation
? ListenerOrderDeterminer.reversedOrder(original)
: ListenerOrderDeterminer.order(original);
? ListenerOrderDeterminer.reversedOrder(
m_invokedMethodListeners, m_configuration.getListenerComparator())
: ListenerOrderDeterminer.order(
m_invokedMethodListeners, m_configuration.getListenerComparator());
if (!isAfterInvocation) {
suiteRunner.beforeInvocation(invokedMethod, testResult);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.testng.internal.invokers;

import static org.testng.ListenerComparator.sort;
import static org.testng.internal.invokers.InvokedMethodListenerMethod.AFTER_INVOCATION;
import static org.testng.internal.invokers.InvokedMethodListenerMethod.BEFORE_INVOCATION;
import static org.testng.internal.invokers.Invoker.CAN_RUN_FROM_CLASS;
Expand Down Expand Up @@ -257,12 +256,12 @@ public void runTestResultListener(ITestResult tr) {
// but regarding
// onTestSkipped/onTestFailedButWithinSuccessPercentage/onTestFailedWithTimeout/onTestFailure/onTestSuccess, it should be reverse order.
boolean isFinished = tr.getStatus() != ITestResult.STARTED;
List<ITestListener> original =
sort(m_notifier.getTestListeners(), m_configuration.getListenerComparator());
List<ITestListener> listeners =
isFinished
? ListenerOrderDeterminer.reversedOrder(original)
: ListenerOrderDeterminer.order(original);
? ListenerOrderDeterminer.reversedOrder(
m_notifier.getTestListeners(), m_configuration.getListenerComparator())
: ListenerOrderDeterminer.order(
m_notifier.getTestListeners(), m_configuration.getListenerComparator());
TestListenerHelper.runTestListeners(tr, listeners);
TestListenerHelper.runTestListeners(
tr, Collections.singletonList(m_notifier.getExitCodeListener()));
Expand Down

0 comments on commit e5591a5

Please sign in to comment.