Skip to content

Commit

Permalink
Support fallback URL properties
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
scottfrederick and philwebb committed May 26, 2021
1 parent e559484 commit a31e976
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 8 deletions.
Expand Up @@ -251,31 +251,42 @@ public static Class<? extends DataSource> findType(ClassLoader classLoader) {
*/
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;
}

}
Expand Down
Expand Up @@ -26,6 +26,7 @@

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;
Expand Down Expand Up @@ -148,6 +149,15 @@ void buildWhenPostgresTypeSpecifiedReturnsExpectedDataSource() {
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(
Expand Down

0 comments on commit a31e976

Please sign in to comment.