Skip to content

Commit 18b4898

Browse files
committed
Polish "Fix deriving DataSources from custom type"
See gh-27453
1 parent d0e2823 commit 18b4898

File tree

2 files changed

+22
-12
lines changed

2 files changed

+22
-12
lines changed

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/jdbc/DataSourceBuilder.java

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -281,22 +281,17 @@ public String toString() {
281281
}
282282

283283
Method findSetter(Class<?> type) {
284-
return extracted("set", type, true);
284+
return extracted("set", type, String.class);
285285
}
286286

287287
Method findGetter(Class<?> type) {
288-
return extracted("get", type, false);
288+
return extracted("get", type);
289289
}
290290

291-
private Method extracted(String prefix, Class<?> type, boolean hasParameter) {
291+
private Method extracted(String prefix, Class<?> type, Class<?>... paramTypes) {
292292
for (String candidate : this.names) {
293-
Method method;
294-
if (hasParameter) {
295-
method = ReflectionUtils.findMethod(type, prefix + StringUtils.capitalize(candidate), String.class);
296-
}
297-
else {
298-
method = ReflectionUtils.findMethod(type, prefix + StringUtils.capitalize(candidate));
299-
}
293+
Method method = ReflectionUtils.findMethod(type, prefix + StringUtils.capitalize(candidate),
294+
paramTypes);
300295
if (method != null) {
301296
return method;
302297
}

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/jdbc/DataSourceBuilderTests.java

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -331,8 +331,23 @@ void buildWhenDerivedFromExistingDatabaseWithTypeChange() {
331331
assertThat(built.getUrl()).isEqualTo("jdbc:postgresql://localhost:5432/postgres");
332332
}
333333

334-
@Test // gh -27295
335-
void buildWhenDerivedFromCustomTypeSpecifiedReturnsDataSource() {
334+
@Test // gh-27295
335+
void buildWhenDerivedFromCustomType() {
336+
CustomDataSource dataSource = new CustomDataSource();
337+
dataSource.setUsername("test");
338+
dataSource.setPassword("secret");
339+
dataSource.setUrl("jdbc:postgresql://localhost:5432/postgres");
340+
DataSourceBuilder<?> builder = DataSourceBuilder.derivedFrom(dataSource).username("alice")
341+
.password("confidential");
342+
CustomDataSource testSource = (CustomDataSource) builder.build();
343+
assertThat(testSource).isNotSameAs(dataSource);
344+
assertThat(testSource.getUsername()).isEqualTo("alice");
345+
assertThat(testSource.getUrl()).isEqualTo("jdbc:postgresql://localhost:5432/postgres");
346+
assertThat(testSource.getPassword()).isEqualTo("confidential");
347+
}
348+
349+
@Test // gh-27295
350+
void buildWhenDerivedFromCustomTypeWithTypeChange() {
336351
CustomDataSource dataSource = new CustomDataSource();
337352
dataSource.setUsername("test");
338353
dataSource.setPassword("secret");

0 commit comments

Comments
 (0)