Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More customizable postgres container #220

Merged
merged 3 commits into from
Sep 22, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please avoid nun-functional changes

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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use this(dockerImageName, "test", "test", "test")

super(dockerImageName);
}

Expand All @@ -26,9 +28,9 @@ protected Integer getLivenessCheckPort() {
protected void configure() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please change back the order to reduce amount of changes


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() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use Lombok's @Getter

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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

postgres.getUsername() ?

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