Skip to content

Commit

Permalink
Introduce method filter support in the ConsoleLauncher
Browse files Browse the repository at this point in the history
  • Loading branch information
yhkuo41 committed Feb 25, 2023
1 parent 4e64cf2 commit ed52df6
Show file tree
Hide file tree
Showing 12 changed files with 863 additions and 248 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -882,6 +882,25 @@ public static String getFullyQualifiedMethodName(Class<?> clazz, String methodNa
return String.format("%s#%s(%s)", clazz.getName(), methodName, ClassUtils.nullSafeToString(parameterTypes));
}

/**
* Build the <em>fully qualified method name</em> without parameters for the method
* described by the supplied class and method.
*
* <p>Note that the class is not necessarily the class in which the method is
* declared.
*
* @param clazz the class from which the method should be referenced; never {@code null}
* @param method the method; never {@code null}
* @return fully qualified method name without parameters; never {@code null}
* @since 1.10
*/
public static String getFullyQualifiedMethodNameWithoutParameters(Class<?> clazz, Method method) {
Preconditions.notNull(clazz, "Class must not be null");
Preconditions.notNull(method, "Method must not be null");

return String.format("%s#%s", clazz.getName(), method.getName());
}

/**
* Parse the supplied <em>fully qualified method name</em> into a 3-element
* {@code String[]} with the following content.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,24 @@ static class FilterOptions {
@Option(names = { "-exclude-package" }, arity = "1", hidden = true)
private List<String> excludePackages2 = new ArrayList<>();

@Option(names = {
"--include-methodname" }, paramLabel = "PATTERN", arity = "1", description = "Provide a regular expression to include only methods whose fully qualified names match. "
+ //
"When this option is repeated, all patterns will be combined using OR semantics.")
private List<String> includeMethodNamePatterns = new ArrayList<>();

@Option(names = { "-include-methodname" }, arity = "1", hidden = true)
private List<String> includeMethodNamePatterns2 = new ArrayList<>();

@Option(names = {
"--exclude-methodname" }, paramLabel = "PATTERN", arity = "1", description = "Provide a regular expression to exclude those methods whose fully qualified names match. "
+ //
"When this option is repeated, all patterns will be combined using OR semantics.")
private List<String> excludeMethodNamePatterns = new ArrayList<>();

@Option(names = { "-exclude-methodname" }, arity = "1", hidden = true)
private List<String> excludeMethodNamePatterns2 = new ArrayList<>();

@Option(names = { "-t",
"--include-tag" }, paramLabel = "TAG", arity = "1", description = "Provide a tag or tag expression to include only tests whose tags match. "
+ //
Expand Down Expand Up @@ -273,6 +291,10 @@ private void applyTo(CommandLineOptions result) {
result.setExcludedClassNamePatterns(merge(this.excludeClassNamePatterns, this.excludeClassNamePatterns2));
result.setIncludedPackages(merge(this.includePackages, this.includePackages2));
result.setExcludedPackages(merge(this.excludePackages, this.excludePackages2));
result.setIncludedMethodNamePatterns(
merge(this.includeMethodNamePatterns, this.includeMethodNamePatterns2));
result.setExcludedMethodNamePatterns(
merge(this.excludeMethodNamePatterns, this.excludeMethodNamePatterns2));
result.setIncludedTagExpressions(merge(this.includedTags, this.includedTags2));
result.setExcludedTagExpressions(merge(this.excludedTags, this.excludedTags2));
result.setIncludedEngines(merge(this.includedEngines, this.includedEngines2));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ public class CommandLineOptions {
private List<String> excludedClassNamePatterns = emptyList();
private List<String> includedPackages = emptyList();
private List<String> excludedPackages = emptyList();
private List<String> includedMethodNamePatterns = emptyList();
private List<String> excludedMethodNamePatterns = emptyList();
private List<String> includedEngines = emptyList();
private List<String> excludedEngines = emptyList();
private List<String> includedTagExpressions = emptyList();
Expand Down Expand Up @@ -312,6 +314,22 @@ public void setExcludedPackages(List<String> excludedPackages) {
this.excludedPackages = excludedPackages;
}

public List<String> getIncludedMethodNamePatterns() {
return includedMethodNamePatterns;
}

public void setIncludedMethodNamePatterns(List<String> includedMethodNamePatterns) {
this.includedMethodNamePatterns = includedMethodNamePatterns;
}

public List<String> getExcludedMethodNamePatterns() {
return excludedMethodNamePatterns;
}

public void setExcludedMethodNamePatterns(List<String> excludedMethodNamePatterns) {
this.excludedMethodNamePatterns = excludedMethodNamePatterns;
}

public List<String> getIncludedEngines() {
return this.includedEngines;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
import static org.junit.platform.engine.discovery.PackageNameFilter.includePackageNames;
import static org.junit.platform.launcher.EngineFilter.excludeEngines;
import static org.junit.platform.launcher.EngineFilter.includeEngines;
import static org.junit.platform.launcher.MethodFilter.excludeMethodNamePatterns;
import static org.junit.platform.launcher.MethodFilter.includeMethodNamePatterns;
import static org.junit.platform.launcher.TagFilter.excludeTags;
import static org.junit.platform.launcher.TagFilter.includeTags;
import static org.junit.platform.launcher.core.LauncherDiscoveryRequestBuilder.request;
Expand Down Expand Up @@ -103,6 +105,14 @@ private void addFilters(LauncherDiscoveryRequestBuilder requestBuilder, CommandL
requestBuilder.filters(excludePackageNames(options.getExcludedPackages()));
}

if (!options.getIncludedMethodNamePatterns().isEmpty()) {
requestBuilder.filters(includeMethodNamePatterns(options.getIncludedMethodNamePatterns()));
}

if (!options.getExcludedMethodNamePatterns().isEmpty()) {
requestBuilder.filters(excludeMethodNamePatterns(options.getExcludedMethodNamePatterns()));
}

if (!options.getIncludedTagExpressions().isEmpty()) {
requestBuilder.filters(includeTags(options.getIncludedTagExpressions()));
}
Expand Down
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);
}
}
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);
}

}
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);
}
}
Loading

0 comments on commit ed52df6

Please sign in to comment.