Skip to content

Commit

Permalink
Enable specific global extension in JUnit 5
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 12, 2024
1 parent 6157803 commit cfb6845
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,16 @@ public CachingJupiterConfiguration(JupiterConfiguration delegate) {
this.delegate = delegate;
}

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

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

@Override
public Optional<String> getRawConfigurationParameter(String key) {
return delegate.getRawConfigurationParameter(key);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,16 @@ public DefaultJupiterConfiguration(ConfigurationParameters configurationParamete
"ConfigurationParameters must not be null");
}

@Override
public Optional<String> getExtensionAutodetectionIncludePattern() {
return configurationParameters.get(EXTENSIONS_AUTODETECTION_INCLUDE_PROPERTY_NAME);
}

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

@Override
public Optional<String> getRawConfigurationParameter(String key) {
return configurationParameters.get(key);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
@API(status = INTERNAL, since = "5.4")
public interface JupiterConfiguration {

String EXTENSIONS_AUTODETECTION_INCLUDE_PROPERTY_NAME = "junit.jupiter.extensions.autodetection.include";
String EXTENSIONS_AUTODETECTION_EXCLUDE_PROPERTY_NAME = "junit.jupiter.extensions.autodetection.exclude";
String DEACTIVATE_CONDITIONS_PATTERN_PROPERTY_NAME = "junit.jupiter.conditions.deactivate";
String PARALLEL_EXECUTION_ENABLED_PROPERTY_NAME = "junit.jupiter.execution.parallel.enabled";
String DEFAULT_EXECUTION_MODE_PROPERTY_NAME = Execution.DEFAULT_EXECUTION_MODE_PROPERTY_NAME;
Expand All @@ -48,6 +50,10 @@ 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();

Optional<String> getRawConfigurationParameter(String key);

<T> Optional<T> getRawConfigurationParameter(String key, Function<String, T> transformer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import java.util.ServiceLoader;
import java.util.Set;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Stream;

import org.apiguardian.api.API;
Expand All @@ -37,6 +38,7 @@
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;

/**
Expand Down Expand Up @@ -83,7 +85,7 @@ public static MutableExtensionRegistry createRegistryWithDefaultExtensions(Jupit
extensionRegistry.registerDefaultExtension(new TempDirectory(configuration));

if (configuration.isExtensionAutoDetectionEnabled()) {
registerAutoDetectedExtensions(extensionRegistry);
registerAutoDetectedExtensions(extensionRegistry, configuration);
}

if (configuration.isThreadDumpOnTimeoutEnabled()) {
Expand All @@ -93,9 +95,23 @@ public static MutableExtensionRegistry createRegistryWithDefaultExtensions(Jupit
return extensionRegistry;
}

private static void registerAutoDetectedExtensions(MutableExtensionRegistry extensionRegistry) {
private static void registerAutoDetectedExtensions(MutableExtensionRegistry extensionRegistry,
JupiterConfiguration configuration) {
Predicate<String> filter = createExtensionFilterByPatterns(
configuration.getExtensionAutodetectionIncludePattern().orElse("*"),
configuration.getExtensionAutodetectionExcludePattern().orElse(""));

ServiceLoader.load(Extension.class, ClassLoaderUtils.getDefaultClassLoader())//
.forEach(extensionRegistry::registerAutoDetectedExtension);
.forEach(extension -> {
if (filter.test(extension.getClass().getName())) {
extensionRegistry.registerAutoDetectedExtension(extension);
}
});
}

private static Predicate<String> createExtensionFilterByPatterns(String include, String exclude) {
return ClassNamePatternFilterUtils.includeMatchingClassNames(include).and(
ClassNamePatternFilterUtils.excludeMatchingClassNames(exclude));
}

/**
Expand Down

0 comments on commit cfb6845

Please sign in to comment.