#29 - Polishing.
Add author tags. Add static factory method to create an unavailable database object instance. Simplify database selection. Javadoc. Original pull request: #51.
This commit is contained in:
@@ -32,11 +32,21 @@ import org.slf4j.LoggerFactory;
|
||||
* {@link org.testcontainers.containers.PostgreSQLContainer}.
|
||||
*
|
||||
* @author Mark Paluch
|
||||
* @author Jens Schauder
|
||||
*/
|
||||
public abstract class ExternalDatabase extends ExternalResource {
|
||||
|
||||
private static Logger LOG = LoggerFactory.getLogger(ExternalDatabase.class);
|
||||
|
||||
/**
|
||||
* Construct an absent database that is used as {@literal null} object if no database is available.
|
||||
*
|
||||
* @return an absent database.
|
||||
*/
|
||||
public static ExternalDatabase unavailable() {
|
||||
return NoAvailableDatabase.INSTANCE;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the post of the database service.
|
||||
*/
|
||||
@@ -70,7 +80,7 @@ public abstract class ExternalDatabase extends ExternalResource {
|
||||
}
|
||||
|
||||
/**
|
||||
* performs a test if the database can actually be reached.
|
||||
* Performs a test if the database can actually be reached.
|
||||
*
|
||||
* @return true, if the database could be reached.
|
||||
*/
|
||||
@@ -79,7 +89,7 @@ public abstract class ExternalDatabase extends ExternalResource {
|
||||
try (Socket socket = new Socket()) {
|
||||
|
||||
socket.connect(new InetSocketAddress(getHostname(), getPort()), Math.toIntExact(TimeUnit.SECONDS.toMillis(5)));
|
||||
return true;
|
||||
return true;
|
||||
|
||||
} catch (IOException e) {
|
||||
LOG.debug("external database not available.", e);
|
||||
@@ -151,8 +161,13 @@ public abstract class ExternalDatabase extends ExternalResource {
|
||||
*
|
||||
* @author Jens Schauder
|
||||
*/
|
||||
static class NoSuchDatabase extends ExternalDatabase {
|
||||
private static class NoAvailableDatabase extends ExternalDatabase {
|
||||
|
||||
private static final NoAvailableDatabase INSTANCE = new NoAvailableDatabase();
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.data.jdbc.core.function.ExternalDatabase#getPort()
|
||||
*/
|
||||
@Override
|
||||
boolean checkValidity() {
|
||||
return false;
|
||||
@@ -160,27 +175,39 @@ public abstract class ExternalDatabase extends ExternalResource {
|
||||
|
||||
@Override
|
||||
public int getPort() {
|
||||
throw new UnsupportedOperationException();
|
||||
throw new UnsupportedOperationException(getClass().getSimpleName());
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.data.jdbc.core.function.ExternalDatabase#getHostname()
|
||||
*/
|
||||
@Override
|
||||
public String getHostname() {
|
||||
throw new UnsupportedOperationException();
|
||||
throw new UnsupportedOperationException(getClass().getSimpleName());
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.data.jdbc.core.function.ExternalDatabase#getDatabase()
|
||||
*/
|
||||
@Override
|
||||
public String getDatabase() {
|
||||
throw new UnsupportedOperationException();
|
||||
throw new UnsupportedOperationException(getClass().getSimpleName());
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.data.jdbc.core.function.ExternalDatabase#getUsername()
|
||||
*/
|
||||
@Override
|
||||
public String getUsername() {
|
||||
throw new UnsupportedOperationException();
|
||||
throw new UnsupportedOperationException(getClass().getSimpleName());
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.data.jdbc.core.function.ExternalDatabase#getPassword()
|
||||
*/
|
||||
@Override
|
||||
public String getPassword() {
|
||||
throw new UnsupportedOperationException();
|
||||
throw new UnsupportedOperationException(getClass().getSimpleName());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@ import io.r2dbc.postgresql.PostgresqlConnectionFactory;
|
||||
import io.r2dbc.spi.ConnectionFactory;
|
||||
|
||||
import java.util.function.Supplier;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
@@ -58,15 +59,13 @@ public class PostgresTestSupport {
|
||||
}
|
||||
}
|
||||
|
||||
private static ExternalDatabase getFirstWorkingDatabase(Supplier<ExternalDatabase> first,
|
||||
Supplier<ExternalDatabase> second) {
|
||||
@SafeVarargs
|
||||
private static ExternalDatabase getFirstWorkingDatabase(Supplier<ExternalDatabase>... suppliers) {
|
||||
|
||||
ExternalDatabase database = first.get();
|
||||
if (database.checkValidity()) {
|
||||
return database;
|
||||
} else {
|
||||
return second.get();
|
||||
}
|
||||
return Stream.of(suppliers).map(Supplier::get) //
|
||||
.filter(ExternalDatabase::checkValidity) //
|
||||
.findFirst() //
|
||||
.orElse(ExternalDatabase.unavailable());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -101,8 +100,8 @@ public class PostgresTestSupport {
|
||||
.password(postgreSQLContainer.getPassword()).build();
|
||||
|
||||
} catch (IllegalStateException ise) {
|
||||
// docker is not available.
|
||||
testContainerDatabase = new ExternalDatabase.NoSuchDatabase();
|
||||
// docker not available.
|
||||
testContainerDatabase = ExternalDatabase.unavailable();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user