mirror of
https://github.com/spring-projects/spring-boot.git
synced 2026-09-17 12:09:16 +00:00
Merge pull request #45976 from chanbinme
* pr/45976: Polish "Avoid NPE in SimpleDataSourceProperties when driver is null" Avoid NPE in SimpleDataSourceProperties when driver is null Closes gh-45976
This commit is contained in:
+5
-1
@@ -513,6 +513,9 @@ public final class DataSourceBuilder<T extends DataSource> {
|
||||
}
|
||||
|
||||
private String convertToString(V value) {
|
||||
if (value == null) {
|
||||
return null;
|
||||
}
|
||||
if (String.class.equals(this.type)) {
|
||||
return (String) value;
|
||||
}
|
||||
@@ -705,7 +708,8 @@ public final class DataSourceBuilder<T extends DataSource> {
|
||||
@SuppressWarnings("unchecked")
|
||||
SimpleDataSourceProperties() {
|
||||
add(DataSourceProperty.URL, SimpleDriverDataSource::getUrl, SimpleDriverDataSource::setUrl);
|
||||
add(DataSourceProperty.DRIVER_CLASS_NAME, Class.class, (dataSource) -> dataSource.getDriver().getClass(),
|
||||
add(DataSourceProperty.DRIVER_CLASS_NAME, Class.class,
|
||||
(dataSource) -> (dataSource.getDriver() != null) ? dataSource.getDriver().getClass() : null,
|
||||
SimpleDriverDataSource::setDriverClass);
|
||||
add(DataSourceProperty.USERNAME, SimpleDriverDataSource::getUsername, SimpleDriverDataSource::setUsername);
|
||||
add(DataSourceProperty.PASSWORD, SimpleDriverDataSource::getPassword, SimpleDriverDataSource::setPassword);
|
||||
|
||||
+10
@@ -357,6 +357,16 @@ class DataSourceBuilderTests {
|
||||
.isThrownBy(() -> DataSourceBuilder.derivedFrom(dataSource).url("example.org").build());
|
||||
}
|
||||
|
||||
@Test
|
||||
void buildWhenDerivedFromSimpleDriverDataSourceAndDriverNotSetBuilds() {
|
||||
SimpleDriverDataSource dataSource = new SimpleDriverDataSource();
|
||||
dataSource.setUsername("test");
|
||||
dataSource.setPassword("secret");
|
||||
dataSource.setUrl("jdbc:postgresql://localhost:5432/postgres");
|
||||
assertThatNoException()
|
||||
.isThrownBy(() -> DataSourceBuilder.derivedFrom(dataSource).type(SimpleDriverDataSource.class).build());
|
||||
}
|
||||
|
||||
@Test
|
||||
void buildWhenDerivedFromOracleDataSourceWithPasswordSetReturnsDataSource() throws Exception {
|
||||
oracle.jdbc.datasource.impl.OracleDataSource dataSource = new oracle.jdbc.datasource.impl.OracleDataSource();
|
||||
|
||||
Reference in New Issue
Block a user