forked from junit-team/junit5
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Issues: junit-team#3107
- Loading branch information
Showing
6 changed files
with
282 additions
and
0 deletions.
There are no files selected for viewing
50 changes: 50 additions & 0 deletions
50
...rm-engine/src/main/java/org/junit/platform/engine/discovery/AbstractMethodNameFilter.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,50 @@ | ||
/* | ||
* Copyright 2015-2023 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.engine.discovery; | ||
|
||
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.function.Predicate; | ||
import java.util.regex.Pattern; | ||
|
||
import org.junit.platform.commons.util.Preconditions; | ||
|
||
/** | ||
* Abstract {@link MethodNameFilter} that servers as a superclass | ||
* for filters including or excluding fully qualified method names | ||
* based on pattern-matching. | ||
* | ||
* @since TODO YHKUO | ||
*/ | ||
abstract class AbstractMethodNameFilter implements MethodNameFilter { | ||
|
||
protected final List<Pattern> patterns; | ||
protected final String patternDescription; | ||
|
||
AbstractMethodNameFilter(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 '", "'", "'")); | ||
} | ||
|
||
@Override | ||
public abstract Predicate<String> toPredicate(); | ||
|
||
protected Optional<Pattern> findMatchingPattern(String methodName) { | ||
return this.patterns.stream().filter(pattern -> pattern.matcher(methodName).matches()).findAny(); | ||
} | ||
|
||
} |
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
63 changes: 63 additions & 0 deletions
63
...orm-engine/src/main/java/org/junit/platform/engine/discovery/ExcludeMethodNameFilter.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,63 @@ | ||
/* | ||
* Copyright 2015-2023 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.engine.discovery; | ||
|
||
import static org.junit.platform.engine.FilterResult.excluded; | ||
import static org.junit.platform.engine.FilterResult.included; | ||
|
||
import java.util.function.Predicate; | ||
import java.util.regex.Pattern; | ||
|
||
import org.junit.platform.engine.FilterResult; | ||
|
||
/** | ||
* {@link MethodNameFilter} 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 TODO YHKUO | ||
*/ | ||
class ExcludeMethodNameFilter extends AbstractMethodNameFilter { | ||
|
||
ExcludeMethodNameFilter(String... patterns) { | ||
super(patterns); | ||
} | ||
|
||
@Override | ||
public FilterResult apply(String methodName) { | ||
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 Predicate<String> toPredicate() { | ||
return methodName -> !findMatchingPattern(methodName).isPresent(); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return String.format("%s that excludes method names that match one of the following regular expressions: %s", | ||
getClass().getSimpleName(), patternDescription); | ||
} | ||
|
||
} |
63 changes: 63 additions & 0 deletions
63
...orm-engine/src/main/java/org/junit/platform/engine/discovery/IncludeMethodNameFilter.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,63 @@ | ||
/* | ||
* Copyright 2015-2023 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.engine.discovery; | ||
|
||
import static org.junit.platform.engine.FilterResult.excluded; | ||
import static org.junit.platform.engine.FilterResult.included; | ||
|
||
import java.util.function.Predicate; | ||
import java.util.regex.Pattern; | ||
|
||
import org.junit.platform.engine.FilterResult; | ||
|
||
/** | ||
* {@link MethodNameFilter} 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 TODO YHKUO | ||
*/ | ||
class IncludeMethodNameFilter extends AbstractMethodNameFilter { | ||
|
||
IncludeMethodNameFilter(String... patterns) { | ||
super(patterns); | ||
} | ||
|
||
@Override | ||
public FilterResult apply(String methodName) { | ||
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, | ||
patternDescription); | ||
} | ||
|
||
@Override | ||
public Predicate<String> toPredicate() { | ||
return methodName -> findMatchingPattern(methodName).isPresent(); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return String.format("%s that includes method names that match one of the following regular expressions: %s", | ||
getClass().getSimpleName(), patternDescription); | ||
} | ||
|
||
} |
104 changes: 104 additions & 0 deletions
104
...t-platform-engine/src/main/java/org/junit/platform/engine/discovery/MethodNameFilter.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,104 @@ | ||
/* | ||
* Copyright 2015-2023 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.engine.discovery; | ||
|
||
import static org.apiguardian.api.API.Status.STABLE; | ||
|
||
import java.util.List; | ||
|
||
import org.apiguardian.api.API; | ||
import org.junit.platform.engine.DiscoveryFilter; | ||
|
||
/** | ||
* {@link DiscoveryFilter} that is applied to the name of a {@link java.lang.reflect.Method}. | ||
* | ||
* @since TODO YHKUO | ||
* @see #includeMethodNames(String...) | ||
* @see #excludeMethodNames(String...) | ||
* @see ClassNameFilter | ||
* @see PackageNameFilter | ||
*/ | ||
// TODO YHKUO @API(status = STABLE, since = "1.0") | ||
public interface MethodNameFilter extends DiscoveryFilter<String> { | ||
|
||
/** | ||
* Create a new <em>include</em> {@link MethodNameFilter} based on the | ||
* supplied method names. | ||
* | ||
* <p>The names are combined using OR semantics, i.e. if the fully | ||
* qualified name of a method starts with at least one of the names, | ||
* the method will be included in the result set. | ||
* | ||
* @param names method names that we be compared against fully qualified | ||
* method names; never {@code null}, empty, or containing {@code null} | ||
* @see java.lang.reflect.Method#getName() | ||
* @see #includeMethodNames(List) | ||
* @see #excludeMethodNames(String...) | ||
*/ | ||
static MethodNameFilter includeMethodNames(String... names) { | ||
return new IncludeMethodNameFilter(names); | ||
} | ||
|
||
/** | ||
* Create a new <em>include</em> {@link MethodNameFilter} based on the | ||
* supplied method names. | ||
* | ||
* <p>The names are combined using OR semantics, i.e. if the fully | ||
* qualified name of a method starts with at least one of the names, | ||
* the method will be included in the result set. | ||
* | ||
* @param names method names that we be compared against fully qualified | ||
* method names; never {@code null}, empty, or containing {@code null} | ||
* @see java.lang.reflect.Method#getName() | ||
* @see #includeMethodNames(String...) | ||
* @see #excludeMethodNames(String...) | ||
*/ | ||
static MethodNameFilter includeMethodNames(List<String> names) { | ||
return includeMethodNames(names.toArray(new String[0])); | ||
} | ||
|
||
/** | ||
* Create a new <em>exclude</em> {@link MethodNameFilter} based on the | ||
* supplied method names. | ||
* | ||
* <p>The names are combined using OR semantics, i.e. if the fully | ||
* qualified name of a method starts with at least one of the names, | ||
* the method will be excluded in the result set. | ||
* | ||
* @param names method names that we be compared against fully qualified | ||
* method names; never {@code null}, empty, or containing {@code null} | ||
* @see java.lang.reflect.Method#getName() | ||
* @see #excludeMethodNames(List) | ||
* @see #includeMethodNames(String...) | ||
*/ | ||
static MethodNameFilter excludeMethodNames(String... names) { | ||
return new ExcludeMethodNameFilter(names); | ||
} | ||
|
||
/** | ||
* Create a new <em>exclude</em> {@link MethodNameFilter} based on the | ||
* supplied method names. | ||
* | ||
* <p>The names are combined using OR semantics, i.e. if the fully | ||
* qualified name of a method starts with at least one of the names, | ||
* the method will be excluded in the result set. | ||
* | ||
* @param names method names that we be compared against fully qualified | ||
* method names; never {@code null}, empty, or containing {@code null} | ||
* @see java.lang.reflect.Method#getName() | ||
* @see #excludeMethodNames(String...) | ||
* @see #includeMethodNames(String...) | ||
*/ | ||
static MethodNameFilter excludeMethodNames(List<String> names) { | ||
return excludeMethodNames(names.toArray(new String[0])); | ||
} | ||
|
||
} |
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