From 2f10a0b97fb6b011bcacbf9c97c627a49e7dec48 Mon Sep 17 00:00:00 2001 From: Julien Lancelot Date: Thu, 31 Jul 2014 10:28:09 +0200 Subject: [PATCH] SONAR-5429 Ignore incorrect sort --- .../server/measure/MeasureFilterFactory.java | 28 +++++++++++++------ .../measure/MeasureFilterFactoryTest.java | 12 ++++++++ .../WEB-INF/app/helpers/measures_helper.rb | 2 +- 3 files changed, 33 insertions(+), 9 deletions(-) diff --git a/server/sonar-server/src/main/java/org/sonar/server/measure/MeasureFilterFactory.java b/server/sonar-server/src/main/java/org/sonar/server/measure/MeasureFilterFactory.java index 06d5143a6d0..ef1d68bd1c3 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/measure/MeasureFilterFactory.java +++ b/server/sonar-server/src/main/java/org/sonar/server/measure/MeasureFilterFactory.java @@ -22,6 +22,7 @@ package org.sonar.server.measure; import com.google.common.base.Function; import com.google.common.base.Joiner; import com.google.common.base.Strings; +import com.google.common.collect.Iterables; import com.google.common.collect.Lists; import org.apache.commons.lang.StringUtils; import org.sonar.api.ServerComponent; @@ -33,11 +34,10 @@ import org.sonar.api.utils.System2; import javax.annotation.CheckForNull; import javax.annotation.Nullable; -import java.util.Arrays; -import java.util.Calendar; -import java.util.Date; -import java.util.List; -import java.util.Map; + +import java.util.*; + +import static com.google.common.collect.Lists.newArrayList; public class MeasureFilterFactory implements ServerComponent { @@ -111,7 +111,10 @@ public class MeasureFilterFactory implements ServerComponent { } } } else { - filter.setSortOn(MeasureFilterSort.Field.valueOf(s.toUpperCase())); + String sort = s.toUpperCase(); + if (sortFieldLabels().contains(sort)) { + filter.setSortOn(MeasureFilterSort.Field.valueOf(sort)); + } } } @@ -120,6 +123,15 @@ public class MeasureFilterFactory implements ServerComponent { } } + private List sortFieldLabels(){ + return newArrayList(Iterables.transform(Arrays.asList(MeasureFilterSort.Field.values()), new Function() { + @Override + public String apply(@Nullable MeasureFilterSort.Field input) { + return input != null ? input.name() : null; + } + })); + } + @CheckForNull private MeasureFilterCondition toCondition(Map props, int index) { MeasureFilterCondition condition = null; @@ -139,7 +151,7 @@ public class MeasureFilterFactory implements ServerComponent { } @CheckForNull - private MeasureFilterCondition alertToCondition(List alertLevels) { + private MeasureFilterCondition alertToCondition(@Nullable List alertLevels) { if (alertLevels == null || alertLevels.isEmpty()) { return null; } @@ -148,7 +160,7 @@ public class MeasureFilterFactory implements ServerComponent { String op = "in"; List alertLevelsUppercase = Lists.transform(alertLevels, new Function() { @Override - public String apply(String input) { + public String apply(@Nullable String input) { return input != null ? input.toUpperCase() : ""; } }); diff --git a/server/sonar-server/src/test/java/org/sonar/server/measure/MeasureFilterFactoryTest.java b/server/sonar-server/src/test/java/org/sonar/server/measure/MeasureFilterFactoryTest.java index bd6348e7a28..e653988c431 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/measure/MeasureFilterFactoryTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/measure/MeasureFilterFactoryTest.java @@ -89,6 +89,18 @@ public class MeasureFilterFactoryTest { assertThat(filter.sort().isAsc()).isTrue(); } + @Test + public void fallback_on_name_sort_when_sort_is_unknown() { + MeasureFilterFactory factory = new MeasureFilterFactory(newMetricFinder(), system); + Map props = ImmutableMap.of("sort", "unknown"); + MeasureFilter filter = factory.create(props); + + assertThat(filter.sort().column()).isEqualTo("p.long_name"); + assertThat(filter.sort().metric()).isNull(); + assertThat(filter.sort().period()).isNull(); + assertThat(filter.sort().isAsc()).isTrue(); + } + @Test public void descending_sort() { MeasureFilterFactory factory = new MeasureFilterFactory(newMetricFinder(), system); diff --git a/server/sonar-web/src/main/webapp/WEB-INF/app/helpers/measures_helper.rb b/server/sonar-web/src/main/webapp/WEB-INF/app/helpers/measures_helper.rb index 23d1e6879d3..b316df15a2e 100644 --- a/server/sonar-web/src/main/webapp/WEB-INF/app/helpers/measures_helper.rb +++ b/server/sonar-web/src/main/webapp/WEB-INF/app/helpers/measures_helper.rb @@ -28,7 +28,7 @@ module MeasuresHelper if column.period html += "
#{Api::Utils.period_abbreviation(column.period)}" end - if filter.sort_key==column.key + if column.sort? && filter.sort_key==column.key html << (filter.sort_asc? ? ' ' : ' ') end "#{html}" -- 2.39.5