Skip to content

Commit cc21a13

Browse files
committed
Merge pull request spring-projects#7560 from eddumelendez:spring-projectsgh-7229
* pr/7560: Polish contribution Add support for property spring.test.database.replace
2 parents 6fb1fb5 + 21b815a commit cc21a13

File tree

6 files changed

+205
-0
lines changed

6 files changed

+205
-0
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/AutoConfigureTestDatabase.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
2929
import org.springframework.boot.autoconfigure.jdbc.EmbeddedDatabaseConnection;
3030
import org.springframework.boot.test.autoconfigure.properties.PropertyMapping;
31+
import org.springframework.boot.test.autoconfigure.properties.SkipPropertyMapping;
3132

3233
/**
3334
* Annotation that can be applied to a test class to configure a test database to use
@@ -48,6 +49,7 @@
4849
* Determines what type of existing DataSource beans can be replaced.
4950
* @return the type of existing DataSource to replace
5051
*/
52+
@PropertyMapping(skip = SkipPropertyMapping.ON_DEFAULT_VALUE)
5153
Replace replace() default Replace.ANY;
5254

5355
/**
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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
* Copyright 2012-2016 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.test.autoconfigure.jdbc;
18+
19+
import javax.sql.DataSource;
20+
21+
import org.junit.Test;
22+
import org.junit.runner.RunWith;
23+
24+
import org.springframework.beans.factory.annotation.Autowired;
25+
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
26+
import org.springframework.boot.autoconfigure.jdbc.EmbeddedDatabaseConnection;
27+
import org.springframework.context.annotation.Bean;
28+
import org.springframework.context.annotation.Configuration;
29+
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
30+
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
31+
import org.springframework.test.context.TestPropertySource;
32+
import org.springframework.test.context.junit4.SpringRunner;
33+
34+
import static org.assertj.core.api.Assertions.assertThat;
35+
36+
/**
37+
* Integration tests for {@link JdbcTest}.
38+
*
39+
* @author Phillip Webb
40+
* @author Stephane Nicoll
41+
*/
42+
@RunWith(SpringRunner.class)
43+
@JdbcTest
44+
@AutoConfigureTestDatabase(connection = EmbeddedDatabaseConnection.HSQL)
45+
@TestPropertySource(properties = "spring.test.database.replace=ANY")
46+
public class JdbcTestWithAutoConfigureTestDatabaseReplacePropertyAnyIntegrationTests {
47+
48+
@Autowired
49+
private DataSource dataSource;
50+
51+
@Test
52+
public void replacesDefinedDataSourceWithExplicit() throws Exception {
53+
// H2 is explicitly defined but HSQL is the override.
54+
String product = this.dataSource.getConnection().getMetaData()
55+
.getDatabaseProductName();
56+
assertThat(product).startsWith("HSQL");
57+
}
58+
59+
@Configuration
60+
@EnableAutoConfiguration
61+
static class Config {
62+
63+
@Bean
64+
public DataSource dataSource() {
65+
EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder()
66+
.generateUniqueName(true)
67+
.setType(EmbeddedDatabaseType.H2);
68+
return builder.build();
69+
}
70+
71+
}
72+
73+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* Copyright 2012-2016 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.test.autoconfigure.jdbc;
18+
19+
import javax.sql.DataSource;
20+
21+
import org.junit.Test;
22+
import org.junit.runner.RunWith;
23+
24+
import org.springframework.beans.factory.annotation.Autowired;
25+
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
26+
import org.springframework.boot.autoconfigure.jdbc.EmbeddedDatabaseConnection;
27+
import org.springframework.context.annotation.Configuration;
28+
import org.springframework.test.context.TestPropertySource;
29+
import org.springframework.test.context.junit4.SpringRunner;
30+
31+
import static org.assertj.core.api.Assertions.assertThat;
32+
33+
/**
34+
* Integration tests for {@link JdbcTest}.
35+
*
36+
* @author Phillip Webb
37+
* @author Stephane Nicoll
38+
*/
39+
@RunWith(SpringRunner.class)
40+
@JdbcTest
41+
@AutoConfigureTestDatabase(connection = EmbeddedDatabaseConnection.HSQL)
42+
@TestPropertySource(properties = "spring.test.database.replace=AUTO_CONFIGURED")
43+
public class JdbcTestWithAutoConfigureTestDatabaseReplacePropertyAutoConfiguredIntegrationTests {
44+
45+
@Autowired
46+
private DataSource dataSource;
47+
48+
@Test
49+
public void replacesAutoConfiguredDataSource() throws Exception {
50+
String product = this.dataSource.getConnection().getMetaData()
51+
.getDatabaseProductName();
52+
assertThat(product).startsWith("HSQL");
53+
}
54+
55+
@Configuration
56+
@EnableAutoConfiguration // Will auto-configure H2
57+
static class Config {
58+
59+
}
60+
61+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Copyright 2012-2016 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.test.autoconfigure.jdbc;
18+
19+
import javax.sql.DataSource;
20+
21+
import org.junit.Test;
22+
import org.junit.runner.RunWith;
23+
24+
import org.springframework.beans.factory.annotation.Autowired;
25+
import org.springframework.test.context.TestPropertySource;
26+
import org.springframework.test.context.junit4.SpringRunner;
27+
28+
import static org.assertj.core.api.Assertions.assertThat;
29+
30+
/**
31+
* Integration tests for {@link JdbcTest}.
32+
*
33+
* @author Phillip Webb
34+
* @author Stephane Nicoll
35+
*/
36+
@RunWith(SpringRunner.class)
37+
@JdbcTest
38+
@TestPropertySource(properties = "spring.test.database.replace=NONE")
39+
public class JdbcTestWithAutoConfigureTestDatabaseReplacePropertyNoneIntegrationTests {
40+
41+
@Autowired
42+
private DataSource dataSource;
43+
44+
@Test
45+
public void usesDefaultEmbeddedDatabase() throws Exception {
46+
// HSQL is explicitly defined and should not be replaced
47+
String product = this.dataSource.getConnection().getMetaData()
48+
.getDatabaseProductName();
49+
assertThat(product).startsWith("HSQL");
50+
}
51+
52+
}

0 commit comments

Comments
 (0)