use of org.sonar.api.rules.Rule in project sonarqube by SonarSource.
the class AnnotationProfileParser method addRule.
private void addRule(Class aClass, BelongsToProfile annotation, RulesProfile profile, String repositoryKey, ValidationMessages messages) {
if ((annotation != null) && StringUtils.equals(annotation.title(), profile.getName())) {
String ruleKey = RuleAnnotationUtils.getRuleKey(aClass);
Rule rule = ruleFinder.findByKey(repositoryKey, ruleKey);
if (rule == null) {
messages.addWarningText("Rule not found: [repository=" + repositoryKey + ", key=" + ruleKey + "]");
} else {
RulePriority priority = null;
if (annotation.priority() != null) {
priority = RulePriority.fromCheckPriority(annotation.priority());
}
profile.activateRule(rule, priority);
}
}
}
use of org.sonar.api.rules.Rule in project sonarqube by SonarSource.
the class FakeRule method shouldParseOnlyWantedProfile.
@Test
public void shouldParseOnlyWantedProfile() {
RuleFinder ruleFinder = mock(RuleFinder.class);
when(ruleFinder.findByKey(anyString(), anyString())).thenAnswer(new Answer<Rule>() {
public Rule answer(InvocationOnMock iom) throws Throwable {
return Rule.create((String) iom.getArguments()[0], (String) iom.getArguments()[1], (String) iom.getArguments()[1]);
}
});
ValidationMessages messages = ValidationMessages.create();
RulesProfile profile = new AnnotationProfileParser(ruleFinder).parse("squid", "Foo way", "java", Lists.<Class>newArrayList(FakeRule.class, RuleOnOtherProfile.class), messages);
assertThat(profile.getActiveRule("squid", "fake")).isNotNull();
assertThat(profile.getActiveRule("squid", "other")).isNull();
}
use of org.sonar.api.rules.Rule in project sonarqube by SonarSource.
the class FakeRule method shouldParseAnnotatedClasses.
@Test
public void shouldParseAnnotatedClasses() {
RuleFinder ruleFinder = mock(RuleFinder.class);
when(ruleFinder.findByKey(anyString(), anyString())).thenAnswer(new Answer<Rule>() {
public Rule answer(InvocationOnMock iom) throws Throwable {
return Rule.create((String) iom.getArguments()[0], (String) iom.getArguments()[1], (String) iom.getArguments()[1]);
}
});
ValidationMessages messages = ValidationMessages.create();
RulesProfile profile = new AnnotationProfileParser(ruleFinder).parse("squid", "Foo way", "java", Lists.<Class>newArrayList(FakeRule.class), messages);
assertThat(profile.getName()).isEqualTo("Foo way");
assertThat(profile.getLanguage()).isEqualTo("java");
assertThat(profile.getActiveRule("squid", "fake").getSeverity()).isEqualTo(RulePriority.BLOCKER);
assertThat(messages.hasErrors()).isFalse();
}
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 QProfileExporters method wrap.
private RulesProfile wrap(QualityProfileDto profile) {
try (DbSession dbSession = dbClient.openSession(false)) {
RulesProfile target = new RulesProfile(profile.getName(), profile.getLanguage());
List<ActiveRuleDto> activeRuleDtos = dbClient.activeRuleDao().selectByProfileKey(dbSession, profile.getKey());
List<ActiveRuleParamDto> activeRuleParamDtos = dbClient.activeRuleDao().selectParamsByActiveRuleIds(dbSession, Lists.transform(activeRuleDtos, ActiveRuleDto::getId));
ListMultimap<Integer, ActiveRuleParamDto> activeRuleParamsByActiveRuleId = FluentIterable.from(activeRuleParamDtos).index(ActiveRuleParamDto::getActiveRuleId);
for (ActiveRuleDto activeRule : activeRuleDtos) {
// TODO all rules should be loaded by using one query with all active rule keys as parameter
Rule rule = ruleFinder.findByKey(activeRule.getKey().ruleKey());
org.sonar.api.rules.ActiveRule wrappedActiveRule = target.activateRule(rule, RulePriority.valueOf(activeRule.getSeverityString()));
List<ActiveRuleParamDto> paramDtos = activeRuleParamsByActiveRuleId.get(activeRule.getId());
for (ActiveRuleParamDto activeRuleParamDto : paramDtos) {
wrappedActiveRule.setParameter(activeRuleParamDto.getKey(), activeRuleParamDto.getValue());
}
}
return target;
}
}
Aggregations