--- /dev/null
+/*
+ * 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");
+ }
+}
@RunWith(Suite.class)
@Suite.SuiteClasses({
- CommonRulesTest.class, IssueWorkflowTest.class, ManualRulesTest.class,
+ CommonRulesTest.class, IssueWorkflowTest.class, ManualRulesTest.class, CustomRulesTest.class
})
public class IssueTestSuite {
--- /dev/null
+<?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
*/
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"
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)
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 {
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();