From 02c91b58bfb3e62fb316d2dc7d6bca37aff4fc47 Mon Sep 17 00:00:00 2001 From: yongjunhong Date: Tue, 19 Nov 2024 23:44:37 +0900 Subject: [PATCH] move code to JupiterConfiguration Issue: #3717 Signed-off-by: yongjunhong --- .../java/org/junit/jupiter/engine/Constants.java | 9 +++++++++ .../engine/config/CachingJupiterConfiguration.java | 10 +++------- .../engine/config/DefaultJupiterConfiguration.java | 14 +++++++++++--- .../engine/config/JupiterConfiguration.java | 5 ++--- .../engine/extension/MutableExtensionRegistry.java | 12 +----------- .../commons/util/ClassNamePatternFilterUtils.java | 2 ++ .../engine/extension/ExtensionRegistryTests.java | 1 + 7 files changed, 29 insertions(+), 24 deletions(-) diff --git a/junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/Constants.java b/junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/Constants.java index 74be3245e2ea..425cf4cc53b4 100644 --- a/junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/Constants.java +++ b/junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/Constants.java @@ -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} + * + *

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} * diff --git a/junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/config/CachingJupiterConfiguration.java b/junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/config/CachingJupiterConfiguration.java index 9f05ce558b20..3640d2eec802 100644 --- a/junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/config/CachingJupiterConfiguration.java +++ b/junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/config/CachingJupiterConfiguration.java @@ -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; @@ -47,13 +48,8 @@ public CachingJupiterConfiguration(JupiterConfiguration delegate) { } @Override - public Optional getExtensionAutodetectionIncludePattern() { - return delegate.getExtensionAutodetectionIncludePattern(); - } - - @Override - public Optional getExtensionAutodetectionExcludePattern() { - return delegate.getExtensionAutodetectionExcludePattern(); + public Predicate> createExtensionFilterByPatterns() { + return delegate.createExtensionFilterByPatterns(); } @Override diff --git a/junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/config/DefaultJupiterConfiguration.java b/junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/config/DefaultJupiterConfiguration.java index 03f90542a714..2807ad753e6c 100644 --- a/junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/config/DefaultJupiterConfiguration.java +++ b/junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/config/DefaultJupiterConfiguration.java @@ -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; @@ -74,12 +75,19 @@ public DefaultJupiterConfiguration(ConfigurationParameters configurationParamete } @Override - public Optional getExtensionAutodetectionIncludePattern() { + public Predicate> createExtensionFilterByPatterns() { + Predicate predicate = ClassNamePatternFilterUtils.includeMatchingClassNames( + getExtensionAutodetectionIncludePattern().orElse(ClassNamePatternFilterUtils.ALL_PATTERN)) // + .and(ClassNamePatternFilterUtils.excludeMatchingClassNames( + getExtensionAutodetectionExcludePattern().orElse(ClassNamePatternFilterUtils.BLANK))); + return clazz -> predicate.test(clazz.getName()); + } + + private Optional getExtensionAutodetectionIncludePattern() { return configurationParameters.get(EXTENSIONS_AUTODETECTION_INCLUDE_PROPERTY_NAME); } - @Override - public Optional getExtensionAutodetectionExcludePattern() { + private Optional getExtensionAutodetectionExcludePattern() { return configurationParameters.get(EXTENSIONS_AUTODETECTION_EXCLUDE_PROPERTY_NAME); } diff --git a/junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/config/JupiterConfiguration.java b/junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/config/JupiterConfiguration.java index a90ece7beedb..f18e575641ee 100644 --- a/junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/config/JupiterConfiguration.java +++ b/junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/config/JupiterConfiguration.java @@ -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; @@ -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 getExtensionAutodetectionIncludePattern(); - - Optional getExtensionAutodetectionExcludePattern(); + Predicate> createExtensionFilterByPatterns(); Optional getRawConfigurationParameter(String key); diff --git a/junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/extension/MutableExtensionRegistry.java b/junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/extension/MutableExtensionRegistry.java index 3fb18d1fa200..746a35d80fac 100644 --- a/junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/extension/MutableExtensionRegistry.java +++ b/junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/extension/MutableExtensionRegistry.java @@ -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; @@ -98,9 +97,7 @@ public static MutableExtensionRegistry createRegistryWithDefaultExtensions(Jupit private static void registerAutoDetectedExtensions(MutableExtensionRegistry extensionRegistry, JupiterConfiguration configuration) { - Predicate> filter = createExtensionFilterByPatterns( - configuration.getExtensionAutodetectionIncludePattern().orElse(ClassNamePatternFilterUtils.ALL_PATTERN), - configuration.getExtensionAutodetectionExcludePattern().orElse("")); + Predicate> filter = configuration.createExtensionFilterByPatterns(); ServiceLoader serviceLoader = ServiceLoader.load(Extension.class, ClassLoaderUtils.getDefaultClassLoader()); @@ -108,13 +105,6 @@ private static void registerAutoDetectedExtensions(MutableExtensionRegistry exte .forEach(extensionRegistry::registerAutoDetectedExtension); } - private static Predicate> createExtensionFilterByPatterns(String include, - String exclude) { - Predicate 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. diff --git a/junit-platform-commons/src/main/java/org/junit/platform/commons/util/ClassNamePatternFilterUtils.java b/junit-platform-commons/src/main/java/org/junit/platform/commons/util/ClassNamePatternFilterUtils.java index f9ec3d257baa..90f9d72b4b20 100644 --- a/junit-platform-commons/src/main/java/org/junit/platform/commons/util/ClassNamePatternFilterUtils.java +++ b/junit-platform-commons/src/main/java/org/junit/platform/commons/util/ClassNamePatternFilterUtils.java @@ -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 diff --git a/jupiter-tests/src/test/java/org/junit/jupiter/engine/extension/ExtensionRegistryTests.java b/jupiter-tests/src/test/java/org/junit/jupiter/engine/extension/ExtensionRegistryTests.java index dd1babf69f0e..2c3b0c687d1a 100644 --- a/jupiter-tests/src/test/java/org/junit/jupiter/engine/extension/ExtensionRegistryTests.java +++ b/jupiter-tests/src/test/java/org/junit/jupiter/engine/extension/ExtensionRegistryTests.java @@ -60,6 +60,7 @@ void newRegistryWithoutParentHasDefaultExtensions() { void newRegistryWithoutParentHasDefaultExtensionsPlusAutodetectedExtensionsLoadedViaServiceLoader() { when(configuration.isExtensionAutoDetectionEnabled()).thenReturn(true); + when(configuration.createExtensionFilterByPatterns()).thenReturn(__ -> true); registry = createRegistryWithDefaultExtensions(configuration); List extensions = registry.getExtensions(Extension.class);