|
| 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.rnorth.visibleassertions.VisibleAssertions.assertNotNull; |
| 12 | +import static org.rnorth.visibleassertions.VisibleAssertions.assertNull; |
| 13 | +import static org.rnorth.visibleassertions.VisibleAssertions.assertTrue; |
| 14 | + |
| 15 | +/** |
| 16 | + * Created by inikolaev on 08/06/2017. |
| 17 | + */ |
| 18 | +public class DatabaseDriverTest { |
| 19 | + @AfterClass |
| 20 | + public static void testCleanup() { |
| 21 | + ContainerDatabaseDriver.killContainers(); |
| 22 | + } |
| 23 | + |
| 24 | + @Test |
| 25 | + public void shouldStopContainerWhenAllConnectionsClosed() throws SQLException { |
| 26 | + final String jdbcUrl = "jdbc:tc:postgresql://hostname/databasename"; |
| 27 | + |
| 28 | + getConnectionAndClose(jdbcUrl); |
| 29 | + |
| 30 | + JdbcDatabaseContainer<?> container = ContainerDatabaseDriver.getContainer(jdbcUrl); |
| 31 | + assertNull("Database container instance is null as expected", container); |
| 32 | + } |
| 33 | + |
| 34 | + @Test |
| 35 | + public void shouldNotStopDaemonContainerWhenAllConnectionsClosed() throws SQLException { |
| 36 | + final String jdbcUrl = "jdbc:tc:postgresql://hostname/databasename?TC_DAEMON=true"; |
| 37 | + |
| 38 | + getConnectionAndClose(jdbcUrl); |
| 39 | + |
| 40 | + JdbcDatabaseContainer<?> container = ContainerDatabaseDriver.getContainer(jdbcUrl); |
| 41 | + assertNotNull("Database container instance is not null as expected", container); |
| 42 | + assertTrue("Database container is running as expected", container.isRunning()); |
| 43 | + } |
| 44 | + |
| 45 | + private void getConnectionAndClose(String jdbcUrl) throws SQLException { |
| 46 | + try (Connection connection = DriverManager.getConnection(jdbcUrl)) { |
| 47 | + assertNotNull("Obtained connection as expected", connection); |
| 48 | + } |
| 49 | + } |
| 50 | +} |
0 commit comments