-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Segregate native & user listeners before ordering
Closes #2771
- Loading branch information
1 parent
141aa0d
commit a164c2d
Showing
13 changed files
with
172 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
110 changes: 110 additions & 0 deletions
110
testng-core/src/main/java/org/testng/internal/ListenerOrderDeterminer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
package org.testng.internal; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.Collection; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Objects; | ||
import java.util.function.Predicate; | ||
import org.testng.collections.Lists; | ||
import org.testng.internal.collections.Pair; | ||
|
||
/** | ||
* A Utility that helps us differentiate between a user's listener and TestNG's native listeners( | ||
* which also includes IntelliJ IDEA Listener). | ||
* | ||
* <p>When dealing with TestNG listeners we would need to ensure that the built-in TestNG listeners | ||
* are always executed after the user provided listeners. This is required so that we always honour | ||
* any state changes that a user's listener may have done to the internal state of objects that can | ||
* affect the outcome of the execution (for e.g.,{@link org.testng.ITestResult}) | ||
* | ||
* <p>The ordering must be done such that, when dealing with "beforeXXX|afterXXX" we group all the | ||
* TestNG built-in listeners at the end. | ||
* | ||
* <p>If this is not done, then there can always be a situation wherein a user's listener altered | ||
* the state but TestNG failed to recognize the altered state, because it got executed first and the | ||
* user's listener executed after that. | ||
* | ||
* <p>This class also ensures that it explicitly deals with all the TestNG built-in listeners | ||
* including reporting listeners, because TestNG reporting listeners are NOT modelled such that they | ||
* are READ ONLY and they still allow the state to be altered and thus affect the outcome of the | ||
* execution. | ||
*/ | ||
public final class ListenerOrderDeterminer { | ||
|
||
private ListenerOrderDeterminer() { | ||
// Defeat instantiation | ||
} | ||
|
||
private static final List<String> KNOWN = | ||
Arrays.asList( | ||
"org.testng.internal.ExitCodeListener", | ||
"org.testng.SuiteRunner", | ||
"org.testng.TestRunner.ConfigurationListener", | ||
"org.testng.reporters.DotTestListener", | ||
"org.testng.reporters.EmailableReporter", | ||
"org.testng.reporters.EmailableReporter2", | ||
"org.testng.reporters.ExitCodeListener", | ||
"org.testng.reporters.FailedReporter", | ||
"org.testng.reporters.JUnitReportReporter", | ||
"org.testng.reporters.JUnitXMLReporter", | ||
"org.testng.reporters.SuiteHTMLReporter", | ||
"org.testng.reporters.TestHTMLReporter", | ||
"org.testng.reporters.TextReporter", | ||
"org.testng.reporters.VerboseReporter", | ||
"org.testng.reporters.XMLReporter", | ||
"org.testng.reporters.jq.Main"); | ||
|
||
private static final Predicate<Class<?>> INTELLIJ_IDE_LISTENER = | ||
clazz -> clazz.getName().contains(RuntimeBehavior.getIntellijPackageName()); | ||
|
||
/** | ||
* @param original - The original collection of listeners | ||
* @return - A re-ordered collection wherein TestNG's known listeners are added at the end | ||
*/ | ||
public static <T> List<T> order(Collection<T> original) { | ||
Pair<List<T>, List<T>> ordered = arrange(original); | ||
List<T> nativeListeners = ordered.first(); | ||
List<T> foreignListeners = ordered.second(); | ||
return Lists.merge(foreignListeners, nativeListeners); | ||
} | ||
|
||
/** | ||
* @param original - The original collection of listeners | ||
* @return - A reversed ordered list wherein the user listeners are found in reverse order | ||
* followed by TestNG known listeners also in reverse order. | ||
*/ | ||
public static <T> List<T> reversedOrder(Collection<T> original) { | ||
Pair<List<T>, List<T>> ordered = arrange(original); | ||
List<T> nativeListeners = ordered.first(); | ||
List<T> foreignListeners = ordered.second(); | ||
Collections.reverse(foreignListeners); | ||
Collections.reverse(nativeListeners); | ||
return Lists.merge(foreignListeners, nativeListeners); | ||
} | ||
|
||
private static boolean isNativeListener(Class<?> clazz) { | ||
return KNOWN.stream() | ||
.anyMatch( | ||
each -> | ||
each.equalsIgnoreCase(clazz.getCanonicalName()) | ||
|| INTELLIJ_IDE_LISTENER.test(clazz)); | ||
} | ||
|
||
private static <T> Pair<List<T>, List<T>> arrange(Collection<T> original) { | ||
List<T> nativeListeners = new ArrayList<>(); | ||
List<T> foreignListeners = new ArrayList<>(); | ||
original.stream() | ||
.filter(Objects::nonNull) | ||
.forEach( | ||
each -> { | ||
if (isNativeListener(each.getClass())) { | ||
nativeListeners.add(each); | ||
} else { | ||
foreignListeners.add(each); | ||
} | ||
}); | ||
return new Pair<>(nativeListeners, foreignListeners); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
testng-core/src/test/java/test/listeners/issue2771/CustomSoftAssert.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package test.listeners.issue2771; | ||
|
||
import org.testng.ITestResult; | ||
import org.testng.reporters.ExitCodeListener; | ||
|
||
public class CustomSoftAssert extends ExitCodeListener { | ||
@Override | ||
public void onTestSuccess(ITestResult result) { | ||
result.setStatus(ITestResult.FAILURE); | ||
result.setThrowable(new AssertionError("There have been some failed soft asserts")); | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
testng-core/src/test/java/test/listeners/issue2771/TestCaseSample.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package test.listeners.issue2771; | ||
|
||
import org.testng.annotations.Listeners; | ||
import org.testng.annotations.Test; | ||
|
||
@Listeners(CustomSoftAssert.class) | ||
public class TestCaseSample { | ||
@Test | ||
public void someCustomSoftAsserts() { | ||
// doing custom soft asserts | ||
// which will trigger test failure in listener | ||
} | ||
} |