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
pull/17391/head
András Deák 6 years ago committed by Andy Wilkinson
parent ba85394e83
commit 92256c80d0

@ -79,6 +79,7 @@ import org.springframework.util.StringUtils;
* @author Eddú Meléndez
* @author Dominic Gunn
* @author Dan Zheng
* @author András Deák
* @since 1.1.0
*/
@SuppressWarnings("deprecation")
@ -156,9 +157,10 @@ public class FlywayAutoConfiguration {
private DataSource configureDataSource(FluentConfiguration configuration) {
if (this.properties.isCreateDataSource()) {
String url = getProperty(this.properties::getUrl, this.dataSourceProperties::getUrl);
String user = getProperty(this.properties::getUser, this.dataSourceProperties::getUsername);
String password = getProperty(this.properties::getPassword, this.dataSourceProperties::getPassword);
String url = getProperty(this.properties::getUrl, this.dataSourceProperties::determineUrl);
String user = getProperty(this.properties::getUser, this.dataSourceProperties::determineUsername);
String password = getProperty(this.properties::getPassword,
this.dataSourceProperties::determinePassword);
configuration.dataSource(url, user, password);
if (!CollectionUtils.isEmpty(this.properties.getInitSqls())) {
String initSql = StringUtils.collectionToDelimitedString(this.properties.getInitSqls(), "\n");

@ -61,6 +61,7 @@ import org.springframework.util.Assert;
* @author Andy Wilkinson
* @author Dominic Gunn
* @author Dan Zheng
* @author András Deák
* @since 1.1.0
*/
@Configuration
@ -153,9 +154,9 @@ public class LiquibaseAutoConfiguration {
}
private DataSource createNewDataSource() {
String url = getProperty(this.properties::getUrl, this.dataSourceProperties::getUrl);
String user = getProperty(this.properties::getUser, this.dataSourceProperties::getUsername);
String password = getProperty(this.properties::getPassword, this.dataSourceProperties::getPassword);
String url = getProperty(this.properties::getUrl, this.dataSourceProperties::determineUrl);
String user = getProperty(this.properties::getUser, this.dataSourceProperties::determineUsername);
String password = getProperty(this.properties::getPassword, this.dataSourceProperties::determinePassword);
return DataSourceBuilder.create().url(url).username(user).password(password).build();
}

@ -30,6 +30,7 @@ import org.flywaydb.core.api.callback.Callback;
import org.flywaydb.core.api.callback.Context;
import org.flywaydb.core.api.callback.Event;
import org.flywaydb.core.api.callback.FlywayCallback;
import org.flywaydb.core.internal.jdbc.DriverDataSource;
import org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform;
import org.junit.Test;
import org.mockito.InOrder;
@ -66,6 +67,7 @@ import static org.mockito.Mockito.mock;
* @author Eddú Meléndez
* @author Stephane Nicoll
* @author Dominic Gunn
* @author András Deák
*/
@SuppressWarnings("deprecation")
public class FlywayAutoConfigurationTests {
@ -98,6 +100,30 @@ public class FlywayAutoConfigurationTests {
});
}
@Test
public void createDataSourceFallbackToEmbeddedProperties() {
this.contextRunner.withUserConfiguration(EmbeddedDataSourceConfiguration.class)
.withPropertyValues("spring.flyway.url:jdbc:hsqldb:mem:flywaytest").run((context) -> {
assertThat(context).hasSingleBean(Flyway.class);
assertThat(context.getBean(Flyway.class).getDataSource()).isNotNull();
assertThat(((DriverDataSource) context.getBean(Flyway.class).getDataSource()).getUser())
.isEqualTo("sa");
assertThat(((DriverDataSource) context.getBean(Flyway.class).getDataSource()).getPassword())
.isEqualTo("");
});
}
@Test
public void createDataSourceWithUserAndFallbackToEmbeddedProperties() {
this.contextRunner.withUserConfiguration(EmbeddedDataSourceConfiguration.class)
.withPropertyValues("spring.flyway.user:sa").run((context) -> {
assertThat(context).hasSingleBean(Flyway.class);
assertThat(context.getBean(Flyway.class).getDataSource()).isNotNull();
assertThat(((DriverDataSource) context.getBean(Flyway.class).getDataSource()).getUrl())
.startsWith("jdbc:h2:mem:");
});
}
@Test
public void flywayDataSource() {
this.contextRunner

@ -60,6 +60,7 @@ import static org.assertj.core.api.Assertions.assertThat;
* @author Andy Wilkinson
* @author Stephane Nicoll
* @author Dominic Gunn
* @author András Deák
*/
public class LiquibaseAutoConfigurationTests {
@ -199,6 +200,28 @@ public class LiquibaseAutoConfigurationTests {
}));
}
@Test
public void overrideDataSourceAndFallbackToEmbeddedProperties() {
this.contextRunner.withUserConfiguration(EmbeddedDataSourceConfiguration.class)
.withPropertyValues("spring.liquibase.url:jdbc:hsqldb:mem:liquibase")
.run(assertLiquibase((liquibase) -> {
DataSource dataSource = liquibase.getDataSource();
assertThat(((HikariDataSource) dataSource).isClosed()).isTrue();
assertThat(((HikariDataSource) dataSource).getUsername()).isEqualTo("sa");
assertThat(((HikariDataSource) dataSource).getPassword()).isEqualTo("");
}));
}
@Test
public void overrideUserAndFallbackToEmbeddedProperties() {
this.contextRunner.withUserConfiguration(EmbeddedDataSourceConfiguration.class)
.withPropertyValues("spring.liquibase.user:sa").run(assertLiquibase((liquibase) -> {
DataSource dataSource = liquibase.getDataSource();
assertThat(((HikariDataSource) dataSource).isClosed()).isTrue();
assertThat(((HikariDataSource) dataSource).getJdbcUrl()).startsWith("jdbc:h2:mem:");
}));
}
@Test
public void overrideTestRollbackOnUpdate() {
this.contextRunner.withUserConfiguration(EmbeddedDataSourceConfiguration.class)

@ -35,4 +35,5 @@
<suppress files="LogbackMetricsAutoConfiguration\.java" checks="IllegalImport" />
<suppress files="RemoteUrlPropertyExtractorTests\.java" checks="IllegalImport" />
<suppress files="SampleLogbackApplication\.java" checks="IllegalImport" />
<suppress files="FlywayAutoConfigurationTests\.java" checks="IllegalImport" />
</suppressions>

Loading…
Cancel
Save