Skip to content

Commit

Permalink
Merge branch '3.1.x'
Browse files Browse the repository at this point in the history
Closes gh-36661
  • Loading branch information
wilkinsona committed Aug 1, 2023
2 parents d0d5454 + 0ad6878 commit 806aa45
Show file tree
Hide file tree
Showing 29 changed files with 153 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -61,7 +61,9 @@
/**
* 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")
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -25,13 +25,15 @@
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;
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;
Expand All @@ -49,6 +51,8 @@
*/
class ImportAutoConfigurationImportSelector extends AutoConfigurationImportSelector implements DeterminableImports {

private static final String OPTIONAL_PREFIX = "optional:";

private static final Set<String> ANNOTATION_NAMES;

static {
Expand Down Expand Up @@ -92,13 +96,27 @@ private Collection<String> getConfigurationsForAnnotation(Class<?> source, Annot
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<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,25 @@ void importsAreSelectedUsingClassesAttribute() throws Exception {
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());
Expand Down Expand Up @@ -312,6 +331,18 @@ Class<?>[] excludeAutoConfiguration() default {

}

@Retention(RetentionPolicy.RUNTIME)
@ImportAutoConfiguration
@interface FromImportsFile {

}

@Retention(RetentionPolicy.RUNTIME)
@ImportAutoConfiguration
@interface FromImportsFileIgnoresMissingOptionalClasses {

}

static class TestImportAutoConfigurationImportSelector extends ImportAutoConfigurationImportSelector {

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
org.springframework.boot.autoconfigure.freemarker.FreeMarkerAutoConfiguration
org.springframework.boot.autoconfigure.missing.MissingAutoConfiguration
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
optional:org.springframework.boot.autoconfigure.freemarker.FreeMarkerAutoConfiguration
optional:org.springframework.boot.autoconfigure.missing.MissingAutoConfiguration
org.springframework.boot.autoconfigure.thymeleaf.ThymeleafAutoConfiguration
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ private AutoConfigurationImportedCondition(Class<?> autoConfigurationClass) {
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());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,15 @@
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;
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}.
Expand Down Expand Up @@ -84,6 +86,11 @@ void testRepository() {
this.exampleRepository.deleteAll();
}

@Test
void serviceConnectionAutoConfigurationWasImported() {
assertThat(this.applicationContext).has(importedAutoConfiguration(ServiceConnectionAutoConfiguration.class));
}

@TestConfiguration(proxyBeanMethods = false)
static class KeyspaceTestConfiguration {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,14 @@
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}.
Expand Down Expand Up @@ -81,4 +83,9 @@ void testRepository() {
this.exampleRepository.deleteAll();
}

@Test
void serviceConnectionAutoConfigurationWasImported() {
assertThat(this.applicationContext).has(importedAutoConfiguration(ServiceConnectionAutoConfiguration.class));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,14 @@
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}
Expand Down Expand Up @@ -82,4 +84,9 @@ void testRepository() {
this.exampleRepository.deleteAll();
}

@Test
void serviceConnectionAutoConfigurationWasImported() {
assertThat(this.applicationContext).has(importedAutoConfiguration(ServiceConnectionAutoConfiguration.class));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
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;
Expand Down Expand Up @@ -83,4 +84,9 @@ void liquibaseAutoConfigurationWasImported() {
assertThat(this.applicationContext).has(importedAutoConfiguration(LiquibaseAutoConfiguration.class));
}

@Test
void serviceConnectionAutoConfigurationWasImported() {
assertThat(this.applicationContext).has(importedAutoConfiguration(ServiceConnectionAutoConfiguration.class));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,14 @@
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}
Expand Down Expand Up @@ -74,4 +76,9 @@ void didNotInjectExampleService() {
.isThrownBy(() -> this.applicationContext.getBean(ExampleService.class));
}

@Test
void serviceConnectionAutoConfigurationWasImported() {
assertThat(this.applicationContext).has(importedAutoConfiguration(ServiceConnectionAutoConfiguration.class));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,14 @@
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}.
Expand Down Expand Up @@ -76,4 +78,9 @@ void didNotInjectExampleService() {
.isThrownBy(() -> this.applicationContext.getBean(ExampleService.class));
}

@Test
void serviceConnectionAutoConfigurationWasImported() {
assertThat(this.applicationContext).has(importedAutoConfiguration(ServiceConnectionAutoConfiguration.class));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,12 @@
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}.
Expand Down Expand Up @@ -65,4 +67,9 @@ void registersExampleRepository() {
assertThat(this.applicationContext.getBeanNamesForType(ExampleRepository.class)).isNotEmpty();
}

@Test
void serviceConnectionAutoConfigurationWasImported() {
assertThat(this.applicationContext).has(importedAutoConfiguration(ServiceConnectionAutoConfiguration.class));
}

}
Loading

0 comments on commit 806aa45

Please sign in to comment.