Avoid NPE in SimpleDataSourceProperties when driver is null

See gh-45976

Signed-off-by: chanbinme <gksmfcksqls@gmail.com>
This commit is contained in:
chanbinme
2025-06-17 14:38:50 +02:00
committed by Stéphane Nicoll
parent 5a2a6dbe00
commit 1cf0d4e7fe
2 changed files with 15 additions and 1 deletions
@@ -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 ? null : dataSource.getDriver().getClass(),
SimpleDriverDataSource::setDriverClass);
add(DataSourceProperty.USERNAME, SimpleDriverDataSource::getUsername, SimpleDriverDataSource::setUsername);
add(DataSourceProperty.PASSWORD, SimpleDriverDataSource::getPassword, SimpleDriverDataSource::setPassword);
@@ -357,6 +357,16 @@ class DataSourceBuilderTests {
.isThrownBy(() -> DataSourceBuilder.derivedFrom(dataSource).url("example.org").build());
}
@Test
void buildWhenDerivedFromSimpleDriverDataSourceWithDriverNotSetSucceeds() throws Exception {
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();