]> source.dussan.org Git - sonarqube.git/commitdiff
Add an IT for custom rules 562/head
authorJulien HENRY <julien.henry@sonarsource.com>
Fri, 2 Oct 2015 12:29:11 +0000 (14:29 +0200)
committerJulien HENRY <julien.henry@sonarsource.com>
Fri, 2 Oct 2015 13:03:20 +0000 (15:03 +0200)
it/it-tests/src/test/java/issue/suite/CustomRulesTest.java [new file with mode: 0644]
it/it-tests/src/test/java/issue/suite/IssueTestSuite.java
it/it-tests/src/test/resources/issue/suite/CustomRulesTest/custom.xml [new file with mode: 0644]
plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/rule/XooRulesDefinition.java
plugins/sonar-xoo-plugin/src/test/java/org/sonar/xoo/rule/XooRulesDefinitionTest.java

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 (file)
index 0000000..b0d2944
--- /dev/null
@@ -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<Issue> 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");
+  }
+}
index a4eb333301d679f622f0c3ea723648585e30149c..6757552276e03ed9ba03b2dbe0d0247cd6913f60 100644 (file)
@@ -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 (file)
index 0000000..b04d126
--- /dev/null
@@ -0,0 +1,12 @@
+<?xml version="1.0" encoding="UTF-8"?><!-- Generated by Sonar -->
+<profile>
+  <name>Custom</name>
+  <language>xoo</language>
+  <rules>
+    <rule>
+      <repositoryKey>xoo</repositoryKey>
+      <key>MyCustomRule</key>
+      <priority>CRITICAL</priority>
+    </rule>
+  </rules>
+</profile>
\ No newline at end of file
index ef8d3df6af6be0f450450acf0a8c00157fe9fa3c..adc9dc9d0c9e3a03d318220cec28a497f85f585d 100644 (file)
  */
 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)
index 923f4d94f5fe2837d382815ef53df5f450c51f52..ee2d1504b3efbec9e707f912305c1c169fbe59b0 100644 (file)
 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();