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;
}
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());
}
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();
}
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();
}
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());
}
}
}
}
}
Aggregations