Allow customizations of embedded database connections

This commit allows EmbeddedDatabaseConfigurer instances to be further
customized if necessary. EmbeddedDatabaseBuilder has a way now to set
a DatabaseConfigurer rather than just a type to provide full control,
or customize an existing supported database type using the new
EmbeddedDatabaseConfigurers#customizeConfigurer callback.

Closes gh-21160
This commit is contained in:
Stéphane Nicoll
2024-01-04 16:32:05 +01:00
parent bcccba50c1
commit 18ea43c905
7 changed files with 224 additions and 20 deletions

View File

@@ -121,6 +121,23 @@ class EmbeddedDatabaseBuilderTests {
});
}
@Test
void setTypeConfigurerToCustomH2() {
doTwice(() -> {
EmbeddedDatabase db = builder
.setDatabaseConfigurer(EmbeddedDatabaseConfigurers.customizeConfigurer(H2, defaultConfigurer ->
new EmbeddedDatabaseConfigurerDelegate(defaultConfigurer) {
@Override
public void configureConnectionProperties(ConnectionProperties properties, String databaseName) {
super.configureConnectionProperties(properties, databaseName);
}
}))
.addScripts("db-schema.sql", "db-test-data.sql")//
.build();
assertDatabaseCreatedAndShutdown(db);
});
}
@Test
void setTypeToDerbyAndIgnoreFailedDrops() {
doTwice(() -> {

View File

@@ -17,6 +17,7 @@
package org.springframework.jdbc.datasource.embedded;
import java.sql.Connection;
import java.sql.SQLException;
import org.junit.jupiter.api.Test;
@@ -25,11 +26,14 @@ import org.springframework.jdbc.datasource.init.DatabasePopulator;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Tests for {@link EmbeddedDatabaseFactory}.
*
* @author Keith Donald
* @author Stephane Nicoll
*/
class EmbeddedDatabaseFactoryTests {
private EmbeddedDatabaseFactory factory = new EmbeddedDatabaseFactory();
private final EmbeddedDatabaseFactory factory = new EmbeddedDatabaseFactory();
@Test
@@ -41,6 +45,45 @@ class EmbeddedDatabaseFactoryTests {
db.shutdown();
}
@Test
void customizeConfigurerWithAnotherDatabaseName() throws SQLException {
this.factory.setDatabaseName("original-db-mame");
this.factory.setDatabaseConfigurer(EmbeddedDatabaseConfigurers.customizeConfigurer(
EmbeddedDatabaseType.H2, defaultConfigurer ->
new EmbeddedDatabaseConfigurerDelegate(defaultConfigurer) {
@Override
public void configureConnectionProperties(ConnectionProperties properties, String databaseName) {
super.configureConnectionProperties(properties, "custom-db-name");
}
}));
EmbeddedDatabase db = this.factory.getDatabase();
try (Connection connection = db.getConnection()) {
assertThat(connection.getMetaData().getURL()).contains("custom-db-name")
.doesNotContain("original-db-mame");
}
db.shutdown();
}
@Test
void customizeConfigurerWithCustomizedUrl() throws SQLException {
this.factory.setDatabaseName("original-db-mame");
this.factory.setDatabaseConfigurer(EmbeddedDatabaseConfigurers.customizeConfigurer(
EmbeddedDatabaseType.H2, defaultConfigurer ->
new EmbeddedDatabaseConfigurerDelegate(defaultConfigurer) {
@Override
public void configureConnectionProperties(ConnectionProperties properties, String databaseName) {
super.configureConnectionProperties(properties, databaseName);
properties.setUrl("jdbc:h2:mem:custom-db-name;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=false;MODE=MariaDB");
}
}));
EmbeddedDatabase db = this.factory.getDatabase();
try (Connection connection = db.getConnection()) {
assertThat(connection.getMetaData().getURL()).contains("custom-db-name")
.doesNotContain("original-db-mame");
}
db.shutdown();
}
private static class StubDatabasePopulator implements DatabasePopulator {