@Slf4j
@RequiredArgsConstructor
public class TestContainerPostgresSQLDelegate extends AbstractDatabaseDelegate<Connection> {
private final ContainerState container;
@Override
protected Connection createNewConnection() {
try {
Properties connectionProps = new Properties();
connectionProps.put("user", "test"); (1)
connectionProps.put("password", "test"); (2)
return DriverManager.getConnection(
String.format("jdbc:postgresql://localhost:%s/test", container.getFirstMappedPort()),
connectionProps); (3)
} catch (Exception e) {
log.error("Could not obtain PostgresSQL connection");
throw new ConnectionCreationException("Could not obtain PostgresSQL connection", e);
}
}
@Override
public void execute(String statement, String scriptPath, int lineNumber, boolean continueOnError, boolean ignoreFailedDrops) {
try {
ResultSet result = getConnection().prepareStatement(statement).executeQuery();
result.next();
if (result.getObject(1, Integer.class).equals(666)) { (4)
log.debug("Statement {} was applied", statement);
} else {
throw new ScriptUtils.ScriptStatementFailedException(statement, lineNumber, scriptPath);
}
} catch (Exception e) {
throw new ScriptUtils.ScriptStatementFailedException(statement, lineNumber, scriptPath, e);
}
}
@Override
protected void closeConnectionQuietly(Connection connection) {
try {
connection.close();
} catch (Exception e) {
log.error("Could not close PostgresSQL connection", e);
}
}
}