testcontainers-java

Форк
0
52 строки · 2.2 Кб
1
package org.testcontainers.jdbc;
2

3
import org.junit.AfterClass;
4
import org.junit.Test;
5
import org.testcontainers.containers.JdbcDatabaseContainer;
6

7
import java.sql.Connection;
8
import java.sql.DriverManager;
9
import java.sql.SQLException;
10

11
import static org.assertj.core.api.Assertions.assertThat;
12

13
/**
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.
18
 */
19
public class DatabaseDriverShutdownTest {
20

21
    @AfterClass
22
    public static void testCleanup() {
23
        ContainerDatabaseDriver.killContainers();
24
    }
25

26
    @Test
27
    public void shouldStopContainerWhenAllConnectionsClosed() throws SQLException {
28
        final String jdbcUrl = "jdbc:tc:postgresql:9.6.8://hostname/databasename";
29

30
        getConnectionAndClose(jdbcUrl);
31

32
        JdbcDatabaseContainer<?> container = ContainerDatabaseDriver.getContainer(jdbcUrl);
33
        assertThat(container).as("Database container instance is null as expected").isNull();
34
    }
35

36
    @Test
37
    public void shouldNotStopDaemonContainerWhenAllConnectionsClosed() throws SQLException {
38
        final String jdbcUrl = "jdbc:tc:postgresql:9.6.8://hostname/databasename?TC_DAEMON=true";
39

40
        getConnectionAndClose(jdbcUrl);
41

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();
45
    }
46

47
    private void getConnectionAndClose(String jdbcUrl) throws SQLException {
48
        try (Connection connection = DriverManager.getConnection(jdbcUrl)) {
49
            assertThat(connection).as("Obtained connection as expected").isNotNull();
50
        }
51
    }
52
}
53

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.