From fc7b6dc2a4558c0825a83987f6672cd19a428002 Mon Sep 17 00:00:00 2001 From: Julien HENRY Date: Fri, 2 Oct 2015 14:29:11 +0200 Subject: [PATCH] Add an IT for custom rules --- .../java/issue/suite/CustomRulesTest.java | 58 +++++++++++++++++++ .../test/java/issue/suite/IssueTestSuite.java | 2 +- .../issue/suite/CustomRulesTest/custom.xml | 12 ++++ .../sonar/xoo/rule/XooRulesDefinition.java | 13 +++-- .../xoo/rule/XooRulesDefinitionTest.java | 4 +- 5 files changed, 81 insertions(+), 8 deletions(-) create mode 100644 it/it-tests/src/test/java/issue/suite/CustomRulesTest.java create mode 100644 it/it-tests/src/test/resources/issue/suite/CustomRulesTest/custom.xml diff --git a/it/it-tests/src/test/java/issue/suite/CustomRulesTest.java b/it/it-tests/src/test/java/issue/suite/CustomRulesTest.java new file mode 100644 index 00000000000..b0d29444734 --- /dev/null +++ b/it/it-tests/src/test/java/issue/suite/CustomRulesTest.java @@ -0,0 +1,58 @@ +/* + * Copyright (C) 2009-2014 SonarSource SA + * All rights reserved + * mailto:contact AT sonarsource DOT com + */ +package issue.suite; + +import com.sonar.orchestrator.Orchestrator; +import com.sonar.orchestrator.build.SonarRunner; +import com.sonar.orchestrator.locator.FileLocation; +import java.util.List; +import org.junit.Before; +import org.junit.ClassRule; +import org.junit.Test; +import org.sonar.wsclient.issue.Issue; +import org.sonar.wsclient.issue.IssueQuery; +import util.ItUtils; + +import static org.assertj.core.api.Assertions.assertThat; + +public class CustomRulesTest { + + @ClassRule + public static Orchestrator orchestrator = IssueTestSuite.ORCHESTRATOR; + + @Before + public void deleteData() { + orchestrator.resetData(); + } + + @Test + public void analyzeProjectWithCustomRules() throws Exception { + + orchestrator.getServer().adminWsClient().post("api/rules/create", + "template_key", "xoo:TemplateRule", + "custom_key", "MyCustomRule", + "markdown_description", "My description", + "name", "My custom rule", + "severity", "BLOCKER", + "params", "line=2"); + + orchestrator.getServer().restoreProfile(FileLocation.ofClasspath("/issue/suite/CustomRulesTest/custom.xml")); + + orchestrator.getServer().provisionProject("sample", "Sample"); + orchestrator.getServer().associateProjectToQualityProfile("sample", "xoo", "Custom"); + + orchestrator.executeBuild(SonarRunner.create().setProjectDir(ItUtils.projectDir("shared/xoo-sample"))); + + List issues = orchestrator.getServer().adminWsClient().issueClient().find(IssueQuery.create()).list(); + assertThat(issues).hasSize(1); + + Issue issue = issues.get(0); + assertThat(issue.ruleKey()).isEqualTo("xoo:MyCustomRule"); + assertThat(issue.line()).isEqualTo(2); + // Overriden in quality profile + assertThat(issue.severity()).isEqualTo("CRITICAL"); + } +} diff --git a/it/it-tests/src/test/java/issue/suite/IssueTestSuite.java b/it/it-tests/src/test/java/issue/suite/IssueTestSuite.java index a4eb333301d..6757552276e 100644 --- a/it/it-tests/src/test/java/issue/suite/IssueTestSuite.java +++ b/it/it-tests/src/test/java/issue/suite/IssueTestSuite.java @@ -19,7 +19,7 @@ import static org.assertj.core.api.Assertions.assertThat; @RunWith(Suite.class) @Suite.SuiteClasses({ - CommonRulesTest.class, IssueWorkflowTest.class, ManualRulesTest.class, + CommonRulesTest.class, IssueWorkflowTest.class, ManualRulesTest.class, CustomRulesTest.class }) public class IssueTestSuite { diff --git a/it/it-tests/src/test/resources/issue/suite/CustomRulesTest/custom.xml b/it/it-tests/src/test/resources/issue/suite/CustomRulesTest/custom.xml new file mode 100644 index 00000000000..b04d126115d --- /dev/null +++ b/it/it-tests/src/test/resources/issue/suite/CustomRulesTest/custom.xml @@ -0,0 +1,12 @@ + + + Custom + xoo + + + xoo + MyCustomRule + CRITICAL + + + \ No newline at end of file diff --git a/plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/rule/XooRulesDefinition.java b/plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/rule/XooRulesDefinition.java index ef8d3df6af6..adc9dc9d0c9 100644 --- a/plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/rule/XooRulesDefinition.java +++ b/plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/rule/XooRulesDefinition.java @@ -19,11 +19,12 @@ */ package org.sonar.xoo.rule; -import org.sonar.xoo.Xoo2; - import org.sonar.api.server.rule.RuleParamType; import org.sonar.api.server.rule.RulesDefinition; +import org.sonar.api.server.rule.RulesDefinitionAnnotationLoader; import org.sonar.xoo.Xoo; +import org.sonar.xoo.Xoo2; +import org.sonar.xoo.checks.Check; /** * Define all the coding rules that are supported on the repositories named "xoo" and "xoo2" @@ -50,13 +51,15 @@ public class XooRulesDefinition implements RulesDefinition { oneIssuePerLine.setDebtSubCharacteristic(RulesDefinition.SubCharacteristics.MEMORY_EFFICIENCY) .setDebtRemediationFunction(hasTag.debtRemediationFunctions().linear("1min")) .setEffortToFixDescription("It takes about 1 minute to an experienced software craftsman to remove a line of code"); - + repo.done(); } - private static void defineRulesXoo(Context context) { + private void defineRulesXoo(Context context) { NewRepository repo = context.createRepository(XOO_REPOSITORY, Xoo.KEY).setName("Xoo"); - + + new RulesDefinitionAnnotationLoader().load(repo, Check.ALL); + NewRule hasTag = repo.createRule(HasTagSensor.RULE_KEY).setName("Has Tag") .setHtmlDescription("Search for a given tag in Xoo files"); hasTag.setDebtSubCharacteristic(RulesDefinition.SubCharacteristics.READABILITY) diff --git a/plugins/sonar-xoo-plugin/src/test/java/org/sonar/xoo/rule/XooRulesDefinitionTest.java b/plugins/sonar-xoo-plugin/src/test/java/org/sonar/xoo/rule/XooRulesDefinitionTest.java index 923f4d94f5f..ee2d1504b3e 100644 --- a/plugins/sonar-xoo-plugin/src/test/java/org/sonar/xoo/rule/XooRulesDefinitionTest.java +++ b/plugins/sonar-xoo-plugin/src/test/java/org/sonar/xoo/rule/XooRulesDefinitionTest.java @@ -20,10 +20,10 @@ package org.sonar.xoo.rule; import org.junit.Before; - import org.junit.Test; import org.sonar.api.server.debt.DebtRemediationFunction; import org.sonar.api.server.rule.RulesDefinition; + import static org.assertj.core.api.Assertions.assertThat; public class XooRulesDefinitionTest { @@ -42,7 +42,7 @@ public class XooRulesDefinitionTest { assertThat(repo).isNotNull(); assertThat(repo.name()).isEqualTo("Xoo"); assertThat(repo.language()).isEqualTo("xoo"); - assertThat(repo.rules()).hasSize(11); + assertThat(repo.rules()).hasSize(12); RulesDefinition.Rule rule = repo.rule(OneIssuePerLineSensor.RULE_KEY); assertThat(rule.name()).isNotEmpty(); -- 2.39.5