-
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
8634720
commit 755cfd4
Showing
13 changed files
with
156 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
94 changes: 94 additions & 0 deletions
94
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,94 @@ | ||
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; | ||
|
||
/** | ||
* A Utility that helps us differentiate between a user's listener and TestNG's native listeners( | ||
* which also includes IntelliJ IDEA Listener) | ||
*/ | ||
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) { | ||
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 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) { | ||
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); | ||
} | ||
}); | ||
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)); | ||
} | ||
} |
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 | ||
} | ||
} |