8
8
9
9
import org .postgresql .ds .PGSimpleDataSource ;
10
10
import org .springframework .data .r2dbc .testing .ExternalDatabase .ProvidedDatabase ;
11
+ import org .testcontainers .containers .PostgreSQLContainer ;
11
12
12
13
/**
13
14
* Utility class for testing against Postgres.
14
15
*
15
16
* @author Mark Paluch
17
+ * @author Jens Schauder
16
18
*/
17
19
public class PostgresTestSupport {
18
20
21
+ private static final PostgreSQLContainer POSTGRESQL_CONTAINER = new PostgreSQLContainer ();
22
+
19
23
public static String CREATE_TABLE_LEGOSET = "CREATE TABLE legoset (\n " //
20
24
+ " id integer CONSTRAINT id PRIMARY KEY,\n " //
21
25
+ " name varchar(255) NOT NULL,\n " //
@@ -31,30 +35,60 @@ public class PostgresTestSupport {
31
35
public static String INSERT_INTO_LEGOSET = "INSERT INTO legoset (id, name, manual) VALUES($1, $2, $3)" ;
32
36
33
37
/**
34
- * Returns a locally provided database at {@code postgres:@localhost:5432/postgres}.
38
+ * Returns a database either hosted locally at {@code postgres:@localhost:5432/postgres} or running inside Docker .
35
39
*
36
- * @return
40
+ * @return information about the database. Guaranteed to be not {@literal null}.
37
41
*/
38
42
public static ExternalDatabase database () {
39
- return local ();
43
+
44
+ ExternalDatabase local = local ();
45
+ if (local .checkValidity ()) {
46
+ return local ;
47
+ } else {
48
+ return testContainer ();
49
+ }
40
50
}
41
51
42
52
/**
43
53
* Returns a locally provided database at {@code postgres:@localhost:5432/postgres}.
44
- *
45
- * @return
46
54
*/
47
55
private static ExternalDatabase local () {
48
- return ProvidedDatabase .builder ().hostname ("localhost" ).port (5432 ).database ("postgres" ).username ("postgres" )
56
+
57
+ return ProvidedDatabase .builder () //
58
+ .hostname ("localhost" ) //
59
+ .port (5432 ) //
60
+ .database ("postgres" ) //
61
+ .username ("postgres" ) //
49
62
.password ("" ).build ();
50
63
}
51
64
65
+ /**
66
+ * Returns a database provided via Testcontainers.
67
+ */
68
+ private static ExternalDatabase testContainer () {
69
+
70
+ POSTGRESQL_CONTAINER .start ();
71
+
72
+ return ProvidedDatabase .builder () //
73
+ .hostname ("localhost" ) //
74
+ .port (POSTGRESQL_CONTAINER .getFirstMappedPort ()) //
75
+ .database (POSTGRESQL_CONTAINER .getDatabaseName ()) //
76
+ .username (POSTGRESQL_CONTAINER .getUsername ()) //
77
+ .password (POSTGRESQL_CONTAINER .getPassword ()).build ();
78
+ }
79
+
52
80
/**
53
81
* Creates a new {@link ConnectionFactory} configured from the {@link ExternalDatabase}..
54
82
*/
55
83
public static ConnectionFactory createConnectionFactory (ExternalDatabase database ) {
56
- return new PostgresqlConnectionFactory (PostgresqlConnectionConfiguration .builder ().host (database .getHostname ())
57
- .database (database .getDatabase ()).username (database .getUsername ()).password (database .getPassword ()).build ());
84
+
85
+ return new PostgresqlConnectionFactory (PostgresqlConnectionConfiguration .builder () //
86
+ .host (database .getHostname ()) //
87
+ .database (database .getDatabase ()) //
88
+ .port (database .getPort ()) //
89
+ .username (database .getUsername ()) //
90
+ .password (database .getPassword ()) //
91
+ .build ());
58
92
}
59
93
60
94
/**
0 commit comments