From 4ec30e1145d7221338e420b5727693b5fe073716 Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Tue, 18 Feb 2020 17:22:30 +0100 Subject: [PATCH] Add support for SimpleDriverDataSource This commit makes sure that DataSourceBuilder can configure SimpleDriverDataSource by adding an alias for the driver's class name. Closes gh-20220 Co-authored-by: Dmytro Nosan --- .../springframework/boot/jdbc/DataSourceBuilder.java | 1 + .../boot/jdbc/DataSourceBuilderTests.java | 11 +++++++++++ 2 files changed, 12 insertions(+) diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/jdbc/DataSourceBuilder.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/jdbc/DataSourceBuilder.java index f21da930ef..be44f8d515 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/jdbc/DataSourceBuilder.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/jdbc/DataSourceBuilder.java @@ -87,6 +87,7 @@ public final class DataSourceBuilder { private void bind(DataSource result) { ConfigurationPropertySource source = new MapConfigurationPropertySource(this.properties); ConfigurationPropertyNameAliases aliases = new ConfigurationPropertyNameAliases(); + aliases.addAliases("driver-class-name", "driver-class"); aliases.addAliases("url", "jdbc-url"); aliases.addAliases("username", "user"); Binder binder = new Binder(source.withAliases(aliases)); diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/jdbc/DataSourceBuilderTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/jdbc/DataSourceBuilderTests.java index 2fe8667868..b9d0fb1dab 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/jdbc/DataSourceBuilderTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/jdbc/DataSourceBuilderTests.java @@ -26,9 +26,12 @@ import javax.sql.DataSource; import com.zaxxer.hikari.HikariDataSource; import org.apache.commons.dbcp2.BasicDataSource; +import org.h2.Driver; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.Test; +import org.springframework.jdbc.datasource.SimpleDriverDataSource; + import static org.assertj.core.api.Assertions.assertThat; /** @@ -74,6 +77,14 @@ class DataSourceBuilderTests { assertThat(hikariDataSource).isInstanceOf(HikariDataSource.class); } + @Test + void dataSourceCanBeCreatedWithSimpleDriverDataSource() { + this.dataSource = DataSourceBuilder.create().url("jdbc:h2:test").type(SimpleDriverDataSource.class).build(); + assertThat(this.dataSource).isInstanceOf(SimpleDriverDataSource.class); + assertThat(((SimpleDriverDataSource) this.dataSource).getUrl()).isEqualTo("jdbc:h2:test"); + assertThat(((SimpleDriverDataSource) this.dataSource).getDriver()).isInstanceOf(Driver.class); + } + final class HidePackagesClassLoader extends URLClassLoader { private final String[] hiddenPackages;