Skip to content

Commit

Permalink
Add username alias for Postgres's PGSimpleDataSource
Browse files Browse the repository at this point in the history
Closes gh-25363
  • Loading branch information
snicoll committed Feb 18, 2021
1 parent bb56de7 commit 526474f
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
Expand Up @@ -198,6 +198,8 @@ private static class DataSourceSettingsResolver {
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) {
Expand Down
Expand Up @@ -33,6 +33,7 @@
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;

Expand Down Expand Up @@ -129,6 +130,15 @@ void dataSourceCanBeCreatedWithH2JdbcDataSource() {
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")
Expand Down

0 comments on commit 526474f

Please sign in to comment.