From 779b3b39d9727fc63d7a2b4980637a8a77878276 Mon Sep 17 00:00:00 2001 From: Julien Herr Date: Sun, 18 Feb 2024 15:16:10 +0100 Subject: [PATCH] refacto: use the same logic once --- .../java/org/testng/DataProviderHolder.java | 28 ++++------------ .../java/org/testng/ListenerComparator.java | 32 ++++++++++++++----- .../src/main/java/org/testng/SuiteRunner.java | 8 ++--- .../src/main/java/org/testng/TestNG.java | 13 +++----- .../src/main/java/org/testng/TestRunner.java | 23 ++++--------- .../testng/internal/invokers/BaseInvoker.java | 12 +++---- .../internal/invokers/ConfigInvoker.java | 9 ++---- .../testng/internal/invokers/TestInvoker.java | 9 ++---- .../internal/invokers/TestMethodWorker.java | 17 ++++------ 9 files changed, 59 insertions(+), 92 deletions(-) diff --git a/testng-core/src/main/java/org/testng/DataProviderHolder.java b/testng-core/src/main/java/org/testng/DataProviderHolder.java index b5e862908..3a6b57102 100644 --- a/testng-core/src/main/java/org/testng/DataProviderHolder.java +++ b/testng-core/src/main/java/org/testng/DataProviderHolder.java @@ -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; @@ -18,33 +17,18 @@ public class DataProviderHolder { private final Map, IDataProviderListener> listeners = Maps.newConcurrentMap(); private final Collection 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 getListeners() { - List 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 getInterceptors() { - List 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 listeners) { diff --git a/testng-core/src/main/java/org/testng/ListenerComparator.java b/testng-core/src/main/java/org/testng/ListenerComparator.java index 44cf3fa4f..80097ff01 100644 --- a/testng-core/src/main/java/org/testng/ListenerComparator.java +++ b/testng-core/src/main/java/org/testng/ListenerComparator.java @@ -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. @@ -14,13 +20,23 @@ * */ @FunctionalInterface -public interface ListenerComparator { +public interface ListenerComparator extends Comparator { + static List sort(List list, ListenerComparator comparator) { + if (comparator == null) { + return Collections.unmodifiableList(list); + } + List 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 Collection sort( + Collection list, ListenerComparator comparator) { + if (comparator == null) { + return Collections.unmodifiableCollection(list); + } + List original = Lists.newArrayList(list); + original.sort(comparator); + return Collections.unmodifiableCollection(original); + } } diff --git a/testng-core/src/main/java/org/testng/SuiteRunner.java b/testng-core/src/main/java/org/testng/SuiteRunner.java index 5a8844ca1..81f23479f 100644 --- a/testng-core/src/main/java/org/testng/SuiteRunner.java +++ b/testng-core/src/main/java/org/testng/SuiteRunner.java @@ -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; @@ -236,11 +237,8 @@ public ITestListener getExitCodeListener() { } private void invokeListeners(boolean start) { - List original = Lists.newArrayList(listeners.values()); - ListenerComparator comparator = this.configuration.getListenerComparator(); - if (comparator != null) { - original.sort(comparator::compare); - } + Collection original = + sort(listeners.values(), this.configuration.getListenerComparator()); if (start) { for (ISuiteListener sl : ListenerOrderDeterminer.order(original)) { sl.onStart(this); diff --git a/testng-core/src/main/java/org/testng/TestNG.java b/testng-core/src/main/java/org/testng/TestNG.java index c20d6d961..167b29258 100644 --- a/testng-core/src/main/java/org/testng/TestNG.java +++ b/testng-core/src/main/java/org/testng/TestNG.java @@ -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; @@ -1138,20 +1139,16 @@ protected List runSuites() { } private void runSuiteAlterationListeners() { - List original = Lists.newArrayList(m_alterSuiteListeners.values()); - Optional.ofNullable(m_configuration.getListenerComparator()) - .ifPresent(it -> original.sort(it::compare)); - + Collection original = + sort(m_alterSuiteListeners.values(), m_configuration.getListenerComparator()); for (IAlterSuiteListener l : original) { l.alter(m_suites); } } private void runExecutionListeners(boolean start) { - List original = m_configuration.getExecutionListeners(); - Optional.ofNullable(m_configuration.getListenerComparator()) - .ifPresent(it -> original.sort(it::compare)); - + List original = + sort(m_configuration.getExecutionListeners(), m_configuration.getListenerComparator()); List executionListeners = ListenerOrderDeterminer.order(original); if (start) { for (IExecutionListener l : executionListeners) { diff --git a/testng-core/src/main/java/org/testng/TestRunner.java b/testng-core/src/main/java/org/testng/TestRunner.java index 23521be22..86bbb27a9 100644 --- a/testng-core/src/main/java/org/testng/TestRunner.java +++ b/testng-core/src/main/java/org/testng/TestRunner.java @@ -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; @@ -695,12 +696,8 @@ private void privateRun(XmlTest xmlTest) { } } - List original = Lists.newArrayList(this.visualisers); - ListenerComparator listenerComparator = m_configuration.getListenerComparator(); - if (listenerComparator != null) { - original.sort(listenerComparator::compare); - } - + Collection 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 @@ -745,12 +742,8 @@ private ITestNGMethod[] intercept(ITestNGMethod[] methods) { List methodInstances = MethodHelper.methodsToMethodInstances(Arrays.asList(methods)); - List original = Lists.newArrayList(m_methodInterceptors); - ListenerComparator listenerComparator = m_configuration.getListenerComparator(); - if (listenerComparator != null) { - original.sort(listenerComparator::compare); - } - + List original = + sort(m_methodInterceptors, m_configuration.getListenerComparator()); for (IMethodInterceptor m_methodInterceptor : original) { methodInstances = m_methodInterceptor.intercept(methodInstances, this); } @@ -859,11 +852,7 @@ private void logStart() { * finish */ private void fireEvent(boolean isStart) { - List original = Lists.newArrayList(m_testListeners); - ListenerComparator listenerComparator = m_configuration.getListenerComparator(); - if (listenerComparator != null) { - original.sort(listenerComparator::compare); - } + List original = sort(m_testListeners, m_configuration.getListenerComparator()); if (isStart) { for (ITestListener itl : ListenerOrderDeterminer.order(original)) { itl.onStart(this); diff --git a/testng-core/src/main/java/org/testng/internal/invokers/BaseInvoker.java b/testng-core/src/main/java/org/testng/internal/invokers/BaseInvoker.java index 84cdab6e6..2274d7970 100644 --- a/testng-core/src/main/java/org/testng/internal/invokers/BaseInvoker.java +++ b/testng-core/src/main/java/org/testng/internal/invokers/BaseInvoker.java @@ -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; @@ -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; @@ -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 original = Lists.newArrayList(m_invokedMethodListeners); - ListenerComparator comparator = m_configuration.getListenerComparator(); - if (comparator != null) { - original.sort(comparator::compare); - } + Collection original = + sort(m_invokedMethodListeners, m_configuration.getListenerComparator()); Collection listeners = isAfterInvocation ? ListenerOrderDeterminer.reversedOrder(original) diff --git a/testng-core/src/main/java/org/testng/internal/invokers/ConfigInvoker.java b/testng-core/src/main/java/org/testng/internal/invokers/ConfigInvoker.java index d33ecba3e..d1536dee2 100644 --- a/testng-core/src/main/java/org/testng/internal/invokers/ConfigInvoker.java +++ b/testng-core/src/main/java/org/testng/internal/invokers/ConfigInvoker.java @@ -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; @@ -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; @@ -442,11 +441,7 @@ private IConfigurable computeConfigurableInstance( private void runConfigurationListeners(ITestResult tr, ITestNGMethod tm, boolean before) { List 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); diff --git a/testng-core/src/main/java/org/testng/internal/invokers/TestInvoker.java b/testng-core/src/main/java/org/testng/internal/invokers/TestInvoker.java index 81ddf0c73..e0d332e61 100644 --- a/testng-core/src/main/java/org/testng/internal/invokers/TestInvoker.java +++ b/testng-core/src/main/java/org/testng/internal/invokers/TestInvoker.java @@ -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; @@ -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; @@ -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 original = m_notifier.getTestListeners(); - ListenerComparator comparator = m_configuration.getListenerComparator(); - if (comparator != null) { - original.sort(comparator::compare); - } + List original = + sort(m_notifier.getTestListeners(), m_configuration.getListenerComparator()); List listeners = isFinished ? ListenerOrderDeterminer.reversedOrder(original) diff --git a/testng-core/src/main/java/org/testng/internal/invokers/TestMethodWorker.java b/testng-core/src/main/java/org/testng/internal/invokers/TestMethodWorker.java index 9a3821252..248022fd3 100644 --- a/testng-core/src/main/java/org/testng/internal/invokers/TestMethodWorker.java +++ b/testng-core/src/main/java/org/testng/internal/invokers/TestMethodWorker.java @@ -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; @@ -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.*; @@ -166,11 +167,8 @@ protected void invokeBeforeClassMethods(ITestClass testClass, IMethodInstance mi Object instance = mi.getInstance(); if (!instances.contains(instance)) { instances.add(instance); - List original = Lists.newArrayList(m_listeners); - ListenerComparator comparator = m_configInvoker.getConfiguration().getListenerComparator(); - if (comparator != null) { - original.sort(comparator::compare); - } + List original = + sort(m_listeners, m_configInvoker.getConfiguration().getListenerComparator()); for (IClassListener listener : original) { listener.onBeforeClass(testClass); } @@ -237,11 +235,8 @@ private void invokeAfterClassConfigurations(ITestClass testClass, List i } private void invokeListenersOnAfterClass(ITestClass testClass, List listeners) { - List original = Lists.newArrayList(listeners); - ListenerComparator comparator = m_configInvoker.getConfiguration().getListenerComparator(); - if (comparator != null) { - original.sort(comparator::compare); - } + List original = + sort(listeners, m_configInvoker.getConfiguration().getListenerComparator()); for (IClassListener listener : original) { listener.onAfterClass(testClass); }