Merge pull request #16814 from deakandris

* gh-16814:
  Polish "Consider properties from @AutoConfigureTestDatabase"
  Consider properties from @AutoConfigureTestDatabase

Closes gh-16814
This commit is contained in:
Andy Wilkinson
2019-06-17 11:45:03 +01:00
4 changed files with 56 additions and 6 deletions

View File

@@ -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");

View File

@@ -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();
}

View File

@@ -66,6 +66,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 +99,29 @@ 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);
DataSource dataSource = context.getBean(Flyway.class).getDataSource();
assertThat(dataSource).isNotNull();
assertThat(dataSource).hasFieldOrPropertyWithValue("user", "sa");
assertThat(dataSource).hasFieldOrPropertyWithValue("password", "");
});
}
@Test
public void createDataSourceWithUserAndFallbackToEmbeddedProperties() {
this.contextRunner.withUserConfiguration(EmbeddedDataSourceConfiguration.class)
.withPropertyValues("spring.flyway.user:sa").run((context) -> {
assertThat(context).hasSingleBean(Flyway.class);
DataSource dataSource = context.getBean(Flyway.class).getDataSource();
assertThat(dataSource).isNotNull();
assertThat(dataSource).extracting("url").hasSize(1).first().asString().startsWith("jdbc:h2:mem:");
});
}
@Test
public void flywayDataSource() {
this.contextRunner

View File

@@ -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)