Find service connections declaring in enclosing classes
Fixes gh-34790
This commit is contained in:
@@ -78,4 +78,8 @@ class ServiceConnectionContextCustomizer implements ContextCustomizer {
|
||||
return new RootBeanDefinition((Class<T>) instance.getClass(), () -> instance);
|
||||
}
|
||||
|
||||
List<ContainerConnectionSource<?, ?, ?>> getSources() {
|
||||
return this.sources;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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<ContextConfigurationAttributes> configAttributes) {
|
||||
List<ContainerConnectionSource<?, ?, ?>> sources = new ArrayList<>();
|
||||
ReflectionUtils.doWithFields(testClass, (field) -> {
|
||||
findSources(testClass, sources);
|
||||
return (sources.isEmpty()) ? null : new ServiceConnectionContextCustomizer(sources);
|
||||
}
|
||||
|
||||
private void findSources(Class<?> clazz, List<ContainerConnectionSource<?, ?, ?>> 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,
|
||||
|
||||
@@ -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 {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user