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.
Introduce method filter support in the ConsoleLauncher
Issue: junit-team#3107
- Loading branch information
Showing
12 changed files
with
863 additions
and
248 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
60 changes: 60 additions & 0 deletions
60
junit-platform-launcher/src/main/java/org/junit/platform/launcher/AbstractMethodFilter.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,60 @@ | ||
/* | ||
* 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.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 | ||
* based on pattern-matching. | ||
* | ||
* @since 1.10 | ||
*/ | ||
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 -> ((MethodSource) methodSource)) // | ||
.map(methodSource -> ReflectionUtils.getFullyQualifiedMethodNameWithoutParameters( | ||
methodSource.getJavaClass(), methodSource.getJavaMethod())) // | ||
.orElse(null); | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
junit-platform-launcher/src/main/java/org/junit/platform/launcher/ExcludeMethodFilter.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,59 @@ | ||
/* | ||
* 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.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.10 | ||
*/ | ||
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 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-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.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.10 | ||
*/ | ||
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, | ||
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); | ||
} | ||
} |
Oops, something went wrong.