/** * @deprecated visibility should be reduced to protected or package */ @Deprecated public ActiveRule(RulesProfile profile, Rule rule, RulePriority severity) { this.rule = rule; this.overriddenSeverity = severity; if (severity == null && rule != null) { this.severity = rule.getSeverity(); } else { this.severity = severity; } this.rulesProfile = profile; }
@Test public void default_priority_is_major() { Rule rule = Rule.create(); assertThat(rule.getSeverity()).isEqualTo(RulePriority.MAJOR); rule = new Rule("name", "key"); assertThat(rule.getSeverity()).isEqualTo(RulePriority.MAJOR); rule.setSeverity(RulePriority.BLOCKER); assertThat(rule.getSeverity()).isEqualTo(RulePriority.BLOCKER); rule.setSeverity(null); assertThat(rule.getSeverity()).isEqualTo(RulePriority.MAJOR); }
@Test public void rule_without_key() { List<Rule> rules = parseAnnotatedClass(RuleWithoutKey.class); assertThat(rules).hasSize(1); Rule rule = rules.get(0); assertThat(rule.getKey()).isEqualTo(RuleWithoutKey.class.getCanonicalName()); assertThat(rule.getName()).isEqualTo("foo"); assertThat(rule.getDescription()).isNull(); assertThat(rule.getSeverity()).isEqualTo(RulePriority.MAJOR); }
@Test public void overridden_rule() { List<Rule> rules = parseAnnotatedClass(OverridingRule.class); assertThat(rules).hasSize(1); Rule rule = rules.get(0); assertThat(rule.getKey()).isEqualTo("overriding_foo"); assertThat(rule.getName()).isEqualTo("Overriding Foo"); assertThat(rule.getDescription()).isNull(); assertThat(rule.getSeverity()).isEqualTo(RulePriority.MAJOR); assertThat(rule.getParams()).hasSize(2); }
@Test public void rule_without_name_nor_description() { List<Rule> rules = parseAnnotatedClass(RuleWithoutNameNorDescription.class); assertThat(rules).hasSize(1); Rule rule = rules.get(0); assertThat(rule.getKey()).isEqualTo("foo"); assertThat(rule.getSeverity()).isEqualTo(RulePriority.MAJOR); assertThat(rule.getName()).isNull(); assertThat(rule.getDescription()).isNull(); }
@Test public void rule_with_property() { List<Rule> rules = parseAnnotatedClass(RuleWithProperty.class); assertThat(rules).hasSize(1); Rule rule = rules.get(0); assertThat(rule.getKey()).isEqualTo("foo"); assertThat(rule.getName()).isEqualTo("bar"); assertThat(rule.getDescription()).isEqualTo("Foo Bar"); assertThat(rule.getSeverity()).isEqualTo(RulePriority.BLOCKER); assertThat(rule.getStatus()).isEqualTo(Rule.STATUS_READY); assertThat(rule.getParams()).hasSize(1); RuleParam prop = rule.getParam("property"); assertThat(prop.getKey()).isEqualTo("property"); assertThat(prop.getDescription()).isEqualTo("Ignore ?"); assertThat(prop.getDefaultValue()).isEqualTo("false"); assertThat(prop.getType()).isEqualTo(PropertyType.STRING.name()); }
private void verifyRule(Rule rule, RuleDefinitionDto ruleDefinition, RuleParamDto ruleParam) { assertThat(rule).isNotNull(); assertThat(rule.getName()).isEqualTo(ruleDefinition.getName()); assertThat(rule.getLanguage()).isEqualTo(ruleDefinition.getLanguage()); assertThat(rule.getKey()).isEqualTo(ruleDefinition.getRuleKey()); assertThat(rule.getConfigKey()).isEqualTo(ruleDefinition.getConfigKey()); assertThat(rule.isTemplate()).isEqualTo(ruleDefinition.isTemplate()); assertThat(rule.getCreatedAt().getTime()).isEqualTo(ruleDefinition.getCreatedAt()); assertThat(rule.getUpdatedAt().getTime()).isEqualTo(ruleDefinition.getUpdatedAt()); assertThat(rule.getRepositoryKey()).isEqualTo(ruleDefinition.getRepositoryKey()); assertThat(rule.getSeverity().name()).isEqualTo(ruleDefinition.getSeverityString()); assertThat(rule.getSystemTags()).isEqualTo(ruleDefinition.getSystemTags().stream().toArray(String[]::new)); assertThat(rule.getTags()).isEmpty(); assertThat(rule.getId()).isEqualTo(ruleDefinition.getId()); assertThat(rule.getDescription()).isEqualTo(ruleDefinition.getDescription()); assertThat(rule.getParams()).hasSize(1); org.sonar.api.rules.RuleParam param = rule.getParams().iterator().next(); assertThat(param.getRule()).isSameAs(rule); assertThat(param.getKey()).isEqualTo(ruleParam.getName()); assertThat(param.getDescription()).isEqualTo(ruleParam.getDescription()); assertThat(param.getType()).isEqualTo(ruleParam.getType()); assertThat(param.getDefaultValue()).isEqualTo(ruleParam.getDefaultValue()); } }
newRule.setInternalKey(rule.getConfigKey()); newRule.setTemplate(rule.isTemplate()); newRule.setSeverity(rule.getSeverity().toString()); newRule.setStatus(rule.getStatus() == null ? RuleStatus.defaultStatus() : RuleStatus.valueOf(rule.getStatus())); newRule.setTags(rule.getTags());
/** * @deprecated visibility should be reduced to protected or package */ @Deprecated public ActiveRule(RulesProfile profile, Rule rule, RulePriority severity) { this.rule = rule; if (severity == null && rule != null) { this.severity = rule.getSeverity(); } else { this.severity = severity; } this.rulesProfile = profile; }
/** * @deprecated visibility should be reduced to protected or package */ @Deprecated public ActiveRule(RulesProfile profile, Rule rule, RulePriority severity) { this.rule = rule; this.overriddenSeverity = severity; if (severity == null && rule != null) { this.severity = rule.getSeverity(); } else { this.severity = severity; } this.rulesProfile = profile; }
private Violation createViolationOnResource(Resource<?> resource, Integer lineNumber) { if (StringUtils.isNotEmpty(currentSource) && resource == null) { LOG.debug("Ignoring violation on file outside current project ({})", currentSource); return null; } if (resource != null && !resourceHelper.isResourceInProject(resource, project)) { LOG.debug("Ignoring violation on file outside current project ({})", resource.getKey()); return null; } Violation violation = Violation.create(currentRule, resource); if (lineNumber != null) { violation.setLineId(lineNumber); } if (StringUtils.isEmpty(currentMessage)) { violation.setMessage(currentDefaultViolationMessage); } else { violation.setMessage(currentMessage); } violation.setSeverity(currentRule.getSeverity()); context.saveViolation(violation); return violation; }
/** * @param optionalSeverity if null, then the default rule severity is used */ public ActiveRule activateRule(final Rule rule, @Nullable RulePriority optionalSeverity) { if (Iterables.any(activeRules, new Predicate<ActiveRule>() { @Override public boolean apply(ActiveRule input) { return input.getRule().equals(rule); } })) { throw MessageException.of(String.format( "The definition of the profile '%s' (language '%s') contains multiple occurrences of the '%s:%s' rule. The plugin which declares this profile should fix this.", getName(), getLanguage(), rule.getRepositoryKey(), rule.getKey())); } ActiveRule activeRule = new ActiveRule(); activeRule.setRule(rule); activeRule.setRulesProfile(this); activeRule.setSeverity(optionalSeverity == null ? rule.getSeverity() : optionalSeverity); activeRules.add(activeRule); return activeRule; }
private static ActiveRule createActiveRule(DelphiRule fRule, List<Rule> rulesRepository) { String name = fRule.getName(); RulePriority fRulePriority = severityFromLevel(fRule.getPriority()); for (Rule rule : rulesRepository) { if (rule.getKey().equals(name)) { RulePriority priority = fRulePriority != null ? fRulePriority : rule.getSeverity(); ActiveRule activeRule = new ActiveRule(null, rule, priority); activeRule.setActiveRuleParams(buildActiveRuleParams(fRule, rule, activeRule)); return activeRule; } } return null; }
private void createViolationFromMessageAtProjectLevel(SMInputCursor messagesCursor) throws XMLStreamException { Rule currentRule = ruleFinder.find(RuleQuery.create().withRepositoryKey(repositoryKey) .withKey(messagesCursor.getAttrValue(TYPENAME))); if (currentRule != null) { // the violation is saved at project level, not on a specific resource Violation violation = Violation.create(currentRule, project); violation.setMessage(messagesCursor.collectDescendantText().trim()); violation.setSeverity(currentRule.getSeverity()); context.saveViolation(violation); } else { LOG.debug("Could not find the following rule in the FxCop rule repository: " + messagesCursor.getAttrValue(TYPENAME)); } }
violation.setSeverity(currentRule.getSeverity()); context.saveViolation(violation);
@Override public void define(Context context) { NewRepository repository = context .createRepository(DelphiPmdConstants.REPOSITORY_KEY, DelphiLanguage.KEY) .setName(DelphiPmdConstants.REPOSITORY_NAME); List<org.sonar.api.rules.Rule> rules = DelphiRulesUtils.getInitialReferential(); // TODO Review // https://github.com/SonarCommunity/sonar-pmd/blob/master/src/main/java/org/sonar/plugins/pmd/PmdRulesDefinition.java for (org.sonar.api.rules.Rule rule : rules) { NewRule newRule = repository.createRule(rule.getKey()) .setName(rule.getName()) .setHtmlDescription(rule.getDescription()) .setInternalKey(rule.getConfigKey()) .setSeverity(rule.getSeverity().name()); for (RuleParam param : rule.getParams()) { newRule.createParam(param.getKey()) .setDefaultValue(param.getDefaultValue()) .setType(RuleParamType.parse(param.getType())) .setDescription(param.getDescription()); } } SqaleXmlLoader.load(repository, "/org/sonar/plugins/delphi/sqale/delphi-model.xml"); repository.done(); }
newRule.setInternalKey(rule.getConfigKey()); newRule.setTemplate(rule.isTemplate()); newRule.setSeverity(rule.getSeverity().toString()); newRule.setStatus(rule.getStatus() == null ? RuleStatus.defaultStatus() : RuleStatus.valueOf(rule.getStatus())); newRule.setTags(rule.getTags());