Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use beanName as default name of @ServiceConnection #36064

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -38,6 +40,7 @@
* {@link ServiceConnectionAutoConfiguration}.
*
* @author Phillip Webb
* @author Yanming Zhou
*/
class ServiceConnectionAutoConfigurationRegistrar implements ImportBeanDefinitionRegistrar {

Expand Down Expand Up @@ -94,8 +97,21 @@ private <C extends Container<?>> ContainerConnectionSource<C> createSource(
ServiceConnection annotation) {
Origin origin = new BeanOrigin(beanName, beanDefinition);
Class<C> containerType = (Class<C>) 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<String, Object> 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));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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();
}

}

}