-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Support database aliases in R2DBC #2599
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| package org.testcontainers.utility; | ||
|
|
||
| import org.jetbrains.annotations.NotNull; | ||
| import org.junit.rules.TestRule; | ||
| import org.junit.runner.Description; | ||
| import org.junit.runners.model.Statement; | ||
|
|
||
| public class MockLicenseAcceptanceRule implements TestRule { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. IMO this is not a mock, but more a way to programmatically accept licences, maybe Also renaming |
||
|
|
||
| @NotNull | ||
| @Override | ||
| public Statement apply(@NotNull Statement base, @NotNull Description description) { | ||
| return new Statement() { | ||
| @Override | ||
| public void evaluate() throws Throwable { | ||
| try { | ||
| base.evaluate(); | ||
| } finally { | ||
| LicenseAcceptance.ACCEPTED_IMAGE_NAMES.clear(); | ||
| } | ||
| } | ||
| }; | ||
| } | ||
|
|
||
| public void acceptLicense(String imageName) { | ||
| LicenseAcceptance.ACCEPTED_IMAGE_NAMES.add(imageName); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,75 @@ | ||||||
| package org.testcontainers.r2dbc; | ||||||
|
|
||||||
| import io.r2dbc.spi.ConnectionFactoryOptions; | ||||||
| import lombok.NonNull; | ||||||
| import org.testcontainers.utility.TestcontainersConfiguration; | ||||||
|
|
||||||
| import java.util.Properties; | ||||||
|
|
||||||
| public abstract class AbstractR2DBCDatabaseContainerProvider implements R2DBCDatabaseContainerProvider { | ||||||
|
|
||||||
| final String originalDriver; | ||||||
|
|
||||||
| protected AbstractR2DBCDatabaseContainerProvider(@NonNull String originalDriver) { | ||||||
| this.originalDriver = originalDriver; | ||||||
| } | ||||||
|
|
||||||
| @Override | ||||||
| public boolean supports(ConnectionFactoryOptions options) { | ||||||
| return originalDriver.equals(getDriver(options)); | ||||||
| } | ||||||
|
|
||||||
| private String getDriver(ConnectionFactoryOptions options) { | ||||||
| Properties properties = TestcontainersConfiguration.getInstance().getProperties(); | ||||||
| return properties.getProperty( | ||||||
| driverPropertyName(options.getRequiredValue(ConnectionFactoryOptions.DRIVER)), | ||||||
| originalDriver | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
This will fix failing Fix is already included in #3019 |
||||||
| ); | ||||||
| } | ||||||
|
|
||||||
| private String driverPropertyName(String alias) { | ||||||
| return String.format("db.alias.%s.r2dbcDriver", alias); | ||||||
| } | ||||||
|
|
||||||
| @Override | ||||||
| public final R2DBCDatabaseContainer createContainer(ConnectionFactoryOptions options) { | ||||||
| String driver = options.getRequiredValue(ConnectionFactoryOptions.DRIVER); | ||||||
|
|
||||||
| Properties properties = TestcontainersConfiguration.getInstance().getProperties(); | ||||||
| if (properties.containsKey(driverPropertyName(driver))) { | ||||||
| String imagePropertyName = String.format("db.alias.%s.image", driver); | ||||||
| String image = properties.getProperty(imagePropertyName); | ||||||
| if (image == null) { | ||||||
| throw new IllegalArgumentException(String.format("Property '%s' is not set", imagePropertyName)); | ||||||
| } | ||||||
|
|
||||||
| options = options.mutate() | ||||||
| .option(ConnectionFactoryOptions.DRIVER, originalDriver) | ||||||
| .option(IMAGE_OPTION, image) | ||||||
| .build(); | ||||||
| } | ||||||
|
|
||||||
| R2DBCDatabaseContainer container = doCreateContainer(options); | ||||||
| return new R2DBCDatabaseContainer() { | ||||||
| @Override | ||||||
| public ConnectionFactoryOptions configure(ConnectionFactoryOptions options) { | ||||||
| options = options.mutate() | ||||||
| .option(ConnectionFactoryOptions.DRIVER, originalDriver) | ||||||
| .build(); | ||||||
| return container.configure(options); | ||||||
| } | ||||||
|
|
||||||
| @Override | ||||||
| public void start() { | ||||||
| container.start(); | ||||||
| } | ||||||
|
|
||||||
| @Override | ||||||
| public void stop() { | ||||||
| container.stop(); | ||||||
| } | ||||||
| }; | ||||||
| } | ||||||
|
|
||||||
| protected abstract R2DBCDatabaseContainer doCreateContainer(ConnectionFactoryOptions options); | ||||||
| } | ||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -58,7 +58,7 @@ public Publisher<? extends Connection> create() { | |
| } | ||
| } | ||
| return future.thenApply(it -> { | ||
| return ConnectionFactories.find( | ||
| return ConnectionFactories.get( | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could be replaced by a lambda expression. |
||
| it.configure(options) | ||
| ); | ||
| }); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since we use
@UtilityClass, all static modifiers in the class can be omitted.