|
31 | 31 | import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;
|
32 | 32 | import org.springframework.beans.factory.support.RootBeanDefinition;
|
33 | 33 | import org.springframework.boot.autoconfigure.AutoConfigureBefore;
|
| 34 | +import org.springframework.boot.autoconfigure.condition.ConditionMessage; |
| 35 | +import org.springframework.boot.autoconfigure.condition.ConditionOutcome; |
34 | 36 | import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
35 |
| -import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; |
| 37 | +import org.springframework.boot.autoconfigure.condition.SpringBootCondition; |
36 | 38 | import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
|
37 | 39 | import org.springframework.boot.autoconfigure.jdbc.EmbeddedDatabaseConnection;
|
| 40 | +import org.springframework.boot.bind.RelaxedPropertyResolver; |
38 | 41 | import org.springframework.context.EnvironmentAware;
|
39 | 42 | import org.springframework.context.annotation.Bean;
|
| 43 | +import org.springframework.context.annotation.ConditionContext; |
| 44 | +import org.springframework.context.annotation.Conditional; |
40 | 45 | import org.springframework.context.annotation.Configuration;
|
41 | 46 | import org.springframework.core.Ordered;
|
42 | 47 | import org.springframework.core.annotation.Order;
|
43 | 48 | import org.springframework.core.env.Environment;
|
| 49 | +import org.springframework.core.type.AnnotatedTypeMetadata; |
44 | 50 | import org.springframework.jdbc.datasource.embedded.EmbeddedDatabase;
|
45 | 51 | import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
|
46 | 52 | import org.springframework.util.Assert;
|
|
50 | 56 | * Auto-configuration for a test database.
|
51 | 57 | *
|
52 | 58 | * @author Phillip Webb
|
| 59 | + * @author Eddú Meléndez |
53 | 60 | * @since 1.4.0
|
54 | 61 | * @see AutoConfigureTestDatabase
|
55 | 62 | */
|
56 | 63 | @Configuration
|
57 | 64 | @AutoConfigureBefore(DataSourceAutoConfiguration.class)
|
58 | 65 | public class TestDatabaseAutoConfiguration {
|
59 | 66 |
|
| 67 | + private static final String SPRING_TEST_DATABASE_PREFIX = "spring.test.database."; |
| 68 | + private static final String REPLACE_PROPERTY = "replace"; |
| 69 | + |
60 | 70 | private final Environment environment;
|
61 | 71 |
|
62 | 72 | TestDatabaseAutoConfiguration(Environment environment) {
|
63 | 73 | this.environment = environment;
|
64 | 74 | }
|
65 | 75 |
|
66 | 76 | @Bean
|
67 |
| - @ConditionalOnProperty(prefix = "spring.test.database", name = "replace", havingValue = "AUTO_CONFIGURED") |
| 77 | + @Conditional(TestDatabaseReplaceAutoConfiguredCondition.class) |
68 | 78 | @ConditionalOnMissingBean
|
69 | 79 | public DataSource dataSource() {
|
70 | 80 | return new EmbeddedDataSourceFactory(this.environment).getEmbeddedDatabase();
|
71 | 81 | }
|
72 | 82 |
|
73 | 83 | @Bean
|
74 |
| - @ConditionalOnProperty(prefix = "spring.test.database", name = "replace", havingValue = "ANY", matchIfMissing = true) |
| 84 | + @Conditional(TestDatabaseReplaceAnyCondition.class) |
75 | 85 | public static EmbeddedDataSourceBeanFactoryPostProcessor embeddedDataSourceBeanFactoryPostProcessor() {
|
76 | 86 | return new EmbeddedDataSourceBeanFactoryPostProcessor();
|
77 | 87 | }
|
78 | 88 |
|
| 89 | + static class TestDatabaseReplaceAutoConfiguredCondition extends SpringBootCondition { |
| 90 | + |
| 91 | + @Override |
| 92 | + public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata |
| 93 | + metadata) { |
| 94 | + RelaxedPropertyResolver resolver = new RelaxedPropertyResolver(context.getEnvironment(), SPRING_TEST_DATABASE_PREFIX); |
| 95 | + ConditionMessage.Builder message = ConditionMessage |
| 96 | + .forCondition("Test Database Replace Property"); |
| 97 | + if (resolver.containsProperty(REPLACE_PROPERTY) && "NONE".equals(resolver.getProperty(REPLACE_PROPERTY))) { |
| 98 | + return ConditionOutcome.noMatch(message.didNotFind("NONE").atAll()); |
| 99 | + } |
| 100 | + else if (resolver.containsProperty(REPLACE_PROPERTY) && "AUTO_CONFIGURED".equals(resolver.getProperty(REPLACE_PROPERTY))) { |
| 101 | + return ConditionOutcome.match(message.found("AUTO_CONFIGURED").atAll()); |
| 102 | + } |
| 103 | + return ConditionOutcome.noMatch(message.didNotFind("spring.test.database.replace").atAll()); |
| 104 | + } |
| 105 | + |
| 106 | + } |
| 107 | + |
| 108 | + static class TestDatabaseReplaceAnyCondition extends SpringBootCondition { |
| 109 | + |
| 110 | + @Override |
| 111 | + public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) { |
| 112 | + RelaxedPropertyResolver resolver = new RelaxedPropertyResolver(context.getEnvironment(), SPRING_TEST_DATABASE_PREFIX); |
| 113 | + ConditionMessage.Builder message = ConditionMessage |
| 114 | + .forCondition("Test Database Replace Property"); |
| 115 | + if (resolver.containsProperty(REPLACE_PROPERTY) && "NONE".equals(resolver.getProperty(REPLACE_PROPERTY))) { |
| 116 | + return ConditionOutcome.noMatch(message.didNotFind("NONE").atAll()); |
| 117 | + } |
| 118 | + else if (!resolver.containsProperty(REPLACE_PROPERTY) || "ANY".equals(resolver.getProperty(REPLACE_PROPERTY))) { |
| 119 | + return ConditionOutcome.match(message.found("ANY").atAll()); |
| 120 | + } |
| 121 | + return ConditionOutcome.noMatch(message.didNotFind("spring.test.database.replace").atAll()); |
| 122 | + } |
| 123 | + |
| 124 | + } |
| 125 | + |
79 | 126 | @Order(Ordered.LOWEST_PRECEDENCE)
|
80 | 127 | private static class EmbeddedDataSourceBeanFactoryPostProcessor
|
81 | 128 | implements BeanDefinitionRegistryPostProcessor {
|
|
0 commit comments