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 {
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;
}
}

@ -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(

Loading…
Cancel
Save