Merge pull request #39297 from Tish17

* gh-39297:
  Polish "Prevent double registration of event publisher registrar"
  Prevent double registration of event publisher registrar

Closes gh-39297
This commit is contained in:
Andy Wilkinson 2024-01-30 16:48:23 +00:00
commit 87e44a37f9
2 changed files with 31 additions and 4 deletions

View File

@ -115,7 +115,7 @@ public class TestcontainersPropertySource extends EnumerablePropertySource<Map<S
if (eventPublisher != null) {
propertySource.addEventPublisher(eventPublisher);
}
else if (registry != null) {
else if (registry != null && !registry.containsBeanDefinition(EventPublisherRegistrar.NAME)) {
registry.registerBeanDefinition(EventPublisherRegistrar.NAME, new RootBeanDefinition(
EventPublisherRegistrar.class, () -> new EventPublisherRegistrar(environment)));
}
@ -138,7 +138,7 @@ public class TestcontainersPropertySource extends EnumerablePropertySource<Map<S
* to the {@link TestcontainersPropertySource}. This class is a
* {@link BeanFactoryPostProcessor} so that it is initialized as early as possible.
*/
private static class EventPublisherRegistrar implements BeanFactoryPostProcessor, ApplicationEventPublisherAware {
static class EventPublisherRegistrar implements BeanFactoryPostProcessor, ApplicationEventPublisherAware {
static final String NAME = EventPublisherRegistrar.class.getName();

View File

@ -23,6 +23,8 @@ import java.util.Map;
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.properties.TestcontainersPropertySource.EventPublisherRegistrar;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.support.GenericApplicationContext;
import org.springframework.core.env.EnumerablePropertySource;
@ -42,6 +44,13 @@ class TestcontainersPropertySourceTests {
private MockEnvironment environment = new MockEnvironment();
private GenericApplicationContext context = new GenericApplicationContext();
TestcontainersPropertySourceTests() {
((DefaultListableBeanFactory) this.context.getBeanFactory()).setAllowBeanDefinitionOverriding(false);
this.context.setEnvironment(this.environment);
}
@Test
void getPropertyWhenHasValueSupplierReturnsSuppliedValue() {
DynamicPropertyRegistry registry = TestcontainersPropertySource.attach(this.environment);
@ -90,14 +99,14 @@ class TestcontainersPropertySourceTests {
}
@Test
void attachWhenNotAttachedAttaches() {
void attachToEnvironmentWhenNotAttachedAttaches() {
TestcontainersPropertySource.attach(this.environment);
PropertySource<?> propertySource = this.environment.getPropertySources().get(TestcontainersPropertySource.NAME);
assertThat(propertySource).isNotNull();
}
@Test
void attachWhenAlreadyAttachedReturnsExisting() {
void attachToEnvironmentWhenAlreadyAttachedReturnsExisting() {
DynamicPropertyRegistry r1 = TestcontainersPropertySource.attach(this.environment);
PropertySource<?> p1 = this.environment.getPropertySources().get(TestcontainersPropertySource.NAME);
DynamicPropertyRegistry r2 = TestcontainersPropertySource.attach(this.environment);
@ -106,6 +115,24 @@ class TestcontainersPropertySourceTests {
assertThat(p1).isSameAs(p2);
}
@Test
void attachToEnvironmentAndContextWhenNotAttachedAttaches() {
TestcontainersPropertySource.attach(this.environment, this.context);
PropertySource<?> propertySource = this.environment.getPropertySources().get(TestcontainersPropertySource.NAME);
assertThat(propertySource).isNotNull();
assertThat(this.context.containsBean(EventPublisherRegistrar.NAME));
}
@Test
void attachToEnvironmentAndContextWhenAlreadyAttachedReturnsExisting() {
DynamicPropertyRegistry r1 = TestcontainersPropertySource.attach(this.environment, this.context);
PropertySource<?> p1 = this.environment.getPropertySources().get(TestcontainersPropertySource.NAME);
DynamicPropertyRegistry r2 = TestcontainersPropertySource.attach(this.environment, this.context);
PropertySource<?> p2 = this.environment.getPropertySources().get(TestcontainersPropertySource.NAME);
assertThat(r1).isSameAs(r2);
assertThat(p1).isSameAs(p2);
}
@Test
void getPropertyPublishesEvent() {
try (GenericApplicationContext applicationContext = new GenericApplicationContext()) {