Polish "Prevent double registration of event publisher registrar"
See gh-39297
This commit is contained in:
parent
50c44e301a
commit
61ca87f7a4
|
@ -138,7 +138,7 @@ public class TestcontainersPropertySource extends EnumerablePropertySource<Map<S
|
||||||
* to the {@link TestcontainersPropertySource}. This class is a
|
* to the {@link TestcontainersPropertySource}. This class is a
|
||||||
* {@link BeanFactoryPostProcessor} so that it is initialized as early as possible.
|
* {@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();
|
static final String NAME = EventPublisherRegistrar.class.getName();
|
||||||
|
|
||||||
|
|
|
@ -23,6 +23,8 @@ import java.util.Map;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
|
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.ApplicationEvent;
|
||||||
import org.springframework.context.support.GenericApplicationContext;
|
import org.springframework.context.support.GenericApplicationContext;
|
||||||
import org.springframework.core.env.EnumerablePropertySource;
|
import org.springframework.core.env.EnumerablePropertySource;
|
||||||
|
@ -42,6 +44,13 @@ class TestcontainersPropertySourceTests {
|
||||||
|
|
||||||
private MockEnvironment environment = new MockEnvironment();
|
private MockEnvironment environment = new MockEnvironment();
|
||||||
|
|
||||||
|
private GenericApplicationContext context = new GenericApplicationContext();
|
||||||
|
|
||||||
|
TestcontainersPropertySourceTests() {
|
||||||
|
((DefaultListableBeanFactory) this.context.getBeanFactory()).setAllowBeanDefinitionOverriding(false);
|
||||||
|
this.context.setEnvironment(this.environment);
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void getPropertyWhenHasValueSupplierReturnsSuppliedValue() {
|
void getPropertyWhenHasValueSupplierReturnsSuppliedValue() {
|
||||||
DynamicPropertyRegistry registry = TestcontainersPropertySource.attach(this.environment);
|
DynamicPropertyRegistry registry = TestcontainersPropertySource.attach(this.environment);
|
||||||
|
@ -90,14 +99,14 @@ class TestcontainersPropertySourceTests {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void attachWhenNotAttachedAttaches() {
|
void attachToEnvironmentWhenNotAttachedAttaches() {
|
||||||
TestcontainersPropertySource.attach(this.environment);
|
TestcontainersPropertySource.attach(this.environment);
|
||||||
PropertySource<?> propertySource = this.environment.getPropertySources().get(TestcontainersPropertySource.NAME);
|
PropertySource<?> propertySource = this.environment.getPropertySources().get(TestcontainersPropertySource.NAME);
|
||||||
assertThat(propertySource).isNotNull();
|
assertThat(propertySource).isNotNull();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void attachWhenAlreadyAttachedReturnsExisting() {
|
void attachToEnvironmentWhenAlreadyAttachedReturnsExisting() {
|
||||||
DynamicPropertyRegistry r1 = TestcontainersPropertySource.attach(this.environment);
|
DynamicPropertyRegistry r1 = TestcontainersPropertySource.attach(this.environment);
|
||||||
PropertySource<?> p1 = this.environment.getPropertySources().get(TestcontainersPropertySource.NAME);
|
PropertySource<?> p1 = this.environment.getPropertySources().get(TestcontainersPropertySource.NAME);
|
||||||
DynamicPropertyRegistry r2 = TestcontainersPropertySource.attach(this.environment);
|
DynamicPropertyRegistry r2 = TestcontainersPropertySource.attach(this.environment);
|
||||||
|
@ -106,6 +115,24 @@ class TestcontainersPropertySourceTests {
|
||||||
assertThat(p1).isSameAs(p2);
|
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
|
@Test
|
||||||
void getPropertyPublishesEvent() {
|
void getPropertyPublishesEvent() {
|
||||||
try (GenericApplicationContext applicationContext = new GenericApplicationContext()) {
|
try (GenericApplicationContext applicationContext = new GenericApplicationContext()) {
|
||||||
|
|
Loading…
Reference in New Issue