Fix deriving DataSources from custom type

Eliminate the unsupported datasource property exception thrown
when trying to derive a datasource from an unknown datasource type.

See gh-27453
This commit is contained in:
saraswathy-krish 2021-07-22 02:40:46 -07:00 committed by Andy Wilkinson
parent 44a9531ace
commit d0e2823c49
2 changed files with 23 additions and 5 deletions

View File

@ -281,17 +281,22 @@ public final class DataSourceBuilder<T extends DataSource> {
}
Method findSetter(Class<?> type) {
return extracted("set", type);
return extracted("set", type, true);
}
Method findGetter(Class<?> type) {
return extracted("get", type);
return extracted("get", type, false);
}
private Method extracted(String prefix, Class<?> type) {
private Method extracted(String prefix, Class<?> type, boolean hasParameter) {
for (String candidate : this.names) {
Method method = ReflectionUtils.findMethod(type, prefix + StringUtils.capitalize(candidate),
String.class);
Method method;
if (hasParameter) {
method = ReflectionUtils.findMethod(type, prefix + StringUtils.capitalize(candidate), String.class);
}
else {
method = ReflectionUtils.findMethod(type, prefix + StringUtils.capitalize(candidate));
}
if (method != null) {
return method;
}

View File

@ -331,6 +331,19 @@ class DataSourceBuilderTests {
assertThat(built.getUrl()).isEqualTo("jdbc:postgresql://localhost:5432/postgres");
}
@Test // gh -27295
void buildWhenDerivedFromCustomTypeSpecifiedReturnsDataSource() {
CustomDataSource dataSource = new CustomDataSource();
dataSource.setUsername("test");
dataSource.setPassword("secret");
dataSource.setUrl("jdbc:postgresql://localhost:5432/postgres");
DataSourceBuilder<?> builder = DataSourceBuilder.derivedFrom(dataSource).type(SimpleDriverDataSource.class);
SimpleDriverDataSource testSource = (SimpleDriverDataSource) builder.build();
assertThat(testSource.getUsername()).isEqualTo("test");
assertThat(testSource.getUrl()).isEqualTo("jdbc:postgresql://localhost:5432/postgres");
assertThat(testSource.getPassword()).isEqualTo("secret");
}
final class HidePackagesClassLoader extends URLClassLoader {
private final String[] hiddenPackages;