From 2267430bdb299321a95c1759e4f550d249e26ed0 Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Tue, 28 Mar 2023 10:22:29 +0100 Subject: [PATCH] Find service connections declaring in enclosing classes Fixes gh-34790 --- .../ServiceConnectionContextCustomizer.java | 4 + ...iceConnectionContextCustomizerFactory.java | 12 ++- ...nnectionContextCustomizerFactoryTests.java | 84 +++++++++++++++++++ 3 files changed, 98 insertions(+), 2 deletions(-) create mode 100644 spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/service/connection/ServiceConnectionContextCustomizerFactoryTests.java diff --git a/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/service/connection/ServiceConnectionContextCustomizer.java b/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/service/connection/ServiceConnectionContextCustomizer.java index 243662ca1c..839b125dac 100644 --- a/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/service/connection/ServiceConnectionContextCustomizer.java +++ b/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/service/connection/ServiceConnectionContextCustomizer.java @@ -78,4 +78,8 @@ class ServiceConnectionContextCustomizer implements ContextCustomizer { return new RootBeanDefinition((Class) instance.getClass(), () -> instance); } + List> getSources() { + return this.sources; + } + } diff --git a/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/service/connection/ServiceConnectionContextCustomizerFactory.java b/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/service/connection/ServiceConnectionContextCustomizerFactory.java index 485bf3f145..425b1929ca 100644 --- a/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/service/connection/ServiceConnectionContextCustomizerFactory.java +++ b/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/service/connection/ServiceConnectionContextCustomizerFactory.java @@ -28,6 +28,7 @@ import org.springframework.core.annotation.MergedAnnotations; import org.springframework.test.context.ContextConfigurationAttributes; import org.springframework.test.context.ContextCustomizer; import org.springframework.test.context.ContextCustomizerFactory; +import org.springframework.test.context.TestContextAnnotationUtils; import org.springframework.util.Assert; import org.springframework.util.ReflectionUtils; @@ -44,12 +45,19 @@ class ServiceConnectionContextCustomizerFactory implements ContextCustomizerFact public ContextCustomizer createContextCustomizer(Class testClass, List configAttributes) { List> sources = new ArrayList<>(); - ReflectionUtils.doWithFields(testClass, (field) -> { + findSources(testClass, sources); + return (sources.isEmpty()) ? null : new ServiceConnectionContextCustomizer(sources); + } + + private void findSources(Class clazz, List> sources) { + ReflectionUtils.doWithFields(clazz, (field) -> { MergedAnnotations annotations = MergedAnnotations.from(field); annotations.stream(ServiceConnection.class) .forEach((annotation) -> sources.add(createSource(field, annotation))); }); - return (sources.isEmpty()) ? null : new ServiceConnectionContextCustomizer(sources); + if (TestContextAnnotationUtils.searchEnclosingClass(clazz)) { + findSources(clazz.getEnclosingClass(), sources); + } } private ContainerConnectionSource createSource(Field field, diff --git a/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/service/connection/ServiceConnectionContextCustomizerFactoryTests.java b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/service/connection/ServiceConnectionContextCustomizerFactoryTests.java new file mode 100644 index 0000000000..e5980710cd --- /dev/null +++ b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/service/connection/ServiceConnectionContextCustomizerFactoryTests.java @@ -0,0 +1,84 @@ +/* + * 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.test.autoconfigure.service.connection; + +import org.junit.jupiter.api.Nested; +import org.junit.jupiter.api.Test; +import org.testcontainers.containers.GenericContainer; + +import org.springframework.boot.autoconfigure.service.connection.ConnectionDetails; +import org.springframework.boot.test.autoconfigure.service.connection.ServiceConnectionContextCustomizerFactoryTests.ServiceConnections.NestedClass; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * Tests for {@link ServiceConnectionContextCustomizerFactory}. + * + * @author Andy Wilkinson + */ +public class ServiceConnectionContextCustomizerFactoryTests { + + private final ServiceConnectionContextCustomizerFactory factory = new ServiceConnectionContextCustomizerFactory(); + + @Test + void whenClassHasNoServiceConnectionsThenCreateReturnsNull() { + assertThat(this.factory.createContextCustomizer(NoServiceConnections.class, null)).isNull(); + } + + @Test + void whenClassHasServiceConnectionsThenCreateReturnsCustomizer() { + ServiceConnectionContextCustomizer customizer = (ServiceConnectionContextCustomizer) this.factory + .createContextCustomizer(ServiceConnections.class, null); + assertThat(customizer).isNotNull(); + assertThat(customizer.getSources()).hasSize(2); + } + + @Test + void whenEnclosingClassHasServiceConnectionsThenCreateReturnsCustomizer() { + ServiceConnectionContextCustomizer customizer = (ServiceConnectionContextCustomizer) this.factory + .createContextCustomizer(NestedClass.class, null); + assertThat(customizer).isNotNull(); + assertThat(customizer.getSources()).hasSize(3); + } + + static class NoServiceConnections { + + } + + static class ServiceConnections { + + @ServiceConnection(TestConnectionDetails.class) + private static GenericContainer service1 = new GenericContainer<>("example"); + + @ServiceConnection(TestConnectionDetails.class) + private static GenericContainer service2 = new GenericContainer<>("example"); + + @Nested + class NestedClass { + + @ServiceConnection(TestConnectionDetails.class) + private static GenericContainer service3 = new GenericContainer<>("example"); + + } + + } + + static class TestConnectionDetails implements ConnectionDetails { + + } + +}