Polish "Fix deriving DataSources from custom type"

See gh-27453
This commit is contained in:
Andy Wilkinson 2021-07-22 11:17:29 +01:00
parent d0e2823c49
commit 18b4898977
2 changed files with 22 additions and 12 deletions

View File

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

View File

@ -331,8 +331,23 @@ class DataSourceBuilderTests {
assertThat(built.getUrl()).isEqualTo("jdbc:postgresql://localhost:5432/postgres"); assertThat(built.getUrl()).isEqualTo("jdbc:postgresql://localhost:5432/postgres");
} }
@Test // gh -27295 @Test // gh-27295
void buildWhenDerivedFromCustomTypeSpecifiedReturnsDataSource() { void buildWhenDerivedFromCustomType() {
CustomDataSource dataSource = new CustomDataSource();
dataSource.setUsername("test");
dataSource.setPassword("secret");
dataSource.setUrl("jdbc:postgresql://localhost:5432/postgres");
DataSourceBuilder<?> builder = DataSourceBuilder.derivedFrom(dataSource).username("alice")
.password("confidential");
CustomDataSource testSource = (CustomDataSource) builder.build();
assertThat(testSource).isNotSameAs(dataSource);
assertThat(testSource.getUsername()).isEqualTo("alice");
assertThat(testSource.getUrl()).isEqualTo("jdbc:postgresql://localhost:5432/postgres");
assertThat(testSource.getPassword()).isEqualTo("confidential");
}
@Test // gh-27295
void buildWhenDerivedFromCustomTypeWithTypeChange() {
CustomDataSource dataSource = new CustomDataSource(); CustomDataSource dataSource = new CustomDataSource();
dataSource.setUsername("test"); dataSource.setUsername("test");
dataSource.setPassword("secret"); dataSource.setPassword("secret");