Commit 85f1e2c9 authored by Phillip Webb's avatar Phillip Webb

Refactor DataSourceBuilder and add derivedFrom

Refactor `DataSourceBuilder` to use direct property mappers rather than
the `Binder` and aliases. Supported DataSource types now include two-way
mappers which allows us to both get and set properties in a uniform way.

A new `derivedFrom` factory method has been added which allows a new
`DataSource` to be derived from an existing one. This update is
primarily to allow Flyway and Liquibase migrations to work against a
`@Bean` configured DataSource rather than assuming that the primary
DataSource was always created via auto-configuration.

See gh-25643
parent 6e92daa0
...@@ -20,8 +20,10 @@ dependencies { ...@@ -20,8 +20,10 @@ dependencies {
optional("com.atomikos:transactions-jms") optional("com.atomikos:transactions-jms")
optional("com.atomikos:transactions-jta") optional("com.atomikos:transactions-jta")
optional("com.fasterxml.jackson.core:jackson-databind") optional("com.fasterxml.jackson.core:jackson-databind")
optional("com.h2database:h2")
optional("com.google.code.gson:gson") optional("com.google.code.gson:gson")
optional("com.oracle.database.jdbc:ucp") optional("com.oracle.database.jdbc:ucp")
optional("com.oracle.database.jdbc:ojdbc8")
optional("com.samskivert:jmustache") optional("com.samskivert:jmustache")
optional("com.zaxxer:HikariCP") optional("com.zaxxer:HikariCP")
optional("io.netty:netty-tcnative-boringssl-static") optional("io.netty:netty-tcnative-boringssl-static")
...@@ -58,6 +60,7 @@ dependencies { ...@@ -58,6 +60,7 @@ dependencies {
optional("org.jboss:jboss-transaction-spi") optional("org.jboss:jboss-transaction-spi")
optional("org.jooq:jooq") optional("org.jooq:jooq")
optional("org.liquibase:liquibase-core") optional("org.liquibase:liquibase-core")
optional("org.postgresql:postgresql")
optional("org.slf4j:jul-to-slf4j") optional("org.slf4j:jul-to-slf4j")
optional("org.slf4j:slf4j-api") optional("org.slf4j:slf4j-api")
optional("org.springframework:spring-messaging") optional("org.springframework:spring-messaging")
...@@ -76,11 +79,9 @@ dependencies { ...@@ -76,11 +79,9 @@ dependencies {
testImplementation(project(":spring-boot-project:spring-boot-tools:spring-boot-test-support")) testImplementation(project(":spring-boot-project:spring-boot-tools:spring-boot-test-support"))
testImplementation("com.google.appengine:appengine-api-1.0-sdk") testImplementation("com.google.appengine:appengine-api-1.0-sdk")
testImplementation("com.h2database:h2")
testImplementation("com.ibm.db2:jcc") testImplementation("com.ibm.db2:jcc")
testImplementation("com.jayway.jsonpath:json-path") testImplementation("com.jayway.jsonpath:json-path")
testImplementation("com.microsoft.sqlserver:mssql-jdbc") testImplementation("com.microsoft.sqlserver:mssql-jdbc")
testImplementation("com.oracle.database.jdbc:ojdbc8")
testImplementation("com.squareup.okhttp3:okhttp") testImplementation("com.squareup.okhttp3:okhttp")
testImplementation("com.sun.xml.messaging.saaj:saaj-impl") testImplementation("com.sun.xml.messaging.saaj:saaj-impl")
testImplementation("io.projectreactor:reactor-test") testImplementation("io.projectreactor:reactor-test")
...@@ -97,7 +98,6 @@ dependencies { ...@@ -97,7 +98,6 @@ dependencies {
testImplementation("org.mariadb.jdbc:mariadb-java-client") testImplementation("org.mariadb.jdbc:mariadb-java-client")
testImplementation("org.mockito:mockito-core") testImplementation("org.mockito:mockito-core")
testImplementation("org.mockito:mockito-junit-jupiter") testImplementation("org.mockito:mockito-junit-jupiter")
testImplementation("org.postgresql:postgresql")
testImplementation("org.springframework:spring-context-support") testImplementation("org.springframework:spring-context-support")
testImplementation("org.springframework.data:spring-data-redis") testImplementation("org.springframework.data:spring-data-redis")
testImplementation("org.springframework.data:spring-data-r2dbc") testImplementation("org.springframework.data:spring-data-r2dbc")
......
/*
* Copyright 2012-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.jdbc;
import java.util.function.Supplier;
/**
* {@link RuntimeException} thrown from {@link DataSourceBuilder} when an unsupported
* property is used.
*
* @author Phillip Webb
* @since 2.5.0
*/
public class UnsupportedDataSourcePropertyException extends RuntimeException {
UnsupportedDataSourcePropertyException(String message) {
super(message);
}
static void throwIf(boolean test, Supplier<String> message) {
if (test) {
throw new UnsupportedDataSourcePropertyException(message.get());
}
}
}
/*
* Copyright 2012-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.jdbc;
import javax.sql.DataSource;
import org.junit.jupiter.api.Test;
import org.springframework.boot.testsupport.classpath.ClassPathExclusions;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Tests for {@link DataSourceBuilder} when Hikari is not on the classpath.
*
* @author Phillip Webb
*/
@ClassPathExclusions("Hikari*.jar")
class DataSourceBuilderNoHikariTests {
@Test
void findTypeReturnsTomcatDataSource() {
assertThat(DataSourceBuilder.findType(null)).isEqualTo(org.apache.tomcat.jdbc.pool.DataSource.class);
}
@Test
void createAndBuildReturnsTomcatDataSource() {
DataSource dataSource = DataSourceBuilder.create().build();
assertThat(dataSource).isInstanceOf(org.apache.tomcat.jdbc.pool.DataSource.class);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment