From f9d33a50c6c91879d0e0525c1a1659f03f9d35d7 Mon Sep 17 00:00:00 2001 From: Simon Brandhof Date: Thu, 6 Dec 2012 15:06:00 +0100 Subject: [PATCH] SONAR-3825 do not allow to full scan measures and projects --- .../java/org/sonar/core/measure/MeasureFilter.java | 9 +++++++-- .../sonar/core/measure/MeasureFilterEngine.java | 4 ++-- .../sonar/core/measure/MeasureFilterExecutor.java | 4 ++++ .../core/measure/MeasureFilterExecutorTest.java | 14 +++++++++++--- 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 resourceScopes = Collections.emptyList(); private List resourceQualifiers = Collections.emptyList(); private List 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 sanitize(@Nullable List list) { return isBlank(list) ? Collections.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 execute(Map 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 execute(MeasureFilter filter, MeasureFilterContext context) throws SQLException { + if (filter.isEmpty()) { + return Collections.emptyList(); + } + List 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 @@ -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 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 rows = executor.execute(filter, new MeasureFilterContext().setUserId(50L)); assertThat(rows).hasSize(2); -- 2.39.5