diff options
author | Simon Brandhof <simon.brandhof@sonarsource.com> | 2014-11-16 22:45:40 +0100 |
---|---|---|
committer | Simon Brandhof <simon.brandhof@sonarsource.com> | 2014-11-19 08:27:08 +0100 |
commit | e08827e2b700cd4c76574316a54f8c116e64ccb3 (patch) | |
tree | 4f2d77c5a5b4ec4a95d3b6c4cf3d1ee51909a94e /sonar-core/src | |
parent | ecf3d2f0e87f0d66d2333db4c65d549ba77ce578 (diff) | |
download | sonarqube-e08827e2b700cd4c76574316a54f8c116e64ccb3.tar.gz sonarqube-e08827e2b700cd4c76574316a54f8c116e64ccb3.zip |
Refactor the way to declare ES indices.
- BaseIndex has too many responsibilities
- SearchClient must not override TransportClient, else it can't be easily used in tests
Diffstat (limited to 'sonar-core/src')
-rw-r--r-- | sonar-core/src/test/java/org/sonar/core/persistence/DatabaseCommands.java | 36 | ||||
-rw-r--r-- | sonar-core/src/test/java/org/sonar/core/persistence/TestDatabase.java | 10 |
2 files changed, 31 insertions, 15 deletions
diff --git a/sonar-core/src/test/java/org/sonar/core/persistence/DatabaseCommands.java b/sonar-core/src/test/java/org/sonar/core/persistence/DatabaseCommands.java index 75b9d9bd64a..03b0237a59d 100644 --- a/sonar-core/src/test/java/org/sonar/core/persistence/DatabaseCommands.java +++ b/sonar-core/src/test/java/org/sonar/core/persistence/DatabaseCommands.java @@ -21,6 +21,7 @@ package org.sonar.core.persistence; import com.google.common.base.Preconditions; import com.google.common.collect.ImmutableMap; +import org.apache.commons.dbutils.DbUtils; import org.apache.commons.lang.StringUtils; import org.dbunit.dataset.datatype.DefaultDataTypeFactory; import org.dbunit.dataset.datatype.IDataTypeFactory; @@ -30,9 +31,14 @@ import org.dbunit.ext.mssql.MsSqlDataTypeFactory; import org.dbunit.ext.mysql.MySqlDataTypeFactory; import org.dbunit.ext.oracle.Oracle10DataTypeFactory; import org.dbunit.ext.postgresql.PostgresqlDataTypeFactory; -import org.sonar.core.persistence.dialect.*; +import org.sonar.core.persistence.dialect.Dialect; +import org.sonar.core.persistence.dialect.MsSql; +import org.sonar.core.persistence.dialect.MySql; +import org.sonar.core.persistence.dialect.Oracle; +import org.sonar.core.persistence.dialect.PostgreSql; import javax.sql.DataSource; + import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; @@ -116,21 +122,23 @@ public abstract class DatabaseCommands { public void truncateDatabase(DataSource dataSource) throws SQLException { Connection connection = dataSource.getConnection(); - connection.setAutoCommit(false); - - Statement statement = connection.createStatement(); - for (String table : DatabaseVersion.TABLES) { - try { - statement.executeUpdate("TRUNCATE TABLE " + table); - connection.commit(); - } catch (Exception e) { - // ignore - connection.rollback(); + Statement statement = null; + try { + connection.setAutoCommit(false); + statement = connection.createStatement(); + for (String table : DatabaseVersion.TABLES) { + try { + statement.executeUpdate("TRUNCATE TABLE " + table); + connection.commit(); + } catch (Exception e) { + // ignore + connection.rollback(); + } } + } finally { + DbUtils.closeQuietly(connection); + DbUtils.closeQuietly(statement); } - - statement.close(); - connection.close(); } public void resetPrimaryKeys(DataSource dataSource) throws SQLException { diff --git a/sonar-core/src/test/java/org/sonar/core/persistence/TestDatabase.java b/sonar-core/src/test/java/org/sonar/core/persistence/TestDatabase.java index 56eadb4aace..3f3bd71c3e8 100644 --- a/sonar-core/src/test/java/org/sonar/core/persistence/TestDatabase.java +++ b/sonar-core/src/test/java/org/sonar/core/persistence/TestDatabase.java @@ -127,7 +127,15 @@ public class TestDatabase extends ExternalResource { myBatis = new MyBatis(db, new Logback(), queue); myBatis.start(); - commands.truncateDatabase(db.getDataSource()); + truncateTables(); + } + + public void truncateTables() { + try { + commands.truncateDatabase(db.getDataSource()); + } catch (SQLException e) { + throw new IllegalStateException("Fail to truncate db tables", e); + } } @Override |