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 c3622aa6cc..7d6fdc4fb3 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 @@ -251,31 +251,42 @@ public final class DataSourceBuilder { */ private enum DataSourceProperty { - URL("url"), + URL("url", "URL"), DRIVER_CLASS_NAME("driverClassName"), - USERNAME("username"), + USERNAME("username", "user"), PASSWORD("password"); - private final String name; + private final String[] names; - DataSourceProperty(String name) { - this.name = name; + DataSourceProperty(String... names) { + this.names = names; } @Override public String toString() { - return this.name; + return this.names[0]; } Method findSetter(Class type) { - return ReflectionUtils.findMethod(type, "set" + StringUtils.capitalize(this.name), String.class); + return extracted("set", type); } Method findGetter(Class type) { - return ReflectionUtils.findMethod(type, "get" + StringUtils.capitalize(this.name), String.class); + return extracted("get", type); + } + + private Method extracted(String prefix, Class type) { + for (String candidate : this.names) { + Method method = ReflectionUtils.findMethod(type, prefix + StringUtils.capitalize(candidate), + String.class); + if (method != null) { + return method; + } + } + return null; } } 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 4717650b94..c995a58713 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,6 +26,7 @@ import java.util.Arrays; import javax.sql.DataSource; +import com.microsoft.sqlserver.jdbc.SQLServerDataSource; import com.zaxxer.hikari.HikariDataSource; import oracle.jdbc.internal.OpaqueString; import oracle.jdbc.pool.OracleDataSource; @@ -148,6 +149,15 @@ class DataSourceBuilderTests { assertThat(pgDataSource.getUser()).isEqualTo("test"); } + @Test // gh-26647 + void buildWhenSqlServerTypeSpecifiedReturnsExpectedDataSource() { + this.dataSource = DataSourceBuilder.create().url("jdbc:sqlserver://localhost/test") + .type(SQLServerDataSource.class).username("test").build(); + assertThat(this.dataSource).isInstanceOf(SQLServerDataSource.class); + SQLServerDataSource sqlServerDataSource = (SQLServerDataSource) this.dataSource; + assertThat(sqlServerDataSource.getUser()).isEqualTo("test"); + } + @Test void buildWhenMappedTypeSpecifiedAndNoSuitableMappingThrowsException() { assertThatExceptionOfType(UnsupportedDataSourcePropertyException.class).isThrownBy(