Skip to content

Commit f4cd903

Browse files
committed
Merge branch '3.2.x'
Closes gh-39346
2 parents 10e750a + 87e44a3 commit f4cd903

File tree

2 files changed

+31
-4
lines changed

2 files changed

+31
-4
lines changed

spring-boot-project/spring-boot-testcontainers/src/main/java/org/springframework/boot/testcontainers/properties/TestcontainersPropertySource.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ private static DynamicPropertyRegistry attach(Environment environment, Applicati
115115
if (eventPublisher != null) {
116116
propertySource.addEventPublisher(eventPublisher);
117117
}
118-
else if (registry != null) {
118+
else if (registry != null && !registry.containsBeanDefinition(EventPublisherRegistrar.NAME)) {
119119
registry.registerBeanDefinition(EventPublisherRegistrar.NAME, new RootBeanDefinition(
120120
EventPublisherRegistrar.class, () -> new EventPublisherRegistrar(environment)));
121121
}
@@ -138,7 +138,7 @@ static TestcontainersPropertySource getOrAdd(ConfigurableEnvironment environment
138138
* to the {@link TestcontainersPropertySource}. This class is a
139139
* {@link BeanFactoryPostProcessor} so that it is initialized as early as possible.
140140
*/
141-
private static class EventPublisherRegistrar implements BeanFactoryPostProcessor, ApplicationEventPublisherAware {
141+
static class EventPublisherRegistrar implements BeanFactoryPostProcessor, ApplicationEventPublisherAware {
142142

143143
static final String NAME = EventPublisherRegistrar.class.getName();
144144

spring-boot-project/spring-boot-testcontainers/src/test/java/org/springframework/boot/testcontainers/properties/TestcontainersPropertySourceTests.java

+29-2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
import org.junit.jupiter.api.Test;
2424

2525
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
26+
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
27+
import org.springframework.boot.testcontainers.properties.TestcontainersPropertySource.EventPublisherRegistrar;
2628
import org.springframework.context.ApplicationEvent;
2729
import org.springframework.context.support.GenericApplicationContext;
2830
import org.springframework.core.env.EnumerablePropertySource;
@@ -42,6 +44,13 @@ class TestcontainersPropertySourceTests {
4244

4345
private MockEnvironment environment = new MockEnvironment();
4446

47+
private GenericApplicationContext context = new GenericApplicationContext();
48+
49+
TestcontainersPropertySourceTests() {
50+
((DefaultListableBeanFactory) this.context.getBeanFactory()).setAllowBeanDefinitionOverriding(false);
51+
this.context.setEnvironment(this.environment);
52+
}
53+
4554
@Test
4655
void getPropertyWhenHasValueSupplierReturnsSuppliedValue() {
4756
DynamicPropertyRegistry registry = TestcontainersPropertySource.attach(this.environment);
@@ -90,14 +99,14 @@ void getSourceReturnsImmutableSource() {
9099
}
91100

92101
@Test
93-
void attachWhenNotAttachedAttaches() {
102+
void attachToEnvironmentWhenNotAttachedAttaches() {
94103
TestcontainersPropertySource.attach(this.environment);
95104
PropertySource<?> propertySource = this.environment.getPropertySources().get(TestcontainersPropertySource.NAME);
96105
assertThat(propertySource).isNotNull();
97106
}
98107

99108
@Test
100-
void attachWhenAlreadyAttachedReturnsExisting() {
109+
void attachToEnvironmentWhenAlreadyAttachedReturnsExisting() {
101110
DynamicPropertyRegistry r1 = TestcontainersPropertySource.attach(this.environment);
102111
PropertySource<?> p1 = this.environment.getPropertySources().get(TestcontainersPropertySource.NAME);
103112
DynamicPropertyRegistry r2 = TestcontainersPropertySource.attach(this.environment);
@@ -106,6 +115,24 @@ void attachWhenAlreadyAttachedReturnsExisting() {
106115
assertThat(p1).isSameAs(p2);
107116
}
108117

118+
@Test
119+
void attachToEnvironmentAndContextWhenNotAttachedAttaches() {
120+
TestcontainersPropertySource.attach(this.environment, this.context);
121+
PropertySource<?> propertySource = this.environment.getPropertySources().get(TestcontainersPropertySource.NAME);
122+
assertThat(propertySource).isNotNull();
123+
assertThat(this.context.containsBean(EventPublisherRegistrar.NAME));
124+
}
125+
126+
@Test
127+
void attachToEnvironmentAndContextWhenAlreadyAttachedReturnsExisting() {
128+
DynamicPropertyRegistry r1 = TestcontainersPropertySource.attach(this.environment, this.context);
129+
PropertySource<?> p1 = this.environment.getPropertySources().get(TestcontainersPropertySource.NAME);
130+
DynamicPropertyRegistry r2 = TestcontainersPropertySource.attach(this.environment, this.context);
131+
PropertySource<?> p2 = this.environment.getPropertySources().get(TestcontainersPropertySource.NAME);
132+
assertThat(r1).isSameAs(r2);
133+
assertThat(p1).isSameAs(p2);
134+
}
135+
109136
@Test
110137
void getPropertyPublishesEvent() {
111138
try (GenericApplicationContext applicationContext = new GenericApplicationContext()) {

0 commit comments

Comments
 (0)