Skip to content

Commit 6f4d598

Browse files
committed
Merge pull request #16814 from deakandris
* gh-16814: Polish "Consider properties from @AutoConfigureTestDatabase" Consider properties from @AutoConfigureTestDatabase Closes gh-16814
2 parents ba85394 + 5dc2339 commit 6f4d598

File tree

4 files changed

+56
-6
lines changed

4 files changed

+56
-6
lines changed

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/flyway/FlywayAutoConfiguration.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@
7979
* @author Eddú Meléndez
8080
* @author Dominic Gunn
8181
* @author Dan Zheng
82+
* @author András Deák
8283
* @since 1.1.0
8384
*/
8485
@SuppressWarnings("deprecation")
@@ -156,9 +157,10 @@ public Flyway flyway() {
156157

157158
private DataSource configureDataSource(FluentConfiguration configuration) {
158159
if (this.properties.isCreateDataSource()) {
159-
String url = getProperty(this.properties::getUrl, this.dataSourceProperties::getUrl);
160-
String user = getProperty(this.properties::getUser, this.dataSourceProperties::getUsername);
161-
String password = getProperty(this.properties::getPassword, this.dataSourceProperties::getPassword);
160+
String url = getProperty(this.properties::getUrl, this.dataSourceProperties::determineUrl);
161+
String user = getProperty(this.properties::getUser, this.dataSourceProperties::determineUsername);
162+
String password = getProperty(this.properties::getPassword,
163+
this.dataSourceProperties::determinePassword);
162164
configuration.dataSource(url, user, password);
163165
if (!CollectionUtils.isEmpty(this.properties.getInitSqls())) {
164166
String initSql = StringUtils.collectionToDelimitedString(this.properties.getInitSqls(), "\n");

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/liquibase/LiquibaseAutoConfiguration.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
* @author Andy Wilkinson
6262
* @author Dominic Gunn
6363
* @author Dan Zheng
64+
* @author András Deák
6465
* @since 1.1.0
6566
*/
6667
@Configuration
@@ -153,9 +154,9 @@ private DataSource getDataSource() {
153154
}
154155

155156
private DataSource createNewDataSource() {
156-
String url = getProperty(this.properties::getUrl, this.dataSourceProperties::getUrl);
157-
String user = getProperty(this.properties::getUser, this.dataSourceProperties::getUsername);
158-
String password = getProperty(this.properties::getPassword, this.dataSourceProperties::getPassword);
157+
String url = getProperty(this.properties::getUrl, this.dataSourceProperties::determineUrl);
158+
String user = getProperty(this.properties::getUser, this.dataSourceProperties::determineUsername);
159+
String password = getProperty(this.properties::getPassword, this.dataSourceProperties::determinePassword);
159160
return DataSourceBuilder.create().url(url).username(user).password(password).build();
160161
}
161162

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/flyway/FlywayAutoConfigurationTests.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666
* @author Eddú Meléndez
6767
* @author Stephane Nicoll
6868
* @author Dominic Gunn
69+
* @author András Deák
6970
*/
7071
@SuppressWarnings("deprecation")
7172
public class FlywayAutoConfigurationTests {
@@ -98,6 +99,29 @@ public void createDataSourceWithUser() {
9899
});
99100
}
100101

102+
@Test
103+
public void createDataSourceFallbackToEmbeddedProperties() {
104+
this.contextRunner.withUserConfiguration(EmbeddedDataSourceConfiguration.class)
105+
.withPropertyValues("spring.flyway.url:jdbc:hsqldb:mem:flywaytest").run((context) -> {
106+
assertThat(context).hasSingleBean(Flyway.class);
107+
DataSource dataSource = context.getBean(Flyway.class).getDataSource();
108+
assertThat(dataSource).isNotNull();
109+
assertThat(dataSource).hasFieldOrPropertyWithValue("user", "sa");
110+
assertThat(dataSource).hasFieldOrPropertyWithValue("password", "");
111+
});
112+
}
113+
114+
@Test
115+
public void createDataSourceWithUserAndFallbackToEmbeddedProperties() {
116+
this.contextRunner.withUserConfiguration(EmbeddedDataSourceConfiguration.class)
117+
.withPropertyValues("spring.flyway.user:sa").run((context) -> {
118+
assertThat(context).hasSingleBean(Flyway.class);
119+
DataSource dataSource = context.getBean(Flyway.class).getDataSource();
120+
assertThat(dataSource).isNotNull();
121+
assertThat(dataSource).extracting("url").hasSize(1).first().asString().startsWith("jdbc:h2:mem:");
122+
});
123+
}
124+
101125
@Test
102126
public void flywayDataSource() {
103127
this.contextRunner

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/liquibase/LiquibaseAutoConfigurationTests.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
* @author Andy Wilkinson
6161
* @author Stephane Nicoll
6262
* @author Dominic Gunn
63+
* @author András Deák
6364
*/
6465
public class LiquibaseAutoConfigurationTests {
6566

@@ -199,6 +200,28 @@ public void overrideUser() {
199200
}));
200201
}
201202

203+
@Test
204+
public void overrideDataSourceAndFallbackToEmbeddedProperties() {
205+
this.contextRunner.withUserConfiguration(EmbeddedDataSourceConfiguration.class)
206+
.withPropertyValues("spring.liquibase.url:jdbc:hsqldb:mem:liquibase")
207+
.run(assertLiquibase((liquibase) -> {
208+
DataSource dataSource = liquibase.getDataSource();
209+
assertThat(((HikariDataSource) dataSource).isClosed()).isTrue();
210+
assertThat(((HikariDataSource) dataSource).getUsername()).isEqualTo("sa");
211+
assertThat(((HikariDataSource) dataSource).getPassword()).isEqualTo("");
212+
}));
213+
}
214+
215+
@Test
216+
public void overrideUserAndFallbackToEmbeddedProperties() {
217+
this.contextRunner.withUserConfiguration(EmbeddedDataSourceConfiguration.class)
218+
.withPropertyValues("spring.liquibase.user:sa").run(assertLiquibase((liquibase) -> {
219+
DataSource dataSource = liquibase.getDataSource();
220+
assertThat(((HikariDataSource) dataSource).isClosed()).isTrue();
221+
assertThat(((HikariDataSource) dataSource).getJdbcUrl()).startsWith("jdbc:h2:mem:");
222+
}));
223+
}
224+
202225
@Test
203226
public void overrideTestRollbackOnUpdate() {
204227
this.contextRunner.withUserConfiguration(EmbeddedDataSourceConfiguration.class)

0 commit comments

Comments
 (0)