Add username alias for Postgres's PGSimpleDataSource

Closes gh-25363
pull/25507/head
Stephane Nicoll 4 years ago
parent bb56de715b
commit 526474f3b8

@ -198,6 +198,8 @@ public final class DataSourceBuilder<T extends DataSource> {
create(classLoader, "oracle.jdbc.datasource.OracleDataSource", OracleDataSourceSettings::new));
addIfAvailable(this.allDataSourceSettings, create(classLoader, "org.h2.jdbcx.JdbcDataSource",
(type) -> new DataSourceSettings(type, (aliases) -> aliases.addAliases("username", "user"))));
addIfAvailable(this.allDataSourceSettings, create(classLoader, "org.postgresql.ds.PGSimpleDataSource",
(type) -> new DataSourceSettings(type, (aliases) -> aliases.addAliases("username", "user"))));
}
private static List<DataSourceSettings> resolveAvailableDataSourceSettings(ClassLoader classLoader) {

@ -33,6 +33,7 @@ import org.h2.Driver;
import org.h2.jdbcx.JdbcDataSource;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
import org.postgresql.ds.PGSimpleDataSource;
import org.springframework.jdbc.datasource.SimpleDriverDataSource;
@ -129,6 +130,15 @@ class DataSourceBuilderTests {
assertThat(h2DataSource.getUser()).isEqualTo("test");
}
@Test
void dataSourceCanBeCreatedWithPGDataSource() {
this.dataSource = DataSourceBuilder.create().url("jdbc:postgresql://localhost/test")
.type(PGSimpleDataSource.class).username("test").build();
assertThat(this.dataSource).isInstanceOf(PGSimpleDataSource.class);
PGSimpleDataSource pgDataSource = (PGSimpleDataSource) this.dataSource;
assertThat(pgDataSource.getUser()).isEqualTo("test");
}
@Test
void dataSourceAliasesAreOnlyAppliedToRelevantDataSource() {
this.dataSource = DataSourceBuilder.create().url("jdbc:h2:test").type(TestDataSource.class).username("test")

Loading…
Cancel
Save