Skip to content

Commit

Permalink
move code to JupiterConfiguration
Browse files Browse the repository at this point in the history
Issue: junit-team#3717
Signed-off-by: yongjunhong <[email protected]>
  • Loading branch information
YongGoose committed Nov 21, 2024
1 parent f86c1d6 commit 02c91b5
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,15 @@ public final class Constants {
*/
public static final String DEACTIVATE_ALL_CONDITIONS_PATTERN = ClassNamePatternFilterUtils.ALL_PATTERN;

/**
* A blank pattern used for class name filtering: {@value}
*
* <p>This constant is used to represent an empty or blank pattern in class name filtering operations.
*
* @see ClassNamePatternFilterUtils#BLANK
*/
public static final String BLANK = ClassNamePatternFilterUtils.BLANK;

/**
* Property name used to set the default display name generator class name: {@value}
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.junit.jupiter.api.MethodOrderer;
import org.junit.jupiter.api.TestInstance;
import org.junit.jupiter.api.extension.ExecutionCondition;
import org.junit.jupiter.api.extension.Extension;
import org.junit.jupiter.api.extension.TestInstantiationAwareExtension.ExtensionContextScope;
import org.junit.jupiter.api.io.CleanupMode;
import org.junit.jupiter.api.io.TempDirFactory;
Expand All @@ -47,13 +48,8 @@ public CachingJupiterConfiguration(JupiterConfiguration delegate) {
}

@Override
public Optional<String> getExtensionAutodetectionIncludePattern() {
return delegate.getExtensionAutodetectionIncludePattern();
}

@Override
public Optional<String> getExtensionAutodetectionExcludePattern() {
return delegate.getExtensionAutodetectionExcludePattern();
public Predicate<Class<? extends Extension>> createExtensionFilterByPatterns() {
return delegate.createExtensionFilterByPatterns();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.junit.jupiter.api.MethodOrderer;
import org.junit.jupiter.api.TestInstance.Lifecycle;
import org.junit.jupiter.api.extension.ExecutionCondition;
import org.junit.jupiter.api.extension.Extension;
import org.junit.jupiter.api.extension.TestInstantiationAwareExtension.ExtensionContextScope;
import org.junit.jupiter.api.io.CleanupMode;
import org.junit.jupiter.api.io.TempDirFactory;
Expand Down Expand Up @@ -74,12 +75,19 @@ public DefaultJupiterConfiguration(ConfigurationParameters configurationParamete
}

@Override
public Optional<String> getExtensionAutodetectionIncludePattern() {
public Predicate<Class<? extends Extension>> createExtensionFilterByPatterns() {
Predicate<String> predicate = ClassNamePatternFilterUtils.includeMatchingClassNames(
getExtensionAutodetectionIncludePattern().orElse(ClassNamePatternFilterUtils.ALL_PATTERN)) //
.and(ClassNamePatternFilterUtils.excludeMatchingClassNames(
getExtensionAutodetectionExcludePattern().orElse(ClassNamePatternFilterUtils.BLANK)));
return clazz -> predicate.test(clazz.getName());
}

private Optional<String> getExtensionAutodetectionIncludePattern() {
return configurationParameters.get(EXTENSIONS_AUTODETECTION_INCLUDE_PROPERTY_NAME);
}

@Override
public Optional<String> getExtensionAutodetectionExcludePattern() {
private Optional<String> getExtensionAutodetectionExcludePattern() {
return configurationParameters.get(EXTENSIONS_AUTODETECTION_EXCLUDE_PROPERTY_NAME);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.junit.jupiter.api.MethodOrderer;
import org.junit.jupiter.api.TestInstance;
import org.junit.jupiter.api.extension.ExecutionCondition;
import org.junit.jupiter.api.extension.Extension;
import org.junit.jupiter.api.extension.PreInterruptCallback;
import org.junit.jupiter.api.extension.TestInstantiationAwareExtension.ExtensionContextScope;
import org.junit.jupiter.api.io.CleanupMode;
Expand Down Expand Up @@ -50,9 +51,7 @@ public interface JupiterConfiguration {
String DEFAULT_TEST_CLASS_ORDER_PROPERTY_NAME = ClassOrderer.DEFAULT_ORDER_PROPERTY_NAME;;
String DEFAULT_TEST_INSTANTIATION_EXTENSION_CONTEXT_SCOPE_PROPERTY_NAME = ExtensionContextScope.DEFAULT_SCOPE_PROPERTY_NAME;

Optional<String> getExtensionAutodetectionIncludePattern();

Optional<String> getExtensionAutodetectionExcludePattern();
Predicate<Class<? extends Extension>> createExtensionFilterByPatterns();

Optional<String> getRawConfigurationParameter(String key);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
import org.junit.platform.commons.logging.LoggerFactory;
import org.junit.platform.commons.support.ReflectionSupport;
import org.junit.platform.commons.util.ClassLoaderUtils;
import org.junit.platform.commons.util.ClassNamePatternFilterUtils;
import org.junit.platform.commons.util.Preconditions;
import org.junit.platform.commons.util.ServiceLoaderUtils;

Expand Down Expand Up @@ -98,23 +97,14 @@ public static MutableExtensionRegistry createRegistryWithDefaultExtensions(Jupit

private static void registerAutoDetectedExtensions(MutableExtensionRegistry extensionRegistry,
JupiterConfiguration configuration) {
Predicate<Class<? extends Extension>> filter = createExtensionFilterByPatterns(
configuration.getExtensionAutodetectionIncludePattern().orElse(ClassNamePatternFilterUtils.ALL_PATTERN),
configuration.getExtensionAutodetectionExcludePattern().orElse(""));
Predicate<Class<? extends Extension>> filter = configuration.createExtensionFilterByPatterns();

ServiceLoader<Extension> serviceLoader = ServiceLoader.load(Extension.class,
ClassLoaderUtils.getDefaultClassLoader());
ServiceLoaderUtils.filter(serviceLoader, filter) //
.forEach(extensionRegistry::registerAutoDetectedExtension);
}

private static Predicate<Class<? extends Extension>> createExtensionFilterByPatterns(String include,
String exclude) {
Predicate<String> predicate = ClassNamePatternFilterUtils.includeMatchingClassNames(include) //
.and(ClassNamePatternFilterUtils.excludeMatchingClassNames(exclude));
return clazz -> predicate.test(clazz.getName());
}

/**
* Factory for creating and populating a new registry from a list of
* extension types and a parent registry.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ private ClassNamePatternFilterUtils() {

public static final String ALL_PATTERN = "*";

public static final String BLANK = "";

/**
* Create a {@link Predicate} that can be used to exclude (i.e., filter out)
* objects of type {@code T} whose fully qualified class names match any of
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ void newRegistryWithoutParentHasDefaultExtensions() {
void newRegistryWithoutParentHasDefaultExtensionsPlusAutodetectedExtensionsLoadedViaServiceLoader() {

when(configuration.isExtensionAutoDetectionEnabled()).thenReturn(true);
when(configuration.createExtensionFilterByPatterns()).thenReturn(__ -> true);
registry = createRegistryWithDefaultExtensions(configuration);

List<Extension> extensions = registry.getExtensions(Extension.class);
Expand Down

0 comments on commit 02c91b5

Please sign in to comment.