]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-4922 Trim search terms when matching on key
authorJean-Baptiste Lievremont <jean-baptiste.lievremont@sonarsource.com>
Mon, 17 Feb 2014 13:30:25 +0000 (14:30 +0100)
committerJean-Baptiste Lievremont <jean-baptiste.lievremont@sonarsource.com>
Mon, 17 Feb 2014 13:30:25 +0000 (14:30 +0100)
sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileRuleLookup.java
sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfileRuleLookupTest.java

index 34a158fe9e28e09c008ba60e796ea86847405b9c..15c7ba8f3d843a6d9f1b0eb173c9978577fc9748 100644 (file)
@@ -38,6 +38,7 @@ import org.sonar.server.es.ESIndex;
 import org.sonar.server.rule.RuleDocument;
 
 import javax.annotation.CheckForNull;
+
 import java.util.Collection;
 import java.util.List;
 import java.util.Map;
@@ -284,7 +285,7 @@ public class QProfileRuleLookup implements ServerExtension {
     if (StringUtils.isNotBlank(query.nameOrKey())) {
       result.must(
         queryFilter(
-          multiMatchQuery(query.nameOrKey(), RuleDocument.FIELD_NAME + ".search", RuleDocument.FIELD_KEY)
+          multiMatchQuery(query.nameOrKey().trim(), RuleDocument.FIELD_NAME + ".search", RuleDocument.FIELD_KEY)
             .operator(Operator.AND)));
     }
 
index 3813dd4e2705ba34b79d28604f79505488d4041d..b1a0a4838dbf7d467ca0b4b636394e3f2cef5f5c 100644 (file)
@@ -150,10 +150,12 @@ public class QProfileRuleLookupTest {
 
     // Match on key
     assertThat(profileRules.search(ProfileRuleQuery.create(1).setNameOrKey("DM_CONVERT_CASE"), paging).rules()).hasSize(1);
+    assertThat(profileRules.search(ProfileRuleQuery.create(1).setNameOrKey(" \n\r\t DM_CONVERT_CASE \r\n\t "), paging).rules()).hasSize(1);
 
     // Match on name
     assertThat(profileRules.search(ProfileRuleQuery.create(1).setNameOrKey("Unused Check"), paging).rules()).hasSize(1);
     assertThat(profileRules.search(ProfileRuleQuery.create(1).setNameOrKey("unus"), paging).rules()).hasSize(1);
+    assertThat(profileRules.search(ProfileRuleQuery.create(1).setNameOrKey(" \n\r\t Unused Check \n\r\t "), paging).rules()).hasSize(1);
 
     // Match on repositoryKey
     assertThat(profileRules.search(ProfileRuleQuery.create(1).addRepositoryKeys("findbugs"), paging).rules()).hasSize(1);
@@ -301,9 +303,18 @@ public class QProfileRuleLookupTest {
     // Search of inactive rule on profile 1
     assertThat(profileRules.searchInactives(ProfileRuleQuery.create(1), paging).rules()).hasSize(2);
 
-    // Match on key
+    // Match on name
     assertThat(profileRules.searchInactives(ProfileRuleQuery.create(2).setNameOrKey("Boolean expressions"), paging).rules()).hasSize(1);
 
+    // Match on name - trimmed
+    assertThat(profileRules.searchInactives(ProfileRuleQuery.create(2).setNameOrKey(" \r\t\n Boolean expressions \n\r\t "), paging).rules()).hasSize(1);
+
+    // Match on key
+    assertThat(profileRules.searchInactives(ProfileRuleQuery.create(2).setNameOrKey("S1125"), paging).rules()).hasSize(1);
+
+    // Match on key - trimmed
+    assertThat(profileRules.searchInactives(ProfileRuleQuery.create(2).setNameOrKey(" \n\r\t S1125 \n\r\t "), paging).rules()).hasSize(1);
+
     // Mach on severity
     assertThat(profileRules.searchInactives(ProfileRuleQuery.create(2).addSeverities(Severity.INFO), paging).rules()).hasSize(1);
   }