diff options
author | Simon Brandhof <simon.brandhof@gmail.com> | 2013-07-05 10:02:17 +0200 |
---|---|---|
committer | Simon Brandhof <simon.brandhof@gmail.com> | 2013-07-05 10:02:17 +0200 |
commit | 44398cd47f416547002ed4830a7cbea2ae6f4186 (patch) | |
tree | 0f76714a91e8f3d2c5b51f6b692d9d7147e16f49 /sonar-core/src | |
parent | 070d16bb14152fbda6f2a7e3eb13f5ca752d42ad (diff) | |
download | sonarqube-44398cd47f416547002ed4830a7cbea2ae6f4186.tar.gz sonarqube-44398cd47f416547002ed4830a7cbea2ae6f4186.zip |
SONAR-4470 revert org.sonar.core.persistence.DatabaseUtils
Diffstat (limited to 'sonar-core/src')
-rw-r--r-- | sonar-core/src/main/java/org/sonar/core/persistence/DatabaseUtils.java | 69 | ||||
-rw-r--r-- | sonar-core/src/test/java/org/sonar/core/persistence/DatabaseUtilsTest.java | 146 |
2 files changed, 215 insertions, 0 deletions
diff --git a/sonar-core/src/main/java/org/sonar/core/persistence/DatabaseUtils.java b/sonar-core/src/main/java/org/sonar/core/persistence/DatabaseUtils.java new file mode 100644 index 00000000000..1a2db153130 --- /dev/null +++ b/sonar-core/src/main/java/org/sonar/core/persistence/DatabaseUtils.java @@ -0,0 +1,69 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2013 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * SonarQube is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.core.persistence; + +import org.slf4j.LoggerFactory; + +import javax.annotation.Nullable; +import java.sql.Connection; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; + +/** + * @since 2.13 + */ +public final class DatabaseUtils { + private DatabaseUtils() { + } + + public static void closeQuietly(@Nullable Connection connection) { + if (connection != null) { + try { + connection.close(); + } catch (SQLException e) { + LoggerFactory.getLogger(DatabaseUtils.class).warn("Fail to close connection", e); + // ignore + } + } + } + + public static void closeQuietly(@Nullable Statement stmt) { + if (stmt != null) { + try { + stmt.close(); + } catch (SQLException e) { + LoggerFactory.getLogger(DatabaseUtils.class).warn("Fail to close statement", e); + // ignore + } + } + } + + public static void closeQuietly(@Nullable ResultSet rs) { + if (rs != null) { + try { + rs.close(); + } catch (SQLException e) { + LoggerFactory.getLogger(DatabaseUtils.class).warn("Fail to close result set", e); + // ignore + } + } + } +} diff --git a/sonar-core/src/test/java/org/sonar/core/persistence/DatabaseUtilsTest.java b/sonar-core/src/test/java/org/sonar/core/persistence/DatabaseUtilsTest.java new file mode 100644 index 00000000000..28e1ef948f7 --- /dev/null +++ b/sonar-core/src/test/java/org/sonar/core/persistence/DatabaseUtilsTest.java @@ -0,0 +1,146 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2013 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * SonarQube is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.core.persistence; + +import org.junit.Test; +import org.sonar.core.persistence.dialect.Oracle; + +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; + +import static org.fest.assertions.Assertions.assertThat; +import static org.mockito.Mockito.doThrow; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; + +public class DatabaseUtilsTest extends AbstractDaoTestCase { + + @Test + public void should_close_connection() throws SQLException { + Connection connection = getConnection(); + assertThat(isClosed(connection)).isFalse(); + + DatabaseUtils.closeQuietly(connection); + assertThat(isClosed(connection)).isTrue(); + } + + @Test + public void should_support_null_connection() { + DatabaseUtils.closeQuietly((Connection) null); + // no failure + } + + @Test + public void should_close_statement_and_resultset() throws SQLException { + Connection connection = getConnection(); + try { + PreparedStatement statement = connection.prepareStatement(selectDual()); + ResultSet rs = statement.executeQuery(); + + DatabaseUtils.closeQuietly(rs); + DatabaseUtils.closeQuietly(statement); + + assertThat(isClosed(statement)).isTrue(); + assertThat(isClosed(rs)).isTrue(); + } finally { + DatabaseUtils.closeQuietly(connection); + } + } + + @Test + public void should_not_fail_on_connection_errors() throws SQLException { + Connection connection = mock(Connection.class); + doThrow(new SQLException()).when(connection).close(); + + DatabaseUtils.closeQuietly(connection); + + // no failure + verify(connection).close(); // just to be sure + } + + @Test + public void should_not_fail_on_statement_errors() throws SQLException { + Statement statement = mock(Statement.class); + doThrow(new SQLException()).when(statement).close(); + + DatabaseUtils.closeQuietly(statement); + + // no failure + verify(statement).close(); // just to be sure + } + + @Test + public void should_not_fail_on_resulset_errors() throws SQLException { + ResultSet rs = mock(ResultSet.class); + doThrow(new SQLException()).when(rs).close(); + + DatabaseUtils.closeQuietly(rs); + + // no failure + verify(rs).close(); // just to be sure + } + + /** + * Connection.isClosed() has been introduced in java 1.6 + */ + private boolean isClosed(Connection c) { + try { + c.createStatement().execute(selectDual()); + return false; + } catch (Exception e) { + return true; + } + } + + /** + * Statement.isClosed() has been introduced in java 1.6 + */ + private boolean isClosed(Statement s) { + try { + s.execute("SELECT 1"); + return false; + } catch (Exception e) { + return true; + } + } + + /** + * ResultSet.isClosed() has been introduced in java 1.6 + */ + private boolean isClosed(ResultSet rs) { + try { + rs.next(); + return false; + } catch (Exception e) { + return true; + } + } + + private String selectDual() { + String sql = "SELECT 1"; + if (Oracle.ID.equals(getDatabase().getDialect().getId())) { + sql = "SELECT 1 FROM DUAL"; + } + return sql; + } +} |