Commit 21b815aa authored by Stephane Nicoll's avatar Stephane Nicoll

Polish contribution

Closes gh-7560
parent 5a7624df
...@@ -1155,4 +1155,11 @@ content into your application; rather pick only the properties that you need. ...@@ -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= # 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. 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.
---- ----
...@@ -31,22 +31,16 @@ import org.springframework.beans.factory.support.BeanDefinitionRegistry; ...@@ -31,22 +31,16 @@ import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor; import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;
import org.springframework.beans.factory.support.RootBeanDefinition; import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.boot.autoconfigure.AutoConfigureBefore; 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.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.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.jdbc.EmbeddedDatabaseConnection; import org.springframework.boot.autoconfigure.jdbc.EmbeddedDatabaseConnection;
import org.springframework.boot.bind.RelaxedPropertyResolver;
import org.springframework.context.EnvironmentAware; import org.springframework.context.EnvironmentAware;
import org.springframework.context.annotation.Bean; 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.context.annotation.Configuration;
import org.springframework.core.Ordered; import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order; import org.springframework.core.annotation.Order;
import org.springframework.core.env.Environment; 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.EmbeddedDatabase;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder; import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
import org.springframework.util.Assert; import org.springframework.util.Assert;
...@@ -56,7 +50,6 @@ import org.springframework.util.ObjectUtils; ...@@ -56,7 +50,6 @@ import org.springframework.util.ObjectUtils;
* Auto-configuration for a test database. * Auto-configuration for a test database.
* *
* @author Phillip Webb * @author Phillip Webb
* @author Eddú Meléndez
* @since 1.4.0 * @since 1.4.0
* @see AutoConfigureTestDatabase * @see AutoConfigureTestDatabase
*/ */
...@@ -64,9 +57,6 @@ import org.springframework.util.ObjectUtils; ...@@ -64,9 +57,6 @@ import org.springframework.util.ObjectUtils;
@AutoConfigureBefore(DataSourceAutoConfiguration.class) @AutoConfigureBefore(DataSourceAutoConfiguration.class)
public class TestDatabaseAutoConfiguration { 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; private final Environment environment;
TestDatabaseAutoConfiguration(Environment environment) { TestDatabaseAutoConfiguration(Environment environment) {
...@@ -74,55 +64,18 @@ public class TestDatabaseAutoConfiguration { ...@@ -74,55 +64,18 @@ public class TestDatabaseAutoConfiguration {
} }
@Bean @Bean
@Conditional(TestDatabaseReplaceAutoConfiguredCondition.class) @ConditionalOnProperty(prefix = "spring.test.database", name = "replace", havingValue = "AUTO_CONFIGURED")
@ConditionalOnMissingBean @ConditionalOnMissingBean
public DataSource dataSource() { public DataSource dataSource() {
return new EmbeddedDataSourceFactory(this.environment).getEmbeddedDatabase(); return new EmbeddedDataSourceFactory(this.environment).getEmbeddedDatabase();
} }
@Bean @Bean
@Conditional(TestDatabaseReplaceAnyCondition.class) @ConditionalOnProperty(prefix = "spring.test.database", name = "replace", havingValue = "ANY", matchIfMissing = true)
public static EmbeddedDataSourceBeanFactoryPostProcessor embeddedDataSourceBeanFactoryPostProcessor() { public static EmbeddedDataSourceBeanFactoryPostProcessor embeddedDataSourceBeanFactoryPostProcessor() {
return new 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) @Order(Ordered.LOWEST_PRECEDENCE)
private static class EmbeddedDataSourceBeanFactoryPostProcessor private static class EmbeddedDataSourceBeanFactoryPostProcessor
implements BeanDefinitionRegistryPostProcessor { implements BeanDefinitionRegistryPostProcessor {
......
{
"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
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment