-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This PR adds support for [R2DBC](https://r2dbc.io). ## URL support The URL support is similar to the JDBC's, but: 1. Does not reuse containers by URL - it is unclear yet how to "hash" `ConnectionFactoryOptions` 1. The image tag must be explicitly provided via `TC_IMAGE_TAG` r2dbc option 1. TODO: daemon mode 1. TODO: alias support (see #4) ## Example usage The user **must add the `org.testcontainers:r2dbc` module** - unlike JDBC, we use `compileOnly` dependency on R2DBC, since it is a 3rd party dependency. Once both the `r2dbc` and database's module are added, one can use: ```java String url = "r2dbc:tc:postgresql:///db?TC_IMAGE_TAG=10-alpine"; ConnectionFactory connectionFactory = ConnectionFactories.get(url); ``` ## Programmatic access For those who start containers manually and want to obtain `ConnectionFactoryOptions` for already started containers, we provide a helper static method: ```java try (PostgreSQLContainer<?> container = new PostgreSQLContainer<>()) { container.start(); ConnectionFactory connectionFactory = ConnectionFactories.get( PostgreSQLR2DBCDatabaseContainer.getOptions(container) ); } ```
- Loading branch information
Showing
37 changed files
with
1,212 additions
and
113 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
# JDBC support | ||
|
||
You can obtain a temporary database in one of two ways: | ||
|
||
* **Using a specially modified JDBC URL**: after making a very simple modification to your system's JDBC URL string, Testcontainers will provide a disposable stand-in database that can be used without requiring modification to your application code. | ||
* **JUnit @Rule/@ClassRule**: this mode starts a database inside a container before your tests and tears it down afterwards. | ||
|
||
## Database containers launched via JDBC URL scheme | ||
|
||
As long as you have Testcontainers and the appropriate JDBC driver on your classpath, you can simply modify regular JDBC connection URLs to get a fresh containerized instance of the database each time your application starts up. | ||
|
||
_N.B:_ | ||
|
||
* _TC needs to be on your application's classpath at runtime for this to work_ | ||
* _For Spring Boot (Before version `2.3.0`) you need to specify the driver manually `spring.datasource.driver-class-name=org.testcontainers.jdbc.ContainerDatabaseDriver`_ | ||
|
||
**Original URL**: `jdbc:mysql://localhost:3306/databasename` | ||
|
||
Insert `tc:` after `jdbc:` as follows. Note that the hostname, port and database name will be ignored; you can leave these as-is or set them to any value. | ||
|
||
!!! note | ||
We will use `///` (host-less URIs) from now on to emphasis the unimportance of the `host:port` pair. | ||
From Testcontainers' perspective, `jdbc:mysql:5.7.22://localhost:3306/databasename` and `jdbc:mysql:5.7.22:///databasename` is the same URI. | ||
|
||
!!! warning | ||
If you're using the JDBC URL support, there is no need to instantiate an instance of the container - Testcontainers will do it automagically. | ||
|
||
### JDBC URL examples | ||
|
||
#### Using Testcontainers with a fixed version | ||
|
||
`jdbc:tc:mysql:5.6.23:///databasename` | ||
|
||
#### Using PostgreSQL | ||
|
||
`jdbc:tc:postgresql:9.6.8:///databasename` | ||
|
||
#### Using PostGIS | ||
|
||
`jdbc:tc:postgis:9.6:///databasename` | ||
|
||
#### Using Presto | ||
|
||
`jdbc:tc:presto:329://localhost/memory/default` | ||
|
||
### Using a classpath init script | ||
|
||
Testcontainers can run an init script after the database container is started, but before your code is given a connection to it. The script must be on the classpath, and is referenced as follows: | ||
|
||
`jdbc:tc:mysql:5.7.22:///databasename?TC_INITSCRIPT=somepath/init_mysql.sql` | ||
|
||
This is useful if you have a fixed script for setting up database schema, etc. | ||
|
||
### Using an init script from a file | ||
|
||
If the init script path is prefixed `file:`, it will be loaded from a file (relative to the working directory, which will usually be the project root). | ||
|
||
`jdbc:tc:mysql:5.7.22:///databasename?TC_INITSCRIPT=file:src/main/resources/init_mysql.sql` | ||
|
||
### Using an init function | ||
|
||
Instead of running a fixed script for DB setup, it may be useful to call a Java function that you define. This is intended to allow you to trigger database schema migration tools. To do this, add TC_INITFUNCTION to the URL as follows, passing a full path to the class name and method: | ||
|
||
`jdbc:tc:mysql:5.7.22:///databasename?TC_INITFUNCTION=org.testcontainers.jdbc.JDBCDriverTest::sampleInitFunction` | ||
|
||
The init function must be a public static method which takes a `java.sql.Connection` as its only parameter, e.g. | ||
```java | ||
public class JDBCDriverTest { | ||
public static void sampleInitFunction(Connection connection) throws SQLException { | ||
// e.g. run schema setup or Flyway/liquibase/etc DB migrations here... | ||
} | ||
... | ||
``` | ||
|
||
### Running container in daemon mode | ||
|
||
By default database container is being stopped as soon as last connection is closed. There are cases when you might need to start container and keep it running till you stop it explicitly or JVM is shutdown. To do this, add `TC_DAEMON` parameter to the URL as follows: | ||
|
||
`jdbc:tc:mysql:5.7.22:///databasename?TC_DAEMON=true` | ||
|
||
With this parameter database container will keep running even when there're no open connections. | ||
### Running container with tmpfs options | ||
Container can have `tmpfs` mounts for storing data in host memory. This is useful if you want to speed up your database tests. Be aware that the data will be lost when the container stops. | ||
To pass this option to the container, add `TC_TMPFS` parameter to the URL as follows: | ||
`jdbc:tc:postgresql:9.6.8:///databasename?TC_TMPFS=/testtmpfs:rw` | ||
If you need more than one option, separate them by comma (e.g. `TC_TMPFS=key:value,key1:value1&other_parameters=foo`). | ||
For more information about `tmpfs` mount, see [the official Docker documentation](https://docs.docker.com/storage/tmpfs/). | ||
## Database container objects | ||
In case you can't use the URL support, or need to fine-tune the container, you can instantiate it yourself. | ||
|
||
Add a @Rule or @ClassRule to your test class, e.g.: | ||
|
||
```java | ||
public class SimpleMySQLTest { | ||
@Rule | ||
public MySQLContainer mysql = new MySQLContainer(); | ||
``` | ||
|
||
Now, in your test code (or a suitable setup method), you can obtain details necessary to connect to this database: | ||
|
||
* `mysql.getJdbcUrl()` provides a JDBC URL your code can connect to | ||
* `mysql.getUsername()` provides the username your code should pass to the driver | ||
* `mysql.getPassword()` provides the password your code should pass to the driver | ||
|
||
Note that if you use `@Rule`, you will be given an isolated container for each test method. If you use `@ClassRule`, you will get on isolated container for all the methods in the test class. | ||
|
||
Examples/Tests: | ||
|
||
* [MySQL](https://github.com/testcontainers/testcontainers-java/blob/master/modules/jdbc-test/src/test/java/org/testcontainers/junit/SimpleMySQLTest.java) | ||
* [PostgreSQL](https://github.com/testcontainers/testcontainers-java/blob/master/modules/jdbc-test/src/test/java/org/testcontainers/junit/SimplePostgreSQLTest.java) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
# R2DBC support | ||
|
||
You can obtain a temporary database in one of two ways: | ||
|
||
* **Using a specially modified R2DBC URL**: after making a very simple modification to your system's R2DBC URL string, Testcontainers will provide a disposable stand-in database that can be used without requiring modification to your application code. | ||
* **JUnit @Rule/@ClassRule**: this mode starts a database inside a container before your tests and tears it down afterwards. | ||
|
||
## Database containers launched via R2DBC URL scheme | ||
|
||
As long as you have Testcontainers and the appropriate R2DBC driver on your classpath, you can simply modify regular R2DBC connection URLs to get a fresh containerized instance of the database each time your application starts up. | ||
|
||
The started container will be terminated when the `ConnectionFactory` is closed. | ||
|
||
!!! warning | ||
Both the database module (e.g. `org.testcontainers:mysql`) **and** `org.testcontainers:r2dbc` need to be on your application's classpath at runtime. | ||
|
||
**Original URL**: `r2dbc:mysql://localhost:3306/databasename` | ||
|
||
1. Insert `tc:` after `r2dbc:` as follows. Note that the hostname, port and database name will be ignored; you can leave these as-is or set them to any value. | ||
1. Specify the mandatory Docker tag of the database's official image that you want using a `TC_IMAGE_TAG` query parameter. | ||
|
||
**Note that, unlike Testcontainers' JDBC URL support, it is not possible to specify an image tag in the 'scheme' part of the URL, and it is always necessary to specify a tag using `TC_IMAGE_TAG`.** | ||
|
||
So that the URL becomes: | ||
`r2dbc:tc:mysql:///databasename?TC_IMAGE_TAG=5.7.22` | ||
|
||
!!! note | ||
We will use `///` (host-less URIs) from now on to emphasis the unimportance of the `host:port` pair. | ||
From Testcontainers' perspective, `r2dbc:mysql://localhost:3306/databasename` and `r2dbc:mysql:///databasename` is the same URI. | ||
|
||
!!! warning | ||
If you're using the R2DBC URL support, there is no need to instantiate an instance of the container - Testcontainers will do it automagically. | ||
|
||
### R2DBC URL examples | ||
|
||
#### Using MySQL | ||
|
||
`r2dbc:tc:mysql:///databasename?TC_IMAGE_TAG=5.6.23` | ||
|
||
#### Using MariaDB | ||
|
||
`r2dbc:tc:mariadb:///databasename?TC_IMAGE_TAG=10.3.6` | ||
|
||
#### Using PostgreSQL | ||
|
||
`r2dbc:tc:postgresql:///databasename?TC_IMAGE_TAG=9.6.8` | ||
|
||
#### Using MSSQL: | ||
|
||
`r2dbc:tc:sqlserver:///?TC_IMAGE_TAG=2017-CU12` | ||
|
||
## Obtaining `ConnectionFactoryOptions` from database container objects | ||
|
||
If you already have an instance of the database container, you can get an instance of `ConnectionFactoryOptions` from it: | ||
<!--codeinclude--> | ||
[Creating `ConnectionFactoryOptions` from an instance)](../../../modules/postgresql/src/test/java/org/testcontainers/containers/PostgreSQLR2DBCDatabaseContainerTest.java) inside_block:get_options | ||
<!--/codeinclude--> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,20 @@ | ||
description = "Testcontainers :: JDBC :: MariaDB" | ||
|
||
dependencies { | ||
annotationProcessor 'com.google.auto.service:auto-service:1.0-rc6' | ||
compileOnly 'com.google.auto.service:auto-service:1.0-rc6' | ||
|
||
compile project(':jdbc') | ||
|
||
compileOnly project(':r2dbc') | ||
compileOnly 'org.mariadb:r2dbc-mariadb:0.8.1-alpha1' | ||
|
||
testCompile 'org.mariadb.jdbc:mariadb-java-client:2.6.0' | ||
testCompile 'com.zaxxer:HikariCP-java6:2.3.13' | ||
testCompile 'commons-dbutils:commons-dbutils:1.7' | ||
testCompile 'org.apache.tomcat:tomcat-jdbc:9.0.33' | ||
testCompile 'org.vibur:vibur-dbcp:25.0' | ||
|
||
testCompile testFixtures(project(':r2dbc')) | ||
testCompile 'org.mariadb:r2dbc-mariadb:0.8.1-alpha1' | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
...es/mariadb/src/main/java/org/testcontainers/containers/MariaDBR2DBCDatabaseContainer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package org.testcontainers.containers; | ||
|
||
import io.r2dbc.spi.ConnectionFactoryOptions; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.experimental.Delegate; | ||
import org.testcontainers.lifecycle.Startable; | ||
import org.testcontainers.r2dbc.R2DBCDatabaseContainer; | ||
|
||
@RequiredArgsConstructor | ||
public class MariaDBR2DBCDatabaseContainer implements R2DBCDatabaseContainer { | ||
|
||
@Delegate(types = Startable.class) | ||
private final MariaDBContainer<?> container; | ||
|
||
public static ConnectionFactoryOptions getOptions(MariaDBContainer<?> container) { | ||
ConnectionFactoryOptions options = ConnectionFactoryOptions.builder() | ||
.option(ConnectionFactoryOptions.DRIVER, MariaDBR2DBCDatabaseContainerProvider.DRIVER) | ||
.build(); | ||
|
||
return new MariaDBR2DBCDatabaseContainer(container).configure(options); | ||
} | ||
|
||
@Override | ||
public ConnectionFactoryOptions configure(ConnectionFactoryOptions options) { | ||
return options.mutate() | ||
.option(ConnectionFactoryOptions.HOST, container.getContainerIpAddress()) | ||
.option(ConnectionFactoryOptions.PORT, container.getMappedPort(MariaDBContainer.MARIADB_PORT)) | ||
.option(ConnectionFactoryOptions.DATABASE, container.getDatabaseName()) | ||
.option(ConnectionFactoryOptions.USER, container.getUsername()) | ||
.option(ConnectionFactoryOptions.PASSWORD, container.getPassword()) | ||
.build(); | ||
} | ||
} |
Oops, something went wrong.