]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-3825 do not allow to full scan measures and projects
authorSimon Brandhof <simon.brandhof@gmail.com>
Thu, 6 Dec 2012 14:06:00 +0000 (15:06 +0100)
committerSimon Brandhof <simon.brandhof@gmail.com>
Thu, 6 Dec 2012 14:06:00 +0000 (15:06 +0100)
sonar-core/src/main/java/org/sonar/core/measure/MeasureFilter.java
sonar-core/src/main/java/org/sonar/core/measure/MeasureFilterEngine.java
sonar-core/src/main/java/org/sonar/core/measure/MeasureFilterExecutor.java
sonar-core/src/test/java/org/sonar/core/measure/MeasureFilterExecutorTest.java

index 4145203540bbe24246ff443d4a4bf8d75b7b96e4..3b88f5528bf5687570174f07427462a454983915 100644 (file)
@@ -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);
index d394709789e1d4adb0aac219ad45f536d5d2df7d..69bc849667c3d64420a95492c62409c292114cea 100644 (file)
@@ -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);
index 64179399792e6d819ffe7aeec05d8d2f632c6067..da5004aef389240678378354b33603d6c22ea8bd 100644 (file)
@@ -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;
index a755be6cdbe81d2f07b9a5f3a0bcec4fa7fd3507..3bc247897a21960816a8a2e92279fc1e34c5f189 100644 (file)
@@ -55,6 +55,14 @@ public class MeasureFilterExecutorTest extends AbstractDaoTestCase {
     setupData("shared");
   }
 
+  @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);
@@ -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);