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;
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;
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);
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;
@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);
}
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;
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);
}
@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());
}
@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);