Search in sources :

Example 6 with Rule

use of org.sonar.api.rules.Rule in project sonarqube by SonarSource.

the class XMLProfileParserTest method newRuleFinder.

private RuleFinder newRuleFinder() {
    RuleFinder ruleFinder = mock(RuleFinder.class);
    when(ruleFinder.findByKey(anyString(), anyString())).thenAnswer(new Answer<Rule>() {

        public Rule answer(InvocationOnMock iom) throws Throwable {
            Rule rule = Rule.create((String) iom.getArguments()[0], (String) iom.getArguments()[1], (String) iom.getArguments()[1]);
            rule.createParameter("format");
            rule.createParameter("message");
            return rule;
        }
    });
    return ruleFinder;
}
Also used : InvocationOnMock(org.mockito.invocation.InvocationOnMock) Rule(org.sonar.api.rules.Rule) ActiveRule(org.sonar.api.rules.ActiveRule) Matchers.anyString(org.mockito.Matchers.anyString) RuleFinder(org.sonar.api.rules.RuleFinder)

Example 7 with Rule

use of org.sonar.api.rules.Rule in project sonarqube by SonarSource.

the class XMLProfileSerializerTest method exportRuleParameters.

@Test
public void exportRuleParameters() throws IOException, SAXException {
    Writer writer = new StringWriter();
    RulesProfile profile = RulesProfile.create("sonar way", "java");
    Rule rule = Rule.create("checkstyle", "IllegalRegexp", "illegal regexp");
    rule.createParameter("format");
    rule.createParameter("message");
    rule.createParameter("tokens");
    ActiveRule activeRule = profile.activateRule(rule, RulePriority.BLOCKER);
    activeRule.setParameter("format", "foo");
    activeRule.setParameter("message", "with special characters < > &");
    // the tokens parameter is not set
    new XMLProfileSerializer().write(profile, writer);
    assertSimilarXml("exportRuleParameters.xml", writer.toString());
}
Also used : StringWriter(java.io.StringWriter) ActiveRule(org.sonar.api.rules.ActiveRule) Rule(org.sonar.api.rules.Rule) ActiveRule(org.sonar.api.rules.ActiveRule) StringWriter(java.io.StringWriter) Writer(java.io.Writer) Test(org.junit.Test)

Example 8 with Rule

use of org.sonar.api.rules.Rule in project sonarqube by SonarSource.

the class IssuePatternTest method shouldMatchViolation.

@Test
public void shouldMatchViolation() {
    Rule rule = Rule.create("checkstyle", "IllegalRegexp", "");
    String javaFile = "org.foo.Bar";
    IssuePattern pattern = new IssuePattern("*", "*");
    pattern.addLine(12);
    assertThat(pattern.match(create(rule, javaFile, null))).isFalse();
    assertThat(pattern.match(create(rule, javaFile, 12))).isTrue();
    assertThat(pattern.match(create(rule, null, null))).isFalse();
}
Also used : Rule(org.sonar.api.rules.Rule) Test(org.junit.Test)

Example 9 with Rule

use of org.sonar.api.rules.Rule in project sonarqube by SonarSource.

the class DefaultRuleFinderTest method should_success_finder_wrap.

@Test
public void should_success_finder_wrap() {
    // has Id
    assertThat(underTest.findById(rule1.getId()).getId()).isEqualTo(rule1.getId());
    // should_find_by_id
    assertThat(underTest.findById(rule3.getId()).getConfigKey()).isEqualTo("Checker/Treewalker/AnnotationUseStyleCheck");
    // should_not_find_disabled_rule_by_id
    assertThat(underTest.findById(rule2.getId())).isNull();
    // should_find_by_ids
    assertThat(underTest.findByIds(newArrayList(rule2.getId(), rule3.getId()))).hasSize(2);
    // should_find_by_key
    Rule rule = underTest.findByKey("checkstyle", "com.puppycrawl.tools.checkstyle.checks.header.HeaderCheck");
    assertThat(rule).isNotNull();
    assertThat(rule.getKey()).isEqualTo(("com.puppycrawl.tools.checkstyle.checks.header.HeaderCheck"));
    assertThat(rule.isEnabled()).isTrue();
    // find_should_return_null_if_no_results
    assertThat(underTest.findByKey("checkstyle", "unknown")).isNull();
    assertThat(underTest.find(RuleQuery.create().withRepositoryKey("checkstyle").withConfigKey("unknown"))).isNull();
    // find_repository_rules
    assertThat(underTest.findAll(RuleQuery.create().withRepositoryKey("checkstyle"))).hasSize(2);
    // find_all_enabled
    assertThat(underTest.findAll(RuleQuery.create())).extracting("id").containsOnly(rule1.getId(), rule3.getId(), rule4.getId());
    assertThat(underTest.findAll(RuleQuery.create())).hasSize(3);
    // do_not_find_disabled_rules
    assertThat(underTest.findByKey("checkstyle", "DisabledCheck")).isNull();
    // do_not_find_unknown_rules
    assertThat(underTest.findAll(RuleQuery.create().withRepositoryKey("unknown_repository"))).isEmpty();
}
Also used : Rule(org.sonar.api.rules.Rule) Test(org.junit.Test)

Example 10 with Rule

use of org.sonar.api.rules.Rule in project sonarqube by SonarSource.

the class XMLProfileParser method processRules.

private void processRules(SMInputCursor rulesCursor, RulesProfile profile, ValidationMessages messages) throws XMLStreamException {
    Map<String, String> parameters = new HashMap<>();
    while (rulesCursor.getNext() != null) {
        SMInputCursor ruleCursor = rulesCursor.childElementCursor();
        String repositoryKey = null;
        String key = null;
        RulePriority priority = null;
        parameters.clear();
        while (ruleCursor.getNext() != null) {
            String nodeName = ruleCursor.getLocalName();
            if (StringUtils.equals("repositoryKey", nodeName)) {
                repositoryKey = StringUtils.trim(ruleCursor.collectDescendantText(false));
            } else if (StringUtils.equals("key", nodeName)) {
                key = StringUtils.trim(ruleCursor.collectDescendantText(false));
            } else if (StringUtils.equals("priority", nodeName)) {
                priority = RulePriority.valueOf(StringUtils.trim(ruleCursor.collectDescendantText(false)));
            } else if (StringUtils.equals("parameters", nodeName)) {
                SMInputCursor propsCursor = ruleCursor.childElementCursor("parameter");
                processParameters(propsCursor, parameters);
            }
        }
        Rule rule = ruleFinder.findByKey(repositoryKey, key);
        if (rule == null) {
            messages.addWarningText("Rule not found: " + ruleToString(repositoryKey, key));
        } else {
            ActiveRule activeRule = profile.activateRule(rule, priority);
            for (Map.Entry<String, String> entry : parameters.entrySet()) {
                if (rule.getParam(entry.getKey()) == null) {
                    messages.addWarningText("The parameter '" + entry.getKey() + "' does not exist in the rule: " + ruleToString(repositoryKey, key));
                } else {
                    activeRule.setParameter(entry.getKey(), entry.getValue());
                }
            }
        }
    }
}
Also used : SMInputCursor(org.codehaus.staxmate.in.SMInputCursor) HashMap(java.util.HashMap) ActiveRule(org.sonar.api.rules.ActiveRule) RulePriority(org.sonar.api.rules.RulePriority) Rule(org.sonar.api.rules.Rule) ActiveRule(org.sonar.api.rules.ActiveRule) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

Rule (org.sonar.api.rules.Rule)16 Test (org.junit.Test)11 ActiveRule (org.sonar.api.rules.ActiveRule)4 Matchers.anyString (org.mockito.Matchers.anyString)3 InvocationOnMock (org.mockito.invocation.InvocationOnMock)3 RuleFinder (org.sonar.api.rules.RuleFinder)3 Map (java.util.Map)2 RulesProfile (org.sonar.api.profiles.RulesProfile)2 RulePriority (org.sonar.api.rules.RulePriority)2 ValidationMessages (org.sonar.api.utils.ValidationMessages)2 StringWriter (java.io.StringWriter)1 Writer (java.io.Writer)1 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 SMInputCursor (org.codehaus.staxmate.in.SMInputCursor)1 MessageException (org.sonar.api.utils.MessageException)1 DbSession (org.sonar.db.DbSession)1 ActiveRuleDto (org.sonar.db.qualityprofile.ActiveRuleDto)1 ActiveRuleParamDto (org.sonar.db.qualityprofile.ActiveRuleParamDto)1