Skip to content

Commit

Permalink
Add username alias for H2's JdbcDataSource
Browse files Browse the repository at this point in the history
Closes gh-25263
  • Loading branch information
snicoll committed Feb 17, 2021
1 parent 87efacf commit 5d1bb30
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2020 the original author or authors.
* Copyright 2012-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -196,6 +196,8 @@ private static class DataSourceSettingsResolver {
(aliases) -> aliases.addAliases("driver-class-name", "driver-class"))));
addIfAvailable(this.allDataSourceSettings,
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"))));
}

private static List<DataSourceSettings> resolveAvailableDataSourceSettings(ClassLoader classLoader) {
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2020 the original author or authors.
* Copyright 2012-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -30,6 +30,7 @@
import oracle.ucp.jdbc.PoolDataSourceImpl;
import org.apache.commons.dbcp2.BasicDataSource;
import org.h2.Driver;
import org.h2.jdbcx.JdbcDataSource;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -119,6 +120,15 @@ void dataSourceCanBeCreatedWithOracleUcpDataSource() {
assertThat(upcDataSource.getUser()).isEqualTo("test");
}

@Test
void dataSourceCanBeCreatedWithH2JdbcDataSource() {
this.dataSource = DataSourceBuilder.create().url("jdbc:h2:test").type(JdbcDataSource.class).username("test")
.build();
assertThat(this.dataSource).isInstanceOf(JdbcDataSource.class);
JdbcDataSource h2DataSource = (JdbcDataSource) this.dataSource;
assertThat(h2DataSource.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 5d1bb30

Please sign in to comment.