diff --git a/modules/postgresql/src/main/java/org/testcontainers/containers/PostgreSQLContainer.java b/modules/postgresql/src/main/java/org/testcontainers/containers/PostgreSQLContainer.java index 7f205487b7e..725f73d639c 100644 --- a/modules/postgresql/src/main/java/org/testcontainers/containers/PostgreSQLContainer.java +++ b/modules/postgresql/src/main/java/org/testcontainers/containers/PostgreSQLContainer.java @@ -4,16 +4,18 @@ * @author richardnorth */ public class PostgreSQLContainer> extends JdbcDatabaseContainer { - public static final String NAME = "postgresql"; public static final String IMAGE = "postgres"; public static final Integer POSTGRESQL_PORT = 5432; + private String databaseName = "test"; + private String username = "test"; + private String password = "test"; public PostgreSQLContainer() { - super(IMAGE + ":latest"); + this(IMAGE + ":latest"); } - public PostgreSQLContainer(String dockerImageName) { + public PostgreSQLContainer(final String dockerImageName) { super(dockerImageName); } @@ -26,9 +28,9 @@ protected Integer getLivenessCheckPort() { protected void configure() { addExposedPort(POSTGRESQL_PORT); - addEnv("POSTGRES_DATABASE", "test"); - addEnv("POSTGRES_USER", "test"); - addEnv("POSTGRES_PASSWORD", "test"); + addEnv("POSTGRES_DB", databaseName); + addEnv("POSTGRES_USER", username); + addEnv("POSTGRES_PASSWORD", password); setCommand("postgres"); } @@ -39,21 +41,36 @@ public String getDriverClassName() { @Override public String getJdbcUrl() { - return "jdbc:postgresql://" + getContainerIpAddress() + ":" + getMappedPort(POSTGRESQL_PORT) + "/test"; + return "jdbc:postgresql://" + getContainerIpAddress() + ":" + getMappedPort(POSTGRESQL_PORT) + "/" + databaseName; } @Override public String getUsername() { - return "test"; + return username; } @Override public String getPassword() { - return "test"; + return password; } @Override public String getTestQueryString() { return "SELECT 1"; } + + public SELF withDatabaseName(final String databaseName) { + this.databaseName = databaseName; + return self(); + } + + public SELF withUsername(final String username) { + this.username = username; + return self(); + } + + public SELF withPassword(final String password) { + this.password = password; + return self(); + } } diff --git a/modules/postgresql/src/test/java/org/testcontainers/junit/CustomizablePostgreSQLTest.java b/modules/postgresql/src/test/java/org/testcontainers/junit/CustomizablePostgreSQLTest.java new file mode 100644 index 00000000000..8180ae2a872 --- /dev/null +++ b/modules/postgresql/src/test/java/org/testcontainers/junit/CustomizablePostgreSQLTest.java @@ -0,0 +1,48 @@ +package org.testcontainers.junit; + +import com.zaxxer.hikari.HikariConfig; +import com.zaxxer.hikari.HikariDataSource; +import org.junit.Rule; +import org.junit.Test; +import org.testcontainers.containers.PostgreSQLContainer; + +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; + +import static org.rnorth.visibleassertions.VisibleAssertions.assertEquals; + +/** + * @author richardnorth + */ +public class CustomizablePostgreSQLTest { + private static final String DB_NAME = "foo"; + private static final String USER = "bar"; + private static final String PWD = "baz"; + + @Rule + public PostgreSQLContainer postgres = new PostgreSQLContainer("postgres:latest") + .withDatabaseName(DB_NAME) + .withUsername(USER) + .withPassword(PWD); + + @Test + public void testSimple() throws SQLException { + HikariConfig hikariConfig = new HikariConfig(); + hikariConfig.setJdbcUrl("jdbc:postgresql://" + + postgres.getContainerIpAddress() + + ":" + postgres.getMappedPort(PostgreSQLContainer.POSTGRESQL_PORT) + + "/" + DB_NAME); + hikariConfig.setUsername(USER); + hikariConfig.setPassword(PWD); + + HikariDataSource ds = new HikariDataSource(hikariConfig); + Statement statement = ds.getConnection().createStatement(); + statement.execute("SELECT 1"); + ResultSet resultSet = statement.getResultSet(); + + resultSet.next(); + int resultSetInt = resultSet.getInt(1); + assertEquals("A basic SELECT query succeeds", 1, resultSetInt); + } +}