]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-5395 Fail to save a measure filter when name criteria contains a comma
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Thu, 31 Jul 2014 11:22:18 +0000 (13:22 +0200)
committerJulien Lancelot <julien.lancelot@sonarsource.com>
Thu, 31 Jul 2014 11:22:25 +0000 (13:22 +0200)
server/sonar-server/src/main/java/org/sonar/server/measure/MeasureFilterFactory.java
server/sonar-server/src/test/java/org/sonar/server/measure/MeasureFilterFactoryTest.java
server/sonar-web/src/main/webapp/WEB-INF/app/models/measure_filter.rb
server/sonar-web/src/main/webapp/WEB-INF/app/views/measures/search.html.erb

index ef1d68bd1c36b54250539d76a856824df46cf5a4..bb6dcdda810598b6d8584fcbd34bd7c62a413b41 100644 (file)
@@ -62,7 +62,7 @@ public class MeasureFilterFactory implements ServerComponent {
     if (onBaseComponents != null) {
       filter.setOnBaseResourceChildren(Boolean.valueOf(onBaseComponents));
     }
-    filter.setResourceName((String) properties.get("nameSearch"));
+    filter.setResourceName(toString(properties.get("nameSearch")));
     filter.setResourceKey((String) properties.get("keySearch"));
     String onFavourites = (String) properties.get("onFavourites");
     if (onFavourites != null) {
@@ -204,4 +204,19 @@ public class MeasureFilterFactory implements ServerComponent {
     return null;
   }
 
+  @CheckForNull
+  public static String toString(@Nullable Object o) {
+    if (o != null) {
+      if (o instanceof List) {
+        return Joiner.on(",").join((List) o);
+      } else if (o instanceof String[]) {
+        // assume that it contains only strings
+        return Joiner.on(",").join((String[]) o);
+      } else {
+        return o.toString();
+      }
+    }
+    return null;
+  }
+
 }
index e653988c43136ad6fce7a653c03072bf2d5adffd..20cc5872beeeeaa70c916a781b08b0a3cc0f2337 100644 (file)
@@ -33,6 +33,7 @@ import java.util.Arrays;
 import java.util.List;
 import java.util.Map;
 
+import static com.google.common.collect.Lists.newArrayList;
 import static org.fest.assertions.Assertions.assertThat;
 import static org.mockito.Matchers.anyString;
 import static org.mockito.Mockito.mock;
@@ -207,6 +208,39 @@ public class MeasureFilterFactoryTest {
     assertThat(conditions.get(0).period()).isNull();
   }
 
+  @Test
+  public void name_conditions() {
+    MeasureFilterFactory factory = new MeasureFilterFactory(newMetricFinder(), system);
+    Map<String, Object> props = ImmutableMap.<String, Object>of(
+      "nameSearch", "SonarQube"
+    );
+    MeasureFilter filter = factory.create(props);
+
+    assertThat(filter.getResourceName()).isEqualTo("SonarQube");
+  }
+
+  @Test
+  public void not_fail_when_name_conditions_contains_array() {
+    MeasureFilterFactory factory = new MeasureFilterFactory(newMetricFinder(), system);
+    Map<String, Object> props = ImmutableMap.<String, Object>of(
+      "nameSearch", new String[]{"sonar", "qube"}
+    );
+    MeasureFilter filter = factory.create(props);
+
+    assertThat(filter.getResourceName()).isEqualTo("sonar,qube");
+  }
+
+  @Test
+  public void not_fail_when_name_conditions_contains_list() {
+    MeasureFilterFactory factory = new MeasureFilterFactory(newMetricFinder(), system);
+    Map<String, Object> props = ImmutableMap.<String, Object>of(
+      "nameSearch", newArrayList("sonar", "qube")
+    );
+    MeasureFilter filter = factory.create(props);
+
+    assertThat(filter.getResourceName()).isEqualTo("sonar,qube");
+  }
+
   @Test
   public void ignore_partial_measure_condition() {
     MeasureFilterFactory factory = new MeasureFilterFactory(newMetricFinder(), system);
index a0a1a25fc0b1b9c77a006e199eeaeb952435a43f..0ec73aabe2735d12adc96810413e414e87b6125f 100644 (file)
@@ -158,8 +158,11 @@ class MeasureFilter < ActiveRecord::Base
       @criteria = self.data.split(CRITERIA_SEPARATOR).inject(HashWithIndifferentAccess.new) do |h, s|
         k, v=s.split('=')
         if k && v
-          # Empty values are removed
-          v=v.split(CRITERIA_KEY_VALUE_SEPARATOR).select{|v| !v.empty?} if v.include?(CRITERIA_KEY_VALUE_SEPARATOR)
+          # nameSearch can contains comma, in this case we should not split the value
+          if k != 'nameSearch'
+            # Empty values are removed
+            v=v.split(CRITERIA_KEY_VALUE_SEPARATOR).select{|v| !v.empty?} if v.include?(CRITERIA_KEY_VALUE_SEPARATOR)
+          end
           h[k]=v
         end
         h
index 13fbbdfdbf1a2907747b3c10c4985237c0630a7e..e2b250d32e83bc11cb1926ebd83dce3c1b9ba846 100644 (file)
       <% end %>
     <% end %>
 
-    { key: 'nameSearch', value: '<%= h @filter.criteria['nameSearch'] -%>' }
+      <% puts "### " +  @filter.criteria.inspect %>
+
+    { key: 'nameSearch', value: '<%= @filter.criteria['nameSearch'] -%>' }
   ];
 </script>