Skip to content

Commit

Permalink
Use driver class name from JdbcConnectionDetails
Browse files Browse the repository at this point in the history
Fixes gh-34777
  • Loading branch information
wilkinsona committed Mar 27, 2023
1 parent b91f814 commit d69335d
Show file tree
Hide file tree
Showing 11 changed files with 49 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,17 +49,18 @@
abstract class DataSourceConfiguration {

@SuppressWarnings("unchecked")
protected static <T> T createDataSource(DataSourceProperties properties, Class<? extends DataSource> type) {
private static <T> T createDataSource(DataSourceProperties properties, Class<? extends DataSource> type) {
return (T) properties.initializeDataSourceBuilder().type(type).build();
}

@SuppressWarnings("unchecked")
protected static <T> T createDataSource(JdbcConnectionDetails connectionDetails, Class<? extends DataSource> type,
private static <T> T createDataSource(JdbcConnectionDetails connectionDetails, Class<? extends DataSource> type,
ClassLoader classLoader) {
return (T) DataSourceBuilder.create(classLoader)
.url(connectionDetails.getJdbcUrl())
.username(connectionDetails.getUsername())
.password(connectionDetails.getPassword())
.driverClassName(connectionDetails.getDriverClassName())
.type(type)
.build();
}
Expand Down Expand Up @@ -213,12 +214,7 @@ DataSource dataSource(DataSourceProperties properties,
ObjectProvider<JdbcConnectionDetails> connectionDetailsProvider) {
JdbcConnectionDetails connectionDetails = connectionDetailsProvider.getIfAvailable();
if (connectionDetails != null) {
return DataSourceBuilder.create(properties.getClassLoader())
.url(connectionDetails.getJdbcUrl())
.username(connectionDetails.getUsername())
.password(connectionDetails.getPassword())
.type(properties.getType())
.build();
return createDataSource(connectionDetails, properties.getType(), properties.getClassLoader());
}
return properties.initializeDataSourceBuilder().build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -261,8 +261,8 @@ void dbcp2UsesJdbcConnectionDetailsIfAvailable() {
.satisfies((dbcp2) -> {
assertThat(dbcp2.getUsername()).isEqualTo("user-1");
assertThat(dbcp2.getPassword()).isEqualTo("password-1");
assertThat(dbcp2.getDriverClassName()).isEqualTo("org.postgresql.Driver");
assertThat(dbcp2.getUrl()).isEqualTo("jdbc:postgresql://postgres.example.com:12345/database-1");
assertThat(dbcp2.getDriverClassName()).isEqualTo(DatabaseDriver.POSTGRESQL.getDriverClassName());
assertThat(dbcp2.getUrl()).isEqualTo("jdbc:customdb://customdb.example.com:12345/database-1");
});
});
}
Expand All @@ -278,8 +278,9 @@ void genericUsesJdbcConnectionDetailsIfAvailable() {
TestDataSource source = (TestDataSource) dataSource;
assertThat(source.getUsername()).isEqualTo("user-1");
assertThat(source.getPassword()).isEqualTo("password-1");
assertThat(source.getDriver().getClass().getName()).isEqualTo("org.postgresql.Driver");
assertThat(source.getUrl()).isEqualTo("jdbc:postgresql://postgres.example.com:12345/database-1");
assertThat(source.getDriver().getClass().getName())
.isEqualTo(DatabaseDriver.POSTGRESQL.getDriverClassName());
assertThat(source.getUrl()).isEqualTo("jdbc:customdb://customdb.example.com:12345/database-1");
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,15 @@
class Dbcp2JdbcConnectionDetailsBeanPostProcessorTests {

@Test
void setUsernamePasswordAndUrl() {
void setUsernamePasswordUrlAndDriverClassName() {
BasicDataSource dataSource = new BasicDataSource();
dataSource.setUrl("will-be-overwritten");
dataSource.setUsername("will-be-overwritten");
dataSource.setPassword("will-be-overwritten");
dataSource.setDriverClassName("will-be-overwritten");
new Dbcp2JdbcConnectionDetailsBeanPostProcessor(null).processDataSource(dataSource,
new TestJdbcConnectionDetails());
assertThat(dataSource.getUrl()).isEqualTo("jdbc:postgresql://postgres.example.com:12345/database-1");
assertThat(dataSource.getUrl()).isEqualTo("jdbc:customdb://customdb.example.com:12345/database-1");
assertThat(dataSource.getUsername()).isEqualTo("user-1");
assertThat(dataSource.getPassword()).isEqualTo("password-1");
assertThat(dataSource.getDriverClassName()).isEqualTo(DatabaseDriver.POSTGRESQL.getDriverClassName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ void usesConnectionDetailsIfAvailable() {
assertThat(hikari.getPassword()).isEqualTo("password-1");
assertThat(hikari.getDriverClassName()).isEqualTo("org.postgresql.Driver");
assertThat(hikari.getJdbcUrl())
.isEqualTo("jdbc:postgresql://postgres.example.com:12345/database-1");
.isEqualTo("jdbc:customdb://customdb.example.com:12345/database-1");
});
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ void setUsernamePasswordAndUrl() {
dataSource.setDriverClassName(DatabaseDriver.H2.getDriverClassName());
new HikariJdbcConnectionDetailsBeanPostProcessor(null).processDataSource(dataSource,
new TestJdbcConnectionDetails());
assertThat(dataSource.getJdbcUrl()).isEqualTo("jdbc:postgresql://postgres.example.com:12345/database-1");
assertThat(dataSource.getJdbcUrl()).isEqualTo("jdbc:customdb://customdb.example.com:12345/database-1");
assertThat(dataSource.getUsername()).isEqualTo("user-1");
assertThat(dataSource.getPassword()).isEqualTo("password-1");
assertThat(dataSource.getDriverClassName()).isEqualTo(DatabaseDriver.POSTGRESQL.getDriverClassName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.junit.jupiter.api.Test;

import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.jdbc.DatabaseDriver;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
Expand Down Expand Up @@ -124,8 +125,9 @@ void usesJdbcConnectionDetailsIfAvailable() {
assertThat(oracleUcp).extracting("password")
.extracting((o) -> ((OpaqueString) o).get())
.isEqualTo("password-1");
assertThat(oracleUcp.getConnectionFactoryClassName()).isEqualTo("org.postgresql.Driver");
assertThat(oracleUcp.getURL()).isEqualTo("jdbc:postgresql://postgres.example.com:12345/database-1");
assertThat(oracleUcp.getConnectionFactoryClassName())
.isEqualTo(DatabaseDriver.POSTGRESQL.getDriverClassName());
assertThat(oracleUcp.getURL()).isEqualTo("jdbc:customdb://customdb.example.com:12345/database-1");
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,15 @@
class OracleUcpJdbcConnectionDetailsBeanPostProcessorTests {

@Test
void setUsernamePasswordAndUrl() throws SQLException {
void setUsernamePasswordUrlAndDriverClassName() throws SQLException {
PoolDataSourceImpl dataSource = new PoolDataSourceImpl();
dataSource.setURL("will-be-overwritten");
dataSource.setUser("will-be-overwritten");
dataSource.setPassword("will-be-overwritten");
dataSource.setConnectionFactoryClassName("will-be-overwritten");
new OracleUcpJdbcConnectionDetailsBeanPostProcessor(null).processDataSource(dataSource,
new TestJdbcConnectionDetails());
assertThat(dataSource.getURL()).isEqualTo("jdbc:postgresql://postgres.example.com:12345/database-1");
assertThat(dataSource.getURL()).isEqualTo("jdbc:customdb://customdb.example.com:12345/database-1");
assertThat(dataSource.getUser()).isEqualTo("user-1");
assertThat(dataSource).extracting("password")
.extracting((password) -> ((OpaqueString) password).get())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package org.springframework.boot.autoconfigure.jdbc;

import org.springframework.boot.jdbc.DatabaseDriver;

/**
* {@link JdbcConnectionDetails} used in tests.
*
Expand All @@ -25,7 +27,7 @@ class TestJdbcConnectionDetails implements JdbcConnectionDetails {

@Override
public String getJdbcUrl() {
return "jdbc:postgresql://postgres.example.com:12345/database-1";
return "jdbc:customdb://customdb.example.com:12345/database-1";
}

@Override
Expand All @@ -38,4 +40,14 @@ public String getPassword() {
return "password-1";
}

@Override
public String getDriverClassName() {
return DatabaseDriver.POSTGRESQL.getDriverClassName();
}

@Override
public String getXaDataSourceClassName() {
return DatabaseDriver.POSTGRESQL.getXaDataSourceClassName();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.boot.jdbc.DatabaseDriver;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.boot.test.util.TestPropertyValues;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
Expand Down Expand Up @@ -125,9 +126,10 @@ void usesJdbcConnectionDetailsIfAvailable() {
org.apache.tomcat.jdbc.pool.DataSource tomcat = (org.apache.tomcat.jdbc.pool.DataSource) dataSource;
assertThat(tomcat.getPoolProperties().getUsername()).isEqualTo("user-1");
assertThat(tomcat.getPoolProperties().getPassword()).isEqualTo("password-1");
assertThat(tomcat.getPoolProperties().getDriverClassName()).isEqualTo("org.postgresql.Driver");
assertThat(tomcat.getPoolProperties().getDriverClassName())
.isEqualTo(DatabaseDriver.POSTGRESQL.getDriverClassName());
assertThat(tomcat.getPoolProperties().getUrl())
.isEqualTo("jdbc:postgresql://postgres.example.com:12345/database-1");
.isEqualTo("jdbc:customdb://customdb.example.com:12345/database-1");
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,15 @@
class TomcatJdbcConnectionDetailsBeanPostProcessorTests {

@Test
void setUsernamePasswordAndUrl() {
void setUsernamePasswordUrlAndDriverClassName() {
DataSource dataSource = new DataSource();
dataSource.setUrl("will-be-overwritten");
dataSource.setUsername("will-be-overwritten");
dataSource.setPassword("will-be-overwritten");
dataSource.setDriverClassName("will-be-overwritten");
new TomcatJdbcConnectionDetailsBeanPostProcessor(null).processDataSource(dataSource,
new TestJdbcConnectionDetails());
assertThat(dataSource.getUrl()).isEqualTo("jdbc:postgresql://postgres.example.com:12345/database-1");
assertThat(dataSource.getUrl()).isEqualTo("jdbc:customdb://customdb.example.com:12345/database-1");
assertThat(dataSource.getUsername()).isEqualTo("user-1");
assertThat(dataSource.getPoolProperties().getPassword()).isEqualTo("password-1");
assertThat(dataSource.getPoolProperties().getDriverClassName())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.postgresql.xa.PGXADataSource;

import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.jdbc.DatabaseDriver;
import org.springframework.boot.jdbc.XADataSourceWrapper;
import org.springframework.boot.test.context.FilteredClassLoader;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
Expand All @@ -35,6 +36,7 @@
import org.springframework.context.annotation.Configuration;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;

/**
Expand Down Expand Up @@ -95,8 +97,16 @@ void createFromClass() throws Exception {

@Test
void shouldUseConnectionDetailsIfAvailable() {
JdbcConnectionDetails connectionDetails = mock(JdbcConnectionDetails.class);
given(connectionDetails.getUsername()).willReturn("user-1");
given(connectionDetails.getPassword()).willReturn("password-1");
given(connectionDetails.getJdbcUrl()).willReturn("jdbc:postgresql://postgres.example.com:12345/database-1");
given(connectionDetails.getDriverClassName()).willReturn(DatabaseDriver.POSTGRESQL.getDriverClassName());
given(connectionDetails.getXaDataSourceClassName())
.willReturn(DatabaseDriver.POSTGRESQL.getXaDataSourceClassName());
new ApplicationContextRunner().withConfiguration(AutoConfigurations.of(XADataSourceAutoConfiguration.class))
.withUserConfiguration(FromProperties.class, JdbcConnectionDetailsConfiguration.class)
.withUserConfiguration(FromProperties.class)
.withBean(JdbcConnectionDetails.class, () -> connectionDetails)
.run((context) -> {
MockXADataSourceWrapper wrapper = context.getBean(MockXADataSourceWrapper.class);
PGXADataSource dataSource = (PGXADataSource) wrapper.getXaDataSource();
Expand Down

0 comments on commit d69335d

Please sign in to comment.