Skip to content

Commit

Permalink
Merge pull request #220 from Barlog-M/master
Browse files Browse the repository at this point in the history
More customizable postgres container
  • Loading branch information
rnorth authored Sep 22, 2016
2 parents ba07bfc + 417d8fe commit beabd2b
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,18 @@
* @author richardnorth
*/
public class PostgreSQLContainer<SELF extends PostgreSQLContainer<SELF>> extends JdbcDatabaseContainer<SELF> {

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);
}

Expand All @@ -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");
}

Expand All @@ -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();
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}

0 comments on commit beabd2b

Please sign in to comment.