11
11
import org .testng .internal .collections .Pair ;
12
12
13
13
/**
14
- * A Utility that helps us differentiate between a user's listener and an IDE Listener.
14
+ * A Utility that helps us differentiate between a user's listener and preferential Listener.
15
15
*
16
16
* <p>When dealing with TestNG listeners we would need to ensure that the user created listeners are
17
- * invoked first followed by IDE listeners. This is required so that we always honour any state
18
- * changes that a user's listener may have done to the internal state of objects that can affect the
19
- * outcome of the execution (for e.g.,{@link org.testng.ITestResult})
17
+ * invoked first followed by Preferential listeners. This is required so that we always honour any
18
+ * state changes that a user's listener may have done to the internal state of objects that can
19
+ * affect the outcome of the execution (for e.g.,{@link org.testng.ITestResult})
20
20
*
21
21
* <p>The ordering must be done such that, when dealing with "beforeXXX|afterXXX" we group all the
22
- * IDE listeners at the end. That way, we can always ensure that the IDE listeners always honour the
23
- * changes that were done to the TestNG internal states and give a consistent experience for users
24
- * in the IDE .
22
+ * IDE listeners at the end. That way, we can always ensure that the preferential listeners (for
23
+ * e.g., IDE listeners) always honour the changes that were done to the TestNG internal states and
24
+ * give a consistent experience for users .
25
25
*/
26
26
public final class ListenerOrderDeterminer {
27
27
28
28
private ListenerOrderDeterminer () {
29
29
// Defeat instantiation
30
30
}
31
31
32
- private static final List <String > IDE_PACKAGES =
33
- RuntimeBehavior .getIdePackageNames ().stream ()
32
+ private static final List <String > PREFERENTIAL_PACKAGES =
33
+ RuntimeBehavior .getPreferentialListeners ().stream ()
34
34
.map (each -> each .replaceAll ("\\ Q.*\\ E" , "" ))
35
35
.collect (Collectors .toList ());
36
36
37
- private static final Predicate <Class <?>> IDE_LISTENER =
38
- clazz -> IDE_PACKAGES .stream ().anyMatch (each -> clazz .getPackage ().getName ().contains (each ));
37
+ private static final Predicate <Class <?>> SHOULD_ADD_AT_END =
38
+ clazz ->
39
+ PREFERENTIAL_PACKAGES .stream ()
40
+ .anyMatch (each -> clazz .getPackage ().getName ().contains (each ));
39
41
40
42
/**
41
43
* @param original - The original collection of listeners
42
- * @return - A re-ordered collection wherein TestNG's known listeners are added at the end
44
+ * @return - A re-ordered collection wherein preferential listeners are added at the end
43
45
*/
44
46
public static <T > List <T > order (Collection <T > original ) {
45
47
Pair <List <T >, List <T >> ordered = arrange (original );
@@ -51,34 +53,30 @@ public static <T> List<T> order(Collection<T> original) {
51
53
/**
52
54
* @param original - The original collection of listeners
53
55
* @return - A reversed ordered list wherein the user listeners are found in reverse order
54
- * followed by TestNG known listeners also in reverse order.
56
+ * followed by preferential listeners also in reverse order.
55
57
*/
56
58
public static <T > List <T > reversedOrder (Collection <T > original ) {
57
59
Pair <List <T >, List <T >> ordered = arrange (original );
58
- List <T > ideListeners = ordered .first ();
60
+ List <T > preferentialListeners = ordered .first ();
59
61
List <T > regularListeners = ordered .second ();
60
62
Collections .reverse (regularListeners );
61
- Collections .reverse (ideListeners );
62
- return Lists .merge (regularListeners , ideListeners );
63
- }
64
-
65
- private static boolean isIDEListener (Class <?> clazz ) {
66
- return IDE_LISTENER .test (clazz );
63
+ Collections .reverse (preferentialListeners );
64
+ return Lists .merge (regularListeners , preferentialListeners );
67
65
}
68
66
69
67
private static <T > Pair <List <T >, List <T >> arrange (Collection <T > original ) {
70
- List <T > ideListeners = new ArrayList <>();
68
+ List <T > preferentialListeners = new ArrayList <>();
71
69
List <T > regularListeners = new ArrayList <>();
72
70
original .stream ()
73
71
.filter (Objects ::nonNull )
74
72
.forEach (
75
73
each -> {
76
- if (isIDEListener (each .getClass ())) {
77
- ideListeners .add (each );
74
+ if (SHOULD_ADD_AT_END . test (each .getClass ())) {
75
+ preferentialListeners .add (each );
78
76
} else {
79
77
regularListeners .add (each );
80
78
}
81
79
});
82
- return new Pair <>(ideListeners , regularListeners );
80
+ return new Pair <>(preferentialListeners , regularListeners );
83
81
}
84
82
}
0 commit comments