@Override @CheckForNull public Rule findByKey(RuleKey key) { return delegate.findByKey(key); }
@Override @CheckForNull public Rule findByKey(String repositoryKey, String key) { return delegate.findByKey(repositoryKey, key); }
@CheckForNull protected Integer getRuleId(Issue issue) { Rule rule = ruleFinder.findByKey(issue.ruleKey()); return rule != null ? rule.getId() : null; } }
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; } }
Rule rule = ruleFinder.findByKey(repositoryKey, key); if (rule == null) { messages.addWarningText("Rule not found: " + ruleToString(repositoryKey, key));
@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(); } }
@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(); }
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); } } } }
private RulesProfile wrap(DbSession dbSession, QProfileDto profile) { RulesProfile target = new RulesProfile(profile.getName(), profile.getLanguage()); List<OrgActiveRuleDto> activeRuleDtos = dbClient.activeRuleDao().selectByProfile(dbSession, profile); 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.getRuleKey()); 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; }
@CheckForNull private String nameFromDB(RuleKey ruleKey) { Rule r = ruleFinder.findByKey(ruleKey); return r != null ? r.getName() : null; }
@CheckForNull private Rule findRule(Issue issue) { RuleKey ruleKey = issue.ruleKey(); return ruleFinder.findByKey(ruleKey); }
@CheckForNull private String nameFromDB(RuleKey ruleKey) { Rule r = ruleFinder.findByKey(ruleKey); return r != null ? r.getName() : null; }
@CheckForNull private Rule findRule(Issue issue) { RuleKey ruleKey = issue.ruleKey(); return ruleFinder.findByKey(ruleKey); }
protected Rule rule(Issue issue) { Rule rule = ruleFinder.findByKey(issue.ruleKey()); if (rule == null) { throw new IllegalStateException("Rule not found: " + issue.ruleKey()); } return rule; } }
private void defineRule(RulesProfile profile, ValidationMessages validation, String repositoryKey, String ruleKey) { Rule rule = ruleFinder.findByKey(repositoryKey, ruleKey); if (rule == null) { LOG.error("Rule not found: [repository=" + repositoryKey + ", key=" + ruleKey + "]"); validation.addWarningText("Rule not found: [repository=" + repositoryKey + ", key=" + ruleKey + "]"); } else { profile.activateRule(rule, null); } } }
private void saveOnRule(DecoratorContext context, SumMap<RuleKey> ruleDebts) { for (Map.Entry<RuleKey, Long> entry : ruleDebts.entrySet()) { org.sonar.api.rules.Rule oldRule = ruleFinder.findByKey(entry.getKey()); if (oldRule != null) { saveRuleMeasure(context, oldRule, entry.getValue().doubleValue(), ResourceUtils.isEntity(context.getResource())); } } }
@Override public RulesProfile createProfile(ValidationMessages validation) { RulesProfile profile = RulesProfile.create(ZapPlugin.LANGUAGE_NAME, ZapPlugin.LANGUAGE_KEY); InputStream inputStream = getClass().getResourceAsStream(ZapPlugin.RULES_FILE); List<String> ruleKeysList = new ArrayList<>(getAllRuleKeysFromFile(inputStream)); for (String ruleKey : ruleKeysList) { Rule rule = ruleFinder.findByKey(ZapPlugin.REPOSITORY_KEY, ruleKey); profile.activateRule(rule, null); } profile.setDefaultProfile(true); return profile; } }
RuleFinder ruleFinder() { return when(ruleFinder.findByKey(anyString(), anyString())).thenAnswer(new Answer<Rule>() { @Override public Rule answer(InvocationOnMock invocation) { Object[] arguments = invocation.getArguments(); return Rule.create((String) arguments[0], (String) arguments[1], (String) arguments[1]); } }).getMock(); }