1
package org.testcontainers.jdbc;
3
import org.junit.AfterClass;
5
import org.testcontainers.containers.JdbcDatabaseContainer;
7
import java.sql.Connection;
8
import java.sql.DriverManager;
9
import java.sql.SQLException;
11
import static org.assertj.core.api.Assertions.assertThat;
14
* This test belongs in the jdbc module, as it is focused on testing the behaviour of {@link org.testcontainers.containers.JdbcDatabaseContainer}.
15
* However, the need to use the {@link org.testcontainers.containers.PostgreSQLContainerProvider} (due to the jdbc:tc:postgresql) URL forces it to live here in
16
* the mysql module, to avoid circular dependencies.
17
* TODO: Move to the jdbc module and either (a) implement a barebones {@link org.testcontainers.containers.JdbcDatabaseContainerProvider} for testing, or (b) refactor into a unit test.
19
public class DatabaseDriverShutdownTest {
22
public static void testCleanup() {
23
ContainerDatabaseDriver.killContainers();
27
public void shouldStopContainerWhenAllConnectionsClosed() throws SQLException {
28
final String jdbcUrl = "jdbc:tc:postgresql:9.6.8://hostname/databasename";
30
getConnectionAndClose(jdbcUrl);
32
JdbcDatabaseContainer<?> container = ContainerDatabaseDriver.getContainer(jdbcUrl);
33
assertThat(container).as("Database container instance is null as expected").isNull();
37
public void shouldNotStopDaemonContainerWhenAllConnectionsClosed() throws SQLException {
38
final String jdbcUrl = "jdbc:tc:postgresql:9.6.8://hostname/databasename?TC_DAEMON=true";
40
getConnectionAndClose(jdbcUrl);
42
JdbcDatabaseContainer<?> container = ContainerDatabaseDriver.getContainer(jdbcUrl);
43
assertThat(container).as("Database container instance is not null as expected").isNotNull();
44
assertThat(container.isRunning()).as("Database container is running as expected").isTrue();
47
private void getConnectionAndClose(String jdbcUrl) throws SQLException {
48
try (Connection connection = DriverManager.getConnection(jdbcUrl)) {
49
assertThat(connection).as("Obtained connection as expected").isNotNull();