diff --git a/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc b/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc index 5b8d7fac96f..43267fbfce7 100644 --- a/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc +++ b/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc @@ -1155,4 +1155,11 @@ content into your application; rather pick only the properties that you need. spring.devtools.remote.secret= # A shared secret required to establish a connection (required to enable remote support). spring.devtools.remote.secret-header-name=X-AUTH-TOKEN # HTTP header used to transfer the shared secret. + + # ---------------------------------------- + # TESTING PROPERTIES + # ---------------------------------------- + + spring.test.database.replace=any # Type of existing DataSource to replace. + ---- diff --git a/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/jdbc/TestDatabaseAutoConfiguration.java b/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/jdbc/TestDatabaseAutoConfiguration.java index 533ef868833..72f7dbc8eb7 100644 --- a/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/jdbc/TestDatabaseAutoConfiguration.java +++ b/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/jdbc/TestDatabaseAutoConfiguration.java @@ -31,22 +31,16 @@ import org.springframework.beans.factory.support.BeanDefinitionRegistry; import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor; import org.springframework.beans.factory.support.RootBeanDefinition; import org.springframework.boot.autoconfigure.AutoConfigureBefore; -import org.springframework.boot.autoconfigure.condition.ConditionMessage; -import org.springframework.boot.autoconfigure.condition.ConditionOutcome; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; -import org.springframework.boot.autoconfigure.condition.SpringBootCondition; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; import org.springframework.boot.autoconfigure.jdbc.EmbeddedDatabaseConnection; -import org.springframework.boot.bind.RelaxedPropertyResolver; import org.springframework.context.EnvironmentAware; import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.ConditionContext; -import org.springframework.context.annotation.Conditional; import org.springframework.context.annotation.Configuration; import org.springframework.core.Ordered; import org.springframework.core.annotation.Order; import org.springframework.core.env.Environment; -import org.springframework.core.type.AnnotatedTypeMetadata; import org.springframework.jdbc.datasource.embedded.EmbeddedDatabase; import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder; import org.springframework.util.Assert; @@ -56,7 +50,6 @@ import org.springframework.util.ObjectUtils; * Auto-configuration for a test database. * * @author Phillip Webb - * @author EddĂș MelĂ©ndez * @since 1.4.0 * @see AutoConfigureTestDatabase */ @@ -64,9 +57,6 @@ import org.springframework.util.ObjectUtils; @AutoConfigureBefore(DataSourceAutoConfiguration.class) public class TestDatabaseAutoConfiguration { - private static final String SPRING_TEST_DATABASE_PREFIX = "spring.test.database."; - private static final String REPLACE_PROPERTY = "replace"; - private final Environment environment; TestDatabaseAutoConfiguration(Environment environment) { @@ -74,55 +64,18 @@ public class TestDatabaseAutoConfiguration { } @Bean - @Conditional(TestDatabaseReplaceAutoConfiguredCondition.class) + @ConditionalOnProperty(prefix = "spring.test.database", name = "replace", havingValue = "AUTO_CONFIGURED") @ConditionalOnMissingBean public DataSource dataSource() { return new EmbeddedDataSourceFactory(this.environment).getEmbeddedDatabase(); } @Bean - @Conditional(TestDatabaseReplaceAnyCondition.class) + @ConditionalOnProperty(prefix = "spring.test.database", name = "replace", havingValue = "ANY", matchIfMissing = true) public static EmbeddedDataSourceBeanFactoryPostProcessor embeddedDataSourceBeanFactoryPostProcessor() { return new EmbeddedDataSourceBeanFactoryPostProcessor(); } - static class TestDatabaseReplaceAutoConfiguredCondition extends SpringBootCondition { - - @Override - public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata - metadata) { - RelaxedPropertyResolver resolver = new RelaxedPropertyResolver(context.getEnvironment(), SPRING_TEST_DATABASE_PREFIX); - ConditionMessage.Builder message = ConditionMessage - .forCondition("Test Database Replace Property"); - if (resolver.containsProperty(REPLACE_PROPERTY) && "NONE".equals(resolver.getProperty(REPLACE_PROPERTY))) { - return ConditionOutcome.noMatch(message.didNotFind("NONE").atAll()); - } - else if (resolver.containsProperty(REPLACE_PROPERTY) && "AUTO_CONFIGURED".equals(resolver.getProperty(REPLACE_PROPERTY))) { - return ConditionOutcome.match(message.found("AUTO_CONFIGURED").atAll()); - } - return ConditionOutcome.noMatch(message.didNotFind("spring.test.database.replace").atAll()); - } - - } - - static class TestDatabaseReplaceAnyCondition extends SpringBootCondition { - - @Override - public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) { - RelaxedPropertyResolver resolver = new RelaxedPropertyResolver(context.getEnvironment(), SPRING_TEST_DATABASE_PREFIX); - ConditionMessage.Builder message = ConditionMessage - .forCondition("Test Database Replace Property"); - if (resolver.containsProperty(REPLACE_PROPERTY) && "NONE".equals(resolver.getProperty(REPLACE_PROPERTY))) { - return ConditionOutcome.noMatch(message.didNotFind("NONE").atAll()); - } - else if (!resolver.containsProperty(REPLACE_PROPERTY) || "ANY".equals(resolver.getProperty(REPLACE_PROPERTY))) { - return ConditionOutcome.match(message.found("ANY").atAll()); - } - return ConditionOutcome.noMatch(message.didNotFind("spring.test.database.replace").atAll()); - } - - } - @Order(Ordered.LOWEST_PRECEDENCE) private static class EmbeddedDataSourceBeanFactoryPostProcessor implements BeanDefinitionRegistryPostProcessor { diff --git a/spring-boot-test-autoconfigure/src/main/resources/META-INF/additional-spring-configuration-metadata.json b/spring-boot-test-autoconfigure/src/main/resources/META-INF/additional-spring-configuration-metadata.json new file mode 100644 index 00000000000..309ca1ae3ff --- /dev/null +++ b/spring-boot-test-autoconfigure/src/main/resources/META-INF/additional-spring-configuration-metadata.json @@ -0,0 +1,10 @@ +{ + "properties": [ + { + "name": "spring.test.database.replace", + "type": "org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase$Replace", + "description": "Type of existing DataSource to replace.", + "defaultValue": "any" + } + ] +} \ No newline at end of file