Skip to content

Commit

Permalink
refacto: use the same logic once
Browse files Browse the repository at this point in the history
  • Loading branch information
juherr authored and krmahadevan committed Feb 18, 2024
1 parent 66aaffc commit f657690
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 92 deletions.
28 changes: 6 additions & 22 deletions testng-core/src/main/java/org/testng/DataProviderHolder.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package org.testng;

import static org.testng.ListenerComparator.*;

import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import org.testng.collections.Lists;
import org.testng.collections.Maps;
import org.testng.collections.Sets;
import org.testng.internal.IConfiguration;
Expand All @@ -18,33 +17,18 @@ public class DataProviderHolder {

private final Map<Class<?>, IDataProviderListener> listeners = Maps.newConcurrentMap();
private final Collection<IDataProviderInterceptor> interceptors = Sets.newHashSet();

private final IConfiguration configuration;
private final ListenerComparator listenerComparator;

public DataProviderHolder(IConfiguration configuration) {
this.configuration = configuration;
this.listenerComparator = Objects.requireNonNull(configuration).getListenerComparator();
}

public Collection<IDataProviderListener> getListeners() {
List<IDataProviderListener> original = Lists.newArrayList(listeners.values());
ListenerComparator comparator = getConfiguration().getListenerComparator();
if (comparator != null) {
original.sort(comparator::compare);
}
return Collections.unmodifiableCollection(original);
}

public IConfiguration getConfiguration() {
return Objects.requireNonNull(configuration);
return sort(listeners.values(), listenerComparator);
}

public Collection<IDataProviderInterceptor> getInterceptors() {
List<IDataProviderInterceptor> original = Lists.newArrayList(interceptors);
ListenerComparator comparator = getConfiguration().getListenerComparator();
if (comparator != null) {
original.sort(comparator::compare);
}
return Collections.unmodifiableCollection(original);
return sort(interceptors, listenerComparator);
}

public void addListeners(Collection<IDataProviderListener> listeners) {
Expand Down
32 changes: 24 additions & 8 deletions testng-core/src/main/java/org/testng/ListenerComparator.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
package org.testng;

import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import org.testng.collections.Lists;

/**
* Listener interface that can be used to determine listener execution order. This interface will
* NOT be used to determine execution order for {@link IReporter} implementations.
Expand All @@ -14,13 +20,23 @@
* </ol>
*/
@FunctionalInterface
public interface ListenerComparator {
public interface ListenerComparator extends Comparator<ITestNGListener> {
static <T extends ITestNGListener> List<T> sort(List<T> list, ListenerComparator comparator) {
if (comparator == null) {
return Collections.unmodifiableList(list);
}
List<T> original = Lists.newArrayList(list);
original.sort(comparator);
return Collections.unmodifiableList(original);
}

/**
* @param l1 - First {@link ITestNGListener} object to be compared.
* @param l2 - Second {@link ITestNGListener} object to be compared.
* @return - a negative integer, zero, or a positive integer as the first argument is less than,
* equal to, or greater than the second.
*/
int compare(ITestNGListener l1, ITestNGListener l2);
static <T extends ITestNGListener> Collection<T> sort(
Collection<T> list, ListenerComparator comparator) {
if (comparator == null) {
return Collections.unmodifiableCollection(list);
}
List<T> original = Lists.newArrayList(list);
original.sort(comparator);
return Collections.unmodifiableCollection(original);
}
}
8 changes: 3 additions & 5 deletions testng-core/src/main/java/org/testng/SuiteRunner.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
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 @@ -236,11 +237,8 @@ public ITestListener getExitCodeListener() {
}

private void invokeListeners(boolean start) {
List<ISuiteListener> original = Lists.newArrayList(listeners.values());
ListenerComparator comparator = this.configuration.getListenerComparator();
if (comparator != null) {
original.sort(comparator::compare);
}
Collection<ISuiteListener> original =
sort(listeners.values(), this.configuration.getListenerComparator());
if (start) {
for (ISuiteListener sl : ListenerOrderDeterminer.order(original)) {
sl.onStart(this);
Expand Down
13 changes: 5 additions & 8 deletions testng-core/src/main/java/org/testng/TestNG.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.testng;

import static org.testng.ListenerComparator.sort;
import static org.testng.internal.Utils.defaultIfStringEmpty;
import static org.testng.internal.Utils.isStringEmpty;
import static org.testng.internal.Utils.isStringNotEmpty;
Expand Down Expand Up @@ -1138,20 +1139,16 @@ protected List<ISuite> runSuites() {
}

private void runSuiteAlterationListeners() {
List<IAlterSuiteListener> original = Lists.newArrayList(m_alterSuiteListeners.values());
Optional.ofNullable(m_configuration.getListenerComparator())
.ifPresent(it -> original.sort(it::compare));

Collection<IAlterSuiteListener> original =
sort(m_alterSuiteListeners.values(), m_configuration.getListenerComparator());
for (IAlterSuiteListener l : original) {
l.alter(m_suites);
}
}

private void runExecutionListeners(boolean start) {
List<IExecutionListener> original = m_configuration.getExecutionListeners();
Optional.ofNullable(m_configuration.getListenerComparator())
.ifPresent(it -> original.sort(it::compare));

List<IExecutionListener> original =
sort(m_configuration.getExecutionListeners(), m_configuration.getListenerComparator());
List<IExecutionListener> executionListeners = ListenerOrderDeterminer.order(original);
if (start) {
for (IExecutionListener l : executionListeners) {
Expand Down
23 changes: 6 additions & 17 deletions testng-core/src/main/java/org/testng/TestRunner.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.testng;

import static org.testng.ListenerComparator.sort;
import static org.testng.internal.MethodHelper.fixMethodsWithClass;

import java.lang.reflect.Method;
Expand Down Expand Up @@ -695,12 +696,8 @@ private void privateRun(XmlTest xmlTest) {
}
}

List<IExecutionVisualiser> original = Lists.newArrayList(this.visualisers);
ListenerComparator listenerComparator = m_configuration.getListenerComparator();
if (listenerComparator != null) {
original.sort(listenerComparator::compare);
}

Collection<IExecutionVisualiser> original =
sort(this.visualisers, m_configuration.getListenerComparator());
graph.setVisualisers(Sets.newLinkedHashSet(original));
// In some cases, additional sorting is needed to make sure tests run in the appropriate order.
// If the user specified a method interceptor, or if we have any methods that have a non-default
Expand Down Expand Up @@ -745,12 +742,8 @@ private ITestNGMethod[] intercept(ITestNGMethod[] methods) {
List<IMethodInstance> methodInstances =
MethodHelper.methodsToMethodInstances(Arrays.asList(methods));

List<IMethodInterceptor> original = Lists.newArrayList(m_methodInterceptors);
ListenerComparator listenerComparator = m_configuration.getListenerComparator();
if (listenerComparator != null) {
original.sort(listenerComparator::compare);
}

List<IMethodInterceptor> original =
sort(m_methodInterceptors, m_configuration.getListenerComparator());
for (IMethodInterceptor m_methodInterceptor : original) {
methodInstances = m_methodInterceptor.intercept(methodInstances, this);
}
Expand Down Expand Up @@ -859,11 +852,7 @@ private void logStart() {
* finish
*/
private void fireEvent(boolean isStart) {
List<ITestListener> original = Lists.newArrayList(m_testListeners);
ListenerComparator listenerComparator = m_configuration.getListenerComparator();
if (listenerComparator != null) {
original.sort(listenerComparator::compare);
}
List<ITestListener> original = sort(m_testListeners, m_configuration.getListenerComparator());
if (isStart) {
for (ITestListener itl : ListenerOrderDeterminer.order(original)) {
itl.onStart(this);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package org.testng.internal.invokers;

import static org.testng.ListenerComparator.sort;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.testng.IInvokedMethod;
Expand All @@ -10,10 +11,8 @@
import org.testng.ITestContext;
import org.testng.ITestNGMethod;
import org.testng.ITestResult;
import org.testng.ListenerComparator;
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 @@ -71,11 +70,8 @@ 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;
List<IInvokedMethodListener> original = Lists.newArrayList(m_invokedMethodListeners);
ListenerComparator comparator = m_configuration.getListenerComparator();
if (comparator != null) {
original.sort(comparator::compare);
}
Collection<IInvokedMethodListener> original =
sort(m_invokedMethodListeners, m_configuration.getListenerComparator());
Collection<IInvokedMethodListener> listeners =
isAfterInvocation
? ListenerOrderDeterminer.reversedOrder(original)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
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.SAME_CLASS;
Expand All @@ -22,12 +23,10 @@
import org.testng.ITestContext;
import org.testng.ITestNGMethod;
import org.testng.ITestResult;
import org.testng.ListenerComparator;
import org.testng.Reporter;
import org.testng.SuiteRunState;
import org.testng.TestNGException;
import org.testng.annotations.IConfigurationAnnotation;
import org.testng.collections.Lists;
import org.testng.collections.Maps;
import org.testng.collections.Sets;
import org.testng.internal.ClassHelper;
Expand Down Expand Up @@ -442,11 +441,7 @@ private IConfigurable computeConfigurableInstance(

private void runConfigurationListeners(ITestResult tr, ITestNGMethod tm, boolean before) {
List<IConfigurationListener> original =
Lists.newArrayList(m_notifier.getConfigurationListeners());
ListenerComparator comparator = m_configuration.getListenerComparator();
if (comparator != null) {
original.sort(comparator::compare);
}
sort(m_notifier.getConfigurationListeners(), m_configuration.getListenerComparator());
if (before) {
TestListenerHelper.runPreConfigurationListeners(
tr, tm, original, internalConfigurationListener);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
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 @@ -34,7 +35,6 @@
import org.testng.ITestListener;
import org.testng.ITestNGMethod;
import org.testng.ITestResult;
import org.testng.ListenerComparator;
import org.testng.Reporter;
import org.testng.SkipException;
import org.testng.SuiteRunState;
Expand Down Expand Up @@ -257,11 +257,8 @@ 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 = m_notifier.getTestListeners();
ListenerComparator comparator = m_configuration.getListenerComparator();
if (comparator != null) {
original.sort(comparator::compare);
}
List<ITestListener> original =
sort(m_notifier.getTestListeners(), m_configuration.getListenerComparator());
List<ITestListener> listeners =
isFinished
? ListenerOrderDeterminer.reversedOrder(original)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.testng.internal.invokers;

import static org.testng.ListenerComparator.sort;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
Expand All @@ -13,7 +15,6 @@
import org.testng.ITestContext;
import org.testng.ITestNGMethod;
import org.testng.ITestResult;
import org.testng.ListenerComparator;
import org.testng.collections.Lists;
import org.testng.collections.Sets;
import org.testng.internal.*;
Expand Down Expand Up @@ -166,11 +167,8 @@ protected void invokeBeforeClassMethods(ITestClass testClass, IMethodInstance mi
Object instance = mi.getInstance();
if (!instances.contains(instance)) {
instances.add(instance);
List<IClassListener> original = Lists.newArrayList(m_listeners);
ListenerComparator comparator = m_configInvoker.getConfiguration().getListenerComparator();
if (comparator != null) {
original.sort(comparator::compare);
}
List<IClassListener> original =
sort(m_listeners, m_configInvoker.getConfiguration().getListenerComparator());
for (IClassListener listener : original) {
listener.onBeforeClass(testClass);
}
Expand Down Expand Up @@ -237,11 +235,8 @@ private void invokeAfterClassConfigurations(ITestClass testClass, List<Object> i
}

private void invokeListenersOnAfterClass(ITestClass testClass, List<IClassListener> listeners) {
List<IClassListener> original = Lists.newArrayList(listeners);
ListenerComparator comparator = m_configInvoker.getConfiguration().getListenerComparator();
if (comparator != null) {
original.sort(comparator::compare);
}
List<IClassListener> original =
sort(listeners, m_configInvoker.getConfiguration().getListenerComparator());
for (IClassListener listener : original) {
listener.onAfterClass(testClass);
}
Expand Down

0 comments on commit f657690

Please sign in to comment.