Skip to content

Commit 92256c8

Browse files
deakandriswilkinsona
authored andcommitted
Consider properties from @AutoConfigureTestDatabase
Both Flyway and Liquibase makes use of DataSourceProperties to get default properties. Previously, both used strictly the configuration properties and failed to consider embedded datasource properties autoconfigured by @AutoConfigureTestDatabase. In case a database layer test e.g. @jdbcTest relies on the autoconfigured embedded datasource, Flyway and Liquibase autoconfiguration fails as they are not aware of the embedded datasource properties. See gh-16814
1 parent ba85394 commit 92256c8

File tree

5 files changed

+59
-6
lines changed

5 files changed

+59
-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: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import org.flywaydb.core.api.callback.Context;
3131
import org.flywaydb.core.api.callback.Event;
3232
import org.flywaydb.core.api.callback.FlywayCallback;
33+
import org.flywaydb.core.internal.jdbc.DriverDataSource;
3334
import org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform;
3435
import org.junit.Test;
3536
import org.mockito.InOrder;
@@ -66,6 +67,7 @@
6667
* @author Eddú Meléndez
6768
* @author Stephane Nicoll
6869
* @author Dominic Gunn
70+
* @author András Deák
6971
*/
7072
@SuppressWarnings("deprecation")
7173
public class FlywayAutoConfigurationTests {
@@ -98,6 +100,30 @@ public void createDataSourceWithUser() {
98100
});
99101
}
100102

103+
@Test
104+
public void createDataSourceFallbackToEmbeddedProperties() {
105+
this.contextRunner.withUserConfiguration(EmbeddedDataSourceConfiguration.class)
106+
.withPropertyValues("spring.flyway.url:jdbc:hsqldb:mem:flywaytest").run((context) -> {
107+
assertThat(context).hasSingleBean(Flyway.class);
108+
assertThat(context.getBean(Flyway.class).getDataSource()).isNotNull();
109+
assertThat(((DriverDataSource) context.getBean(Flyway.class).getDataSource()).getUser())
110+
.isEqualTo("sa");
111+
assertThat(((DriverDataSource) context.getBean(Flyway.class).getDataSource()).getPassword())
112+
.isEqualTo("");
113+
});
114+
}
115+
116+
@Test
117+
public void createDataSourceWithUserAndFallbackToEmbeddedProperties() {
118+
this.contextRunner.withUserConfiguration(EmbeddedDataSourceConfiguration.class)
119+
.withPropertyValues("spring.flyway.user:sa").run((context) -> {
120+
assertThat(context).hasSingleBean(Flyway.class);
121+
assertThat(context.getBean(Flyway.class).getDataSource()).isNotNull();
122+
assertThat(((DriverDataSource) context.getBean(Flyway.class).getDataSource()).getUrl())
123+
.startsWith("jdbc:h2:mem:");
124+
});
125+
}
126+
101127
@Test
102128
public void flywayDataSource() {
103129
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)

src/checkstyle/checkstyle-suppressions.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,5 @@
3535
<suppress files="LogbackMetricsAutoConfiguration\.java" checks="IllegalImport" />
3636
<suppress files="RemoteUrlPropertyExtractorTests\.java" checks="IllegalImport" />
3737
<suppress files="SampleLogbackApplication\.java" checks="IllegalImport" />
38+
<suppress files="FlywayAutoConfigurationTests\.java" checks="IllegalImport" />
3839
</suppressions>

0 commit comments

Comments
 (0)