Polish "Support ServiceConnection beans in slice tests"

See gh-36037
This commit is contained in:
Andy Wilkinson
2023-08-01 17:47:39 +01:00
parent 4dfb60c9a3
commit 0d646d7c26
47 changed files with 153 additions and 518 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2022 the original author or authors.
* 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.
@@ -61,7 +61,9 @@ public @interface ImportAutoConfiguration {
/**
* The auto-configuration classes that should be imported. When empty, the classes are
* specified using a file in {@code META-INF/spring} where the file name is the
* fully-qualified name of the annotated class, suffixed with '.imports'.
* fully-qualified name of the annotated class, suffixed with {@code .imports}. An
* entry in the file may be prefixed with {@code optional:} to indicate that the
* imported class should be ignored if it is not on the classpath.
* @return the classes to import
*/
@AliasFor("value")

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2022 the original author or authors.
* 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.
@@ -25,6 +25,7 @@ import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import org.springframework.boot.context.annotation.DeterminableImports;
@@ -32,6 +33,7 @@ import org.springframework.boot.context.annotation.ImportCandidates;
import org.springframework.core.annotation.AnnotatedElementUtils;
import org.springframework.core.annotation.AnnotationAttributes;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.util.ClassUtils;
import org.springframework.util.LinkedMultiValueMap;
@@ -49,6 +51,8 @@ import org.springframework.util.ObjectUtils;
*/
class ImportAutoConfigurationImportSelector extends AutoConfigurationImportSelector implements DeterminableImports {
private static final String OPTIONAL_PREFIX = "optional:";
private static final Set<String> ANNOTATION_NAMES;
static {
@@ -92,13 +96,27 @@ class ImportAutoConfigurationImportSelector extends AutoConfigurationImportSelec
if (classes.length > 0) {
return Arrays.asList(classes);
}
return loadFactoryNames(source);
Collection<String> factoryNames = loadFactoryNames(source);
return factoryNames.stream().map((name) -> {
if (name.startsWith(OPTIONAL_PREFIX)) {
name = name.substring(OPTIONAL_PREFIX.length());
if (!present(name)) {
return null;
}
}
return name;
}).filter(Objects::nonNull).toList();
}
protected Collection<String> loadFactoryNames(Class<?> source) {
return ImportCandidates.load(source, getBeanClassLoader()).getCandidates();
}
private boolean present(String className) {
String resourcePath = ClassUtils.convertClassNameToResourcePath(className) + ".class";
return new ClassPathResource(resourcePath).exists();
}
@Override
protected Set<String> getExclusions(AnnotationMetadata metadata, AnnotationAttributes attributes) {
Set<String> exclusions = new LinkedHashSet<>();

View File

@@ -74,6 +74,25 @@ class ImportAutoConfigurationImportSelectorTests {
assertThat(imports).containsExactly(FreeMarkerAutoConfiguration.class.getName());
}
@Test
void importsAreSelectedFromImportsFile() throws Exception {
AnnotationMetadata annotationMetadata = getAnnotationMetadata(FromImportsFile.class);
String[] imports = this.importSelector.selectImports(annotationMetadata);
assertThat(imports).containsExactly(
"org.springframework.boot.autoconfigure.freemarker.FreeMarkerAutoConfiguration",
"org.springframework.boot.autoconfigure.missing.MissingAutoConfiguration");
}
@Test
void importsSelectedFromImportsFileIgnoreMissingOptionalClasses() throws Exception {
AnnotationMetadata annotationMetadata = getAnnotationMetadata(
FromImportsFileIgnoresMissingOptionalClasses.class);
String[] imports = this.importSelector.selectImports(annotationMetadata);
assertThat(imports).containsExactly(
"org.springframework.boot.autoconfigure.freemarker.FreeMarkerAutoConfiguration",
"org.springframework.boot.autoconfigure.thymeleaf.ThymeleafAutoConfiguration");
}
@Test
void propertyExclusionsAreApplied() throws IOException {
this.environment.setProperty("spring.autoconfigure.exclude", FreeMarkerAutoConfiguration.class.getName());
@@ -312,6 +331,18 @@ class ImportAutoConfigurationImportSelectorTests {
}
@Retention(RetentionPolicy.RUNTIME)
@ImportAutoConfiguration
@interface FromImportsFile {
}
@Retention(RetentionPolicy.RUNTIME)
@ImportAutoConfiguration
@interface FromImportsFileIgnoresMissingOptionalClasses {
}
static class TestImportAutoConfigurationImportSelector extends ImportAutoConfigurationImportSelector {
@Override

View File

@@ -0,0 +1,2 @@
org.springframework.boot.autoconfigure.freemarker.FreeMarkerAutoConfiguration
org.springframework.boot.autoconfigure.missing.MissingAutoConfiguration

View File

@@ -0,0 +1,3 @@
optional:org.springframework.boot.autoconfigure.freemarker.FreeMarkerAutoConfiguration
optional:org.springframework.boot.autoconfigure.missing.MissingAutoConfiguration
org.springframework.boot.autoconfigure.thymeleaf.ThymeleafAutoConfiguration

View File

@@ -5,3 +5,4 @@ org.springframework.boot.autoconfigure.data.cassandra.CassandraReactiveDataAutoC
org.springframework.boot.autoconfigure.data.cassandra.CassandraReactiveRepositoriesAutoConfiguration
org.springframework.boot.autoconfigure.data.cassandra.CassandraRepositoriesAutoConfiguration
org.springframework.boot.autoconfigure.ssl.SslAutoConfiguration
optional:org.springframework.boot.testcontainers.service.connection.ServiceConnectionAutoConfiguration

View File

@@ -6,3 +6,4 @@ org.springframework.boot.autoconfigure.data.couchbase.CouchbaseReactiveDataAutoC
org.springframework.boot.autoconfigure.data.couchbase.CouchbaseReactiveRepositoriesAutoConfiguration
org.springframework.boot.autoconfigure.data.couchbase.CouchbaseRepositoriesAutoConfiguration
org.springframework.boot.autoconfigure.ssl.SslAutoConfiguration
optional:org.springframework.boot.testcontainers.service.connection.ServiceConnectionAutoConfiguration

View File

@@ -8,3 +8,4 @@ org.springframework.boot.autoconfigure.elasticsearch.ReactiveElasticsearchClient
org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration
org.springframework.boot.autoconfigure.jsonb.JsonbAutoConfiguration
org.springframework.boot.autoconfigure.ssl.SslAutoConfiguration
optional:org.springframework.boot.testcontainers.service.connection.ServiceConnectionAutoConfiguration

View File

@@ -7,3 +7,4 @@ org.springframework.boot.autoconfigure.jdbc.JdbcTemplateAutoConfiguration
org.springframework.boot.autoconfigure.liquibase.LiquibaseAutoConfiguration
org.springframework.boot.autoconfigure.sql.init.SqlInitializationAutoConfiguration
org.springframework.boot.autoconfigure.transaction.TransactionAutoConfiguration
optional:org.springframework.boot.testcontainers.service.connection.ServiceConnectionAutoConfiguration

View File

@@ -7,3 +7,4 @@ org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration
org.springframework.boot.autoconfigure.mongo.MongoReactiveAutoConfiguration
org.springframework.boot.autoconfigure.transaction.TransactionAutoConfiguration
org.springframework.boot.autoconfigure.ssl.SslAutoConfiguration
optional:org.springframework.boot.testcontainers.service.connection.ServiceConnectionAutoConfiguration

View File

@@ -4,4 +4,5 @@ org.springframework.boot.autoconfigure.data.neo4j.Neo4jDataAutoConfiguration
org.springframework.boot.autoconfigure.data.neo4j.Neo4jReactiveDataAutoConfiguration
org.springframework.boot.autoconfigure.data.neo4j.Neo4jReactiveRepositoriesAutoConfiguration
org.springframework.boot.autoconfigure.data.neo4j.Neo4jRepositoriesAutoConfiguration
org.springframework.boot.autoconfigure.transaction.TransactionAutoConfiguration
org.springframework.boot.autoconfigure.transaction.TransactionAutoConfiguration
optional:org.springframework.boot.testcontainers.service.connection.ServiceConnectionAutoConfiguration

View File

@@ -6,4 +6,5 @@ org.springframework.boot.autoconfigure.liquibase.LiquibaseAutoConfiguration
org.springframework.boot.autoconfigure.r2dbc.R2dbcAutoConfiguration
org.springframework.boot.autoconfigure.r2dbc.R2dbcTransactionManagerAutoConfiguration
org.springframework.boot.autoconfigure.sql.init.SqlInitializationAutoConfiguration
org.springframework.boot.autoconfigure.transaction.TransactionAutoConfiguration
org.springframework.boot.autoconfigure.transaction.TransactionAutoConfiguration
optional:org.springframework.boot.testcontainers.service.connection.ServiceConnectionAutoConfiguration

View File

@@ -3,3 +3,4 @@ org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration
org.springframework.boot.autoconfigure.data.redis.RedisReactiveAutoConfiguration
org.springframework.boot.autoconfigure.data.redis.RedisRepositoriesAutoConfiguration
org.springframework.boot.autoconfigure.ssl.SslAutoConfiguration
optional:org.springframework.boot.testcontainers.service.connection.ServiceConnectionAutoConfiguration

View File

@@ -5,4 +5,5 @@ org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConf
org.springframework.boot.autoconfigure.jdbc.JdbcTemplateAutoConfiguration
org.springframework.boot.autoconfigure.liquibase.LiquibaseAutoConfiguration
org.springframework.boot.autoconfigure.sql.init.SqlInitializationAutoConfiguration
org.springframework.boot.autoconfigure.transaction.TransactionAutoConfiguration
org.springframework.boot.autoconfigure.transaction.TransactionAutoConfiguration
optional:org.springframework.boot.testcontainers.service.connection.ServiceConnectionAutoConfiguration

View File

@@ -1,3 +1,4 @@
# AutoConfigureTestDatabase auto-configuration imports
org.springframework.boot.test.autoconfigure.jdbc.TestDatabaseAutoConfiguration
org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration
org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration
optional:org.springframework.boot.testcontainers.service.connection.ServiceConnectionAutoConfiguration

View File

@@ -5,4 +5,5 @@ org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConf
org.springframework.boot.autoconfigure.jooq.JooqAutoConfiguration
org.springframework.boot.autoconfigure.liquibase.LiquibaseAutoConfiguration
org.springframework.boot.autoconfigure.sql.init.SqlInitializationAutoConfiguration
org.springframework.boot.autoconfigure.transaction.TransactionAutoConfiguration
org.springframework.boot.autoconfigure.transaction.TransactionAutoConfiguration
optional:org.springframework.boot.testcontainers.service.connection.ServiceConnectionAutoConfiguration

View File

@@ -7,4 +7,5 @@ org.springframework.boot.autoconfigure.jdbc.JdbcTemplateAutoConfiguration
org.springframework.boot.autoconfigure.liquibase.LiquibaseAutoConfiguration
org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration
org.springframework.boot.autoconfigure.sql.init.SqlInitializationAutoConfiguration
org.springframework.boot.autoconfigure.transaction.TransactionAutoConfiguration
org.springframework.boot.autoconfigure.transaction.TransactionAutoConfiguration
optional:org.springframework.boot.testcontainers.service.connection.ServiceConnectionAutoConfiguration

View File

@@ -41,7 +41,8 @@ public final class AutoConfigurationImportedCondition extends Condition<Applicat
public boolean matches(ApplicationContext context) {
ConditionEvaluationReport report = ConditionEvaluationReport
.get((ConfigurableListableBeanFactory) context.getAutowireCapableBeanFactory());
return report.getConditionAndOutcomesBySource().containsKey(this.autoConfigurationClass.getName());
return report.getConditionAndOutcomesBySource().containsKey(this.autoConfigurationClass.getName())
|| report.getUnconditionalClasses().contains(this.autoConfigurationClass.getName());
}
/**

View File

@@ -29,6 +29,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.data.redis.ExampleService;
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
import org.springframework.boot.testcontainers.service.connection.ServiceConnectionAutoConfiguration;
import org.springframework.boot.testsupport.testcontainers.CassandraContainer;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
@@ -36,6 +37,7 @@ import org.springframework.data.cassandra.core.CassandraTemplate;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.springframework.boot.test.autoconfigure.AutoConfigurationImportedCondition.importedAutoConfiguration;
/**
* Integration test for {@link DataCassandraTest @DataCassandraTest}.
@@ -84,6 +86,11 @@ class DataCassandraTestIntegrationTests {
this.exampleRepository.deleteAll();
}
@Test
void serviceConnectionAutoConfigurationWasImported() {
assertThat(this.applicationContext).has(importedAutoConfiguration(ServiceConnectionAutoConfiguration.class));
}
@TestConfiguration(proxyBeanMethods = false)
static class KeyspaceTestConfiguration {

View File

@@ -1,90 +0,0 @@
/*
* 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.data.cassandra;
import java.util.UUID;
import com.datastax.oss.driver.api.core.CqlSession;
import com.datastax.oss.driver.api.core.CqlSessionBuilder;
import org.junit.jupiter.api.Test;
import org.testcontainers.junit.jupiter.Testcontainers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
import org.springframework.boot.testsupport.testcontainers.CassandraContainer;
import org.springframework.context.annotation.Bean;
import org.springframework.data.cassandra.core.CassandraTemplate;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Integration test for {@link DataCassandraTest @DataCassandraTest}.
*
* @author Artsiom Yudovin
* @author Moritz Halbritter
* @author Andy Wilkinson
* @author Phillip Webb
* @author Eddú Meléndez
*/
@DataCassandraTest(properties = { "spring.cassandra.schema-action=create-if-not-exists",
"spring.cassandra.connection.connect-timeout=60s", "spring.cassandra.connection.init-query-timeout=60s",
"spring.cassandra.request.timeout=60s" })
@Testcontainers(disabledWithoutDocker = true)
class DataCassandraTestWithServiceConnectionBeanIntegrationTests {
@Autowired
private CassandraTemplate cassandraTemplate;
@Autowired
private ExampleRepository exampleRepository;
@Test
void testRepository() {
ExampleEntity entity = new ExampleEntity();
entity.setDescription("Look, new @DataCassandraTest!");
String id = UUID.randomUUID().toString();
entity.setId(id);
ExampleEntity savedEntity = this.exampleRepository.save(entity);
ExampleEntity getEntity = this.cassandraTemplate.selectOneById(id, ExampleEntity.class);
assertThat(getEntity).isNotNull();
assertThat(getEntity.getId()).isNotNull();
assertThat(getEntity.getId()).isEqualTo(savedEntity.getId());
this.exampleRepository.deleteAll();
}
@TestConfiguration(proxyBeanMethods = false)
static class KeyspaceTestConfiguration {
@Bean
@ServiceConnection
CassandraContainer cassandra() {
return new CassandraContainer();
}
@Bean
CqlSession cqlSession(CqlSessionBuilder cqlSessionBuilder) {
try (CqlSession session = cqlSessionBuilder.build()) {
session.execute("CREATE KEYSPACE IF NOT EXISTS boot_test"
+ " WITH REPLICATION = { 'class' : 'SimpleStrategy', 'replication_factor' : 1 };");
}
return cqlSessionBuilder.withKeyspace("boot_test").build();
}
}
}

View File

@@ -27,12 +27,14 @@ import org.testcontainers.junit.jupiter.Testcontainers;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
import org.springframework.boot.testcontainers.service.connection.ServiceConnectionAutoConfiguration;
import org.springframework.boot.testsupport.testcontainers.DockerImageNames;
import org.springframework.context.ApplicationContext;
import org.springframework.data.couchbase.core.CouchbaseTemplate;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.springframework.boot.test.autoconfigure.AutoConfigurationImportedCondition.importedAutoConfiguration;
/**
* Integration test for {@link DataCouchbaseTest @DataCouchbaseTest}.
@@ -81,4 +83,9 @@ class DataCouchbaseTestIntegrationTests {
this.exampleRepository.deleteAll();
}
@Test
void serviceConnectionAutoConfigurationWasImported() {
assertThat(this.applicationContext).has(importedAutoConfiguration(ServiceConnectionAutoConfiguration.class));
}
}

View File

@@ -1,79 +0,0 @@
/*
* 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.data.couchbase;
import java.time.Duration;
import org.junit.jupiter.api.Test;
import org.testcontainers.couchbase.BucketDefinition;
import org.testcontainers.couchbase.CouchbaseContainer;
import org.testcontainers.junit.jupiter.Testcontainers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
import org.springframework.boot.testsupport.testcontainers.DockerImageNames;
import org.springframework.context.annotation.Bean;
import org.springframework.data.couchbase.core.CouchbaseTemplate;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Integration test for {@link DataCouchbaseTest @DataCouchbaseTest}.
*
* @author Eddú Meléndez
* @author Moritz Halbritter
* @author Andy Wilkinson
* @author Phillip Webb
*/
@DataCouchbaseTest(
properties = { "spring.couchbase.env.timeouts.connect=2m", "spring.data.couchbase.bucket-name=cbbucket" })
@Testcontainers(disabledWithoutDocker = true)
class DataCouchbaseTestWithServiceConnectionBeanIntegrationTests {
private static final String BUCKET_NAME = "cbbucket";
@Autowired
private CouchbaseTemplate couchbaseTemplate;
@Autowired
private ExampleRepository exampleRepository;
@Test
void testRepository() {
ExampleDocument document = new ExampleDocument();
document.setText("Look, new @DataCouchbaseTest!");
document = this.exampleRepository.save(document);
assertThat(document.getId()).isNotNull();
assertThat(this.couchbaseTemplate.getBucketName()).isEqualTo(BUCKET_NAME);
this.exampleRepository.deleteAll();
}
@TestConfiguration(proxyBeanMethods = false)
static class ContainerConfig {
@Bean
@ServiceConnection
CouchbaseContainer couchbase() {
return new CouchbaseContainer(DockerImageNames.couchbase()).withStartupAttempts(5)
.withStartupTimeout(Duration.ofMinutes(10))
.withBucket(new BucketDefinition(BUCKET_NAME));
}
}
}

View File

@@ -27,12 +27,14 @@ import org.testcontainers.junit.jupiter.Testcontainers;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
import org.springframework.boot.testcontainers.service.connection.ServiceConnectionAutoConfiguration;
import org.springframework.boot.testsupport.testcontainers.DockerImageNames;
import org.springframework.context.ApplicationContext;
import org.springframework.data.elasticsearch.client.elc.ElasticsearchTemplate;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.springframework.boot.test.autoconfigure.AutoConfigurationImportedCondition.importedAutoConfiguration;
/**
* Sample test for {@link DataElasticsearchTest @DataElasticsearchTest}
@@ -82,4 +84,9 @@ class DataElasticsearchTestIntegrationTests {
this.exampleRepository.deleteAll();
}
@Test
void serviceConnectionAutoConfigurationWasImported() {
assertThat(this.applicationContext).has(importedAutoConfiguration(ServiceConnectionAutoConfiguration.class));
}
}

View File

@@ -1,81 +0,0 @@
/*
* 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.data.elasticsearch;
import java.time.Duration;
import java.util.UUID;
import org.junit.jupiter.api.Test;
import org.testcontainers.elasticsearch.ElasticsearchContainer;
import org.testcontainers.junit.jupiter.Testcontainers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
import org.springframework.boot.testsupport.testcontainers.DockerImageNames;
import org.springframework.context.annotation.Bean;
import org.springframework.data.elasticsearch.client.elc.ElasticsearchTemplate;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Sample test for {@link DataElasticsearchTest @DataElasticsearchTest}
*
* @author Eddú Meléndez
* @author Moritz Halbritter
* @author Andy Wilkinson
* @author Phillip Webb
*/
@DataElasticsearchTest
@Testcontainers(disabledWithoutDocker = true)
class DataElasticsearchTestWithServiceConnectionBeanIntegrationTests {
@Autowired
private ElasticsearchTemplate elasticsearchTemplate;
@Autowired
private ExampleRepository exampleRepository;
@Test
void testRepository() {
ExampleDocument document = new ExampleDocument();
document.setText("Look, new @DataElasticsearchTest!");
String id = UUID.randomUUID().toString();
document.setId(id);
ExampleDocument savedDocument = this.exampleRepository.save(document);
ExampleDocument getDocument = this.elasticsearchTemplate.get(id, ExampleDocument.class);
assertThat(getDocument).isNotNull();
assertThat(getDocument.getId()).isNotNull();
assertThat(getDocument.getId()).isEqualTo(savedDocument.getId());
this.exampleRepository.deleteAll();
}
@TestConfiguration(proxyBeanMethods = false)
static class ContainerConfig {
@Bean
@ServiceConnection
ElasticsearchContainer elasticsearch() {
return new ElasticsearchContainer(DockerImageNames.elasticsearch())
.withEnv("ES_JAVA_OPTS", "-Xms32m -Xmx512m")
.withStartupAttempts(5)
.withStartupTimeout(Duration.ofMinutes(10));
}
}
}

View File

@@ -24,6 +24,7 @@ import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.flyway.FlywayAutoConfiguration;
import org.springframework.boot.autoconfigure.liquibase.LiquibaseAutoConfiguration;
import org.springframework.boot.testcontainers.service.connection.ServiceConnectionAutoConfiguration;
import org.springframework.context.ApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.test.context.TestPropertySource;
@@ -83,4 +84,9 @@ class DataJdbcTestIntegrationTests {
assertThat(this.applicationContext).has(importedAutoConfiguration(LiquibaseAutoConfiguration.class));
}
@Test
void serviceConnectionAutoConfigurationWasImported() {
assertThat(this.applicationContext).has(importedAutoConfiguration(ServiceConnectionAutoConfiguration.class));
}
}

View File

@@ -26,12 +26,14 @@ import org.testcontainers.junit.jupiter.Testcontainers;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
import org.springframework.boot.testcontainers.service.connection.ServiceConnectionAutoConfiguration;
import org.springframework.boot.testsupport.testcontainers.DockerImageNames;
import org.springframework.context.ApplicationContext;
import org.springframework.data.mongodb.core.MongoTemplate;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.springframework.boot.test.autoconfigure.AutoConfigurationImportedCondition.importedAutoConfiguration;
/**
* Sample test for {@link DataMongoTest @DataMongoTest}
@@ -74,4 +76,9 @@ class DataMongoTestIntegrationTests {
.isThrownBy(() -> this.applicationContext.getBean(ExampleService.class));
}
@Test
void serviceConnectionAutoConfigurationWasImported() {
assertThat(this.applicationContext).has(importedAutoConfiguration(ServiceConnectionAutoConfiguration.class));
}
}

View File

@@ -1,74 +0,0 @@
/*
* 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.data.mongo;
import java.time.Duration;
import org.junit.jupiter.api.Test;
import org.testcontainers.containers.MongoDBContainer;
import org.testcontainers.junit.jupiter.Testcontainers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
import org.springframework.boot.testsupport.testcontainers.DockerImageNames;
import org.springframework.context.annotation.Bean;
import org.springframework.data.mongodb.core.MongoTemplate;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Sample test for {@link DataMongoTest @DataMongoTest}
*
* @author Michael Simons
* @author Moritz Halbritter
* @author Andy Wilkinson
* @author Phillip Webb
* @author Eddú Meléndez
*/
@DataMongoTest
@Testcontainers(disabledWithoutDocker = true)
class DataMongoTestWithServiceConnectionBeanIntegrationTests {
@Autowired
private MongoTemplate mongoTemplate;
@Autowired
private ExampleRepository exampleRepository;
@Test
void testRepository() {
ExampleDocument exampleDocument = new ExampleDocument();
exampleDocument.setText("Look, new @DataMongoTest!");
exampleDocument = this.exampleRepository.save(exampleDocument);
assertThat(exampleDocument.getId()).isNotNull();
assertThat(this.mongoTemplate.collectionExists("exampleDocuments")).isTrue();
}
@TestConfiguration(proxyBeanMethods = false)
static class ContainerConfig {
@Bean
@ServiceConnection
MongoDBContainer mongoDB() {
return new MongoDBContainer(DockerImageNames.mongo()).withStartupAttempts(5)
.withStartupTimeout(Duration.ofMinutes(5));
}
}
}

View File

@@ -26,12 +26,14 @@ import org.testcontainers.junit.jupiter.Testcontainers;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
import org.springframework.boot.testcontainers.service.connection.ServiceConnectionAutoConfiguration;
import org.springframework.boot.testsupport.testcontainers.DockerImageNames;
import org.springframework.context.ApplicationContext;
import org.springframework.data.neo4j.core.Neo4jTemplate;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.springframework.boot.test.autoconfigure.AutoConfigurationImportedCondition.importedAutoConfiguration;
/**
* Integration test for {@link DataNeo4jTest @DataNeo4jTest}.
@@ -76,4 +78,9 @@ class DataNeo4jTestIntegrationTests {
.isThrownBy(() -> this.applicationContext.getBean(ExampleService.class));
}
@Test
void serviceConnectionAutoConfigurationWasImported() {
assertThat(this.applicationContext).has(importedAutoConfiguration(ServiceConnectionAutoConfiguration.class));
}
}

View File

@@ -1,79 +0,0 @@
/*
* 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.data.neo4j;
import java.time.Duration;
import org.junit.jupiter.api.Test;
import org.testcontainers.containers.Neo4jContainer;
import org.testcontainers.junit.jupiter.Testcontainers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
import org.springframework.boot.testsupport.testcontainers.DockerImageNames;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.data.neo4j.core.Neo4jTemplate;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Integration test for {@link DataNeo4jTest @DataNeo4jTest}.
*
* @author Eddú Meléndez
* @author Stephane Nicoll
* @author Michael Simons
* @author Moritz Halbritter
* @author Andy Wilkinson
* @author Phillip Webb
*/
@DataNeo4jTest
@Testcontainers(disabledWithoutDocker = true)
class DataNeo4jTestWithServiceConnectionBeanIntegrationTests {
@Autowired
private Neo4jTemplate neo4jTemplate;
@Autowired
private ExampleRepository exampleRepository;
@Autowired
private ApplicationContext applicationContext;
@Test
void testRepository() {
ExampleGraph exampleGraph = new ExampleGraph("Look, new @DataNeo4jTest!");
assertThat(exampleGraph.getId()).isNull();
ExampleGraph savedGraph = this.exampleRepository.save(exampleGraph);
assertThat(savedGraph.getId()).isNotNull();
assertThat(this.neo4jTemplate.count(ExampleGraph.class)).isOne();
}
@TestConfiguration
static class ContainerConfig {
@Bean
@ServiceConnection
Neo4jContainer<?> neo4j() {
return new Neo4jContainer<>(DockerImageNames.neo4j()).withStartupAttempts(5)
.withStartupTimeout(Duration.ofMinutes(10));
}
}
}

View File

@@ -25,10 +25,12 @@ import reactor.core.publisher.Flux;
import reactor.test.StepVerifier;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.testcontainers.service.connection.ServiceConnectionAutoConfiguration;
import org.springframework.context.ApplicationContext;
import org.springframework.r2dbc.core.DatabaseClient;
import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.boot.test.autoconfigure.AutoConfigurationImportedCondition.importedAutoConfiguration;
/**
* Integration tests for {@link DataR2dbcTest}.
@@ -65,4 +67,9 @@ class DataR2dbcTestIntegrationTests {
assertThat(this.applicationContext.getBeanNamesForType(ExampleRepository.class)).isNotEmpty();
}
@Test
void serviceConnectionAutoConfigurationWasImported() {
assertThat(this.applicationContext).has(importedAutoConfiguration(ServiceConnectionAutoConfiguration.class));
}
}

View File

@@ -26,12 +26,14 @@ import org.testcontainers.junit.jupiter.Testcontainers;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.beans.factory.annotation.Autowired;
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.ApplicationContext;
import org.springframework.data.redis.core.RedisOperations;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.springframework.boot.test.autoconfigure.AutoConfigurationImportedCondition.importedAutoConfiguration;
/**
* Integration test for {@link DataRedisTest @DataRedisTest}.
@@ -80,4 +82,9 @@ class DataRedisTestIntegrationTests {
.isThrownBy(() -> this.applicationContext.getBean(ExampleService.class));
}
@Test
void serviceConnectionAutoConfigurationWasImported() {
assertThat(this.applicationContext).has(importedAutoConfiguration(ServiceConnectionAutoConfiguration.class));
}
}

View File

@@ -1,80 +0,0 @@
/*
* 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.data.redis;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import org.junit.jupiter.api.Test;
import org.testcontainers.junit.jupiter.Testcontainers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
import org.springframework.boot.testsupport.testcontainers.RedisContainer;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.core.RedisOperations;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Integration test for {@link DataRedisTest @DataRedisTest}.
*
* @author Jayaram Pradhan
* @author Moritz Halbritter
* @author Andy Wilkinson
* @author Phillip Webb
* @author Eddú Meléndez
*/
@Testcontainers(disabledWithoutDocker = true)
@DataRedisTest
class DataRedisTestWithServiceConnectionBeanIntegrationTests {
private static final Charset CHARSET = StandardCharsets.UTF_8;
@Autowired
private RedisOperations<Object, Object> operations;
@Autowired
private ExampleRepository exampleRepository;
@Test
void testRepository() {
PersonHash personHash = new PersonHash();
personHash.setDescription("Look, new @DataRedisTest!");
assertThat(personHash.getId()).isNull();
PersonHash savedEntity = this.exampleRepository.save(personHash);
assertThat(savedEntity.getId()).isNotNull();
assertThat(this.operations
.execute((org.springframework.data.redis.connection.RedisConnection connection) -> connection.keyCommands()
.exists(("persons:" + savedEntity.getId()).getBytes(CHARSET))))
.isTrue();
this.exampleRepository.deleteAll();
}
@TestConfiguration(proxyBeanMethods = false)
static class ContainerConfig {
@Bean
@ServiceConnection(name = "redis")
RedisContainer redis() {
return new RedisContainer();
}
}
}

View File

@@ -26,6 +26,7 @@ import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.flyway.FlywayAutoConfiguration;
import org.springframework.boot.autoconfigure.liquibase.LiquibaseAutoConfiguration;
import org.springframework.boot.testcontainers.service.connection.ServiceConnectionAutoConfiguration;
import org.springframework.context.ApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.test.context.TestPropertySource;
@@ -86,4 +87,9 @@ class JdbcTestIntegrationTests {
assertThat(this.applicationContext).has(importedAutoConfiguration(LiquibaseAutoConfiguration.class));
}
@Test
void serviceConnectionAutoConfigurationWasImported() {
assertThat(this.applicationContext).has(importedAutoConfiguration(ServiceConnectionAutoConfiguration.class));
}
}

View File

@@ -28,6 +28,7 @@ import org.springframework.boot.autoconfigure.cache.CacheAutoConfiguration;
import org.springframework.boot.autoconfigure.flyway.FlywayAutoConfiguration;
import org.springframework.boot.autoconfigure.liquibase.LiquibaseAutoConfiguration;
import org.springframework.boot.test.autoconfigure.orm.jpa.ExampleComponent;
import org.springframework.boot.testcontainers.service.connection.ServiceConnectionAutoConfiguration;
import org.springframework.context.ApplicationContext;
import static org.assertj.core.api.Assertions.assertThat;
@@ -85,4 +86,9 @@ class JooqTestIntegrationTests {
assertThat(this.applicationContext).has(importedAutoConfiguration(CacheAutoConfiguration.class));
}
@Test
void serviceConnectionAutoConfigurationWasImported() {
assertThat(this.applicationContext).has(importedAutoConfiguration(ServiceConnectionAutoConfiguration.class));
}
}

View File

@@ -24,6 +24,7 @@ import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.flyway.FlywayAutoConfiguration;
import org.springframework.boot.autoconfigure.liquibase.LiquibaseAutoConfiguration;
import org.springframework.boot.testcontainers.service.connection.ServiceConnectionAutoConfiguration;
import org.springframework.context.ApplicationContext;
import org.springframework.data.repository.config.BootstrapMode;
import org.springframework.jdbc.core.JdbcTemplate;
@@ -107,6 +108,11 @@ class DataJpaTestIntegrationTests {
assertThat(this.applicationContext).has(importedAutoConfiguration(LiquibaseAutoConfiguration.class));
}
@Test
void serviceConnectionAutoConfigurationWasImported() {
assertThat(this.applicationContext).has(importedAutoConfiguration(ServiceConnectionAutoConfiguration.class));
}
@Test
void bootstrapModeIsDefaultByDefault() {
assertThat(this.applicationContext.getEnvironment().getProperty("spring.data.jpa.repositories.bootstrap-mode"))

View File

@@ -1,2 +0,0 @@
# AutoConfigureDataCassandra auto-configuration imports
org.springframework.boot.testcontainers.service.connection.ServiceConnectionAutoConfiguration

View File

@@ -1,2 +0,0 @@
# AutoConfigureDataCouchbase auto-configuration imports
org.springframework.boot.testcontainers.service.connection.ServiceConnectionAutoConfiguration

View File

@@ -1,2 +0,0 @@
# AutoConfigureDataElasticsearch auto-configuration imports
org.springframework.boot.testcontainers.service.connection.ServiceConnectionAutoConfiguration

View File

@@ -1,2 +0,0 @@
# AutoConfigureDataJdbc auto-configuration imports
org.springframework.boot.testcontainers.service.connection.ServiceConnectionAutoConfiguration

View File

@@ -1,2 +0,0 @@
# AutoConfigureDataMongo auto-configuration imports
org.springframework.boot.testcontainers.service.connection.ServiceConnectionAutoConfiguration

View File

@@ -1,2 +0,0 @@
# AutoConfigureDataNeo4j auto-configuration imports
org.springframework.boot.testcontainers.service.connection.ServiceConnectionAutoConfiguration

View File

@@ -1,2 +0,0 @@
# AutoConfigureDataR2dbc auto-configuration imports
org.springframework.boot.testcontainers.service.connection.ServiceConnectionAutoConfiguration

View File

@@ -1,2 +0,0 @@
# AutoConfigureDataRedis auto-configuration imports
org.springframework.boot.testcontainers.service.connection.ServiceConnectionAutoConfiguration

View File

@@ -1,2 +0,0 @@
# AutoConfigureJdbc auto-configuration imports
org.springframework.boot.testcontainers.service.connection.ServiceConnectionAutoConfiguration

View File

@@ -1,2 +0,0 @@
# AutoConfigureTestDatabase auto-configuration imports
org.springframework.boot.testcontainers.service.connection.ServiceConnectionAutoConfiguration

View File

@@ -1,2 +0,0 @@
# AutoConfigureJooq auto-configuration imports
org.springframework.boot.testcontainers.service.connection.ServiceConnectionAutoConfiguration

View File

@@ -1,2 +0,0 @@
# AutoConfigureDataJpa auto-configuration imports
org.springframework.boot.testcontainers.service.connection.ServiceConnectionAutoConfiguration