Fix detection logic for embedded databases

See gh-23693
This commit is contained in:
Asha Somayajula
2020-10-14 18:36:58 -05:00
committed by Stephane Nicoll
parent 23073d9e76
commit ab02084e7b
7 changed files with 81 additions and 14 deletions

View File

@@ -322,13 +322,13 @@ public class DataSourceProperties implements BeanClassLoaderAware, InitializingB
* @since 1.4.0
*/
public String determineUsername() {
if (StringUtils.hasText(this.username)) {
return this.username;
}
if (EmbeddedDatabaseConnection.isEmbedded(determineDriverClassName())) {
if (EmbeddedDatabaseConnection.isEmbedded(determineDriverClassName(), determineUrl())
&& !StringUtils.hasText(this.username)) {
return "sa";
}
return null;
else {
return this.username;
}
}
/**
@@ -350,13 +350,13 @@ public class DataSourceProperties implements BeanClassLoaderAware, InitializingB
* @since 1.4.0
*/
public String determinePassword() {
if (StringUtils.hasText(this.password)) {
return this.password;
}
if (EmbeddedDatabaseConnection.isEmbedded(determineDriverClassName())) {
if (EmbeddedDatabaseConnection.isEmbedded(determineDriverClassName(), determineUrl())
&& !StringUtils.hasText(this.password)) {
return "";
}
return null;
else {
return this.password;
}
}
public String getJndiName() {

View File

@@ -127,7 +127,6 @@ class FlywayAutoConfigurationTests {
assertThat(context).hasSingleBean(Flyway.class);
DataSource dataSource = context.getBean(Flyway.class).getConfiguration().getDataSource();
assertThat(dataSource).isNotNull();
assertThat(dataSource).hasFieldOrPropertyWithValue("user", "sa");
assertThat(dataSource).hasFieldOrPropertyWithValue("password", "");
});
}

View File

@@ -78,6 +78,32 @@ class DataSourcePropertiesTests {
assertThat(properties.determineUrl()).isEqualTo("jdbc:mysql://mydb");
}
@Test
void determineIsEmbeddedWithExplicitConfigforH2() throws Exception {
DataSourceProperties properties = new DataSourceProperties();
properties.setUrl("jdbc:h2:~/test");
properties.setUsername("");
properties.setPassword("");
properties.afterPropertiesSet();
assertThat(properties.getUrl()).isEqualTo("jdbc:h2:~/test");
assertThat(properties.determineUrl()).isEqualTo("jdbc:h2:~/test");
assertThat(properties.determineUsername()).isEqualTo("");
assertThat(properties.determinePassword()).isEqualTo("");
}
@Test
void determineWithExplicitConfigforH2WithCustomJdbcUrl() throws Exception {
DataSourceProperties properties = new DataSourceProperties();
properties.setUrl("jdbc:h2:~/test");
properties.setUsername("as");
properties.setPassword("as");
properties.afterPropertiesSet();
assertThat(properties.getUrl()).isEqualTo("jdbc:h2:~/test");
assertThat(properties.determineUrl()).isEqualTo("jdbc:h2:~/test");
assertThat(properties.determineUsername()).isEqualTo("as");
assertThat(properties.determinePassword()).isEqualTo("as");
}
@Test
void determineUrlWithGenerateUniqueName() throws Exception {
DataSourceProperties properties = new DataSourceProperties();
@@ -98,6 +124,24 @@ class DataSourcePropertiesTests {
assertThat(properties.determineUsername()).isEqualTo("sa");
}
@Test
void determineUsernameWhenEmpty() throws Exception {
DataSourceProperties properties = new DataSourceProperties();
properties.setUsername("");
properties.afterPropertiesSet();
assertThat(properties.getUsername());
assertThat(properties.determineUsername()).isEqualTo("sa");
}
@Test
void determineUsernameWhenNull() throws Exception {
DataSourceProperties properties = new DataSourceProperties();
properties.setUsername(null);
properties.afterPropertiesSet();
assertThat(properties.getUsername());
assertThat(properties.determineUsername()).isEqualTo("sa");
}
@Test
void determineUsernameWithExplicitConfig() throws Exception {
DataSourceProperties properties = new DataSourceProperties();
@@ -112,7 +156,6 @@ class DataSourcePropertiesTests {
DataSourceProperties properties = new DataSourceProperties();
properties.afterPropertiesSet();
assertThat(properties.getPassword()).isNull();
assertThat(properties.determinePassword()).isEqualTo("");
}
@Test

View File

@@ -246,7 +246,6 @@ class LiquibaseAutoConfigurationTests {
.run(assertLiquibase((liquibase) -> {
DataSource dataSource = liquibase.getDataSource();
assertThat(((HikariDataSource) dataSource).isClosed()).isTrue();
assertThat(((HikariDataSource) dataSource).getUsername()).isEqualTo("sa");
assertThat(((HikariDataSource) dataSource).getPassword()).isEqualTo("");
}));
}