aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-core/src
diff options
context:
space:
mode:
Diffstat (limited to 'sonar-core/src')
-rw-r--r--sonar-core/src/main/java/org/sonar/core/measure/MeasureFilter.java9
-rw-r--r--sonar-core/src/main/java/org/sonar/core/measure/MeasureFilterEngine.java4
-rw-r--r--sonar-core/src/main/java/org/sonar/core/measure/MeasureFilterExecutor.java4
-rw-r--r--sonar-core/src/test/java/org/sonar/core/measure/MeasureFilterExecutorTest.java14
4 files changed, 24 insertions, 7 deletions
diff --git a/sonar-core/src/main/java/org/sonar/core/measure/MeasureFilter.java b/sonar-core/src/main/java/org/sonar/core/measure/MeasureFilter.java
index 4145203540b..3b88f5528bf 100644
--- a/sonar-core/src/main/java/org/sonar/core/measure/MeasureFilter.java
+++ b/sonar-core/src/main/java/org/sonar/core/measure/MeasureFilter.java
@@ -22,6 +22,7 @@ package org.sonar.core.measure;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Strings;
import com.google.common.collect.Lists;
+import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.builder.ReflectionToStringBuilder;
import org.sonar.api.measures.Metric;
import org.sonar.api.resources.Qualifiers;
@@ -44,8 +45,8 @@ public class MeasureFilter {
private List<String> resourceScopes = Collections.emptyList();
private List<String> resourceQualifiers = Collections.emptyList();
private List<String> resourceLanguages = Collections.emptyList();
- private String resourceKeyRegexp;
- private String resourceName;
+ private String resourceKeyRegexp = null;
+ private String resourceName = null;
private Date fromDate = null, toDate = null;
private boolean userFavourites = false;
@@ -194,6 +195,10 @@ public class MeasureFilter {
return sort;
}
+ public boolean isEmpty() {
+ return resourceQualifiers.isEmpty() && resourceScopes.isEmpty() && StringUtils.isEmpty(baseResourceKey) && baseResourceId==null;
+ }
+
@VisibleForTesting
static List<String> sanitize(@Nullable List<String> list) {
return isBlank(list) ? Collections.<String>emptyList() : Lists.newArrayList(list);
diff --git a/sonar-core/src/main/java/org/sonar/core/measure/MeasureFilterEngine.java b/sonar-core/src/main/java/org/sonar/core/measure/MeasureFilterEngine.java
index d394709789e..69bc849667c 100644
--- a/sonar-core/src/main/java/org/sonar/core/measure/MeasureFilterEngine.java
+++ b/sonar-core/src/main/java/org/sonar/core/measure/MeasureFilterEngine.java
@@ -20,6 +20,7 @@
package org.sonar.core.measure;
import com.google.common.annotations.VisibleForTesting;
+import com.google.common.base.Joiner;
import org.apache.commons.lang.SystemUtils;
import org.json.simple.parser.ParseException;
import org.slf4j.Logger;
@@ -47,10 +48,9 @@ public class MeasureFilterEngine implements ServerComponent {
@VisibleForTesting
List<MeasureFilterRow> execute(Map<String, Object> filterMap, @Nullable Long userId, Logger logger) throws ParseException {
-
MeasureFilterContext context = new MeasureFilterContext();
context.setUserId(userId);
- context.setData(filterMap.toString());
+ context.setData(String.format("{%s}", Joiner.on('|').withKeyValueSeparator("=").join(filterMap)));
try {
long start = System.currentTimeMillis();
MeasureFilter filter = factory.create(filterMap);
diff --git a/sonar-core/src/main/java/org/sonar/core/measure/MeasureFilterExecutor.java b/sonar-core/src/main/java/org/sonar/core/measure/MeasureFilterExecutor.java
index 64179399792..da5004aef38 100644
--- a/sonar-core/src/main/java/org/sonar/core/measure/MeasureFilterExecutor.java
+++ b/sonar-core/src/main/java/org/sonar/core/measure/MeasureFilterExecutor.java
@@ -45,6 +45,10 @@ public class MeasureFilterExecutor implements ServerComponent {
}
public List<MeasureFilterRow> execute(MeasureFilter filter, MeasureFilterContext context) throws SQLException {
+ if (filter.isEmpty()) {
+ return Collections.emptyList();
+ }
+
List<MeasureFilterRow> rows;
SqlSession session = null;
Connection connection = null;
diff --git a/sonar-core/src/test/java/org/sonar/core/measure/MeasureFilterExecutorTest.java b/sonar-core/src/test/java/org/sonar/core/measure/MeasureFilterExecutorTest.java
index a755be6cdbe..3bc247897a2 100644
--- a/sonar-core/src/test/java/org/sonar/core/measure/MeasureFilterExecutorTest.java
+++ b/sonar-core/src/test/java/org/sonar/core/measure/MeasureFilterExecutorTest.java
@@ -56,6 +56,14 @@ public class MeasureFilterExecutorTest extends AbstractDaoTestCase {
}
@Test
+ public void should_return_empty_results_if_empty_filter() throws SQLException {
+ MeasureFilter filter = new MeasureFilter();
+ assertThat(filter.isEmpty()).isTrue();
+
+ assertThat(executor.execute(filter, new MeasureFilterContext())).isEmpty();
+ }
+
+ @Test
public void invalid_filter_should_not_return_results() throws SQLException {
MeasureFilter filter = new MeasureFilter().setUserFavourites(true);
// anonymous user does not have favourites
@@ -371,7 +379,7 @@ public class MeasureFilterExecutorTest extends AbstractDaoTestCase {
}
@Test
- public void filter_by_parent_without_children() throws SQLException {
+ public void filter_by_parent_without_children() throws Exception {
MeasureFilter filter = new MeasureFilter().setResourceQualifiers(Arrays.asList("TRK", "PAC", "CLA")).setBaseResourceKey("java_project:org.sonar.foo.Big").setOnBaseResourceChildren(true);
List<MeasureFilterRow> rows = executor.execute(filter, new MeasureFilterContext());
@@ -379,8 +387,8 @@ public class MeasureFilterExecutorTest extends AbstractDaoTestCase {
}
@Test
- public void filter_by_user_favourites() throws SQLException {
- MeasureFilter filter = new MeasureFilter().setUserFavourites(true);
+ public void filter_by_user_favourites() throws Exception {
+ MeasureFilter filter = new MeasureFilter().setResourceQualifiers(Arrays.asList("TRK", "FIL")).setUserFavourites(true);
List<MeasureFilterRow> rows = executor.execute(filter, new MeasureFilterContext().setUserId(50L));
assertThat(rows).hasSize(2);