From: Julien HENRY Date: Wed, 6 May 2015 08:40:07 +0000 (+0200) Subject: Restore partial support of RuleQuery in RuleFinder to fix issue with Groovy plugin X-Git-Tag: 5.2-RC1~2033^2 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=refs%2Fpull%2F293%2Fhead;p=sonarqube.git Restore partial support of RuleQuery in RuleFinder to fix issue with Groovy plugin --- diff --git a/sonar-batch/src/main/java/org/sonar/batch/rule/RuleFinderCompatibility.java b/sonar-batch/src/main/java/org/sonar/batch/rule/RuleFinderCompatibility.java index 44e0cf47ccc..dc497d31886 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/rule/RuleFinderCompatibility.java +++ b/sonar-batch/src/main/java/org/sonar/batch/rule/RuleFinderCompatibility.java @@ -19,6 +19,11 @@ */ package org.sonar.batch.rule; +import com.google.common.base.Function; +import com.google.common.collect.Collections2; +import org.apache.commons.lang.builder.ReflectionToStringBuilder; +import org.apache.commons.lang.builder.ToStringStyle; +import org.sonar.api.batch.rule.ActiveRule; import org.sonar.api.batch.rule.ActiveRules; import org.sonar.api.batch.rule.internal.DefaultActiveRule; import org.sonar.api.rule.RuleKey; @@ -26,8 +31,18 @@ import org.sonar.api.rules.Rule; import org.sonar.api.rules.RuleFinder; import org.sonar.api.rules.RuleQuery; +import javax.annotation.CheckForNull; +import javax.annotation.Nullable; + +import java.util.Arrays; import java.util.Collection; +import java.util.Collections; +/** + * FIXME Waiting for the list of all server rules on batch side this is implemented by redirecting on ActiveRules. This is not correct + * since there is a difference between a rule that doesn't exists and a rule that is not activated in project quality profile. + * + */ public class RuleFinderCompatibility implements RuleFinder { private final ActiveRules activeRules; @@ -48,18 +63,48 @@ public class RuleFinderCompatibility implements RuleFinder { @Override public Rule findByKey(RuleKey key) { - DefaultActiveRule ar = (DefaultActiveRule) activeRules.find(key); - return ar == null ? null : Rule.create(key.repository(), key.rule()).setName(ar.name()); + return toRule(activeRules.find(key)); } @Override public Rule find(RuleQuery query) { - throw new UnsupportedOperationException("Unable to find rule by query"); + Collection all = findAll(query); + if (all.size() > 1) { + throw new IllegalArgumentException("Non unique result for rule query: " + ReflectionToStringBuilder.toString(query, ToStringStyle.SHORT_PREFIX_STYLE)); + } else if (all.isEmpty()) { + return null; + } else { + return all.iterator().next(); + } } @Override public Collection findAll(RuleQuery query) { + if (query.getConfigKey() != null) { + if (query.getRepositoryKey() != null && query.getKey() == null) { + Rule rule = toRule(activeRules.findByInternalKey(query.getRepositoryKey(), query.getConfigKey())); + return rule != null ? Arrays.asList(rule) : Collections.emptyList(); + } + } else if (query.getRepositoryKey() != null) { + if (query.getKey() != null) { + Rule rule = toRule(activeRules.find(RuleKey.of(query.getRepositoryKey(), query.getKey()))); + return rule != null ? Arrays.asList(rule) : Collections.emptyList(); + } else { + return Collections2.transform(activeRules.findByRepository(query.getRepositoryKey()), new Function() { + @Override + public Rule apply(ActiveRule input) { + return toRule(input); + } + }); + } + } throw new UnsupportedOperationException("Unable to find rule by query"); } + @CheckForNull + private Rule toRule(@Nullable ActiveRule rule) { + DefaultActiveRule ar = (DefaultActiveRule) rule; + return ar == null ? null : Rule.create(ar.ruleKey().repository(), ar.ruleKey().rule()).setName(ar.name()).setConfigKey(ar.internalKey()).setLanguage(ar.language()); + } + } diff --git a/sonar-batch/src/test/java/org/sonar/batch/rule/RuleFinderCompatibilityTest.java b/sonar-batch/src/test/java/org/sonar/batch/rule/RuleFinderCompatibilityTest.java new file mode 100644 index 00000000000..1c61c091b68 --- /dev/null +++ b/sonar-batch/src/test/java/org/sonar/batch/rule/RuleFinderCompatibilityTest.java @@ -0,0 +1,103 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2014 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * SonarQube is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.batch.rule; + +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.sonar.api.batch.rule.ActiveRules; +import org.sonar.api.batch.rule.internal.ActiveRulesBuilder; +import org.sonar.api.rule.RuleKey; +import org.sonar.api.rules.RuleQuery; + +import static org.assertj.core.api.Assertions.assertThat; + +public class RuleFinderCompatibilityTest { + + @Rule + public ExpectedException thrown = ExpectedException.none(); + + private ActiveRules activeRules = new ActiveRulesBuilder() + .create(RuleKey.of("repo1", "rule1")) + .activate() + .create(RuleKey.of("repo1", "rule2")) + .setInternalKey("rule2_internal") + .activate() + .create(RuleKey.of("repo2", "rule1")) + .activate() + .build(); + private RuleFinderCompatibility ruleFinder; + + @Before + public void prepare() { + ruleFinder = new RuleFinderCompatibility(activeRules); + } + + @Test + public void testByInternalKey() { + assertThat(ruleFinder.find(RuleQuery.create().withRepositoryKey("repo1").withConfigKey("rule2_internal")).getKey()).isEqualTo("rule2"); + assertThat(ruleFinder.find(RuleQuery.create().withRepositoryKey("repo1").withConfigKey("rule2_internal2"))).isNull(); + } + + @Test + public void testByKey() { + assertThat(ruleFinder.find(RuleQuery.create().withRepositoryKey("repo1").withKey("rule2")).getKey()).isEqualTo("rule2"); + assertThat(ruleFinder.find(RuleQuery.create().withRepositoryKey("repo1").withKey("rule3"))).isNull(); + assertThat(ruleFinder.findByKey("repo1", "rule2").getKey()).isEqualTo("rule2"); + } + + @Test + public void duplicateResult() { + thrown.expect(IllegalArgumentException.class); + thrown.expectMessage("Non unique result for rule query: RuleQuery[repositoryKey=repo1,key=,configKey=]"); + ruleFinder.find(RuleQuery.create().withRepositoryKey("repo1")); + } + + @Test + public void unsupportedById() { + thrown.expect(UnsupportedOperationException.class); + ruleFinder.findById(1); + } + + @Test + public void unsupportedByInternalKeyWithoutRepo() { + thrown.expect(UnsupportedOperationException.class); + ruleFinder.find(RuleQuery.create().withConfigKey("config")); + } + + @Test + public void unsupportedByKeyWithoutRepo() { + thrown.expect(UnsupportedOperationException.class); + ruleFinder.find(RuleQuery.create().withKey("key")); + } + + @Test + public void unsupportedByKeyAndInternalKey() { + thrown.expect(UnsupportedOperationException.class); + ruleFinder.find(RuleQuery.create().withRepositoryKey("repo").withKey("key").withConfigKey("config")); + } + + @Test + public void unsupportedByKeyAndInternalKeyWithoutRepo() { + thrown.expect(UnsupportedOperationException.class); + ruleFinder.find(RuleQuery.create().withKey("key").withConfigKey("config")); + } +} diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/rules/RuleFinder.java b/sonar-plugin-api/src/main/java/org/sonar/api/rules/RuleFinder.java index 5d8e8478745..a5ecacd1df9 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/rules/RuleFinder.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/rules/RuleFinder.java @@ -30,7 +30,7 @@ import java.util.Collection; /** * @since 2.3 - * @deprecated since 5.1 DB access will soon be removed from batch side. Use {@link ActiveRules} instead. + * @deprecated since 5.1. Use {@link ActiveRules} on batch side. */ @Deprecated public interface RuleFinder extends TaskComponent, ServerComponent { @@ -50,7 +50,7 @@ public interface RuleFinder extends TaskComponent, ServerComponent { Rule findByKey(RuleKey key); /** - * @throw NonUniqueResultException if more than one result + * @throw IllegalArgumentException if more than one result */ @CheckForNull Rule find(RuleQuery query);