mirror of
https://github.com/spring-projects/spring-boot.git
synced 2024-08-29 03:06:45 +08:00
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:
commit
87e44a37f9
@ -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();
|
||||
|
||||
|
@ -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()) {
|
||||
|
Loading…
Reference in New Issue
Block a user