JavaRulesDefinition implements RulesDefinition { @SuppressWarnings("rawtypes") @Override public void define(Context context) { NewRepository repository = context .createRepository(CheckList.REPOSITORY_KEY, Java.KEY) .setName("SonarAnalyzer"); List<Class> checks = getChecks(); new RulesDefinitionAnnotationLoader().load(repository, Iterables.toArray(checks, Class.class)); JavaSonarWayProfile.Profile profile = JavaSonarWayProfile.readProfile(); for (Class ruleClass : checks) { newRule(ruleClass, repository, profile); } repository.done(); } JavaRulesDefinition();  JavaRulesDefinition(Configuration settings); @SuppressWarnings("rawtypes") @Override void define(Context context);  }
@Test void test_security_hotspot() throws Exception { JavaRulesDefinition definition = new JavaRulesDefinition(settings); RulesDefinition.Context context = new RulesDefinition.Context(); definition.define(context); RulesDefinition.Repository repository = context.repository(REPOSITORY_KEY); RulesDefinition.Rule hardcodedCredentialsRule = repository.rule("S1313"); assertThat(hardcodedCredentialsRule.deprecatedRuleKeys()).containsExactly(RuleKey.of("squid", "S1313")); assertThat(hardcodedCredentialsRule.type()).isEqualTo(RuleType.SECURITY_HOTSPOT); assertThat(hardcodedCredentialsRule.activatedByDefault()).isFalse(); }
@Test void test_security_standards() throws Exception { JavaRulesDefinition definition = new JavaRulesDefinition(settings); RulesDefinition.Context context = new RulesDefinition.Context(); definition.define(context); RulesDefinition.Repository repository = context.repository(REPOSITORY_KEY); RulesDefinition.Rule rule = repository.rule("S1989"); assertThat(rule.deprecatedRuleKeys()).containsExactly(RuleKey.of("squid", "S1989")); assertThat(rule.securityStandards()).containsExactlyInAnyOrder("cwe:600", "owaspTop10:a3"); }
@Test void test_deprecated_key() { JavaRulesDefinition definition = new JavaRulesDefinition(settings); RulesDefinition.Context context = new RulesDefinition.Context(); definition.define(context); RulesDefinition.Repository repository = context.repository(REPOSITORY_KEY); RulesDefinition.Rule rule = repository.rule("S1104"); assertThat(rule.activatedByDefault()).isTrue(); assertThat(rule.deprecatedRuleKeys()).containsExactly(RuleKey.of("squid", "ClassVariableVisibilityCheck")); }
@Test void rules_should_not_have_legacy_key() { JavaRulesDefinition definition = new JavaRulesDefinition(); RulesDefinition.Context context = new RulesDefinition.Context(); definition.define(context); Pattern pattern = Pattern.compile("^S[0-9]{3,5}$"); RulesDefinition.Repository repository = context.repository(REPOSITORY_KEY); repository.rules().forEach(r -> { if (!r.key().equals("NoSonar")) { assertThat(pattern.matcher(r.key()).matches()).isTrue(); } }); }