Support import of idomatic testcontainer declaration classes
Add an `@ImportTestcontainers` annotation which can be used to import idomatic testcontainer declaration classes. Closes gh-35245pull/35256/head
parent
26566d4a30
commit
8427e813af
@ -0,0 +1,31 @@
|
|||||||
|
/*
|
||||||
|
* 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.docs.features.testing.testcontainers.atdevelopmenttime.importingcontainerdeclarations;
|
||||||
|
|
||||||
|
import org.testcontainers.containers.MongoDBContainer;
|
||||||
|
import org.testcontainers.containers.Neo4jContainer;
|
||||||
|
import org.testcontainers.junit.jupiter.Container;
|
||||||
|
|
||||||
|
public interface MyContainers {
|
||||||
|
|
||||||
|
@Container
|
||||||
|
MongoDBContainer monogContainer = new MongoDBContainer("mongo:5.0");
|
||||||
|
|
||||||
|
@Container
|
||||||
|
Neo4jContainer<?> neo4jContainer = new Neo4jContainer<>("neo4j:5");
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,26 @@
|
|||||||
|
/*
|
||||||
|
* 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.docs.features.testing.testcontainers.atdevelopmenttime.importingcontainerdeclarations;
|
||||||
|
|
||||||
|
import org.springframework.boot.test.context.TestConfiguration;
|
||||||
|
import org.springframework.boot.testcontainers.context.ImportTestcontainers;
|
||||||
|
|
||||||
|
@TestConfiguration(proxyBeanMethods = false)
|
||||||
|
@ImportTestcontainers(MyContainers.class)
|
||||||
|
public class MyContainersConfiguration {
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,26 @@
|
|||||||
|
/*
|
||||||
|
* 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.docs.features.testing.testcontainers.atdevelopmenttime.importingcontainerdeclarations
|
||||||
|
|
||||||
|
import org.springframework.boot.test.context.TestConfiguration
|
||||||
|
import org.springframework.boot.testcontainers.context.ImportTestcontainers
|
||||||
|
|
||||||
|
@TestConfiguration(proxyBeanMethods = false)
|
||||||
|
@ImportTestcontainers(MyContainers::class)
|
||||||
|
class MyContainersConfiguration {
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,43 @@
|
|||||||
|
/*
|
||||||
|
* 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.beans;
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.config.BeanDefinition;
|
||||||
|
import org.springframework.core.annotation.MergedAnnotations;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Extended {@link org.springframework.beans.factory.config.BeanDefinition} interface used
|
||||||
|
* to register testcontainer beans.
|
||||||
|
*
|
||||||
|
* @author Phillip Webb
|
||||||
|
* @since 3.1.0
|
||||||
|
*/
|
||||||
|
public interface TestcontainerBeanDefinition extends BeanDefinition {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return the container image name or {@code null} if the image name is not yet known.
|
||||||
|
* @return the container image name
|
||||||
|
*/
|
||||||
|
String getContainerImageName();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return any annotations declared alongside the container.
|
||||||
|
* @return annotations declared with the container
|
||||||
|
*/
|
||||||
|
MergedAnnotations getAnnotations();
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,20 @@
|
|||||||
|
/*
|
||||||
|
* 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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Spring bean support classes for Testcontainers.
|
||||||
|
*/
|
||||||
|
package org.springframework.boot.testcontainers.beans;
|
@ -0,0 +1,76 @@
|
|||||||
|
/*
|
||||||
|
* 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.context;
|
||||||
|
|
||||||
|
import java.lang.reflect.Field;
|
||||||
|
import java.lang.reflect.Modifier;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.testcontainers.containers.Container;
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
|
||||||
|
import org.springframework.beans.factory.support.BeanNameGenerator;
|
||||||
|
import org.springframework.util.Assert;
|
||||||
|
import org.springframework.util.ReflectionUtils;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Used by {@link ImportTestcontainersRegistrar} to import {@link Container} fields.
|
||||||
|
*
|
||||||
|
* @author Phillip Webb
|
||||||
|
*/
|
||||||
|
class ContainerFieldsImporter {
|
||||||
|
|
||||||
|
void registerBeanDefinitions(BeanDefinitionRegistry registry, BeanNameGenerator importBeanNameGenerator,
|
||||||
|
Class<?> definitionClass) {
|
||||||
|
for (Field field : getContainerFields(definitionClass)) {
|
||||||
|
assertValid(field);
|
||||||
|
Container<?> container = getContainer(field);
|
||||||
|
registerBeanDefinition(registry, importBeanNameGenerator, field, container);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private List<Field> getContainerFields(Class<?> containersClass) {
|
||||||
|
List<Field> containerFields = new ArrayList<>();
|
||||||
|
ReflectionUtils.doWithFields(containersClass, containerFields::add, this::isContainerField);
|
||||||
|
return List.copyOf(containerFields);
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean isContainerField(Field candidate) {
|
||||||
|
return Container.class.isAssignableFrom(candidate.getType());
|
||||||
|
}
|
||||||
|
|
||||||
|
private void assertValid(Field field) {
|
||||||
|
Assert.state(Modifier.isStatic(field.getModifiers()),
|
||||||
|
() -> "Container field '" + field.getName() + "' must be static");
|
||||||
|
}
|
||||||
|
|
||||||
|
private Container<?> getContainer(Field field) {
|
||||||
|
ReflectionUtils.makeAccessible(field);
|
||||||
|
Container<?> container = (Container<?>) ReflectionUtils.getField(field, null);
|
||||||
|
Assert.state(container != null, () -> "Container field '" + field.getName() + "' must not have a null value");
|
||||||
|
return container;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void registerBeanDefinition(BeanDefinitionRegistry registry, BeanNameGenerator importBeanNameGenerator,
|
||||||
|
Field field, Container<?> container) {
|
||||||
|
TestcontainerFieldBeanDefinition beanDefinition = new TestcontainerFieldBeanDefinition(field, container);
|
||||||
|
String beanName = importBeanNameGenerator.generateBeanName(beanDefinition, registry);
|
||||||
|
registry.registerBeanDefinition(beanName, beanDefinition);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,72 @@
|
|||||||
|
/*
|
||||||
|
* 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.context;
|
||||||
|
|
||||||
|
import java.lang.reflect.Method;
|
||||||
|
import java.lang.reflect.Modifier;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
import org.springframework.boot.testcontainers.properties.TestcontainersPropertySource;
|
||||||
|
import org.springframework.core.MethodIntrospector;
|
||||||
|
import org.springframework.core.annotation.MergedAnnotations;
|
||||||
|
import org.springframework.core.env.Environment;
|
||||||
|
import org.springframework.test.context.DynamicPropertyRegistry;
|
||||||
|
import org.springframework.test.context.DynamicPropertySource;
|
||||||
|
import org.springframework.util.Assert;
|
||||||
|
import org.springframework.util.ReflectionUtils;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Used by {@link ImportTestcontainersRegistrar} to import
|
||||||
|
* {@link DynamicPropertySource @DynamicPropertySource} methods.
|
||||||
|
*
|
||||||
|
* @author Phillip Webb
|
||||||
|
*/
|
||||||
|
class DynamicPropertySourceMethodsImporter {
|
||||||
|
|
||||||
|
private final Environment environment;
|
||||||
|
|
||||||
|
DynamicPropertySourceMethodsImporter(Environment environment) {
|
||||||
|
this.environment = environment;
|
||||||
|
}
|
||||||
|
|
||||||
|
void registerDynamicPropertySources(Class<?> definitionClass) {
|
||||||
|
Set<Method> methods = MethodIntrospector.selectMethods(definitionClass, this::isAnnotated);
|
||||||
|
if (methods.isEmpty()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
DynamicPropertyRegistry registry = TestcontainersPropertySource.attach(this.environment);
|
||||||
|
methods.forEach((method) -> {
|
||||||
|
assertValid(method);
|
||||||
|
ReflectionUtils.makeAccessible(method);
|
||||||
|
ReflectionUtils.invokeMethod(method, null, registry);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean isAnnotated(Method method) {
|
||||||
|
return MergedAnnotations.from(method).isPresent(DynamicPropertySource.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void assertValid(Method method) {
|
||||||
|
Assert.state(Modifier.isStatic(method.getModifiers()),
|
||||||
|
() -> "@DynamicPropertySource method '" + method.getName() + "' must be static");
|
||||||
|
Class<?>[] types = method.getParameterTypes();
|
||||||
|
Assert.state(types.length == 1 && types[0] == DynamicPropertyRegistry.class,
|
||||||
|
() -> "@DynamicPropertySource method '" + method.getName()
|
||||||
|
+ "' must accept a single DynamicPropertyRegistry argument");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,56 @@
|
|||||||
|
/*
|
||||||
|
* 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.context;
|
||||||
|
|
||||||
|
import java.lang.annotation.Documented;
|
||||||
|
import java.lang.annotation.ElementType;
|
||||||
|
import java.lang.annotation.Retention;
|
||||||
|
import java.lang.annotation.RetentionPolicy;
|
||||||
|
import java.lang.annotation.Target;
|
||||||
|
|
||||||
|
import org.testcontainers.containers.Container;
|
||||||
|
|
||||||
|
import org.springframework.context.ApplicationContext;
|
||||||
|
import org.springframework.context.annotation.Import;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Imports idiomatic Testcontainer declaration classes into the Spring
|
||||||
|
* {@link ApplicationContext}. The following elements will be considered from the imported
|
||||||
|
* classes:
|
||||||
|
* <ul>
|
||||||
|
* <li>All static fields that declare {@link Container} values.</li>
|
||||||
|
* <li>All {@code @DynamicPropertySource} annotated methods.</li>
|
||||||
|
* </ul>
|
||||||
|
*
|
||||||
|
* @author Phillip Webb
|
||||||
|
* @since 3.1.0
|
||||||
|
*/
|
||||||
|
@Target(ElementType.TYPE)
|
||||||
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
|
@Documented
|
||||||
|
@Import(ImportTestcontainersRegistrar.class)
|
||||||
|
public @interface ImportTestcontainers {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The declaration classes to import. If no {@code value} is defined then the class
|
||||||
|
* that declares the {@link ImportTestcontainers @ImportTestcontainers} annotation
|
||||||
|
* will be searched.
|
||||||
|
* @return the definition classes to import
|
||||||
|
*/
|
||||||
|
Class<?>[] value() default {};
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,73 @@
|
|||||||
|
/*
|
||||||
|
* 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.context;
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
|
||||||
|
import org.springframework.beans.factory.support.BeanNameGenerator;
|
||||||
|
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
|
||||||
|
import org.springframework.core.annotation.MergedAnnotation;
|
||||||
|
import org.springframework.core.env.Environment;
|
||||||
|
import org.springframework.core.type.AnnotationMetadata;
|
||||||
|
import org.springframework.util.ClassUtils;
|
||||||
|
import org.springframework.util.ObjectUtils;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@link ImportBeanDefinitionRegistrar} for
|
||||||
|
* {@link ImportTestcontainers @ImportTestcontainers}.
|
||||||
|
*
|
||||||
|
* @author Phillip Webb
|
||||||
|
* @see ContainerFieldsImporter
|
||||||
|
* @see DynamicPropertySourceMethodsImporter
|
||||||
|
*/
|
||||||
|
class ImportTestcontainersRegistrar implements ImportBeanDefinitionRegistrar {
|
||||||
|
|
||||||
|
private static final String DYNAMIC_PROPERTY_SOURCE_CLASS = "org.springframework.test.context.DynamicPropertySource";
|
||||||
|
|
||||||
|
private final ContainerFieldsImporter containerFieldsImporter;
|
||||||
|
|
||||||
|
private final DynamicPropertySourceMethodsImporter dynamicPropertySourceMethodsImporter;
|
||||||
|
|
||||||
|
ImportTestcontainersRegistrar(Environment environment) {
|
||||||
|
this.containerFieldsImporter = new ContainerFieldsImporter();
|
||||||
|
this.dynamicPropertySourceMethodsImporter = (!ClassUtils.isPresent(DYNAMIC_PROPERTY_SOURCE_CLASS, null)) ? null
|
||||||
|
: new DynamicPropertySourceMethodsImporter(environment);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry,
|
||||||
|
BeanNameGenerator importBeanNameGenerator) {
|
||||||
|
MergedAnnotation<ImportTestcontainers> annotation = importingClassMetadata.getAnnotations()
|
||||||
|
.get(ImportTestcontainers.class);
|
||||||
|
Class<?>[] definitionClasses = annotation.getClassArray(MergedAnnotation.VALUE);
|
||||||
|
if (ObjectUtils.isEmpty(definitionClasses)) {
|
||||||
|
Class<?> importingClass = ClassUtils.resolveClassName(importingClassMetadata.getClassName(), null);
|
||||||
|
definitionClasses = new Class<?>[] { importingClass };
|
||||||
|
}
|
||||||
|
registerBeanDefinitions(registry, importBeanNameGenerator, definitionClasses);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void registerBeanDefinitions(BeanDefinitionRegistry registry, BeanNameGenerator importBeanNameGenerator,
|
||||||
|
Class<?>[] definitionClasses) {
|
||||||
|
for (Class<?> definitionClass : definitionClasses) {
|
||||||
|
this.containerFieldsImporter.registerBeanDefinitions(registry, importBeanNameGenerator, definitionClass);
|
||||||
|
if (this.dynamicPropertySourceMethodsImporter != null) {
|
||||||
|
this.dynamicPropertySourceMethodsImporter.registerDynamicPropertySources(definitionClass);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,55 @@
|
|||||||
|
/*
|
||||||
|
* 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.context;
|
||||||
|
|
||||||
|
import java.lang.reflect.Field;
|
||||||
|
|
||||||
|
import org.testcontainers.containers.Container;
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.support.RootBeanDefinition;
|
||||||
|
import org.springframework.boot.testcontainers.beans.TestcontainerBeanDefinition;
|
||||||
|
import org.springframework.core.annotation.MergedAnnotations;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@link RootBeanDefinition} used for testcontainer bean definitions.
|
||||||
|
*
|
||||||
|
* @author Phillip Webb
|
||||||
|
*/
|
||||||
|
class TestcontainerFieldBeanDefinition extends RootBeanDefinition implements TestcontainerBeanDefinition {
|
||||||
|
|
||||||
|
private final Container<?> container;
|
||||||
|
|
||||||
|
private final MergedAnnotations annotations;
|
||||||
|
|
||||||
|
TestcontainerFieldBeanDefinition(Field field, Container<?> container) {
|
||||||
|
this.container = container;
|
||||||
|
this.annotations = MergedAnnotations.from(field);
|
||||||
|
this.setBeanClass(container.getClass());
|
||||||
|
setInstanceSupplier(() -> container);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getContainerImageName() {
|
||||||
|
return this.container.getDockerImageName();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public MergedAnnotations getAnnotations() {
|
||||||
|
return this.annotations;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,20 @@
|
|||||||
|
/*
|
||||||
|
* 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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Spring context support classes for Testcontainers.
|
||||||
|
*/
|
||||||
|
package org.springframework.boot.testcontainers.context;
|
@ -0,0 +1,20 @@
|
|||||||
|
/*
|
||||||
|
* 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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Support for testcontainers.
|
||||||
|
*/
|
||||||
|
package org.springframework.boot.testcontainers;
|
@ -1,2 +1,2 @@
|
|||||||
org.springframework.boot.testcontainers.properties.DynamicProperySourceAutoConfiguration
|
org.springframework.boot.testcontainers.properties.TestcontainersPropertySourceAutoConfiguration
|
||||||
org.springframework.boot.testcontainers.service.connection.ServiceConnectionAutoConfiguration
|
org.springframework.boot.testcontainers.service.connection.ServiceConnectionAutoConfiguration
|
||||||
|
@ -0,0 +1,197 @@
|
|||||||
|
/*
|
||||||
|
* 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;
|
||||||
|
|
||||||
|
import java.lang.annotation.Retention;
|
||||||
|
import java.lang.annotation.RetentionPolicy;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.AfterEach;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.testcontainers.containers.Container;
|
||||||
|
import org.testcontainers.containers.PostgreSQLContainer;
|
||||||
|
|
||||||
|
import org.springframework.boot.testcontainers.beans.TestcontainerBeanDefinition;
|
||||||
|
import org.springframework.boot.testcontainers.context.ImportTestcontainers;
|
||||||
|
import org.springframework.boot.testsupport.testcontainers.DockerImageNames;
|
||||||
|
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||||
|
import org.springframework.test.context.DynamicPropertyRegistry;
|
||||||
|
import org.springframework.test.context.DynamicPropertySource;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tests for {@link ImportTestcontainers}.
|
||||||
|
*
|
||||||
|
* @author Phillip Webb
|
||||||
|
*/
|
||||||
|
class ImportTestcontainersTests {
|
||||||
|
|
||||||
|
private AnnotationConfigApplicationContext applicationContext;
|
||||||
|
|
||||||
|
@AfterEach
|
||||||
|
void teardown() {
|
||||||
|
if (this.applicationContext != null) {
|
||||||
|
this.applicationContext.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void importWithoutValueRegistersBeans() {
|
||||||
|
this.applicationContext = new AnnotationConfigApplicationContext(ImportWithoutValue.class);
|
||||||
|
String[] beanNames = this.applicationContext.getBeanNamesForType(PostgreSQLContainer.class);
|
||||||
|
assertThat(beanNames).hasSize(1);
|
||||||
|
assertThat(this.applicationContext.getBean(beanNames[0])).isSameAs(ImportWithoutValue.container);
|
||||||
|
TestcontainerBeanDefinition beanDefinition = (TestcontainerBeanDefinition) this.applicationContext
|
||||||
|
.getBeanDefinition(beanNames[0]);
|
||||||
|
assertThat(beanDefinition.getContainerImageName()).isEqualTo(ImportWithoutValue.container.getDockerImageName());
|
||||||
|
assertThat(beanDefinition.getAnnotations().isPresent(ContainerAnnotation.class)).isTrue();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void importWithValueRegistersBeans() {
|
||||||
|
this.applicationContext = new AnnotationConfigApplicationContext(ImportWithValue.class);
|
||||||
|
String[] beanNames = this.applicationContext.getBeanNamesForType(PostgreSQLContainer.class);
|
||||||
|
assertThat(beanNames).hasSize(1);
|
||||||
|
assertThat(this.applicationContext.getBean(beanNames[0])).isSameAs(ContainerDefinitions.container);
|
||||||
|
TestcontainerBeanDefinition beanDefinition = (TestcontainerBeanDefinition) this.applicationContext
|
||||||
|
.getBeanDefinition(beanNames[0]);
|
||||||
|
assertThat(beanDefinition.getContainerImageName())
|
||||||
|
.isEqualTo(ContainerDefinitions.container.getDockerImageName());
|
||||||
|
assertThat(beanDefinition.getAnnotations().isPresent(ContainerAnnotation.class)).isTrue();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void importWhenHasNoContainerFieldsDoesNothing() {
|
||||||
|
this.applicationContext = new AnnotationConfigApplicationContext(NoContainers.class);
|
||||||
|
String[] beanNames = this.applicationContext.getBeanNamesForType(Container.class);
|
||||||
|
assertThat(beanNames).isEmpty();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void importWhenHasNullContainerFieldThrowsException() {
|
||||||
|
assertThatIllegalStateException()
|
||||||
|
.isThrownBy(() -> this.applicationContext = new AnnotationConfigApplicationContext(NullContainer.class))
|
||||||
|
.withMessage("Container field 'container' must not have a null value");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void importWhenHasNonStaticContainerFieldThrowsException() {
|
||||||
|
assertThatIllegalStateException()
|
||||||
|
.isThrownBy(
|
||||||
|
() -> this.applicationContext = new AnnotationConfigApplicationContext(NonStaticContainer.class))
|
||||||
|
.withMessage("Container field 'container' must be static");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void importWhenHasContainerDefinitionsWithDynamicPropertySource() {
|
||||||
|
this.applicationContext = new AnnotationConfigApplicationContext(
|
||||||
|
ContainerDefinitionsWithDynamicPropertySource.class);
|
||||||
|
assertThat(this.applicationContext.getEnvironment().containsProperty("container.port")).isTrue();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void importWhenHasNonStaticDynamicPropertySourceMethod() {
|
||||||
|
assertThatIllegalStateException()
|
||||||
|
.isThrownBy(() -> this.applicationContext = new AnnotationConfigApplicationContext(
|
||||||
|
NonStaticDynamicPropertySourceMethod.class))
|
||||||
|
.withMessage("@DynamicPropertySource method 'containerProperties' must be static");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void importWhenHasBadArgsDynamicPropertySourceMethod() {
|
||||||
|
assertThatIllegalStateException()
|
||||||
|
.isThrownBy(() -> this.applicationContext = new AnnotationConfigApplicationContext(
|
||||||
|
BadArgsDynamicPropertySourceMethod.class))
|
||||||
|
.withMessage("@DynamicPropertySource method 'containerProperties' must be static");
|
||||||
|
}
|
||||||
|
|
||||||
|
@ImportTestcontainers
|
||||||
|
static class ImportWithoutValue {
|
||||||
|
|
||||||
|
@ContainerAnnotation
|
||||||
|
static PostgreSQLContainer<?> container = new PostgreSQLContainer<>(DockerImageNames.postgresql());
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@ImportTestcontainers(ContainerDefinitions.class)
|
||||||
|
static class ImportWithValue {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@ImportTestcontainers
|
||||||
|
static class NoContainers {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@ImportTestcontainers
|
||||||
|
static class NullContainer {
|
||||||
|
|
||||||
|
static PostgreSQLContainer<?> container = null;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@ImportTestcontainers
|
||||||
|
static class NonStaticContainer {
|
||||||
|
|
||||||
|
PostgreSQLContainer<?> container = new PostgreSQLContainer<>(DockerImageNames.postgresql());
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
interface ContainerDefinitions {
|
||||||
|
|
||||||
|
@ContainerAnnotation
|
||||||
|
PostgreSQLContainer<?> container = new PostgreSQLContainer<>(DockerImageNames.postgresql());
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
|
static @interface ContainerAnnotation {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@ImportTestcontainers
|
||||||
|
static class ContainerDefinitionsWithDynamicPropertySource {
|
||||||
|
|
||||||
|
static PostgreSQLContainer<?> container = new PostgreSQLContainer<>(DockerImageNames.postgresql());
|
||||||
|
|
||||||
|
@DynamicPropertySource
|
||||||
|
static void containerProperties(DynamicPropertyRegistry registry) {
|
||||||
|
registry.add("container.port", container::getFirstMappedPort);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@ImportTestcontainers
|
||||||
|
static class NonStaticDynamicPropertySourceMethod {
|
||||||
|
|
||||||
|
@DynamicPropertySource
|
||||||
|
void containerProperties(DynamicPropertyRegistry registry) {
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@ImportTestcontainers
|
||||||
|
static class BadArgsDynamicPropertySourceMethod {
|
||||||
|
|
||||||
|
@DynamicPropertySource
|
||||||
|
void containerProperties() {
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,44 @@
|
|||||||
|
/*
|
||||||
|
* 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 smoketest.session.redis;
|
||||||
|
|
||||||
|
import org.springframework.boot.SpringApplication;
|
||||||
|
import org.springframework.boot.testcontainers.context.ImportTestcontainers;
|
||||||
|
import org.springframework.boot.testsupport.testcontainers.RedisContainer;
|
||||||
|
import org.springframework.test.context.DynamicPropertyRegistry;
|
||||||
|
import org.springframework.test.context.DynamicPropertySource;
|
||||||
|
|
||||||
|
public class TestPropertiesImportSampleSessionRedisApplication {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
SpringApplication.from(SampleSessionRedisApplication::main).with(ContainerConfiguration.class).run(args);
|
||||||
|
}
|
||||||
|
|
||||||
|
@ImportTestcontainers
|
||||||
|
static class ContainerConfiguration {
|
||||||
|
|
||||||
|
static RedisContainer container = new RedisContainer();
|
||||||
|
|
||||||
|
@DynamicPropertySource
|
||||||
|
static void containerProperties(DynamicPropertyRegistry properties) {
|
||||||
|
properties.add("spring.data.redis.host", container::getHost);
|
||||||
|
properties.add("spring.data.redis.port", container::getFirstMappedPort);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,38 @@
|
|||||||
|
/*
|
||||||
|
* 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 smoketest.session.redis;
|
||||||
|
|
||||||
|
import org.springframework.boot.SpringApplication;
|
||||||
|
import org.springframework.boot.testcontainers.context.ImportTestcontainers;
|
||||||
|
import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
|
||||||
|
import org.springframework.boot.testsupport.testcontainers.RedisContainer;
|
||||||
|
|
||||||
|
public class TestServiceConnectionImportSampleSessionRedisApplication {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
SpringApplication.from(SampleSessionRedisApplication::main).with(ContainerConfiguration.class).run(args);
|
||||||
|
}
|
||||||
|
|
||||||
|
@ImportTestcontainers
|
||||||
|
static class ContainerConfiguration {
|
||||||
|
|
||||||
|
@ServiceConnection // We don't need a name here because we have the container
|
||||||
|
static RedisContainer redisContainer = new RedisContainer();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue