diff --git a/src/main/java/fr/xephi/authme/datasource/MySQL.java b/src/main/java/fr/xephi/authme/datasource/MySQL.java index 0bd6ba86..d04c7c3b 100644 --- a/src/main/java/fr/xephi/authme/datasource/MySQL.java +++ b/src/main/java/fr/xephi/authme/datasource/MySQL.java @@ -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; diff --git a/src/main/java/fr/xephi/authme/datasource/SqlDataSourceUtils.java b/src/main/java/fr/xephi/authme/datasource/SqlDataSourceUtils.java index 7a25b75c..7b9c6aae 100644 --- a/src/main/java/fr/xephi/authme/datasource/SqlDataSourceUtils.java +++ b/src/main/java/fr/xephi/authme/datasource/SqlDataSourceUtils.java @@ -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); - } - } } diff --git a/src/test/java/fr/xephi/authme/datasource/SqlDataSourceUtilsTest.java b/src/test/java/fr/xephi/authme/datasource/SqlDataSourceUtilsTest.java index 94650a56..ca070f59 100644 --- a/src/test/java/fr/xephi/authme/datasource/SqlDataSourceUtilsTest.java +++ b/src/test/java/fr/xephi/authme/datasource/SqlDataSourceUtilsTest.java @@ -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 - } - }