JavaRulesDefinition implements RulesDefinition { @VisibleForTesting protected void newRule(Class<?> ruleClass, NewRepository repository, JavaSonarWayProfile.Profile profile) { org.sonar.check.Rule ruleAnnotation = AnnotationUtils.getAnnotation(ruleClass, org.sonar.check.Rule.class); if (ruleAnnotation == null) { throw new IllegalArgumentException("No Rule annotation was found on " + ruleClass); } String ruleKey = ruleAnnotation.key(); if (StringUtils.isEmpty(ruleKey)) { throw new IllegalArgumentException("No key is defined in Rule annotation of " + ruleClass); } NewRule rule = repository.rule(ruleKey); if (rule == null) { throw new IllegalStateException("No rule was created for " + ruleClass + " in " + repository.key()); } DeprecatedRuleKey deprecatedRuleKeyAnnotation = AnnotationUtils.getAnnotation(ruleClass, DeprecatedRuleKey.class); if (deprecatedRuleKeyAnnotation != null) { rule.addDeprecatedRuleKey(deprecatedRuleKeyAnnotation.repositoryKey(), deprecatedRuleKeyAnnotation.ruleKey()); } else { rule.addDeprecatedRuleKey("squid", ruleKey); } String rspecKey = rspecKey(ruleClass, rule); RuleMetadata ruleMetadata = readRuleMetadata(rspecKey); addMetadata(rule, ruleMetadata); String ruleHtmlDescription = readRuleHtmlDescription(rspecKey); if (ruleHtmlDescription != null) { rule.setHtmlDescription(ruleHtmlDescription); } boolean activatedInProfile = profile.ruleKeys.contains(ruleKey) || profile.ruleKeys.contains(rspecKey); boolean isSecurityHotspot = ruleMetadata != null && ruleMetadata.isSecurityHotspot(); rule.setActivatedByDefault(activatedInProfile && !isSecurityHotspot); rule.setTemplate(TEMPLATE_RULE_KEY.contains(ruleKey)); } JavaRulesDefinition();  JavaRulesDefinition(Configuration settings); @SuppressWarnings("rawtypes") @Override void define(Context context);  }
@Test void test_invalid_checks() throws Exception { RulesDefinition.Context context = new RulesDefinition.Context(); RulesDefinition.NewRepository newRepository = context.createRepository("test", "java"); newRepository.createRule("correctRule"); JavaRulesDefinition definition = new JavaRulesDefinition(settings); JavaSonarWayProfile.Profile profile = new JavaSonarWayProfile.Profile(); profile.ruleKeys = new ArrayList<>(); try { definition.newRule(CheckWithNoAnnotation.class, newRepository, profile); } catch (IllegalArgumentException iae) { assertThat(iae).hasMessage("No Rule annotation was found on class " + CheckWithNoAnnotation.class.getName()); } try { definition.newRule(EmptyRuleKey.class, newRepository, profile); } catch (IllegalArgumentException iae) { assertThat(iae).hasMessage("No key is defined in Rule annotation of class " + EmptyRuleKey.class.getName()); } try { definition.newRule(UnregisteredRule.class, newRepository, profile); } catch (IllegalStateException ise) { assertThat(ise).hasMessage("No rule was created for class " + UnregisteredRule.class.getName() + " in test"); } definition.newRule(CorrectRule.class, newRepository, profile); }
@Test void test_security_standards_not_set_when_unsupported() throws Exception { JavaRulesDefinition definition = new JavaRulesDefinition(settings); RulesDefinition.NewRepository repository = mock(RulesDefinition.NewRepository.class); RulesDefinition.NewRule newRule = mock(RulesDefinition.NewRule.class); when(repository.rule(any())).thenReturn(newRule); definition.newRule(ServletMethodsExceptionsThrownCheck.class, repository, JavaSonarWayProfile.readProfile()); verify(newRule, never()).addOwaspTop10(); verify(newRule, never()).addCwe(); }