3232 * {@link org.testcontainers.containers.PostgreSQLContainer}.
3333 *
3434 * @author Mark Paluch
35+ * @author Jens Schauder
3536 */
3637public 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}
0 commit comments