Move close methods out of sql datasource utils class

As noticed by @Gnat008
- We need two different implementations for MySQL and SQLite because SQLite uses an older version where #isClosed is not implemented
This commit is contained in:
ljacqu 2017-04-29 08:18:43 +02:00
parent e56a3c0ab6
commit 82d74ca0a7
3 changed files with 32 additions and 147 deletions

View File

@ -29,7 +29,6 @@ import java.util.HashSet;
import java.util.List;
import java.util.Set;
import static fr.xephi.authme.datasource.SqlDataSourceUtils.close;
import static fr.xephi.authme.datasource.SqlDataSourceUtils.logSqlException;
public class MySQL implements DataSource {
@ -979,12 +978,42 @@ public class MySQL implements DataSource {
.build();
}
/**
* Closes a {@link ResultSet} safely.
*
* @param rs the result set to close
*/
private static void close(ResultSet rs) {
try {
if (rs != null && !rs.isClosed()) {
rs.close();
}
} catch (SQLException e) {
ConsoleLogger.logException("Could not close ResultSet", e);
}
}
/**
* Closes a {@link Statement} safely.
*
* @param st the statement set to close
*/
private static void close(Statement st) {
try {
if (st != null && !st.isClosed()) {
st.close();
}
} catch (SQLException e) {
ConsoleLogger.logException("Could not close Statement", e);
}
}
/**
* Checks if the last login column has a type that needs to be migrated.
*
* @param con connection to the database
* @param metaData lastlogin column meta data
* @throws SQLException
* @throws SQLException .
*/
private void migrateLastLoginColumn(Connection con, DatabaseMetaData metaData) throws SQLException {
final int columnType;

View File

@ -2,10 +2,7 @@ package fr.xephi.authme.datasource;
import fr.xephi.authme.ConsoleLogger;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
/**
* Utilities for SQL data sources.
@ -23,53 +20,4 @@ final class SqlDataSourceUtils {
static void logSqlException(SQLException e) {
ConsoleLogger.logException("Error during SQL operation:", e);
}
// We use overloaded close() methods instead of one close(AutoCloseable) method in order to limit the
// checked exceptions to SQLException, which is the only checked exception these classes throw.
/**
* Closes a {@link ResultSet} safely.
*
* @param rs the result set to close
*/
static void close(ResultSet rs) {
try {
if (rs != null && !rs.isClosed()) {
rs.close();
}
} catch (SQLException e) {
ConsoleLogger.logException("Could not close ResultSet", e);
}
}
/**
* Closes a {@link Statement} safely.
*
* @param st the statement set to close
*/
static void close(Statement st) {
try {
if (st != null && !st.isClosed()) {
st.close();
}
} catch (SQLException e) {
ConsoleLogger.logException("Could not close Statement", e);
}
}
/**
* Closes a {@link Connection} safely.
*
* @param con the connection set to close
*/
static void close(Connection con) {
try {
if (con != null && !con.isClosed()) {
con.close();
}
} catch (SQLException e) {
ConsoleLogger.logException("Could not close Connection", e);
}
}
}

View File

@ -4,18 +4,12 @@ import fr.xephi.authme.TestHelper;
import org.junit.Before;
import org.junit.Test;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Logger;
import static org.hamcrest.Matchers.containsString;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
import static org.mockito.hamcrest.MockitoHamcrest.argThat;
import static org.mockito.Mockito.verify;
import static org.mockito.hamcrest.MockitoHamcrest.argThat;
/**
* Test for {@link SqlDataSourceUtils}.
@ -46,90 +40,4 @@ public class SqlDataSourceUtilsTest {
// then
verify(logger).warning(argThat(containsString(msg)));
}
@Test
public void shouldCloseStatement() throws SQLException {
// given
Statement st = mock(Statement.class);
// when
SqlDataSourceUtils.close(st);
// then
verify(st).close();
}
@Test
public void shouldHandleExceptionFromStatement() throws SQLException {
// given
Statement st = mock(Statement.class);
doThrow(SQLException.class).when(st).close();
// when
SqlDataSourceUtils.close(st);
// then
verify(logger).warning(anyString());
}
@Test
public void shouldCloseResultSet() throws SQLException {
// given
ResultSet rs = mock(ResultSet.class);
// when
SqlDataSourceUtils.close(rs);
// then
verify(rs).close();
}
@Test
public void shouldHandleExceptionFromResultSet() throws SQLException {
// given
ResultSet rs = mock(ResultSet.class);
doThrow(SQLException.class).when(rs).close();
// when
SqlDataSourceUtils.close(rs);
// then
verify(logger).warning(anyString());
}
@Test
public void shouldCloseConnection() throws SQLException {
// given
Connection con = mock(Connection.class);
// when
SqlDataSourceUtils.close(con);
// then
verify(con).close();
}
@Test
public void shouldHandleExceptionFromConnection() throws SQLException {
// given
Connection con = mock(Connection.class);
doThrow(SQLException.class).when(con).close();
// when
SqlDataSourceUtils.close(con);
// then
verify(logger).warning(anyString());
}
@Test
public void shouldHandleNullArgument() {
// given / when
SqlDataSourceUtils.close((Statement) null);
SqlDataSourceUtils.close((ResultSet) null);
SqlDataSourceUtils.close((Connection) null);
// then - nothing happens
}
}