2222import org .jooq .Table ;
2323import org .jooq .impl .DSL ;
2424import org .junit .Ignore ;
25+ import org .junit .jupiter .api .AfterAll ;
2526import org .junit .jupiter .api .AfterEach ;
2627import org .junit .jupiter .api .BeforeEach ;
2728import org .junit .jupiter .api .Test ;
29+ import org .testcontainers .containers .PostgreSQLContainer ;
30+ import org .testcontainers .utility .DockerImageName ;
2831
2932import java .io .InputStream ;
3033import java .sql .Connection ;
4851
4952public class PostgresEventStoreTest {
5053
54+ private static final PostgreSQLContainer <?> postgreSQLContainer = new PostgreSQLContainer <>(DockerImageName .parse ("postgres:14" ))
55+ .withUsername ("eventsourcing" )
56+ .withPassword ("eventsourcing" )
57+ .withDatabaseName ("eventsourcing" );
58+
59+ protected static boolean isCi () {
60+ return "true" .equals (System .getenv ("CI" ));
61+ }
62+
63+ static {
64+ if (!isCi ()) {
65+ postgreSQLContainer .start ();
66+ }
67+ }
68+
69+
70+ @ AfterAll
71+ public static void stopDb () {
72+ if (!isCi ()) {
73+ postgreSQLContainer .stop ();
74+ }
75+ }
76+
5177 private ActorSystem system ;
5278 private PostgresEventStore <VikingEvent , Void , Void > postgresEventStore ;
5379 private HikariDataSource dataSource ;
@@ -278,6 +304,29 @@ private void initDatas() {
278304 }
279305 }
280306
307+
308+ protected static String host () {
309+ return "localhost" ;
310+ }
311+ protected static String database () {
312+ return "eventsourcing" ;
313+ }
314+ protected static String user () {
315+ return "eventsourcing" ;
316+ }
317+ protected static String password () {
318+ return "eventsourcing" ;
319+ }
320+ protected static Integer port () {
321+ if (isCi ()) {
322+ return 5557 ;
323+ } else {
324+ return postgreSQLContainer .getFirstMappedPort ();
325+ }
326+ }
327+
328+
329+
281330 @ BeforeEach
282331 public void setUp () {
283332
@@ -287,17 +336,17 @@ public void setUp() {
287336 this .system = ActorSystem .create ();
288337 Properties props = new Properties ();
289338 props .setProperty ("dataSourceClassName" , "org.postgresql.ds.PGSimpleDataSource" );
290- props .setProperty ("dataSource.serverName" , "localhost" );
291- props .setProperty ("dataSource.portNumber" , "5557" );
292- props .setProperty ("dataSource.user" , "eventsourcing" );
293- props .setProperty ("dataSource.password" , "eventsourcing" );
294- props .setProperty ("dataSource.databaseName" , "eventsourcing" );
339+ props .setProperty ("dataSource.serverName" , host () );
340+ props .setProperty ("dataSource.portNumber" , port (). toString () );
341+ props .setProperty ("dataSource.user" , user () );
342+ props .setProperty ("dataSource.password" , password () );
343+ props .setProperty ("dataSource.databaseName" , database () );
295344 props .setProperty ("maximumPoolSize" , "20" );
296345 HikariConfig config = new HikariConfig (props );
297346 this .dataSource = new HikariDataSource (config );
298- this .dataSource .setJdbcUrl ("jdbc:postgresql://localhost:5557/eventsourcing" );
299- this .dataSource .setUsername ("eventsourcing" );
300- this .dataSource .setPassword ("eventsourcing" );
347+ this .dataSource .setJdbcUrl ("jdbc:postgresql://%s:%s/%s" . formatted ( host (), port (), database ()) );
348+ this .dataSource .setUsername (user () );
349+ this .dataSource .setPassword (password () );
301350 this .dataSource .setDataSourceClassName ("org.postgresql.ds.PGSimpleDataSource" );
302351
303352 this .dslContext = DSL .using (dataSource , SQLDialect .POSTGRES );
0 commit comments