Skip to content

Commit f90667b

Browse files
committed
#29 - Polishing.
Add author tags. Add static factory method to create an unavailable database object instance. Simplify database selection. Javadoc. Original pull request: #51.
1 parent 6f8bca6 commit f90667b

File tree

2 files changed

+44
-18
lines changed

2 files changed

+44
-18
lines changed

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

+35-8
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,21 @@
3232
* {@link org.testcontainers.containers.PostgreSQLContainer}.
3333
*
3434
* @author Mark Paluch
35+
* @author Jens Schauder
3536
*/
3637
public abstract class ExternalDatabase extends ExternalResource {
3738

3839
private static Logger LOG = LoggerFactory.getLogger(ExternalDatabase.class);
3940

41+
/**
42+
* Construct an absent database that is used as {@literal null} object if no database is available.
43+
*
44+
* @return an absent database.
45+
*/
46+
public static ExternalDatabase unavailable() {
47+
return NoAvailableDatabase.INSTANCE;
48+
}
49+
4050
/**
4151
* @return the post of the database service.
4252
*/
@@ -70,7 +80,7 @@ protected void before() {
7080
}
7181

7282
/**
73-
* performs a test if the database can actually be reached.
83+
* Performs a test if the database can actually be reached.
7484
*
7585
* @return true, if the database could be reached.
7686
*/
@@ -79,7 +89,7 @@ boolean checkValidity() {
7989
try (Socket socket = new Socket()) {
8090

8191
socket.connect(new InetSocketAddress(getHostname(), getPort()), Math.toIntExact(TimeUnit.SECONDS.toMillis(5)));
82-
return true;
92+
return true;
8393

8494
} catch (IOException e) {
8595
LOG.debug("external database not available.", e);
@@ -151,36 +161,53 @@ public String getPassword() {
151161
*
152162
* @author Jens Schauder
153163
*/
154-
static class NoSuchDatabase extends ExternalDatabase {
164+
private static class NoAvailableDatabase extends ExternalDatabase {
165+
166+
private static final NoAvailableDatabase INSTANCE = new NoAvailableDatabase();
155167

168+
/* (non-Javadoc)
169+
* @see org.springframework.data.jdbc.core.function.ExternalDatabase#getPort()
170+
*/
156171
@Override
157172
boolean checkValidity() {
158173
return false;
159174
}
160175

161176
@Override
162177
public int getPort() {
163-
throw new UnsupportedOperationException();
178+
throw new UnsupportedOperationException(getClass().getSimpleName());
164179
}
165180

181+
/* (non-Javadoc)
182+
* @see org.springframework.data.jdbc.core.function.ExternalDatabase#getHostname()
183+
*/
166184
@Override
167185
public String getHostname() {
168-
throw new UnsupportedOperationException();
186+
throw new UnsupportedOperationException(getClass().getSimpleName());
169187
}
170188

189+
/* (non-Javadoc)
190+
* @see org.springframework.data.jdbc.core.function.ExternalDatabase#getDatabase()
191+
*/
171192
@Override
172193
public String getDatabase() {
173-
throw new UnsupportedOperationException();
194+
throw new UnsupportedOperationException(getClass().getSimpleName());
174195
}
175196

197+
/* (non-Javadoc)
198+
* @see org.springframework.data.jdbc.core.function.ExternalDatabase#getUsername()
199+
*/
176200
@Override
177201
public String getUsername() {
178-
throw new UnsupportedOperationException();
202+
throw new UnsupportedOperationException(getClass().getSimpleName());
179203
}
180204

205+
/* (non-Javadoc)
206+
* @see org.springframework.data.jdbc.core.function.ExternalDatabase#getPassword()
207+
*/
181208
@Override
182209
public String getPassword() {
183-
throw new UnsupportedOperationException();
210+
throw new UnsupportedOperationException(getClass().getSimpleName());
184211
}
185212
}
186213
}

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

+9-10
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import io.r2dbc.spi.ConnectionFactory;
66

77
import java.util.function.Supplier;
8+
import java.util.stream.Stream;
89

910
import javax.sql.DataSource;
1011

@@ -58,15 +59,13 @@ public static ExternalDatabase database() {
5859
}
5960
}
6061

61-
private static ExternalDatabase getFirstWorkingDatabase(Supplier<ExternalDatabase> first,
62-
Supplier<ExternalDatabase> second) {
62+
@SafeVarargs
63+
private static ExternalDatabase getFirstWorkingDatabase(Supplier<ExternalDatabase>... suppliers) {
6364

64-
ExternalDatabase database = first.get();
65-
if (database.checkValidity()) {
66-
return database;
67-
} else {
68-
return second.get();
69-
}
65+
return Stream.of(suppliers).map(Supplier::get) //
66+
.filter(ExternalDatabase::checkValidity) //
67+
.findFirst() //
68+
.orElse(ExternalDatabase.unavailable());
7069
}
7170

7271
/**
@@ -101,8 +100,8 @@ private static ExternalDatabase testContainer() {
101100
.password(postgreSQLContainer.getPassword()).build();
102101

103102
} catch (IllegalStateException ise) {
104-
// docker is not available.
105-
testContainerDatabase = new ExternalDatabase.NoSuchDatabase();
103+
// docker not available.
104+
testContainerDatabase = ExternalDatabase.unavailable();
106105
}
107106

108107
}

0 commit comments

Comments
 (0)