Browse Source

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
pull/28375/head
Andy Wilkinson 5 years ago
parent
commit
7225d0e54b
  1. 6
      spring-boot-project/spring-boot/src/main/java/org/springframework/boot/jdbc/DataSourceBuilder.java
  2. 28
      spring-boot-project/spring-boot/src/test/java/org/springframework/boot/jdbc/DataSourceBuilderTests.java

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

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

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

@ -331,6 +331,34 @@ class DataSourceBuilderTests { @@ -331,6 +331,34 @@ class DataSourceBuilderTests {
assertThat(built.getUrl()).isEqualTo("jdbc:postgresql://localhost:5432/postgres");
}
@Test // gh-27295
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();
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;

Loading…
Cancel
Save