@ContextConfiguration(classes = {AppConfig.class}, loader = AnnotationConfigContextLoader.class)
public abstract class StaticContainerInstantiation {
protected static PostgreSQLContainer postgreSQLContainer;
//protected static OracleContainer oracleContainer;
//protected static MSSQLServerContainer mssqlServerContainer;
private static final AtomicBoolean initDB = new AtomicBoolean(false);
@AfterClass
public static void teardown() {
System.out.println("teardown");
postgreSQLContainer.stop();
//oracleContainer.stop();
//mssqlServerContainer.stop();
}
static {
Consumer<CreateContainerCmd> consumer = new Consumer<CreateContainerCmd>() {
@Override
public void accept(CreateContainerCmd createContainerCmd) {
createContainerCmd.withHostConfig(
new HostConfig().withPortBindings(
new PortBinding(Ports.Binding.bindPort(57156), new ExposedPort(5432) // binding Postgres
// new PortBinding(Ports.Binding.bindPort(57156), new ExposedPort(1521) // binding Oracle
//new PortBinding(Ports.Binding.bindPort(57156), new ExposedPort(1433) // binding MSSQL
)
)
);
}
};
postgreSQLContainer = (PostgreSQLContainer) new PostgreSQLContainer("postgres:13.4")
.withDatabaseName("dbExample")
.withUsername("dbUser")
.withPassword("dbPassword")
.withInitScript("postgres/setup.sql")
.withExposedPorts(5432)
.withReuse(true)
.withCreateContainerCmdModifier(consumer);
if (initDB.get() == false) {
postgreSQLContainer.start();
// oracleContainer.start();
//mssqlServerContainer.start();
initDB.set(true);
}
/*
oracleContainer = (OracleContainer) new OracleContainer("gvenzl/oracle-xe:18.4.0-slim")
.withDatabaseName("dbExample")
.withUsername("dbUser")
.withPassword("dbPassword")
.withInitScript("oracle/setup.sql")
.withExposedPorts(1521)
.withCreateContainerCmdModifier(consumer)
.withReuse(true);
mssqlServerContainer = (MSSQLServerContainer) new MSSQLServerContainer<>("mcr.microsoft.com/mssql/server:2017-latest")
.withEnv("ACCEPT_EULA", "Y")
.withEnv("MSSQL_PID", "Express")
.withEnv("SA_PASSWORD", "dbPassword")
.withInitScript("mssql/setup.sql")
.withExposedPorts(1433)
.withCreateContainerCmdModifier(consumer)
.withReuse(true);
*/
}
}