From bafe9408d9ea522b53fce531e14eb06981d9c0d3 Mon Sep 17 00:00:00 2001 From: Yanming Zhou Date: Sun, 25 Jun 2023 17:26:40 +0800 Subject: [PATCH] Use beanName as default name of @ServiceConnection Before this commit, ConnectionDetailsNotFoundException will be raised if unnamed @ServiceConnection is annotated on @Bean method and containerType is not accepted by ContainerConnectionSource. --- .../spring-boot-testcontainers/build.gradle | 1 + ...eConnectionAutoConfigurationRegistrar.java | 20 +++++- ...nectionDetailsFactoryIntegrationTests.java | 64 +++++++++++++++++++ 3 files changed, 83 insertions(+), 2 deletions(-) create mode 100644 spring-boot-project/spring-boot-testcontainers/src/test/java/org/springframework/boot/testcontainers/service/connection/redis/RedisContainerConnectionDetailsFactoryIntegrationTests.java diff --git a/spring-boot-project/spring-boot-testcontainers/build.gradle b/spring-boot-project/spring-boot-testcontainers/build.gradle index 9cfb9709582e..b9a229ba8bc6 100644 --- a/spring-boot-project/spring-boot-testcontainers/build.gradle +++ b/spring-boot-project/spring-boot-testcontainers/build.gradle @@ -47,6 +47,7 @@ dependencies { testImplementation("org.springframework:spring-core-test") testImplementation("org.springframework:spring-r2dbc") testImplementation("org.springframework.amqp:spring-rabbit") + testImplementation("org.springframework.data:spring-data-redis") testImplementation("org.springframework.kafka:spring-kafka") testImplementation("org.testcontainers:junit-jupiter") diff --git a/spring-boot-project/spring-boot-testcontainers/src/main/java/org/springframework/boot/testcontainers/service/connection/ServiceConnectionAutoConfigurationRegistrar.java b/spring-boot-project/spring-boot-testcontainers/src/main/java/org/springframework/boot/testcontainers/service/connection/ServiceConnectionAutoConfigurationRegistrar.java index f03ed2960d5e..cfeb5c90ea4c 100644 --- a/spring-boot-project/spring-boot-testcontainers/src/main/java/org/springframework/boot/testcontainers/service/connection/ServiceConnectionAutoConfigurationRegistrar.java +++ b/spring-boot-project/spring-boot-testcontainers/src/main/java/org/springframework/boot/testcontainers/service/connection/ServiceConnectionAutoConfigurationRegistrar.java @@ -16,7 +16,9 @@ package org.springframework.boot.testcontainers.service.connection; +import java.util.HashMap; import java.util.LinkedHashSet; +import java.util.Map; import java.util.Set; import org.testcontainers.containers.Container; @@ -38,6 +40,7 @@ * {@link ServiceConnectionAutoConfiguration}. * * @author Phillip Webb + * @author Yanming Zhou */ class ServiceConnectionAutoConfigurationRegistrar implements ImportBeanDefinitionRegistrar { @@ -94,8 +97,21 @@ private > ContainerConnectionSource createSource( ServiceConnection annotation) { Origin origin = new BeanOrigin(beanName, beanDefinition); Class containerType = (Class) beanFactory.getType(beanName, false); - String containerImageName = (beanDefinition instanceof TestcontainerBeanDefinition testcontainerBeanDefinition) - ? testcontainerBeanDefinition.getContainerImageName() : null; + String containerImageName = null; + if (beanDefinition instanceof TestcontainerBeanDefinition testcontainerBeanDefinition) { + containerImageName = testcontainerBeanDefinition.getContainerImageName(); + } + else { + if (annotation.name().isEmpty()) { + String serviceName = beanName; + if (serviceName.endsWith("Container")) { + serviceName = serviceName.substring(0, serviceName.length() - 9); + } + Map map = new HashMap<>(MergedAnnotation.from(annotation).asMap()); + map.put("name", serviceName); + annotation = MergedAnnotation.of(null, ServiceConnection.class, map).synthesize(); + } + } return new ContainerConnectionSource<>(beanName, origin, containerType, containerImageName, annotation, () -> beanFactory.getBean(beanName, containerType)); } diff --git a/spring-boot-project/spring-boot-testcontainers/src/test/java/org/springframework/boot/testcontainers/service/connection/redis/RedisContainerConnectionDetailsFactoryIntegrationTests.java b/spring-boot-project/spring-boot-testcontainers/src/test/java/org/springframework/boot/testcontainers/service/connection/redis/RedisContainerConnectionDetailsFactoryIntegrationTests.java new file mode 100644 index 000000000000..71175aa24f55 --- /dev/null +++ b/spring-boot-project/spring-boot-testcontainers/src/test/java/org/springframework/boot/testcontainers/service/connection/redis/RedisContainerConnectionDetailsFactoryIntegrationTests.java @@ -0,0 +1,64 @@ +/* + * Copyright 2012-2023 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.testcontainers.service.connection.redis; + +import org.junit.jupiter.api.Test; +import org.testcontainers.junit.jupiter.Testcontainers; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.ImportAutoConfiguration; +import org.springframework.boot.autoconfigure.data.redis.RedisConnectionDetails; +import org.springframework.boot.testcontainers.service.connection.ServiceConnection; +import org.springframework.boot.testcontainers.service.connection.ServiceConnectionAutoConfiguration; +import org.springframework.boot.testsupport.testcontainers.RedisContainer; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * Tests for {@link RedisContainerConnectionDetailsFactory}. + * + * @author Yanming Zhou + */ +@SpringJUnitConfig +@Testcontainers(disabledWithoutDocker = true) +class RedisContainerConnectionDetailsFactoryIntegrationTests { + + @Autowired + private RedisConnectionDetails connectionDetails; + + @Test + void connectionDetailsShouldBeCreatedByRedisContainerConnectionDetailsFactory() { + assertThat(this.connectionDetails.getClass().getEnclosingClass()) + .isSameAs(RedisContainerConnectionDetailsFactory.class); + } + + @Configuration(proxyBeanMethods = false) + @ImportAutoConfiguration(ServiceConnectionAutoConfiguration.class) + static class ContainerConfig { + + @Bean + @ServiceConnection + RedisContainer redis() { + return new RedisContainer(); + } + + } + +}