Skip to content

Commit 2569310

Browse files
committed
#29 - Preferring TestContainers by default.
We now prefer a database based on TestContainers. Only if this can't be obtained do we try to access a local database for Postgres. This minimizes the risk of accessing a database during tests that is not intended for that purpose. If the system property `spring.data.r2dbc.test.preferLocalDatabase` is set to "true" the local database is preferred.
1 parent 5a4c642 commit 2569310

File tree

2 files changed

+84
-12
lines changed

2 files changed

+84
-12
lines changed

src/test/java/org/springframework/data/r2dbc/testing/ExternalDatabase.java

+38
Original file line numberDiff line numberDiff line change
@@ -145,4 +145,42 @@ public String getPassword() {
145145
return password;
146146
}
147147
}
148+
149+
/**
150+
* An {@link ExternalDatabase} that couldn't get constructed.
151+
*
152+
* @author Jens Schauder
153+
*/
154+
static class NoSuchDatabase extends ExternalDatabase {
155+
156+
@Override
157+
boolean checkValidity() {
158+
return false;
159+
}
160+
161+
@Override
162+
public int getPort() {
163+
throw new UnsupportedOperationException();
164+
}
165+
166+
@Override
167+
public String getHostname() {
168+
throw new UnsupportedOperationException();
169+
}
170+
171+
@Override
172+
public String getDatabase() {
173+
throw new UnsupportedOperationException();
174+
}
175+
176+
@Override
177+
public String getUsername() {
178+
throw new UnsupportedOperationException();
179+
}
180+
181+
@Override
182+
public String getPassword() {
183+
throw new UnsupportedOperationException();
184+
}
185+
}
148186
}

src/test/java/org/springframework/data/r2dbc/testing/PostgresTestSupport.java

+46-12
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import io.r2dbc.postgresql.PostgresqlConnectionFactory;
55
import io.r2dbc.spi.ConnectionFactory;
66

7+
import java.util.function.Supplier;
8+
79
import javax.sql.DataSource;
810

911
import org.postgresql.ds.PGSimpleDataSource;
@@ -18,7 +20,7 @@
1820
*/
1921
public class PostgresTestSupport {
2022

21-
private static final PostgreSQLContainer POSTGRESQL_CONTAINER = new PostgreSQLContainer();
23+
private static ExternalDatabase testContainerDatabase;
2224

2325
public static String CREATE_TABLE_LEGOSET = "CREATE TABLE legoset (\n" //
2426
+ " id integer CONSTRAINT id PRIMARY KEY,\n" //
@@ -41,11 +43,29 @@ public class PostgresTestSupport {
4143
*/
4244
public static ExternalDatabase database() {
4345

44-
ExternalDatabase local = local();
45-
if (local.checkValidity()) {
46-
return local;
46+
if (Boolean.getBoolean("spring.data.r2dbc.test.preferLocalDatabase")) {
47+
48+
return getFirstWorkingDatabase( //
49+
PostgresTestSupport::local, //
50+
PostgresTestSupport::testContainer //
51+
);
52+
} else {
53+
54+
return getFirstWorkingDatabase( //
55+
PostgresTestSupport::testContainer, //
56+
PostgresTestSupport::local //
57+
);
58+
}
59+
}
60+
61+
private static ExternalDatabase getFirstWorkingDatabase(Supplier<ExternalDatabase> first,
62+
Supplier<ExternalDatabase> second) {
63+
64+
ExternalDatabase database = first.get();
65+
if (database.checkValidity()) {
66+
return database;
4767
} else {
48-
return testContainer();
68+
return second.get();
4969
}
5070
}
5171

@@ -67,14 +87,27 @@ private static ExternalDatabase local() {
6787
*/
6888
private static ExternalDatabase testContainer() {
6989

70-
POSTGRESQL_CONTAINER.start();
90+
if (testContainerDatabase == null) {
7191

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();
92+
try {
93+
PostgreSQLContainer postgreSQLContainer = new PostgreSQLContainer();
94+
postgreSQLContainer.start();
95+
96+
testContainerDatabase = ProvidedDatabase.builder() //
97+
.hostname("localhost") //
98+
.port(postgreSQLContainer.getFirstMappedPort()) //
99+
.database(postgreSQLContainer.getDatabaseName()) //
100+
.username(postgreSQLContainer.getUsername()) //
101+
.password(postgreSQLContainer.getPassword()).build();
102+
103+
} catch (IllegalStateException ise) {
104+
// docker is not available.
105+
testContainerDatabase = new ExternalDatabase.NoSuchDatabase();
106+
}
107+
108+
}
109+
110+
return testContainerDatabase;
78111
}
79112

80113
/**
@@ -106,4 +139,5 @@ public static DataSource createDataSource(ExternalDatabase database) {
106139

107140
return dataSource;
108141
}
142+
109143
}

0 commit comments

Comments
 (0)