Browse Source

Upgrade to R2DBC 1.0.

Removed SQL Server and MariaDB tests for the time being until drivers are available.

Closes #1292
pull/1298/head
Mark Paluch 3 years ago
parent
commit
ee6c2c89b5
No known key found for this signature in database
GPG Key ID: 4406B84C1661DCD1
  1. 54
      spring-data-r2dbc/pom.xml
  2. 10
      spring-data-r2dbc/src/main/asciidoc/preface.adoc
  3. 15
      spring-data-r2dbc/src/main/asciidoc/reference/r2dbc-core.adoc
  4. 2
      spring-data-r2dbc/src/test/java/org/springframework/data/r2dbc/core/SqlServerReactiveDataAccessStrategyTests.java
  5. 5
      spring-data-r2dbc/src/test/java/org/springframework/data/r2dbc/dialect/DialectResolverUnitTests.java
  6. 96
      spring-data-r2dbc/src/test/java/org/springframework/data/r2dbc/repository/MariaDbR2dbcRepositoryIntegrationTests.java
  7. 96
      spring-data-r2dbc/src/test/java/org/springframework/data/r2dbc/repository/MariaDbR2dbcRepositoryWithMixedCaseNamesIntegrationTests.java
  8. 2
      spring-data-r2dbc/src/test/java/org/springframework/data/r2dbc/repository/SqlServerR2dbcRepositoryIntegrationTests.java
  9. 2
      spring-data-r2dbc/src/test/java/org/springframework/data/r2dbc/repository/SqlServerR2dbcRepositoryWithMixedCaseNamesIntegrationTests.java
  10. 161
      spring-data-r2dbc/src/test/java/org/springframework/data/r2dbc/testing/MariaDbTestSupport.java

54
spring-data-r2dbc/pom.xml

@ -27,12 +27,12 @@ @@ -27,12 +27,12 @@
<degraph-check.version>0.1.4</degraph-check.version>
<postgresql.version>42.4.0</postgresql.version>
<mysql.version>8.0.29</mysql.version>
<r2dbc-spi-test.version>0.9.1.RELEASE</r2dbc-spi-test.version>
<mariadb-jdbc.version>2.5.4</mariadb-jdbc.version>
<r2dbc-releasetrain.version>Borca-SR1</r2dbc-releasetrain.version>
<reactive-streams.version>1.0.3</reactive-streams.version>
<netty>4.1.75.Final</netty>
<r2dbc-postgresql.version>1.0.0.RC1</r2dbc-postgresql.version>
<r2dbc-h2.version>1.0.0.RC1</r2dbc-h2.version>
<oracle-r2dbc.version>1.0.0</oracle-r2dbc.version>
<r2dbc-spi.version>1.0.0.RELEASE</r2dbc-spi.version>
<reactive-streams.version>1.0.4</reactive-streams.version>
<netty>4.1.79.Final</netty>
</properties>
<inceptionYear>2018</inceptionYear>
@ -64,13 +64,6 @@ @@ -64,13 +64,6 @@
<dependencyManagement>
<dependencies>
<dependency>
<groupId>io.r2dbc</groupId>
<artifactId>r2dbc-bom</artifactId>
<version>${r2dbc-releasetrain.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>testcontainers-bom</artifactId>
@ -135,6 +128,7 @@ @@ -135,6 +128,7 @@
<dependency>
<groupId>io.r2dbc</groupId>
<artifactId>r2dbc-spi</artifactId>
<version>${r2dbc-spi.version}</version>
</dependency>
<dependency>
@ -189,20 +183,6 @@ @@ -189,20 +183,6 @@
<scope>test</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mariadb.jdbc</groupId>
<artifactId>mariadb-java-client</artifactId>
<version>${mariadb-jdbc.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
@ -213,7 +193,7 @@ @@ -213,7 +193,7 @@
<dependency>
<groupId>com.oracle.database.jdbc</groupId>
<artifactId>ojdbc11</artifactId>
<version>21.6.0.0.1</version>
<version>21.5.0.0</version>
<scope>test</scope>
</dependency>
@ -222,38 +202,28 @@ @@ -222,38 +202,28 @@
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>r2dbc-postgresql</artifactId>
<version>${r2dbc-postgresql.version}</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>io.r2dbc</groupId>
<artifactId>r2dbc-h2</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.r2dbc</groupId>
<artifactId>r2dbc-mssql</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mariadb</groupId>
<artifactId>r2dbc-mariadb</artifactId>
<version>${r2dbc-h2.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.oracle.database.r2dbc</groupId>
<artifactId>oracle-r2dbc</artifactId>
<version>0.4.0</version>
<version>${oracle-r2dbc.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.r2dbc</groupId>
<artifactId>r2dbc-spi-test</artifactId>
<version>${r2dbc-spi-test.version}</version>
<version>${r2dbc-spi.version}</version>
<scope>test</scope>
</dependency>

10
spring-data-r2dbc/src/main/asciidoc/preface.adoc

@ -48,7 +48,7 @@ While the open source ecosystem hosts various non-blocking relational database d @@ -48,7 +48,7 @@ While the open source ecosystem hosts various non-blocking relational database d
[[get-started:first-steps:reactive]]
== What is Reactive?
The term, "`reactive`", refers to programming models that are built around reacting to change, availability, and processability-network components reacting to I/O events, UI controllers reacting to mouse events, resources being made available, and others.
The term, "`reactive`", refers to programming models that are built around reacting to change, availability, and processability-network components reacting to I/O events, UI controllers reacting to mouse events, resources being made available, and others.
In that sense, non-blocking is reactive, because, instead of being blocked, we are now in the mode of reacting to notifications as operations complete or data becomes available.
There is also another important mechanism that we on the Spring team associate with reactive, and that is non-blocking back pressure.
@ -62,7 +62,7 @@ The main purpose of Reactive Streams is to let the subscriber control how quickl @@ -62,7 +62,7 @@ The main purpose of Reactive Streams is to let the subscriber control how quickl
[[get-started:first-steps:reactive-api]]
== Reactive API
Reactive Streams plays an important role for interoperability. It is of interest to libraries and infrastructure components but less useful as an application API, because it is too low-level.
Reactive Streams plays an important role for interoperability.It is of interest to libraries and infrastructure components but less useful as an application API, because it is too low-level.
Applications need a higher-level and richer, functional API to compose async logic—-similar to the Java 8 Stream API but not only for tables.
This is the role that reactive libraries play.
@ -79,9 +79,9 @@ Whenever feasible, Spring Data adapts transparently to the use of RxJava or anot @@ -79,9 +79,9 @@ Whenever feasible, Spring Data adapts transparently to the use of RxJava or anot
[[requirements]]
== Requirements
The Spring Data R2DBC 1.x binaries require:
The Spring Data R2DBC 3.x binaries require:
* JDK level 8.0 and above
* JDK level 17 and above
* https://spring.io/docs[Spring Framework] {springVersion} and above
* https://r2dbc.io[R2DBC] {r2dbcVersion} and above
@ -97,7 +97,7 @@ Community Forum :: Spring Data on https://stackoverflow.com/questions/tagged/spr @@ -97,7 +97,7 @@ Community Forum :: Spring Data on https://stackoverflow.com/questions/tagged/spr
Note that registration is needed only for posting.
[[get-started:help:professional]]
Professional Support :: Professional, from-the-source support, with guaranteed response time, is available from https://pivotal.io/[Pivotal Sofware, Inc.], the company behind Spring Data and Spring.
Professional Support :: Professional, from-the-source support, with guaranteed response time, is available from https://pivotal.io/[Pivotal Software, Inc.], the company behind Spring Data and Spring.
[[get-started:up-to-date]]
== Following Development

15
spring-data-r2dbc/src/main/asciidoc/reference/r2dbc-core.adoc

@ -20,18 +20,6 @@ To do so: @@ -20,18 +20,6 @@ To do so:
====
[source,xml,subs="+attributes"]
----
<dependencyManagement>
<dependencies>
<dependency>
<groupId>io.r2dbc</groupId>
<artifactId>r2dbc-bom</artifactId>
<version>${r2dbc-releasetrain.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<!-- other dependency elements omitted -->
@ -46,7 +34,7 @@ To do so: @@ -46,7 +34,7 @@ To do so:
<dependency>
<groupId>io.r2dbc</groupId>
<artifactId>r2dbc-h2</artifactId>
<version>{r2dbcVersion}</version>
<version>x.y.z</version>
</dependency>
</dependencies>
@ -188,7 +176,6 @@ Spring Data R2DBC ships with dialect implementations for the following drivers: @@ -188,7 +176,6 @@ Spring Data R2DBC ships with dialect implementations for the following drivers:
* https://github.com/r2dbc/r2dbc-h2[H2] (`io.r2dbc:r2dbc-h2`)
* https://github.com/mariadb-corporation/mariadb-connector-r2dbc[MariaDB] (`org.mariadb:r2dbc-mariadb`)
* https://github.com/r2dbc/r2dbc-mssql[Microsoft SQL Server] (`io.r2dbc:r2dbc-mssql`)
* https://github.com/mirromutth/r2dbc-mysql[MySQL] (`dev.miku:r2dbc-mysql`)
* https://github.com/jasync-sql/jasync-sql[jasync-sql MySQL] (`com.github.jasync-sql:jasync-r2dbc-mysql`)
* https://github.com/r2dbc/r2dbc-postgresql[Postgres] (`io.r2dbc:r2dbc-postgresql`)
* https://github.com/oracle/oracle-r2dbc[Oracle] (`com.oracle.database.r2dbc:oracle-r2dbc`)

2
spring-data-r2dbc/src/test/java/org/springframework/data/r2dbc/core/SqlServerReactiveDataAccessStrategyTests.java

@ -15,8 +15,6 @@ @@ -15,8 +15,6 @@
*/
package org.springframework.data.r2dbc.core;
import org.springframework.data.r2dbc.core.DefaultReactiveDataAccessStrategy;
import org.springframework.data.r2dbc.core.ReactiveDataAccessStrategy;
import org.springframework.data.r2dbc.dialect.SqlServerDialect;
/**

5
spring-data-r2dbc/src/test/java/org/springframework/data/r2dbc/dialect/DialectResolverUnitTests.java

@ -4,8 +4,6 @@ import static org.assertj.core.api.Assertions.*; @@ -4,8 +4,6 @@ import static org.assertj.core.api.Assertions.*;
import io.r2dbc.h2.H2ConnectionConfiguration;
import io.r2dbc.h2.H2ConnectionFactory;
import io.r2dbc.mssql.MssqlConnectionConfiguration;
import io.r2dbc.mssql.MssqlConnectionFactory;
import io.r2dbc.postgresql.PostgresqlConnectionConfiguration;
import io.r2dbc.postgresql.PostgresqlConnectionFactory;
import io.r2dbc.spi.Connection;
@ -36,12 +34,9 @@ public class DialectResolverUnitTests { @@ -36,12 +34,9 @@ public class DialectResolverUnitTests {
PostgresqlConnectionFactory postgres = new PostgresqlConnectionFactory(PostgresqlConnectionConfiguration.builder()
.host("localhost").database("foo").username("bar").password("password").build());
MssqlConnectionFactory mssql = new MssqlConnectionFactory(MssqlConnectionConfiguration.builder().host("localhost")
.database("foo").username("bar").password("password").build());
H2ConnectionFactory h2 = new H2ConnectionFactory(H2ConnectionConfiguration.builder().inMemory("mem").build());
assertThat(DialectResolver.getDialect(postgres)).isEqualTo(PostgresDialect.INSTANCE);
assertThat(DialectResolver.getDialect(mssql)).isEqualTo(SqlServerDialect.INSTANCE);
assertThat(DialectResolver.getDialect(h2)).isEqualTo(H2Dialect.INSTANCE);
}

96
spring-data-r2dbc/src/test/java/org/springframework/data/r2dbc/repository/MariaDbR2dbcRepositoryIntegrationTests.java

@ -1,96 +0,0 @@ @@ -1,96 +0,0 @@
/*
* Copyright 2019-2022 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.data.r2dbc.repository;
import io.r2dbc.spi.ConnectionFactory;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import javax.sql.DataSource;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan.Filter;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.data.r2dbc.config.AbstractR2dbcConfiguration;
import org.springframework.data.r2dbc.repository.config.EnableR2dbcRepositories;
import org.springframework.data.r2dbc.repository.support.R2dbcRepositoryFactory;
import org.springframework.data.r2dbc.testing.ExternalDatabase;
import org.springframework.data.r2dbc.testing.MariaDbTestSupport;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit.jupiter.SpringExtension;
/**
* Integration tests for {@link LegoSetRepository} using {@link R2dbcRepositoryFactory} against MariaDB.
*
* @author Mark Paluch
*/
@ExtendWith(SpringExtension.class)
@ContextConfiguration
public class MariaDbR2dbcRepositoryIntegrationTests extends AbstractR2dbcRepositoryIntegrationTests {
@RegisterExtension public static final ExternalDatabase database = MariaDbTestSupport.database();
@Configuration
@EnableR2dbcRepositories(considerNestedRepositories = true,
includeFilters = @Filter(classes = MySqlLegoSetRepository.class, type = FilterType.ASSIGNABLE_TYPE))
static class IntegrationTestConfiguration extends AbstractR2dbcConfiguration {
@Bean
@Override
public ConnectionFactory connectionFactory() {
return MariaDbTestSupport.createConnectionFactory(database);
}
}
@Override
protected DataSource createDataSource() {
return MariaDbTestSupport.createDataSource(database);
}
@Override
protected ConnectionFactory createConnectionFactory() {
return MariaDbTestSupport.createConnectionFactory(database);
}
@Override
protected String getCreateTableStatement() {
return MariaDbTestSupport.CREATE_TABLE_LEGOSET_WITH_ID_GENERATION;
}
@Override
protected Class<? extends LegoSetRepository> getRepositoryInterfaceType() {
return MySqlLegoSetRepository.class;
}
interface MySqlLegoSetRepository extends LegoSetRepository {
@Override
@Query("SELECT name FROM legoset")
Flux<Named> findAsProjection();
@Override
@Query("SELECT * FROM legoset WHERE manual = :manual")
Mono<LegoSet> findByManual(int manual);
@Override
@Query("SELECT id FROM legoset")
Flux<Integer> findAllIds();
}
}

96
spring-data-r2dbc/src/test/java/org/springframework/data/r2dbc/repository/MariaDbR2dbcRepositoryWithMixedCaseNamesIntegrationTests.java

@ -1,96 +0,0 @@ @@ -1,96 +0,0 @@
/*
* Copyright 2021-2022 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.data.r2dbc.repository;
import io.r2dbc.spi.ConnectionFactory;
import java.util.Optional;
import javax.sql.DataSource;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan.Filter;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.data.r2dbc.config.AbstractR2dbcConfiguration;
import org.springframework.data.r2dbc.convert.R2dbcCustomConversions;
import org.springframework.data.r2dbc.mapping.R2dbcMappingContext;
import org.springframework.data.r2dbc.repository.config.EnableR2dbcRepositories;
import org.springframework.data.r2dbc.testing.ExternalDatabase;
import org.springframework.data.r2dbc.testing.MariaDbTestSupport;
import org.springframework.data.relational.RelationalManagedTypes;
import org.springframework.data.relational.core.mapping.NamingStrategy;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit.jupiter.SpringExtension;
/**
* Integration tests for {@link LegoSetRepository} with table and column names that contain upper and lower case
* characters against MariaDb.
*
* @author Jens Schauder
*/
@ExtendWith(SpringExtension.class)
@ContextConfiguration
public class MariaDbR2dbcRepositoryWithMixedCaseNamesIntegrationTests
extends AbstractR2dbcRepositoryWithMixedCaseNamesIntegrationTests {
@RegisterExtension public static final ExternalDatabase database = MariaDbTestSupport.database();
@Configuration
@EnableR2dbcRepositories(considerNestedRepositories = true,
includeFilters = @Filter(classes = { LegoSetRepository.class }, type = FilterType.ASSIGNABLE_TYPE))
static class IntegrationTestConfiguration extends AbstractR2dbcConfiguration {
@Bean
@Override
public ConnectionFactory connectionFactory() {
return MariaDbTestSupport.createConnectionFactory(database);
}
@Override
public R2dbcMappingContext r2dbcMappingContext(Optional<NamingStrategy> namingStrategy,
R2dbcCustomConversions r2dbcCustomConversions, RelationalManagedTypes r2dbcManagedTypes) {
R2dbcMappingContext r2dbcMappingContext = super.r2dbcMappingContext(namingStrategy, r2dbcCustomConversions,
r2dbcManagedTypes);
r2dbcMappingContext.setForceQuote(true);
return r2dbcMappingContext;
}
}
@Override
protected DataSource createDataSource() {
return MariaDbTestSupport.createDataSource(database);
}
@Override
protected ConnectionFactory createConnectionFactory() {
return MariaDbTestSupport.createConnectionFactory(database);
}
@Override
protected String getCreateTableStatement() {
return MariaDbTestSupport.CREATE_TABLE_LEGOSET_WITH_MIXED_CASE_NAMES;
}
@Override
protected String getDropTableStatement() {
return MariaDbTestSupport.DROP_TABLE_LEGOSET_WITH_MIXED_CASE_NAMES;
}
}

2
spring-data-r2dbc/src/test/java/org/springframework/data/r2dbc/repository/SqlServerR2dbcRepositoryIntegrationTests.java

@ -22,6 +22,7 @@ import reactor.core.publisher.Mono; @@ -22,6 +22,7 @@ import reactor.core.publisher.Mono;
import javax.sql.DataSource;
import org.junit.Ignore;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.api.extension.RegisterExtension;
@ -44,6 +45,7 @@ import org.springframework.test.context.junit.jupiter.SpringExtension; @@ -44,6 +45,7 @@ import org.springframework.test.context.junit.jupiter.SpringExtension;
*/
@ExtendWith(SpringExtension.class)
@ContextConfiguration
@Disabled("Requires 1.0 driver")
public class SqlServerR2dbcRepositoryIntegrationTests extends AbstractR2dbcRepositoryIntegrationTests {
@RegisterExtension public static final ExternalDatabase database = SqlServerTestSupport.database();

2
spring-data-r2dbc/src/test/java/org/springframework/data/r2dbc/repository/SqlServerR2dbcRepositoryWithMixedCaseNamesIntegrationTests.java

@ -21,6 +21,7 @@ import java.util.Optional; @@ -21,6 +21,7 @@ import java.util.Optional;
import javax.sql.DataSource;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.api.extension.RegisterExtension;
@ -47,6 +48,7 @@ import org.springframework.test.context.junit.jupiter.SpringExtension; @@ -47,6 +48,7 @@ import org.springframework.test.context.junit.jupiter.SpringExtension;
*/
@ExtendWith(SpringExtension.class)
@ContextConfiguration
@Disabled("Requires 1.0 driver")
public class SqlServerR2dbcRepositoryWithMixedCaseNamesIntegrationTests
extends AbstractR2dbcRepositoryWithMixedCaseNamesIntegrationTests {

161
spring-data-r2dbc/src/test/java/org/springframework/data/r2dbc/testing/MariaDbTestSupport.java

@ -1,161 +0,0 @@ @@ -1,161 +0,0 @@
/*
* Copyright 2019-2022 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.data.r2dbc.testing;
import io.r2dbc.spi.ConnectionFactory;
import io.r2dbc.spi.ConnectionFactoryOptions;
import lombok.SneakyThrows;
import java.util.function.Supplier;
import java.util.stream.Stream;
import javax.sql.DataSource;
import org.mariadb.jdbc.MariaDbDataSource;
import org.mariadb.r2dbc.MariadbConnectionFactoryProvider;
import org.springframework.data.r2dbc.testing.ExternalDatabase.ProvidedDatabase;
import org.testcontainers.containers.MariaDBContainer;
/**
* Utility class for testing against MariaDB.
*
* @author Mark Paluch
* @author Jens Schauder
*/
public class MariaDbTestSupport {
private static ExternalDatabase testContainerDatabase;
public static final String CREATE_TABLE_LEGOSET = "CREATE TABLE legoset (\n" //
+ " id integer PRIMARY KEY,\n" //
+ " name varchar(255) NOT NULL,\n" //
+ " manual integer NULL\n," //
+ " cert varbinary(255) NULL\n" //
+ ") ENGINE=InnoDB;";
public static final String CREATE_TABLE_LEGOSET_WITH_ID_GENERATION = "CREATE TABLE legoset (\n" //
+ " id integer AUTO_INCREMENT PRIMARY KEY,\n" //
+ " name varchar(255) NOT NULL,\n" //
+ " flag boolean NOT NULL,\n" //
+ " manual integer NULL\n" //
+ ") ENGINE=InnoDB;";
public static final String CREATE_TABLE_LEGOSET_WITH_MIXED_CASE_NAMES = "CREATE TABLE `LegoSet` (\n" //
+ " `Id` integer AUTO_INCREMENT PRIMARY KEY,\n" //
+ " `Name` varchar(255) NOT NULL,\n" //
+ " `Manual` integer NULL\n" //
+ ") ENGINE=InnoDB;";
public static final String DROP_TABLE_LEGOSET_WITH_MIXED_CASE_NAMES = "DROP TABLE `LegoSet`";
/**
* Returns a database either hosted locally at {@code localhost:3306/mysql} or running inside Docker.
*
* @return information about the database. Guaranteed to be not {@literal null}.
*/
public static ExternalDatabase database() {
if (Boolean.getBoolean("spring.data.r2dbc.test.preferLocalDatabase")) {
return getFirstWorkingDatabase( //
MariaDbTestSupport::local, //
MariaDbTestSupport::testContainer //
);
} else {
return getFirstWorkingDatabase( //
MariaDbTestSupport::testContainer, //
MariaDbTestSupport::local //
);
}
}
@SafeVarargs
private static ExternalDatabase getFirstWorkingDatabase(Supplier<ExternalDatabase>... suppliers) {
return Stream.of(suppliers).map(Supplier::get) //
.filter(ExternalDatabase::checkValidity) //
.findFirst() //
.orElse(ExternalDatabase.unavailable());
}
/**
* Returns a locally provided database .
*/
private static ExternalDatabase local() {
return ProvidedDatabase.builder() //
.hostname("localhost") //
.port(3306) //
.database("mysql") //
.username("root") //
.password("my-secret-pw") //
.jdbcUrl("jdbc:mariadb://localhost:3306/mysql") //
.build();
}
/**
* Returns a database provided via Testcontainers.
*/
private static ExternalDatabase testContainer() {
if (testContainerDatabase == null) {
try {
MariaDBContainer container = new MariaDBContainer("mariadb:10.8.3");
container.start();
testContainerDatabase = ProvidedDatabase.builder(container) //
.username("root") //
.database(container.getDatabaseName()) //
.build();
} catch (IllegalStateException ise) {
// docker not available.
testContainerDatabase = ExternalDatabase.unavailable();
}
}
return testContainerDatabase;
}
/**
* Creates a new R2DBC MariaDB {@link ConnectionFactory} configured from the {@link ExternalDatabase}.
*/
public static ConnectionFactory createConnectionFactory(ExternalDatabase database) {
ConnectionFactoryOptions options = ConnectionUtils.createOptions("mariadb", database);
return new MariadbConnectionFactoryProvider().create(options);
}
/**
* Creates a new {@link DataSource} configured from the {@link ExternalDatabase}.
*/
@SneakyThrows
public static DataSource createDataSource(ExternalDatabase database) {
MariaDbDataSource dataSource = new MariaDbDataSource();
dataSource.setUser(database.getUsername());
dataSource.setPassword(database.getPassword());
dataSource.setDatabaseName(database.getDatabase());
dataSource.setServerName(database.getHostname());
dataSource.setPort(database.getPort());
return dataSource;
}
}
Loading…
Cancel
Save