Prefer DynamicPropertyRegistar to DynamicPropertyRegistry

Closes gh-41996
This commit is contained in:
Andy Wilkinson
2024-09-25 13:54:43 +01:00
parent fdc4a51e24
commit 3c095b4ec2
18 changed files with 341 additions and 77 deletions

View File

@@ -25,10 +25,11 @@ import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.boot.testcontainers.lifecycle.BeforeTestcontainerUsedEvent;
import org.springframework.boot.testcontainers.properties.TestcontainersPropertySource.EventPublisherRegistrar;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.support.GenericApplicationContext;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.EnumerablePropertySource;
import org.springframework.core.env.MapPropertySource;
import org.springframework.core.env.PropertySource;
import org.springframework.mock.env.MockEnvironment;
import org.springframework.test.context.DynamicPropertyRegistry;
@@ -40,10 +41,14 @@ import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
* Tests for {@link TestcontainersPropertySource}.
*
* @author Phillip Webb
* @deprecated since 3.4.0 for removal in 3.6.0
*/
@SuppressWarnings("removal")
@Deprecated(since = "3.4.0", forRemoval = true)
class TestcontainersPropertySourceTests {
private MockEnvironment environment = new MockEnvironment();
private MockEnvironment environment = new MockEnvironment()
.withProperty("spring.testcontainers.dynamic-property-registry-injection", "allow");
private GenericApplicationContext context = new GenericApplicationContext();
@@ -121,7 +126,8 @@ class TestcontainersPropertySourceTests {
TestcontainersPropertySource.attach(this.environment, this.context);
PropertySource<?> propertySource = this.environment.getPropertySources().get(TestcontainersPropertySource.NAME);
assertThat(propertySource).isNotNull();
assertThat(this.context.containsBean(EventPublisherRegistrar.NAME));
assertThat(this.context.containsBean(
org.springframework.boot.testcontainers.properties.TestcontainersPropertySource.EventPublisherRegistrar.NAME));
}
@Test
@@ -137,15 +143,19 @@ class TestcontainersPropertySourceTests {
@Test
void getPropertyPublishesEvent() {
try (GenericApplicationContext applicationContext = new GenericApplicationContext()) {
ConfigurableEnvironment environment = applicationContext.getEnvironment();
environment.getPropertySources()
.addLast(new MapPropertySource("test",
Map.of("spring.testcontainers.dynamic-property-registry-injection", "allow")));
List<ApplicationEvent> events = new ArrayList<>();
applicationContext.addApplicationListener(events::add);
DynamicPropertyRegistry registry = TestcontainersPropertySource.attach(applicationContext.getEnvironment(),
DynamicPropertyRegistry registry = TestcontainersPropertySource.attach(environment,
(BeanDefinitionRegistry) applicationContext.getBeanFactory());
applicationContext.refresh();
registry.add("test", () -> "spring");
assertThat(applicationContext.getEnvironment().containsProperty("test")).isTrue();
assertThat(environment.containsProperty("test")).isTrue();
assertThat(events.isEmpty());
assertThat(applicationContext.getEnvironment().getProperty("test")).isEqualTo("spring");
assertThat(environment.getProperty("test")).isEqualTo("spring");
assertThat(events.stream().filter(BeforeTestcontainerUsedEvent.class::isInstance)).hasSize(1);
}
}

View File

@@ -30,16 +30,12 @@ import org.springframework.boot.autoconfigure.jdbc.JdbcConnectionDetails;
import org.springframework.boot.autoconfigure.service.connection.ConnectionDetails;
import org.springframework.boot.autoconfigure.service.connection.ConnectionDetailsFactory;
import org.springframework.boot.origin.Origin;
import org.springframework.boot.testcontainers.lifecycle.BeforeTestcontainerUsedEvent;
import org.springframework.boot.testcontainers.service.connection.ContainerConnectionDetailsFactoryTests.TestContainerConnectionDetailsFactory.TestContainerConnectionDetails;
import org.springframework.context.ApplicationContext;
import org.springframework.core.annotation.MergedAnnotation;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.BDDMockito.then;
import static org.mockito.Mockito.mock;
/**
@@ -135,14 +131,11 @@ class ContainerConnectionDetailsFactoryTests {
}
@Test
void getContainerWhenInitializedPublishesEventAndReturnsSuppliedContainer() throws Exception {
void getContainerWhenInitializedReturnsSuppliedContainer() throws Exception {
TestContainerConnectionDetailsFactory factory = new TestContainerConnectionDetailsFactory();
TestContainerConnectionDetails connectionDetails = getConnectionDetails(factory, this.source);
ApplicationContext context = mock(ApplicationContext.class);
connectionDetails.setApplicationContext(context);
connectionDetails.afterPropertiesSet();
assertThat(connectionDetails.callGetContainer()).isSameAs(this.container);
then(context).should().publishEvent(any(BeforeTestcontainerUsedEvent.class));
}
@Test