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 1 commit
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,31 +4,47 @@
* @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;
public static final Integer DEFAULT_PORT = 5432;
final String databaseName;
Copy link
Member

Choose a reason for hiding this comment

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

make it private

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

final String username;
final String password;

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);
this.databaseName = "test";
this.username = "test";
this.password = "test";
}

public PostgreSQLContainer(
final String dockerImageName,
final String databaseName,
final String username,
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

final String password
) {
super(dockerImageName);
this.databaseName = databaseName;
this.username = username;
this.password = password;
}

@Override
protected Integer getLivenessCheckPort() {
return getMappedPort(POSTGRESQL_PORT);
return getMappedPort(DEFAULT_PORT);
}

@Override
protected void configure() {

addExposedPort(POSTGRESQL_PORT);
addEnv("POSTGRES_DATABASE", "test");
addEnv("POSTGRES_USER", "test");
addEnv("POSTGRES_PASSWORD", "test");
addExposedPort(DEFAULT_PORT);
addEnv("POSTGRES_DB", databaseName);
addEnv("POSTGRES_USER", username);
addEnv("POSTGRES_PASSWORD", password);
setCommand("postgres");
}

Expand All @@ -39,17 +55,17 @@ public String getDriverClassName() {

@Override
public String getJdbcUrl() {
return "jdbc:postgresql://" + getContainerIpAddress() + ":" + getMappedPort(POSTGRESQL_PORT) + "/test";
return "jdbc:postgresql://" + this.getContainerIpAddress() + ":" + this.getMappedPort(DEFAULT_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
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
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", DB_NAME, USER, PWD);

@Test
public void testSimple() throws SQLException {
HikariConfig hikariConfig = new HikariConfig();
hikariConfig.setJdbcUrl(postgres.getJdbcUrl());
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);
}
}