Skip to content

Commit

Permalink
Support Testcontainers URL
Browse files Browse the repository at this point in the history
  • Loading branch information
mfvanek committed Nov 28, 2024
1 parent 77fc7a8 commit 8afa8d1
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Objects;
import javax.annotation.Nullable;
import javax.sql.DataSource;

/**
Expand Down Expand Up @@ -58,7 +59,7 @@ public class DatabaseStructureHealthAutoConfiguration {
public PgConnection pgConnection(@Qualifier("dataSource") final DataSource dataSource,
@Value("${spring.datasource.url:#{null}}") final String databaseUrl) {
final PgHost host;
if (Objects.isNull(databaseUrl) || databaseUrl.isBlank()) {
if (needToGetUrlFromMetaData(databaseUrl)) {
try (Connection connection = dataSource.getConnection()) {
host = PgHostImpl.ofUrl(connection.getMetaData().getURL());
} catch (SQLException ex) {
Expand All @@ -69,4 +70,10 @@ public PgConnection pgConnection(@Qualifier("dataSource") final DataSource dataS
}
return PgConnectionImpl.of(dataSource, host);
}

private static boolean needToGetUrlFromMetaData(@Nullable final String databaseUrl) {
return Objects.isNull(databaseUrl) ||
databaseUrl.isBlank() ||
databaseUrl.startsWith(DatabaseStructureHealthCondition.TESTCONTAINERS_PG_URL_PREFIX);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,19 @@
*/
public class DatabaseStructureHealthCondition extends SpringBootCondition {

/**
* The URL prefix used in Testcontainers to initialize PostgreSQL containers.
* <p>
* Testcontainers provides a special JDBC URL format that allows for on-the-fly creation and management
* of PostgreSQL database containers during tests. This prefix is part of the JDBC URL and signals
* Testcontainers to handle the lifecycle of the container automatically.
* </p>
*
* @see <a href="https://java.testcontainers.org/modules/databases/jdbc/">Testcontainers JDBC Support</a>
*/
static final String TESTCONTAINERS_PG_URL_PREFIX = "jdbc:tc:postgresql:";

private static final String ORIGINAL_PG_URL_PREFIX = "jdbc:postgresql://";
private static final String PROPERTY_NAME = "spring.datasource.url";

/**
Expand All @@ -34,7 +47,7 @@ public ConditionOutcome getMatchOutcome(final ConditionContext context, final An
final ConditionMessage.Builder message = ConditionMessage.forCondition("pg.index.health.test PostgreSQL condition");
final String jdbcUrl = getJdbcUrl(context);
if (jdbcUrl != null && !jdbcUrl.isBlank()) {
if (jdbcUrl.startsWith("jdbc:postgresql://")) {
if (jdbcUrl.startsWith(ORIGINAL_PG_URL_PREFIX) || jdbcUrl.startsWith(TESTCONTAINERS_PG_URL_PREFIX)) {
return ConditionOutcome.match(message.foundExactly("found PostgreSQL connection " + jdbcUrl));
}
return ConditionOutcome.noMatch(message.notAvailable("not PostgreSQL connection"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,22 @@ void withDataSourceAndWrongConnectionString() {
});
}

@Test
void withDataSourceAndTestcontainersConnectionString() throws SQLException {
try (Connection connectionMock = Mockito.mock(Connection.class)) {
setMocks(connectionMock);

assertWithTestConfig()
.withPropertyValues("spring.datasource.url=jdbc:tc:postgresql:17.2:///test")
.withInitializer(AutoConfigurationTestBase::initialize)
.run(context -> {
assertThatBeansPresent(context);
assertThatBeansAreNotNullBean(context);
assertThatPgConnectionIsValid(context);
});
}
}

@Test
void shouldNotCreateAutoConfigurationWithDisabledProperty() {
assertWithTestConfig()
Expand Down

0 comments on commit 8afa8d1

Please sign in to comment.