aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-core
diff options
context:
space:
mode:
authorSimon Brandhof <simon.brandhof@sonarsource.com>2014-12-10 22:39:22 +0100
committerSimon Brandhof <simon.brandhof@sonarsource.com>2014-12-10 22:39:22 +0100
commit76f8bad1735d5094ff0698f5634829a76342203a (patch)
treeb5f615ca132471590985eb98e9e9c64506205e16 /sonar-core
parent157f9792353132bdc7bbdcd9c8278deb7de48219 (diff)
downloadsonarqube-76f8bad1735d5094ff0698f5634829a76342203a.tar.gz
sonarqube-76f8bad1735d5094ff0698f5634829a76342203a.zip
Complete DbTester with methods countSql(String sql) and countRowsInTable(String table)
Diffstat (limited to 'sonar-core')
-rw-r--r--sonar-core/src/test/java/org/sonar/core/persistence/DbTester.java18
1 files changed, 17 insertions, 1 deletions
diff --git a/sonar-core/src/test/java/org/sonar/core/persistence/DbTester.java b/sonar-core/src/test/java/org/sonar/core/persistence/DbTester.java
index c87c5cb9d8a..3f7787b5320 100644
--- a/sonar-core/src/test/java/org/sonar/core/persistence/DbTester.java
+++ b/sonar-core/src/test/java/org/sonar/core/persistence/DbTester.java
@@ -19,6 +19,7 @@
*/
package org.sonar.core.persistence;
+import com.google.common.base.Preconditions;
import com.google.common.collect.Maps;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.dbutils.DbUtils;
@@ -169,7 +170,22 @@ public class DbTester extends ExternalResource {
}
}
- public int count(String sql) {
+ /**
+ * Returns the number of rows in the table. Example:
+ * <pre>int issues = countTable("issues")</pre>
+ */
+ public int countRowsOfTable(String tableName) {
+ Preconditions.checkArgument(StringUtils.containsNone(tableName, " "), "Parameter must be the name of a table. Got " + tableName);
+ return countSql("select count(*) from " + tableName);
+ }
+
+ /**
+ * Executes a SQL request starting with "SELECT COUNT(something) FROM", for example:
+ * <pre>int OpenIssues = countSql("select count('id') from issues where status is not null")</pre>
+ */
+ public int countSql(String sql) {
+ Preconditions.checkArgument(StringUtils.contains(sql, "count("),
+ "Parameter must be a SQL request containing 'count(x)' function. Got " + sql);
Connection connection = null;
PreparedStatement stmt = null;
ResultSet rs = null;