-
Notifications
You must be signed in to change notification settings - Fork 1k
Segregate native & user listeners before ordering #2864
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
84 changes: 84 additions & 0 deletions
84
testng-core/src/main/java/org/testng/internal/ListenerOrderDeterminer.java
This file contains hidden or 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,84 @@ | ||
package org.testng.internal; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Objects; | ||
import java.util.function.Predicate; | ||
import java.util.stream.Collectors; | ||
import org.testng.collections.Lists; | ||
import org.testng.internal.collections.Pair; | ||
|
||
/** | ||
* A Utility that helps us differentiate between a user's listener and an IDE Listener. | ||
* | ||
* <p>When dealing with TestNG listeners we would need to ensure that the user created listeners are | ||
* invoked first followed by IDE 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 | ||
* IDE listeners at the end. That way, we can always ensure that the IDE listeners always honour the | ||
* changes that were done to the TestNG internal states and give a consistent experience for users | ||
* in the IDE. | ||
*/ | ||
public final class ListenerOrderDeterminer { | ||
|
||
private ListenerOrderDeterminer() { | ||
// Defeat instantiation | ||
} | ||
|
||
private static final List<String> IDE_PACKAGES = | ||
krmahadevan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
RuntimeBehavior.getIdePackageNames().stream() | ||
.map(each -> each.replaceAll("\\Q.*\\E", "")) | ||
.collect(Collectors.toList()); | ||
|
||
private static final Predicate<Class<?>> IDE_LISTENER = | ||
clazz -> IDE_PACKAGES.stream().anyMatch(each -> clazz.getPackage().getName().contains(each)); | ||
|
||
/** | ||
* @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> ideListeners = ordered.first(); | ||
List<T> regularListeners = ordered.second(); | ||
return Lists.merge(regularListeners, ideListeners); | ||
} | ||
|
||
/** | ||
* @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> ideListeners = ordered.first(); | ||
List<T> regularListeners = ordered.second(); | ||
Collections.reverse(regularListeners); | ||
Collections.reverse(ideListeners); | ||
return Lists.merge(regularListeners, ideListeners); | ||
} | ||
|
||
private static boolean isIDEListener(Class<?> clazz) { | ||
return IDE_LISTENER.test(clazz); | ||
} | ||
|
||
private static <T> Pair<List<T>, List<T>> arrange(Collection<T> original) { | ||
List<T> ideListeners = new ArrayList<>(); | ||
List<T> regularListeners = new ArrayList<>(); | ||
original.stream() | ||
.filter(Objects::nonNull) | ||
.forEach( | ||
each -> { | ||
if (isIDEListener(each.getClass())) { | ||
ideListeners.add(each); | ||
} else { | ||
regularListeners.add(each); | ||
} | ||
}); | ||
return new Pair<>(ideListeners, regularListeners); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.