Skip to content

Commit

Permalink
TMP
Browse files Browse the repository at this point in the history
  • Loading branch information
yhkuo41 committed Feb 8, 2023
1 parent 5b04c94 commit 618d5ca
Show file tree
Hide file tree
Showing 6 changed files with 282 additions and 0 deletions.
@@ -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();
}

}
Expand Up @@ -21,6 +21,7 @@
* @since 1.0
* @see #includeClassNamePatterns(String...)
* @see #excludeClassNamePatterns(String...)
* @see MethodNameFilter
* @see PackageNameFilter
*/
@API(status = STABLE, since = "1.0")
Expand Down
@@ -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);
}

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

}
@@ -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]));
}

}
Expand Up @@ -24,6 +24,7 @@
* @see #includePackageNames(String...)
* @see #excludePackageNames(String...)
* @see ClassNameFilter
* @see MethodNameFilter
*/
@API(status = STABLE, since = "1.0")
public interface PackageNameFilter extends DiscoveryFilter<String> {
Expand Down

0 comments on commit 618d5ca

Please sign in to comment.