Polish "Fix deriving DataSources from custom type"
See gh-27453
This commit is contained in:
parent
d0e2823c49
commit
18b4898977
|
|
@ -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;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -332,7 +332,22 @@ class DataSourceBuilderTests {
|
||||||
}
|
}
|
||||||
|
|
||||||
@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");
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue