|
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; |
36 | 34 | import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
37 |
| -import org.springframework.boot.autoconfigure.condition.SpringBootCondition; |
| 35 | +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; |
38 | 36 | import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
|
39 | 37 | import org.springframework.boot.autoconfigure.jdbc.EmbeddedDatabaseConnection;
|
40 |
| -import org.springframework.boot.bind.RelaxedPropertyResolver; |
41 | 38 | import org.springframework.context.EnvironmentAware;
|
42 | 39 | import org.springframework.context.annotation.Bean;
|
43 |
| -import org.springframework.context.annotation.ConditionContext; |
44 |
| -import org.springframework.context.annotation.Conditional; |
45 | 40 | import org.springframework.context.annotation.Configuration;
|
46 | 41 | import org.springframework.core.Ordered;
|
47 | 42 | import org.springframework.core.annotation.Order;
|
48 | 43 | import org.springframework.core.env.Environment;
|
49 |
| -import org.springframework.core.type.AnnotatedTypeMetadata; |
50 | 44 | import org.springframework.jdbc.datasource.embedded.EmbeddedDatabase;
|
51 | 45 | import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
|
52 | 46 | import org.springframework.util.Assert;
|
|
56 | 50 | * Auto-configuration for a test database.
|
57 | 51 | *
|
58 | 52 | * @author Phillip Webb
|
59 |
| - * @author Eddú Meléndez |
60 | 53 | * @since 1.4.0
|
61 | 54 | * @see AutoConfigureTestDatabase
|
62 | 55 | */
|
63 | 56 | @Configuration
|
64 | 57 | @AutoConfigureBefore(DataSourceAutoConfiguration.class)
|
65 | 58 | public class TestDatabaseAutoConfiguration {
|
66 | 59 |
|
67 |
| - private static final String SPRING_TEST_DATABASE_PREFIX = "spring.test.database."; |
68 |
| - private static final String REPLACE_PROPERTY = "replace"; |
69 |
| - |
70 | 60 | private final Environment environment;
|
71 | 61 |
|
72 | 62 | TestDatabaseAutoConfiguration(Environment environment) {
|
73 | 63 | this.environment = environment;
|
74 | 64 | }
|
75 | 65 |
|
76 | 66 | @Bean
|
77 |
| - @Conditional(TestDatabaseReplaceAutoConfiguredCondition.class) |
| 67 | + @ConditionalOnProperty(prefix = "spring.test.database", name = "replace", havingValue = "AUTO_CONFIGURED") |
78 | 68 | @ConditionalOnMissingBean
|
79 | 69 | public DataSource dataSource() {
|
80 | 70 | return new EmbeddedDataSourceFactory(this.environment).getEmbeddedDatabase();
|
81 | 71 | }
|
82 | 72 |
|
83 | 73 | @Bean
|
84 |
| - @Conditional(TestDatabaseReplaceAnyCondition.class) |
| 74 | + @ConditionalOnProperty(prefix = "spring.test.database", name = "replace", havingValue = "ANY", matchIfMissing = true) |
85 | 75 | public static EmbeddedDataSourceBeanFactoryPostProcessor embeddedDataSourceBeanFactoryPostProcessor() {
|
86 | 76 | return new EmbeddedDataSourceBeanFactoryPostProcessor();
|
87 | 77 | }
|
88 | 78 |
|
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 |
| - |
126 | 79 | @Order(Ordered.LOWEST_PRECEDENCE)
|
127 | 80 | private static class EmbeddedDataSourceBeanFactoryPostProcessor
|
128 | 81 | implements BeanDefinitionRegistryPostProcessor {
|
|
0 commit comments