From: Simon Brandhof Date: Thu, 18 Oct 2012 09:24:03 +0000 (+0200) Subject: Fix compatibility with Java 5 X-Git-Tag: 3.4~471 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=a94d6ad391dcc901818a80797b2e91d3b07edf19;p=sonarqube.git Fix compatibility with Java 5 --- 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..7c0080db435 --- /dev/null +++ b/sonar-core/src/test/java/org/sonar/core/persistence/DatabaseUtilsTest.java @@ -0,0 +1,137 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2008-2012 SonarSource + * mailto:contact AT sonarsource DOT com + * + * Sonar 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. + * + * Sonar 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 Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.core.persistence; + +import org.junit.Test; + +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("SELECT 1"); + 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("SELECT 1"); + 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; + } + } +}