Support fallback URL properties

Update `DataSourceBuilder` so that the url property attempts both
`getUrl()` / `setUrl(...)` and `getURL()`/`setURL(...)`.

Fixes gh-26647

Co-authored-by: Phillip Webb <pwebb@vmware.com>
pull/26677/head
Scott Frederick 4 years ago committed by Phillip Webb
parent e55948456f
commit a31e976ec6

@ -251,31 +251,42 @@ public final class DataSourceBuilder<T extends DataSource> {
*/ */
private enum DataSourceProperty { private enum DataSourceProperty {
URL("url"), URL("url", "URL"),
DRIVER_CLASS_NAME("driverClassName"), DRIVER_CLASS_NAME("driverClassName"),
USERNAME("username"), USERNAME("username", "user"),
PASSWORD("password"); PASSWORD("password");
private final String name; private final String[] names;
DataSourceProperty(String name) { DataSourceProperty(String... names) {
this.name = name; this.names = names;
} }
@Override @Override
public String toString() { public String toString() {
return this.name; return this.names[0];
} }
Method findSetter(Class<?> type) { Method findSetter(Class<?> type) {
return ReflectionUtils.findMethod(type, "set" + StringUtils.capitalize(this.name), String.class); return extracted("set", type);
} }
Method findGetter(Class<?> 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;
} }
} }

@ -26,6 +26,7 @@ import java.util.Arrays;
import javax.sql.DataSource; import javax.sql.DataSource;
import com.microsoft.sqlserver.jdbc.SQLServerDataSource;
import com.zaxxer.hikari.HikariDataSource; import com.zaxxer.hikari.HikariDataSource;
import oracle.jdbc.internal.OpaqueString; import oracle.jdbc.internal.OpaqueString;
import oracle.jdbc.pool.OracleDataSource; import oracle.jdbc.pool.OracleDataSource;
@ -148,6 +149,15 @@ class DataSourceBuilderTests {
assertThat(pgDataSource.getUser()).isEqualTo("test"); 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 @Test
void buildWhenMappedTypeSpecifiedAndNoSuitableMappingThrowsException() { void buildWhenMappedTypeSpecifiedAndNoSuitableMappingThrowsException() {
assertThatExceptionOfType(UnsupportedDataSourcePropertyException.class).isThrownBy( assertThatExceptionOfType(UnsupportedDataSourcePropertyException.class).isThrownBy(

Loading…
Cancel
Save