-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. use |
||
super(dockerImageName); | ||
} | ||
|
||
|
@@ -26,9 +28,9 @@ protected Integer getLivenessCheckPort() { | |
protected void configure() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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"); | ||
} | ||
|
||
|
@@ -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() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. use Lombok's |
||
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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
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); | ||
} | ||
} |
There was a problem hiding this comment.
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