-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Introduce method filter support in the ConsoleLauncher #3169
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
marcphilipp
merged 4 commits into
junit-team:main
from
yhkuo41:origin/issues/3107-method-filters
Oct 16, 2024
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
fbfcb36
Introduce method filter support in the ConsoleLauncher
yhkuo41 86d2aef
Merge branch 'main' into origin/issues/3107-method-filters
yhkuo41 6e8b61f
Ensure format consistency in release notes
yhkuo41 c4e4716
Use @Test for single parameter test
yhkuo41 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
64 changes: 64 additions & 0 deletions
64
junit-platform-launcher/src/main/java/org/junit/platform/launcher/AbstractMethodFilter.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,64 @@ | ||
/* | ||
* Copyright 2015-2024 the original author or authors. | ||
* | ||
* All rights reserved. This program and the accompanying materials are | ||
* made available under the terms of the Eclipse Public License v2.0 which | ||
* accompanies this distribution and is available at | ||
* | ||
* https://www.eclipse.org/legal/epl-v20.html | ||
*/ | ||
|
||
package org.junit.platform.launcher; | ||
|
||
import static java.util.stream.Collectors.joining; | ||
import static java.util.stream.Collectors.toList; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.Optional; | ||
import java.util.regex.Pattern; | ||
|
||
import org.junit.platform.commons.util.Preconditions; | ||
import org.junit.platform.commons.util.ReflectionUtils; | ||
import org.junit.platform.engine.TestDescriptor; | ||
import org.junit.platform.engine.support.descriptor.MethodSource; | ||
|
||
/** | ||
* Abstract {@link MethodFilter} that servers as a superclass | ||
* for filters including or excluding fully qualified method names | ||
* without parameters based on pattern-matching. | ||
* | ||
* @since 1.12 | ||
*/ | ||
abstract class AbstractMethodFilter implements MethodFilter { | ||
|
||
protected final List<Pattern> patterns; | ||
protected final String patternDescription; | ||
|
||
AbstractMethodFilter(String... patterns) { | ||
Preconditions.notEmpty(patterns, "patterns array must not be null or empty"); | ||
Preconditions.containsNoNullElements(patterns, "patterns array must not contain null elements"); | ||
this.patterns = Arrays.stream(patterns).map(Pattern::compile).collect(toList()); | ||
this.patternDescription = Arrays.stream(patterns).collect(joining("' OR '", "'", "'")); | ||
} | ||
|
||
protected Optional<Pattern> findMatchingPattern(String methodName) { | ||
if (methodName == null) { | ||
return Optional.empty(); | ||
} | ||
return this.patterns.stream().filter(pattern -> pattern.matcher(methodName).matches()).findAny(); | ||
} | ||
|
||
protected String getFullyQualifiedMethodNameFromDescriptor(TestDescriptor descriptor) { | ||
return descriptor.getSource() // | ||
.filter(source -> source instanceof MethodSource) // | ||
.map(methodSource -> getFullyQualifiedMethodNameWithoutParameters(((MethodSource) methodSource))) // | ||
.orElse(null); | ||
} | ||
|
||
private String getFullyQualifiedMethodNameWithoutParameters(MethodSource methodSource) { | ||
String methodNameWithParentheses = ReflectionUtils.getFullyQualifiedMethodName(methodSource.getJavaClass(), | ||
methodSource.getMethodName(), (Class<?>[]) null); | ||
return methodNameWithParentheses.substring(0, methodNameWithParentheses.length() - 2); | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
junit-platform-launcher/src/main/java/org/junit/platform/launcher/ExcludeMethodFilter.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,58 @@ | ||
/* | ||
* Copyright 2015-2024 the original author or authors. | ||
* | ||
* All rights reserved. This program and the accompanying materials are | ||
* made available under the terms of the Eclipse Public License v2.0 which | ||
* accompanies this distribution and is available at | ||
* | ||
* https://www.eclipse.org/legal/epl-v20.html | ||
*/ | ||
|
||
package org.junit.platform.launcher; | ||
|
||
import static org.junit.platform.engine.FilterResult.excluded; | ||
import static org.junit.platform.engine.FilterResult.included; | ||
|
||
import java.util.regex.Pattern; | ||
|
||
import org.junit.platform.engine.FilterResult; | ||
import org.junit.platform.engine.TestDescriptor; | ||
|
||
/** | ||
* {@link MethodFilter} that matches fully qualified method names against | ||
* patterns in the form of regular expressions. | ||
* | ||
* <p>If the fully qualified name of a method matches against at least one | ||
* pattern, the class will be excluded. | ||
* | ||
* @since 1.12 | ||
*/ | ||
class ExcludeMethodFilter extends AbstractMethodFilter { | ||
|
||
ExcludeMethodFilter(String... patterns) { | ||
super(patterns); | ||
} | ||
|
||
@Override | ||
public FilterResult apply(TestDescriptor descriptor) { | ||
String methodName = getFullyQualifiedMethodNameFromDescriptor(descriptor); | ||
return findMatchingPattern(methodName) // | ||
.map(pattern -> excluded(formatExclusionReason(methodName, pattern))) // | ||
.orElseGet(() -> included(formatInclusionReason(methodName))); | ||
} | ||
|
||
private String formatInclusionReason(String methodName) { | ||
return String.format("Method name [%s] does not match any excluded pattern: %s", methodName, | ||
patternDescription); | ||
} | ||
|
||
private String formatExclusionReason(String methodName, Pattern pattern) { | ||
return String.format("Method name [%s] matches excluded pattern: '%s'", methodName, pattern); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return String.format("%s that excludes method names that match one of the following regular expressions: %s", | ||
getClass().getSimpleName(), patternDescription); | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
junit-platform-launcher/src/main/java/org/junit/platform/launcher/IncludeMethodFilter.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,58 @@ | ||
/* | ||
* Copyright 2015-2024 the original author or authors. | ||
* | ||
* All rights reserved. This program and the accompanying materials are | ||
* made available under the terms of the Eclipse Public License v2.0 which | ||
* accompanies this distribution and is available at | ||
* | ||
* https://www.eclipse.org/legal/epl-v20.html | ||
*/ | ||
|
||
package org.junit.platform.launcher; | ||
|
||
import static org.junit.platform.engine.FilterResult.excluded; | ||
import static org.junit.platform.engine.FilterResult.included; | ||
|
||
import java.util.regex.Pattern; | ||
|
||
import org.junit.platform.engine.FilterResult; | ||
import org.junit.platform.engine.TestDescriptor; | ||
|
||
/** | ||
* {@link MethodFilter} that matches fully qualified method names against | ||
* patterns in the form of regular expressions. | ||
* | ||
* <p>If the fully qualified name of a method matches against at least one | ||
* pattern, the method will be included. | ||
* | ||
* @since 1.12 | ||
*/ | ||
class IncludeMethodFilter extends AbstractMethodFilter { | ||
|
||
IncludeMethodFilter(String... patterns) { | ||
super(patterns); | ||
} | ||
|
||
@Override | ||
public FilterResult apply(TestDescriptor descriptor) { | ||
String methodName = getFullyQualifiedMethodNameFromDescriptor(descriptor); | ||
return findMatchingPattern(methodName) // | ||
.map(pattern -> included(formatInclusionReason(methodName, pattern))) // | ||
.orElseGet(() -> excluded(formatExclusionReason(methodName))); | ||
} | ||
|
||
private String formatInclusionReason(String methodName, Pattern pattern) { | ||
return String.format("Method name [%s] matches included pattern: '%s'", methodName, pattern); | ||
} | ||
|
||
private String formatExclusionReason(String methodName) { | ||
return String.format("Method name [%s] does not match any included pattern: %s", methodName, | ||
marcphilipp marked this conversation as resolved.
Show resolved
Hide resolved
|
||
patternDescription); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return String.format("%s that includes method names that match one of the following regular expressions: %s", | ||
getClass().getSimpleName(), patternDescription); | ||
} | ||
} |
107 changes: 107 additions & 0 deletions
107
junit-platform-launcher/src/main/java/org/junit/platform/launcher/MethodFilter.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,107 @@ | ||
/* | ||
* Copyright 2015-2024 the original author or authors. | ||
* | ||
* All rights reserved. This program and the accompanying materials are | ||
* made available under the terms of the Eclipse Public License v2.0 which | ||
* accompanies this distribution and is available at | ||
* | ||
* https://www.eclipse.org/legal/epl-v20.html | ||
*/ | ||
|
||
package org.junit.platform.launcher; | ||
marcphilipp marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
import static org.apiguardian.api.API.Status.EXPERIMENTAL; | ||
|
||
import java.lang.reflect.Method; | ||
import java.util.List; | ||
|
||
import org.apiguardian.api.API; | ||
|
||
/** | ||
* {@link PostDiscoveryFilter} that is applied to the fully qualified | ||
* {@link Method} name without parameters. | ||
* | ||
* @since 1.12 | ||
* @see #includeMethodNamePatterns(String...) | ||
* @see #excludeMethodNamePatterns(String...) | ||
*/ | ||
@API(status = EXPERIMENTAL, since = "1.12") | ||
public interface MethodFilter extends PostDiscoveryFilter { | ||
marcphilipp marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
/** | ||
* Create a new <em>include</em> {@link MethodFilter} based on the | ||
* supplied patterns. | ||
* | ||
* <p>The patterns are combined using OR semantics, i.e. if the fully | ||
* qualified name of a method matches against at least one of the patterns, | ||
* the method will be included in the result set. | ||
* | ||
* @param patterns regular expressions to match against fully qualified | ||
* method names; never {@code null}, empty, or containing {@code null} | ||
* @see Class#getName() | ||
* @see Method#getName() | ||
* @see #includeMethodNamePatterns(List) | ||
* @see #excludeMethodNamePatterns(String...) | ||
*/ | ||
static MethodFilter includeMethodNamePatterns(String... patterns) { | ||
return new IncludeMethodFilter(patterns); | ||
} | ||
|
||
/** | ||
* Create a new <em>include</em> {@link MethodFilter} based on the | ||
* supplied patterns. | ||
* | ||
* <p>The patterns are combined using OR semantics, i.e. if the fully | ||
* qualified name of a method matches against at least one of the patterns, | ||
* the method will be included in the result set. | ||
* | ||
* @param patterns regular expressions to match against fully qualified | ||
* method names; never {@code null}, empty, or containing {@code null} | ||
* @see Class#getName() | ||
* @see Method#getName() | ||
* @see #includeMethodNamePatterns(String...) | ||
* @see #excludeMethodNamePatterns(String...) | ||
*/ | ||
static MethodFilter includeMethodNamePatterns(List<String> patterns) { | ||
return includeMethodNamePatterns(patterns.toArray(new String[0])); | ||
} | ||
|
||
/** | ||
* Create a new <em>exclude</em> {@link MethodFilter} based on the | ||
* supplied patterns. | ||
* | ||
* <p>The patterns are combined using OR semantics, i.e. if the fully | ||
* qualified name of a method matches against at least one of the patterns, | ||
* the method will be excluded from the result set. | ||
* | ||
* @param patterns regular expressions to match against fully qualified | ||
* method names; never {@code null}, empty, or containing {@code null} | ||
* @see Class#getName() | ||
* @see Method#getName() | ||
* @see #excludeMethodNamePatterns(List) | ||
* @see #includeMethodNamePatterns(String...) | ||
*/ | ||
static MethodFilter excludeMethodNamePatterns(String... patterns) { | ||
return new ExcludeMethodFilter(patterns); | ||
} | ||
|
||
/** | ||
* Create a new <em>exclude</em> {@link MethodFilter} based on the | ||
* supplied patterns. | ||
* | ||
* <p>The patterns are combined using OR semantics, i.e. if the fully | ||
* qualified name of a method matches against at least one of the patterns, | ||
* the method will be excluded from the result set. | ||
* | ||
* @param patterns regular expressions to match against fully qualified | ||
* method names; never {@code null}, empty, or containing {@code null} | ||
* @see Class#getName() | ||
* @see Method#getName() | ||
* @see #excludeMethodNamePatterns(String...) | ||
* @see #includeMethodNamePatterns(String...) | ||
*/ | ||
static MethodFilter excludeMethodNamePatterns(List<String> patterns) { | ||
return excludeMethodNamePatterns(patterns.toArray(new String[0])); | ||
} | ||
|
||
} |
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.