Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make DataSourceBuilder be able to derive driverClassName from derived url #39376

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -186,8 +186,8 @@ public T build() {
}
if (!applied.contains(DataSourceProperty.DRIVER_CLASS_NAME)
&& properties.canSet(DataSourceProperty.DRIVER_CLASS_NAME)
&& this.values.containsKey(DataSourceProperty.URL)) {
String url = this.values.get(DataSourceProperty.URL);
&& applied.contains(DataSourceProperty.URL)) {
String url = properties.get(dataSource, DataSourceProperty.URL);
DatabaseDriver driver = DatabaseDriver.fromJdbcUrl(url);
properties.set(dataSource, DataSourceProperty.DRIVER_CLASS_NAME, driver.getDriverClassName());
}
Expand Down
Expand Up @@ -457,6 +457,20 @@ void buildWhenDerivedFromCustomTypeWithTypeChange() {
assertThat(testSource.getPassword()).isEqualTo("secret");
}

@Test
void buildWhenDerivedFromCustomTypeDeriveDriverClassNameFromDerivedUrl() {
UrlCapableLimitedCustomDataSource dataSource = new UrlCapableLimitedCustomDataSource();
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");
assertThat(testSource.getDriver()).isInstanceOf(org.postgresql.Driver.class);
}

@Test // gh-31920
void buildWhenC3P0TypeSpecifiedReturnsExpectedDataSource() {
this.dataSource = DataSourceBuilder.create()
Expand Down Expand Up @@ -620,12 +634,10 @@ void setPassword(String password) {

}

static class CustomDataSource extends LimitedCustomDataSource {
static class UrlCapableLimitedCustomDataSource extends LimitedCustomDataSource {

private String url;

private String driverClassName;

String getUrl() {
return this.url;
}
Expand All @@ -634,6 +646,13 @@ void setUrl(String url) {
this.url = url;
}

}

static class CustomDataSource extends UrlCapableLimitedCustomDataSource {

private String driverClassName;


String getDriverClassName() {
return this.driverClassName;
}
Expand Down