Skip to content

Commit c087eaa

Browse files
authored
Remove internal listeners from user listeners (#2872)
* Remove TestRunner.ConfigurationListener from user listeners * Remove SuiteRunner from user listeners * Formatting the code
1 parent 141aa0d commit c087eaa

File tree

10 files changed

+147
-56
lines changed

10 files changed

+147
-56
lines changed

testng-core/src/main/java/org/testng/SuiteRunner.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,6 @@ public <T> T newInstance(Constructor<T> constructor, Object... parameters) {
204204
Optional.ofNullable(invokedMethodListener).orElse(Collections.emptyList())) {
205205
invokedMethodListeners.put(listener.getClass(), listener);
206206
}
207-
invokedMethodListeners.put(getClass(), this);
208207

209208
skipFailedInvocationCounts = suite.skipFailedInvocationCounts();
210209
if (null != testListeners) {
@@ -289,7 +288,8 @@ private ITestRunnerFactory buildRunnerFactory(Comparator<ITestNGMethod> comparat
289288
testListeners.toArray(new ITestListener[0]),
290289
useDefaultListeners,
291290
skipFailedInvocationCounts,
292-
comparator);
291+
comparator,
292+
this);
293293
} else {
294294
factory =
295295
new ProxyTestRunnerFactory(testListeners.toArray(new ITestListener[0]), tmpRunnerFactory);
@@ -588,18 +588,21 @@ private static class DefaultTestRunnerFactory implements ITestRunnerFactory {
588588
private final boolean skipFailedInvocationCounts;
589589
private final IConfiguration configuration;
590590
private final Comparator<ITestNGMethod> comparator;
591+
private final SuiteRunner suiteRunner;
591592

592593
public DefaultTestRunnerFactory(
593594
IConfiguration configuration,
594595
ITestListener[] failureListeners,
595596
boolean useDefaultListeners,
596597
boolean skipFailedInvocationCounts,
597-
Comparator<ITestNGMethod> comparator) {
598+
Comparator<ITestNGMethod> comparator,
599+
SuiteRunner suiteRunner) {
598600
this.configuration = configuration;
599601
this.failureGenerators = failureListeners;
600602
this.useDefaultListeners = useDefaultListeners;
601603
this.skipFailedInvocationCounts = skipFailedInvocationCounts;
602604
this.comparator = comparator;
605+
this.suiteRunner = suiteRunner;
603606
}
604607

605608
@Override
@@ -645,7 +648,8 @@ public TestRunner newTestRunner(
645648
listeners,
646649
classListeners,
647650
comparator,
648-
holder);
651+
holder,
652+
suiteRunner);
649653

650654
if (useDefaultListeners) {
651655
testRunner.addListener(new TestHTMLReporter());

testng-core/src/main/java/org/testng/TestRunner.java

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,8 @@ protected TestRunner(
173173
Collection<IInvokedMethodListener> invokedMethodListeners,
174174
List<IClassListener> classListeners,
175175
Comparator<ITestNGMethod> comparator,
176-
DataProviderHolder otherHolder) {
176+
DataProviderHolder otherHolder,
177+
SuiteRunner suiteRunner) {
177178
this.comparator = comparator;
178179
this.holder.merge(otherHolder);
179180
init(
@@ -184,7 +185,8 @@ protected TestRunner(
184185
finder,
185186
skipFailedInvocationCounts,
186187
invokedMethodListeners,
187-
classListeners);
188+
classListeners,
189+
suiteRunner);
188190
}
189191

190192
public TestRunner(
@@ -194,7 +196,8 @@ public TestRunner(
194196
boolean skipFailedInvocationCounts,
195197
Collection<IInvokedMethodListener> invokedMethodListeners,
196198
List<IClassListener> classListeners,
197-
Comparator<ITestNGMethod> comparator) {
199+
Comparator<ITestNGMethod> comparator,
200+
SuiteRunner suiteRunner) {
198201
this.comparator = comparator;
199202
init(
200203
configuration,
@@ -204,7 +207,8 @@ public TestRunner(
204207
suite.getAnnotationFinder(),
205208
skipFailedInvocationCounts,
206209
invokedMethodListeners,
207-
classListeners);
210+
classListeners,
211+
suiteRunner);
208212
}
209213

210214
/* /!\ This constructor is used by testng-remote, any changes related to it please contact with testng-team. */
@@ -214,7 +218,8 @@ public TestRunner(
214218
XmlTest test,
215219
boolean skipFailedInvocationCounts,
216220
Collection<IInvokedMethodListener> invokedMethodListeners,
217-
List<IClassListener> classListeners) {
221+
List<IClassListener> classListeners,
222+
SuiteRunner suiteRunner) {
218223
this.comparator = Systematiser.getComparator();
219224
init(
220225
configuration,
@@ -224,7 +229,8 @@ public TestRunner(
224229
suite.getAnnotationFinder(),
225230
skipFailedInvocationCounts,
226231
invokedMethodListeners,
227-
classListeners);
232+
classListeners,
233+
suiteRunner);
228234
}
229235

230236
private void init(
@@ -235,7 +241,8 @@ private void init(
235241
IAnnotationFinder annotationFinder,
236242
boolean skipFailedInvocationCounts,
237243
Collection<IInvokedMethodListener> invokedMethodListeners,
238-
List<IClassListener> classListeners) {
244+
List<IClassListener> classListeners,
245+
SuiteRunner suiteRunner) {
239246
m_configuration = configuration;
240247
m_xmlTest = test;
241248
m_suite = suite;
@@ -277,7 +284,9 @@ private void init(
277284
skipFailedInvocationCounts,
278285
invokedMethodListeners,
279286
classListeners,
280-
holder);
287+
holder,
288+
m_confListener,
289+
suiteRunner);
281290

282291
if (test.getParallel() != null) {
283292
log("Running the tests in '" + test.getName() + "' with parallel mode:" + test.getParallel());
@@ -337,7 +346,6 @@ private void init() {
337346
}
338347

339348
initListeners();
340-
addConfigurationListener(m_confListener);
341349
for (IConfigurationListener cl : m_configuration.getConfigurationListeners()) {
342350
addConfigurationListener(cl);
343351
}

testng-core/src/main/java/org/testng/internal/TestListenerHelper.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,11 @@ private TestListenerHelper() {
2222
}
2323

2424
public static void runPreConfigurationListeners(
25-
ITestResult tr, ITestNGMethod tm, List<IConfigurationListener> listeners) {
25+
ITestResult tr,
26+
ITestNGMethod tm,
27+
List<IConfigurationListener> listeners,
28+
IConfigurationListener internal) {
29+
internal.beforeConfiguration(tr);
2630
for (IConfigurationListener icl : listeners) {
2731
icl.beforeConfiguration(tr);
2832
try {
@@ -34,8 +38,12 @@ public static void runPreConfigurationListeners(
3438
}
3539

3640
public static void runPostConfigurationListeners(
37-
ITestResult tr, ITestNGMethod tm, List<IConfigurationListener> listeners) {
41+
ITestResult tr,
42+
ITestNGMethod tm,
43+
List<IConfigurationListener> listeners,
44+
IConfigurationListener internal) {
3845
List<IConfigurationListener> listenersreversed = Lists.newReversedArrayList(listeners);
46+
listenersreversed.add(internal);
3947
for (IConfigurationListener icl : listenersreversed) {
4048
switch (tr.getStatus()) {
4149
case ITestResult.SKIP:

testng-core/src/main/java/org/testng/internal/invokers/BaseInvoker.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import org.testng.ITestResult;
1111
import org.testng.SkipException;
1212
import org.testng.SuiteRunState;
13+
import org.testng.SuiteRunner;
1314
import org.testng.collections.Lists;
1415
import org.testng.collections.Maps;
1516
import org.testng.internal.IConfiguration;
@@ -33,17 +34,21 @@ class BaseInvoker {
3334
// Instead we now would be using this special object.
3435
protected final Object NULL_OBJECT = new Object();
3536

37+
private final SuiteRunner suiteRunner;
38+
3639
public BaseInvoker(
3740
ITestResultNotifier notifier,
3841
Collection<IInvokedMethodListener> invokedMethodListeners,
3942
ITestContext testContext,
4043
SuiteRunState suiteState,
41-
IConfiguration configuration) {
44+
IConfiguration configuration,
45+
SuiteRunner suiteRunner) {
4246
this.m_notifier = notifier;
4347
this.m_invokedMethodListeners = invokedMethodListeners;
4448
this.m_testContext = testContext;
4549
this.m_suiteState = suiteState;
4650
this.m_configuration = configuration;
51+
this.suiteRunner = suiteRunner;
4752
}
4853

4954
protected IAnnotationFinder annotationFinder() {
@@ -67,6 +72,9 @@ protected void runInvokedMethodListeners(
6772
isAfterInvocation
6873
? Lists.newReversedArrayList(m_invokedMethodListeners)
6974
: m_invokedMethodListeners;
75+
if (!isAfterInvocation) {
76+
suiteRunner.beforeInvocation(invokedMethod, testResult);
77+
}
7078
for (IInvokedMethodListener currentListener : listeners) {
7179
try {
7280
invoker.invokeListener(currentListener, invokedMethod);
@@ -82,6 +90,9 @@ protected void runInvokedMethodListeners(
8290
testResult.setThrowable(e);
8391
}
8492
}
93+
if (isAfterInvocation) {
94+
suiteRunner.afterInvocation(invokedMethod, testResult);
95+
}
8596
}
8697

8798
private boolean noListenersPresent() {

testng-core/src/main/java/org/testng/internal/invokers/ConfigInvoker.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,14 @@
1414
import org.testng.ConfigurationNotInvokedException;
1515
import org.testng.IClass;
1616
import org.testng.IConfigurable;
17+
import org.testng.IConfigurationListener;
1718
import org.testng.IInvokedMethodListener;
1819
import org.testng.ITestContext;
1920
import org.testng.ITestNGMethod;
2021
import org.testng.ITestResult;
2122
import org.testng.Reporter;
2223
import org.testng.SuiteRunState;
24+
import org.testng.SuiteRunner;
2325
import org.testng.TestNGException;
2426
import org.testng.annotations.IConfigurationAnnotation;
2527
import org.testng.collections.Maps;
@@ -54,16 +56,21 @@ class ConfigInvoker extends BaseInvoker implements IConfigInvoker {
5456
/** Group failures must be synced as the Invoker is accessed concurrently */
5557
private final Map<String, Boolean> m_beforegroupsFailures = Maps.newConcurrentMap();
5658

59+
private final IConfigurationListener internalConfigurationListener;
60+
5761
public ConfigInvoker(
5862
ITestResultNotifier notifier,
5963
Collection<IInvokedMethodListener> invokedMethodListeners,
6064
ITestContext testContext,
6165
SuiteRunState suiteState,
62-
IConfiguration configuration) {
63-
super(notifier, invokedMethodListeners, testContext, suiteState, configuration);
66+
IConfiguration configuration,
67+
IConfigurationListener internalConfigurationListener,
68+
SuiteRunner suiteRunner) {
69+
super(notifier, invokedMethodListeners, testContext, suiteState, configuration, suiteRunner);
6470
this.m_continueOnFailedConfiguration =
6571
testContext.getSuite().getXmlSuite().getConfigFailurePolicy()
6672
== XmlSuite.FailurePolicy.CONTINUE;
73+
this.internalConfigurationListener = internalConfigurationListener;
6774
}
6875

6976
/**
@@ -403,10 +410,10 @@ private IConfigurable computeConfigurableInstance(
403410
private void runConfigurationListeners(ITestResult tr, ITestNGMethod tm, boolean before) {
404411
if (before) {
405412
TestListenerHelper.runPreConfigurationListeners(
406-
tr, tm, m_notifier.getConfigurationListeners());
413+
tr, tm, m_notifier.getConfigurationListeners(), internalConfigurationListener);
407414
} else {
408415
TestListenerHelper.runPostConfigurationListeners(
409-
tr, tm, m_notifier.getConfigurationListeners());
416+
tr, tm, m_notifier.getConfigurationListeners(), internalConfigurationListener);
410417
}
411418
}
412419

testng-core/src/main/java/org/testng/internal/invokers/Invoker.java

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@
66
import org.testng.DataProviderHolder;
77
import org.testng.IClass;
88
import org.testng.IClassListener;
9+
import org.testng.IConfigurationListener;
910
import org.testng.IInvokedMethodListener;
1011
import org.testng.ITestContext;
1112
import org.testng.ITestNGMethod;
1213
import org.testng.SuiteRunState;
14+
import org.testng.SuiteRunner;
1315
import org.testng.internal.IConfiguration;
1416
import org.testng.internal.ITestResultNotifier;
1517

@@ -37,9 +39,18 @@ public Invoker(
3739
boolean skipFailedInvocationCounts,
3840
Collection<IInvokedMethodListener> invokedMethodListeners,
3941
List<IClassListener> classListeners,
40-
DataProviderHolder holder) {
42+
DataProviderHolder holder,
43+
IConfigurationListener internalConfigurationListener,
44+
SuiteRunner suiteRunner) {
4145
m_configInvoker =
42-
new ConfigInvoker(notifier, invokedMethodListeners, testContext, state, configuration);
46+
new ConfigInvoker(
47+
notifier,
48+
invokedMethodListeners,
49+
testContext,
50+
state,
51+
configuration,
52+
internalConfigurationListener,
53+
suiteRunner);
4354
m_testInvoker =
4455
new TestInvoker(
4556
notifier,
@@ -50,7 +61,8 @@ public Invoker(
5061
holder,
5162
classListeners,
5263
skipFailedInvocationCounts,
53-
m_configInvoker);
64+
m_configInvoker,
65+
suiteRunner);
5466
}
5567

5668
public ConfigInvoker getConfigInvoker() {

testng-core/src/main/java/org/testng/internal/invokers/TestInvoker.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,15 @@ public TestInvoker(
6969
DataProviderHolder holder,
7070
List<IClassListener> m_classListeners,
7171
boolean m_skipFailedInvocationCounts,
72-
ConfigInvoker invoker) {
73-
super(m_notifier, m_invokedMethodListeners, m_testContext, m_suiteState, m_configuration);
72+
ConfigInvoker invoker,
73+
SuiteRunner suiteRunner) {
74+
super(
75+
m_notifier,
76+
m_invokedMethodListeners,
77+
m_testContext,
78+
m_suiteState,
79+
m_configuration,
80+
suiteRunner);
7481
this.holder = holder;
7582
this.m_classListeners = m_classListeners;
7683
this.m_skipFailedInvocationCounts = m_skipFailedInvocationCounts;

testng-core/src/test/java/org/testng/TestRunnerTest.java

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import static java.util.Collections.emptyList;
44
import static org.assertj.core.api.Assertions.assertThat;
55

6+
import java.util.Collection;
67
import java.util.List;
78
import java.util.stream.Collectors;
89
import org.testng.annotations.BeforeMethod;
@@ -85,10 +86,28 @@ private TestRunner createTestRunner(Class<?> testClass) {
8586
XmlClass xmlClass = new XmlClass(testClass.getName());
8687
xmlTest.getXmlClasses().add(xmlClass);
8788
String outputDir = "build/reports/tests/test";
88-
ITestRunnerFactory factory =
89-
(suite, test, listeners, classListeners) ->
90-
new TestRunner(configuration, suite, test, false, listeners, classListeners);
91-
ISuite suite = new SuiteRunner(configuration, xmlSuite, outputDir, factory, (o1, o2) -> 0);
89+
LocalFactory factory = new LocalFactory(configuration);
90+
SuiteRunner suite = new SuiteRunner(configuration, xmlSuite, outputDir, factory, (o1, o2) -> 0);
91+
factory.suiteRunner = suite;
9292
return factory.newTestRunner(suite, xmlTest, emptyList(), emptyList());
9393
}
94+
95+
private static class LocalFactory implements ITestRunnerFactory {
96+
private final IConfiguration configuration;
97+
private SuiteRunner suiteRunner;
98+
99+
private LocalFactory(IConfiguration configuration) {
100+
this.configuration = configuration;
101+
}
102+
103+
@Override
104+
public TestRunner newTestRunner(
105+
ISuite suite,
106+
XmlTest test,
107+
Collection<IInvokedMethodListener> listeners,
108+
List<IClassListener> classListeners) {
109+
return new TestRunner(
110+
configuration, suite, test, false, listeners, classListeners, suiteRunner);
111+
}
112+
}
94113
}

0 commit comments

Comments
 (0)