Skip to content

Commit

Permalink
Simplify DynamicPropertyRegistrar support
Browse files Browse the repository at this point in the history
  • Loading branch information
sbrannen committed Sep 11, 2024
1 parent 0f28d50 commit 626bfc2
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 80 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,11 @@ public void customizeContext(ConfigurableApplicationContext context, MergedConte

if (!this.methods.isEmpty()) {
ConfigurableEnvironment environment = context.getEnvironment();
DefaultDynamicPropertyRegistry dynamicPropertyRegistry = new DefaultDynamicPropertyRegistry(environment);
DynamicValuesPropertySource propertySource = DynamicValuesPropertySource.getOrCreate(environment);
DynamicPropertyRegistry registry = propertySource.dynamicPropertyRegistry;
this.methods.forEach(method -> {
ReflectionUtils.makeAccessible(method);
ReflectionUtils.invokeMethod(method, null, dynamicPropertyRegistry);
ReflectionUtils.invokeMethod(method, null, registry);
});
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.springframework.core.env.Environment;
import org.springframework.lang.Nullable;
import org.springframework.test.context.DynamicPropertyRegistrar;
import org.springframework.test.context.DynamicPropertyRegistry;

/**
* Internal component which eagerly initializes {@link DynamicPropertyRegistrar}
Expand Down Expand Up @@ -60,13 +61,14 @@ public void initialize(ListableBeanFactory beanFactory) {
String[] beanNames = BeanFactoryUtils.beanNamesForTypeIncludingAncestors(
beanFactory, DynamicPropertyRegistrar.class);
if (beanNames.length > 0) {
DefaultDynamicPropertyRegistry dynamicPropertyRegistry = new DefaultDynamicPropertyRegistry(this.environment);
DynamicValuesPropertySource propertySource = DynamicValuesPropertySource.getOrCreate(this.environment);
DynamicPropertyRegistry registry = propertySource.dynamicPropertyRegistry;
for (String name : beanNames) {
if (logger.isDebugEnabled()) {
logger.debug("Eagerly initializing DynamicPropertyRegistrar bean '%s'".formatted(name));
}
DynamicPropertyRegistrar registrar = beanFactory.getBean(name, DynamicPropertyRegistrar.class);
registrar.accept(dynamicPropertyRegistry);
registrar.accept(registry);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.function.Supplier;

import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.MapPropertySource;
import org.springframework.core.env.MutablePropertySources;
import org.springframework.core.env.PropertySource;
import org.springframework.lang.Nullable;
import org.springframework.test.context.DynamicPropertyRegistry;
import org.springframework.util.Assert;
import org.springframework.util.function.SupplierUtils;

/**
Expand All @@ -42,9 +42,7 @@ class DynamicValuesPropertySource extends MapPropertySource {

static final String PROPERTY_SOURCE_NAME = "Dynamic Test Properties";

private static final Lock propertySourcesLock = new ReentrantLock();

final Map<String, Supplier<Object>> valueSuppliers;
final DynamicPropertyRegistry dynamicPropertyRegistry;


DynamicValuesPropertySource() {
Expand All @@ -53,7 +51,11 @@ class DynamicValuesPropertySource extends MapPropertySource {

DynamicValuesPropertySource(Map<String, Supplier<Object>> valueSuppliers) {
super(PROPERTY_SOURCE_NAME, Collections.unmodifiableMap(valueSuppliers));
this.valueSuppliers = valueSuppliers;
this.dynamicPropertyRegistry = (name, valueSupplier) -> {
Assert.hasText(name, "'name' must not be null or blank");
Assert.notNull(valueSupplier, "'valueSupplier' must not be null");
valueSuppliers.put(name, valueSupplier);
};
}


Expand All @@ -64,27 +66,25 @@ public Object getProperty(String name) {
}


/**
* Get the {@code DynamicValuesPropertySource} registered in the environment
* or create and register a new {@code DynamicValuesPropertySource} in the
* environment.
*/
static DynamicValuesPropertySource getOrCreate(ConfigurableEnvironment environment) {
propertySourcesLock.lock();
try {
MutablePropertySources propertySources = environment.getPropertySources();
PropertySource<?> ps = propertySources.get(PROPERTY_SOURCE_NAME);
if (ps instanceof DynamicValuesPropertySource dynamicValuesPropertySource) {
return dynamicValuesPropertySource;
}
else if (ps == null) {
DynamicValuesPropertySource dynamicValuesPropertySource = new DynamicValuesPropertySource();
propertySources.addFirst(dynamicValuesPropertySource);
return dynamicValuesPropertySource;
}
else {
throw new IllegalStateException(
"PropertySource with name '%s' must be a DynamicValuesPropertySource"
.formatted(PROPERTY_SOURCE_NAME));
}
MutablePropertySources propertySources = environment.getPropertySources();
PropertySource<?> propertySource = propertySources.get(PROPERTY_SOURCE_NAME);
if (propertySource instanceof DynamicValuesPropertySource dynamicValuesPropertySource) {
return dynamicValuesPropertySource;
}
else if (propertySource == null) {
DynamicValuesPropertySource dynamicValuesPropertySource = new DynamicValuesPropertySource();
propertySources.addFirst(dynamicValuesPropertySource);
return dynamicValuesPropertySource;
}
finally {
propertySourcesLock.unlock();
else {
throw new IllegalStateException("PropertySource with name '%s' must be a DynamicValuesPropertySource"
.formatted(PROPERTY_SOURCE_NAME));
}
}

Expand Down

0 comments on commit 626bfc2

Please sign in to comment.