32
32
* {@link org.testcontainers.containers.PostgreSQLContainer}.
33
33
*
34
34
* @author Mark Paluch
35
+ * @author Jens Schauder
35
36
*/
36
37
public abstract class ExternalDatabase extends ExternalResource {
37
38
38
39
private static Logger LOG = LoggerFactory .getLogger (ExternalDatabase .class );
39
40
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
+
40
50
/**
41
51
* @return the post of the database service.
42
52
*/
@@ -70,7 +80,7 @@ protected void before() {
70
80
}
71
81
72
82
/**
73
- * performs a test if the database can actually be reached.
83
+ * Performs a test if the database can actually be reached.
74
84
*
75
85
* @return true, if the database could be reached.
76
86
*/
@@ -79,7 +89,7 @@ boolean checkValidity() {
79
89
try (Socket socket = new Socket ()) {
80
90
81
91
socket .connect (new InetSocketAddress (getHostname (), getPort ()), Math .toIntExact (TimeUnit .SECONDS .toMillis (5 )));
82
- return true ;
92
+ return true ;
83
93
84
94
} catch (IOException e ) {
85
95
LOG .debug ("external database not available." , e );
@@ -151,36 +161,53 @@ public String getPassword() {
151
161
*
152
162
* @author Jens Schauder
153
163
*/
154
- static class NoSuchDatabase extends ExternalDatabase {
164
+ private static class NoAvailableDatabase extends ExternalDatabase {
165
+
166
+ private static final NoAvailableDatabase INSTANCE = new NoAvailableDatabase ();
155
167
168
+ /* (non-Javadoc)
169
+ * @see org.springframework.data.jdbc.core.function.ExternalDatabase#getPort()
170
+ */
156
171
@ Override
157
172
boolean checkValidity () {
158
173
return false ;
159
174
}
160
175
161
176
@ Override
162
177
public int getPort () {
163
- throw new UnsupportedOperationException ();
178
+ throw new UnsupportedOperationException (getClass (). getSimpleName () );
164
179
}
165
180
181
+ /* (non-Javadoc)
182
+ * @see org.springframework.data.jdbc.core.function.ExternalDatabase#getHostname()
183
+ */
166
184
@ Override
167
185
public String getHostname () {
168
- throw new UnsupportedOperationException ();
186
+ throw new UnsupportedOperationException (getClass (). getSimpleName () );
169
187
}
170
188
189
+ /* (non-Javadoc)
190
+ * @see org.springframework.data.jdbc.core.function.ExternalDatabase#getDatabase()
191
+ */
171
192
@ Override
172
193
public String getDatabase () {
173
- throw new UnsupportedOperationException ();
194
+ throw new UnsupportedOperationException (getClass (). getSimpleName () );
174
195
}
175
196
197
+ /* (non-Javadoc)
198
+ * @see org.springframework.data.jdbc.core.function.ExternalDatabase#getUsername()
199
+ */
176
200
@ Override
177
201
public String getUsername () {
178
- throw new UnsupportedOperationException ();
202
+ throw new UnsupportedOperationException (getClass (). getSimpleName () );
179
203
}
180
204
205
+ /* (non-Javadoc)
206
+ * @see org.springframework.data.jdbc.core.function.ExternalDatabase#getPassword()
207
+ */
181
208
@ Override
182
209
public String getPassword () {
183
- throw new UnsupportedOperationException ();
210
+ throw new UnsupportedOperationException (getClass (). getSimpleName () );
184
211
}
185
212
}
186
213
}
0 commit comments