Skip to content

Commit 208d750

Browse files
committed
Merge pull request #50033 from kwondh5217
* pr/50033: Polish 'Include @bean method annotations in ContainerConnectionSource' Include @bean method annotations in ContainerConnectionSource Closes gh-50033
2 parents 019eb63 + d3c8829 commit 208d750

6 files changed

Lines changed: 174 additions & 2 deletions

File tree

spring-boot-project/spring-boot-testcontainers/src/main/java/org/springframework/boot/testcontainers/service/connection/ServiceConnectionAutoConfigurationRegistrar.java

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
import org.springframework.beans.factory.BeanFactory;
2525
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
26+
import org.springframework.beans.factory.annotation.AnnotatedBeanDefinition;
2627
import org.springframework.beans.factory.config.BeanDefinition;
2728
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
2829
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
@@ -33,12 +34,14 @@
3334
import org.springframework.core.annotation.MergedAnnotation;
3435
import org.springframework.core.annotation.MergedAnnotations;
3536
import org.springframework.core.type.AnnotationMetadata;
37+
import org.springframework.core.type.MethodMetadata;
3638

3739
/**
3840
* {@link ImportBeanDefinitionRegistrar} used by
3941
* {@link ServiceConnectionAutoConfiguration}.
4042
*
4143
* @author Phillip Webb
44+
* @author Daeho Kwon
4245
*/
4346
class ServiceConnectionAutoConfigurationRegistrar implements ImportBeanDefinitionRegistrar {
4447

@@ -60,8 +63,7 @@ private void registerBeanDefinitions(ConfigurableListableBeanFactory beanFactory
6063
new ConnectionDetailsFactories(null));
6164
for (String beanName : beanFactory.getBeanNamesForType(Container.class)) {
6265
BeanDefinition beanDefinition = getBeanDefinition(beanFactory, beanName);
63-
MergedAnnotations annotations = (beanDefinition instanceof TestcontainerBeanDefinition testcontainerBeanDefinition)
64-
? testcontainerBeanDefinition.getAnnotations() : null;
66+
MergedAnnotations annotations = getAnnotations(beanDefinition);
6567
for (ServiceConnection serviceConnection : getServiceConnections(beanFactory, beanName, annotations)) {
6668
ContainerConnectionSource<?> source = createSource(beanFactory, beanName, beanDefinition, annotations,
6769
serviceConnection);
@@ -92,6 +94,17 @@ private BeanDefinition getBeanDefinition(ConfigurableListableBeanFactory beanFac
9294
}
9395
}
9496

97+
private MergedAnnotations getAnnotations(BeanDefinition beanDefinition) {
98+
if (beanDefinition instanceof TestcontainerBeanDefinition testcontainerBeanDefinition) {
99+
return testcontainerBeanDefinition.getAnnotations();
100+
}
101+
if (beanDefinition instanceof AnnotatedBeanDefinition annotatedBeanDefinition) {
102+
MethodMetadata metadata = annotatedBeanDefinition.getFactoryMethodMetadata();
103+
return (metadata != null) ? metadata.getAnnotations() : null;
104+
}
105+
return null;
106+
}
107+
95108
@SuppressWarnings("unchecked")
96109
private <C extends Container<?>> ContainerConnectionSource<C> createSource(
97110
ConfigurableListableBeanFactory beanFactory, String beanName, BeanDefinition beanDefinition,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* Copyright 2012-present the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.testcontainers.service.connection;
18+
19+
import org.springframework.boot.autoconfigure.service.connection.ConnectionDetails;
20+
21+
public interface DatabaseConnectionDetails extends ConnectionDetails {
22+
23+
String getJdbcUrl();
24+
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* Copyright 2012-present the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.testcontainers.service.connection;
18+
19+
import org.testcontainers.containers.JdbcDatabaseContainer;
20+
21+
public class DatabaseContainerDatabaseConnectionDetails
22+
extends ContainerConnectionDetailsFactory<JdbcDatabaseContainer<?>, DatabaseConnectionDetails> {
23+
24+
@Override
25+
protected DatabaseConnectionDetails getContainerConnectionDetails(
26+
ContainerConnectionSource<JdbcDatabaseContainer<?>> source) {
27+
return new TestDatabaseConnectionDetails(source);
28+
}
29+
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* Copyright 2012-present the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.testcontainers.service.connection;
18+
19+
import org.junit.jupiter.api.Test;
20+
import org.testcontainers.containers.PostgreSQLContainer;
21+
22+
import org.springframework.boot.autoconfigure.AutoConfigurations;
23+
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
24+
import org.springframework.boot.testcontainers.service.connection.ContainerConnectionDetailsFactory.ContainerConnectionDetails;
25+
import org.springframework.context.annotation.Bean;
26+
import org.springframework.context.annotation.Configuration;
27+
28+
import static org.assertj.core.api.Assertions.assertThat;
29+
import static org.mockito.Mockito.mock;
30+
31+
/**
32+
* Tests for {@link ServiceConnectionAutoConfigurationRegistrar}.
33+
*
34+
* @author Daeho Kwon
35+
*/
36+
class ServiceConnectionAutoConfigurationRegistrarTests {
37+
38+
@Test
39+
void sslAnnotationOnBeanMethodShouldBeDetectedInContainerConnectionSource() {
40+
new ApplicationContextRunner()
41+
.withConfiguration(AutoConfigurations.of(ServiceConnectionAutoConfiguration.class))
42+
.withUserConfiguration(ContainerConfiguration.class)
43+
.run((context) -> {
44+
ContainerConnectionDetails<?> connectionDetails = (ContainerConnectionDetails<?>) context
45+
.getBean(DatabaseConnectionDetails.class);
46+
assertThat(connectionDetails.hasAnnotation(Ssl.class)).isTrue();
47+
});
48+
}
49+
50+
@Configuration(proxyBeanMethods = false)
51+
static class ContainerConfiguration {
52+
53+
@Bean
54+
@ServiceConnection
55+
@Ssl
56+
PostgreSQLContainer<?> container() {
57+
return mock();
58+
}
59+
60+
}
61+
62+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Copyright 2012-present the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.testcontainers.service.connection;
18+
19+
import org.testcontainers.containers.JdbcDatabaseContainer;
20+
21+
import org.springframework.boot.testcontainers.service.connection.ContainerConnectionDetailsFactory.ContainerConnectionDetails;
22+
23+
class TestDatabaseConnectionDetails extends ContainerConnectionDetails<JdbcDatabaseContainer<?>>
24+
implements DatabaseConnectionDetails {
25+
26+
TestDatabaseConnectionDetails(ContainerConnectionSource<JdbcDatabaseContainer<?>> source) {
27+
super(source);
28+
}
29+
30+
@Override
31+
public String getJdbcUrl() {
32+
return getContainer().getJdbcUrl();
33+
}
34+
35+
JdbcDatabaseContainer<?> callGetContainer() {
36+
return super.getContainer();
37+
}
38+
39+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Test Connection Details Factories
2+
org.springframework.boot.autoconfigure.service.connection.ConnectionDetailsFactory=\
3+
org.springframework.boot.testcontainers.service.connection.DatabaseContainerDatabaseConnectionDetails

0 commit comments

Comments
 (0)