From 3caeab21dddda314892af3e7c9fa66e2bce3eea9 Mon Sep 17 00:00:00 2001 From: Jean-Baptiste Lievremont Date: Mon, 17 Feb 2014 14:30:25 +0100 Subject: [PATCH] SONAR-4922 Trim search terms when matching on key --- .../server/qualityprofile/QProfileRuleLookup.java | 3 ++- .../qualityprofile/QProfileRuleLookupTest.java | 13 ++++++++++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileRuleLookup.java b/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileRuleLookup.java index 34a158fe9e2..15c7ba8f3d8 100644 --- a/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileRuleLookup.java +++ b/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileRuleLookup.java @@ -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))); } diff --git a/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfileRuleLookupTest.java b/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfileRuleLookupTest.java index 3813dd4e270..b1a0a4838db 100644 --- a/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfileRuleLookupTest.java +++ b/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfileRuleLookupTest.java @@ -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); } -- 2.39.5