Skip to content

Commit 21b815a

Browse files
committed
Polish contribution
Closes gh-7560
1 parent 5a7624d commit 21b815a

File tree

3 files changed

+20
-50
lines changed

3 files changed

+20
-50
lines changed

spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1155,4 +1155,11 @@ content into your application; rather pick only the properties that you need.
11551155
spring.devtools.remote.secret= # A shared secret required to establish a connection (required to enable remote support).
11561156
spring.devtools.remote.secret-header-name=X-AUTH-TOKEN # HTTP header used to transfer the shared secret.
11571157
1158+
1159+
# ----------------------------------------
1160+
# TESTING PROPERTIES
1161+
# ----------------------------------------
1162+
1163+
spring.test.database.replace=any # Type of existing DataSource to replace.
1164+
11581165
----

spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/jdbc/TestDatabaseAutoConfiguration.java

Lines changed: 3 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -31,22 +31,16 @@
3131
import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;
3232
import org.springframework.beans.factory.support.RootBeanDefinition;
3333
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
34-
import org.springframework.boot.autoconfigure.condition.ConditionMessage;
35-
import org.springframework.boot.autoconfigure.condition.ConditionOutcome;
3634
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
37-
import org.springframework.boot.autoconfigure.condition.SpringBootCondition;
35+
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
3836
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
3937
import org.springframework.boot.autoconfigure.jdbc.EmbeddedDatabaseConnection;
40-
import org.springframework.boot.bind.RelaxedPropertyResolver;
4138
import org.springframework.context.EnvironmentAware;
4239
import org.springframework.context.annotation.Bean;
43-
import org.springframework.context.annotation.ConditionContext;
44-
import org.springframework.context.annotation.Conditional;
4540
import org.springframework.context.annotation.Configuration;
4641
import org.springframework.core.Ordered;
4742
import org.springframework.core.annotation.Order;
4843
import org.springframework.core.env.Environment;
49-
import org.springframework.core.type.AnnotatedTypeMetadata;
5044
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabase;
5145
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
5246
import org.springframework.util.Assert;
@@ -56,73 +50,32 @@
5650
* Auto-configuration for a test database.
5751
*
5852
* @author Phillip Webb
59-
* @author Eddú Meléndez
6053
* @since 1.4.0
6154
* @see AutoConfigureTestDatabase
6255
*/
6356
@Configuration
6457
@AutoConfigureBefore(DataSourceAutoConfiguration.class)
6558
public class TestDatabaseAutoConfiguration {
6659

67-
private static final String SPRING_TEST_DATABASE_PREFIX = "spring.test.database.";
68-
private static final String REPLACE_PROPERTY = "replace";
69-
7060
private final Environment environment;
7161

7262
TestDatabaseAutoConfiguration(Environment environment) {
7363
this.environment = environment;
7464
}
7565

7666
@Bean
77-
@Conditional(TestDatabaseReplaceAutoConfiguredCondition.class)
67+
@ConditionalOnProperty(prefix = "spring.test.database", name = "replace", havingValue = "AUTO_CONFIGURED")
7868
@ConditionalOnMissingBean
7969
public DataSource dataSource() {
8070
return new EmbeddedDataSourceFactory(this.environment).getEmbeddedDatabase();
8171
}
8272

8373
@Bean
84-
@Conditional(TestDatabaseReplaceAnyCondition.class)
74+
@ConditionalOnProperty(prefix = "spring.test.database", name = "replace", havingValue = "ANY", matchIfMissing = true)
8575
public static EmbeddedDataSourceBeanFactoryPostProcessor embeddedDataSourceBeanFactoryPostProcessor() {
8676
return new EmbeddedDataSourceBeanFactoryPostProcessor();
8777
}
8878

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-
12679
@Order(Ordered.LOWEST_PRECEDENCE)
12780
private static class EmbeddedDataSourceBeanFactoryPostProcessor
12881
implements BeanDefinitionRegistryPostProcessor {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"properties": [
3+
{
4+
"name": "spring.test.database.replace",
5+
"type": "org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase$Replace",
6+
"description": "Type of existing DataSource to replace.",
7+
"defaultValue": "any"
8+
}
9+
]
10+
}

0 commit comments

Comments
 (0)