Skip to content

Commit 7225d0e

Browse files
committed
Merge pull request #27453 from saraswathy-krish
* gh-27453: Polish "Fix deriving DataSources from custom type" Fix deriving DataSources from custom type Closes gh-27453
2 parents 44a9531 + 18b4898 commit 7225d0e

File tree

2 files changed

+31
-3
lines changed

2 files changed

+31
-3
lines changed

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

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

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

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

291-
private Method extracted(String prefix, Class<?> type) {
291+
private Method extracted(String prefix, Class<?> type, Class<?>... paramTypes) {
292292
for (String candidate : this.names) {
293293
Method method = ReflectionUtils.findMethod(type, prefix + StringUtils.capitalize(candidate),
294-
String.class);
294+
paramTypes);
295295
if (method != null) {
296296
return method;
297297
}

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

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

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() {
351+
CustomDataSource dataSource = new CustomDataSource();
352+
dataSource.setUsername("test");
353+
dataSource.setPassword("secret");
354+
dataSource.setUrl("jdbc:postgresql://localhost:5432/postgres");
355+
DataSourceBuilder<?> builder = DataSourceBuilder.derivedFrom(dataSource).type(SimpleDriverDataSource.class);
356+
SimpleDriverDataSource testSource = (SimpleDriverDataSource) builder.build();
357+
assertThat(testSource.getUsername()).isEqualTo("test");
358+
assertThat(testSource.getUrl()).isEqualTo("jdbc:postgresql://localhost:5432/postgres");
359+
assertThat(testSource.getPassword()).isEqualTo("secret");
360+
}
361+
334362
final class HidePackagesClassLoader extends URLClassLoader {
335363

336364
private final String[] hiddenPackages;

0 commit comments

Comments
 (0)