@Test public void build_rules() { NewActiveRule activeRule = new NewActiveRule.Builder() .setRuleKey(RuleKey.of("squid", "S0001")) .setName("My Rule") .setSeverity(Severity.CRITICAL) .setInternalKey("__S0001__") .setParam("min", "20") .build(); .addRule(new NewActiveRule.Builder().setRuleKey(RuleKey.of("squid", "S0002")).build()) .addRule(new NewActiveRule.Builder() .setRuleKey(RuleKey.of("findbugs", "NPE")) .setInternalKey(null) .setSeverity(null) .setParam("foo", null) .build()) .build();
private static NewActiveRule transform(LoadedActiveRule activeRule, String qProfileKey) { NewActiveRule.Builder builder = new NewActiveRule.Builder(); builder .setRuleKey(activeRule.getRuleKey()) .setName(activeRule.getName()) .setSeverity(activeRule.getSeverity()) .setCreatedAt(activeRule.getCreatedAt()) .setUpdatedAt(activeRule.getUpdatedAt()) .setLanguage(activeRule.getLanguage()) .setInternalKey(activeRule.getInternalKey()) .setTemplateRuleKey(activeRule.getTemplateRuleKey()) .setQProfileKey(qProfileKey); // load parameters if (activeRule.getParams() != null) { for (Map.Entry<String, String> params : activeRule.getParams().entrySet()) { builder.setParam(params.getKey(), params.getValue()); } } return builder.build(); }
Builder newAr = new NewActiveRule.Builder() .setRuleKey(ruleKey) .setLanguage(repo.language()) .setName(rule.name()) .setSeverity(rule.severity()) .setInternalKey(rule.internalKey()); for (Param param : rule.params()) { newAr.setParam(param.key(), param.defaultValue()); builder.addRule(newAr.build());
private static void createNewActiveRule(ActiveRulesBuilder builder, ActiveRule activeRule, Sonarlint.Rules storageRules, String language, Rules rules) { RuleKey ruleKey = RuleKey.of(activeRule.getRepo(), activeRule.getKey()); Rule rule = rules.find(ruleKey); Sonarlint.Rules.Rule storageRule; try { storageRule = storageRules.getRulesByKeyOrThrow(ruleKey.toString()); } catch (IllegalArgumentException e) { throw new MessageException("Unknown active rule in the quality profile of the project. Please update the SonarQube server binding."); } Builder newActiveRule = new NewActiveRule.Builder() .setRuleKey(ruleKey) .setLanguage(language) .setName(rule.name()) .setInternalKey(rule.internalKey()) .setSeverity(activeRule.getSeverity()); if (!StringUtils.isEmpty(storageRule.getTemplateKey())) { RuleKey templateRuleKey = RuleKey.parse(storageRule.getTemplateKey()); newActiveRule.setTemplateRuleKey(templateRuleKey.rule()); } for (Map.Entry<String, String> param : activeRule.getParamsMap().entrySet()) { newActiveRule.setParam(param.getKey(), param.getValue()); } builder.addRule(newActiveRule.build()); }
private static NewActiveRule transform(LoadedActiveRule activeRule, String qProfileKey) { NewActiveRule.Builder builder = new NewActiveRule.Builder(); builder .setRuleKey(activeRule.getRuleKey()) .setName(activeRule.getName()) .setSeverity(activeRule.getSeverity()) .setCreatedAt(activeRule.getCreatedAt()) .setUpdatedAt(activeRule.getUpdatedAt()) .setLanguage(activeRule.getLanguage()) .setInternalKey(activeRule.getInternalKey()) .setTemplateRuleKey(activeRule.getTemplateRuleKey()) .setQProfileKey(qProfileKey); // load parameters if (activeRule.getParams() != null) { for (Map.Entry<String, String> params : activeRule.getParams().entrySet()) { builder.setParam(params.getKey(), params.getValue()); } } return builder.build(); }
private static void registerProfile(ActiveRulesBuilder builder, String language, Map.Entry<String, Collection<RulesProfile>> entry) { for (RulesProfile rp : entry.getValue()) { for (ActiveRule ar : rp.getActiveRules()) { Builder newAr = new NewActiveRule.Builder() .setRuleKey(RuleKey.of(ar.getRepositoryKey(), ar.getRuleKey())) .setLanguage(language) .setName(ar.getRule().getName()) .setSeverity(ar.getSeverity().name()) .setInternalKey(ar.getConfigKey()); for (ActiveRuleParam param : ar.getActiveRuleParams()) { newAr.setParam(param.getKey(), param.getValue()); } builder.addRule(newAr.build()); } } }
/** * SONAR-2900 */ @Test public void checks_as_objects() { RuleKey ruleKey = RuleKey.of("squid", "org.sonar.api.batch.rule.CheckWithStringProperty"); NewActiveRule rule = new NewActiveRule.Builder() .setRuleKey(ruleKey) .setParam("pattern", "foo") .build(); builder.addRule(rule); CheckFactory checkFactory = new CheckFactory(builder.build()); CheckWithStringProperty check = new CheckWithStringProperty(); Checks checks = checkFactory.create("squid").addAnnotatedChecks(check); Object createdCheck = checks.of(ruleKey); assertThat(createdCheck).isSameAs(check); assertThat(((CheckWithStringProperty) createdCheck).getPattern()).isEqualTo("foo"); } }
@Test public void add_issue_to_cache() { ruleBuilder.add(SQUID_RULE_KEY).setName(SQUID_RULE_NAME); activeRulesBuilder.addRule(new NewActiveRule.Builder() .setRuleKey(SQUID_RULE_KEY) .setSeverity(Severity.INFO) .setQProfileKey("qp-1") .build()); initModuleIssues(); DefaultIssue issue = new DefaultIssue(project) .at(new DefaultIssueLocation().on(file).at(file.selectLine(3)).message("Foo")) .forRule(SQUID_RULE_KEY) .overrideSeverity(org.sonar.api.batch.rule.Severity.CRITICAL); when(filters.accept(any(InputComponent.class), any(ScannerReport.Issue.class))).thenReturn(true); boolean added = moduleIssues.initAndAddIssue(issue); assertThat(added).isTrue(); ArgumentCaptor<ScannerReport.Issue> argument = ArgumentCaptor.forClass(ScannerReport.Issue.class); verify(reportPublisher.getWriter()).appendComponentIssue(eq(file.scannerId()), argument.capture()); assertThat(argument.getValue().getSeverity()).isEqualTo(org.sonar.scanner.protocol.Constants.Severity.CRITICAL); }
@Test public void use_severity_from_active_rule_if_no_severity_on_issue() { ruleBuilder.add(SQUID_RULE_KEY).setName(SQUID_RULE_NAME); activeRulesBuilder.addRule(new NewActiveRule.Builder() .setRuleKey(SQUID_RULE_KEY) .setSeverity(Severity.INFO) .setQProfileKey("qp-1") .build()); initModuleIssues(); DefaultIssue issue = new DefaultIssue(project) .at(new DefaultIssueLocation().on(file).at(file.selectLine(3)).message("Foo")) .forRule(SQUID_RULE_KEY); when(filters.accept(any(InputComponent.class), any(ScannerReport.Issue.class))).thenReturn(true); moduleIssues.initAndAddIssue(issue); ArgumentCaptor<ScannerReport.Issue> argument = ArgumentCaptor.forClass(ScannerReport.Issue.class); verify(reportPublisher.getWriter()).appendComponentIssue(eq(file.scannerId()), argument.capture()); assertThat(argument.getValue().getSeverity()).isEqualTo(org.sonar.scanner.protocol.Constants.Severity.INFO); }
@Test public void should_accept_issues_on_no_sonar_rules() { // The "No Sonar" rule logs violations on the lines that are flagged with "NOSONAR" !! ruleBuilder.add(NOSONAR_RULE_KEY).setName("No Sonar"); activeRulesBuilder.addRule(new NewActiveRule.Builder() .setRuleKey(NOSONAR_RULE_KEY) .setSeverity(Severity.INFO) .setQProfileKey("qp-1") .build()); initModuleIssues(); file.noSonarAt(new HashSet<>(Collections.singletonList(3))); DefaultIssue issue = new DefaultIssue(project) .at(new DefaultIssueLocation().on(file).at(file.selectLine(3)).message("")) .forRule(NOSONAR_RULE_KEY); when(filters.accept(any(InputComponent.class), any(ScannerReport.Issue.class))).thenReturn(true); boolean added = moduleIssues.initAndAddIssue(issue); assertThat(added).isTrue(); verify(reportPublisher.getWriter()).appendComponentIssue(eq(file.scannerId()), any()); }
@Test public void filter_issue() { ruleBuilder.add(SQUID_RULE_KEY).setName(SQUID_RULE_NAME); activeRulesBuilder.addRule(new NewActiveRule.Builder() .setRuleKey(SQUID_RULE_KEY) .setSeverity(Severity.INFO) .setQProfileKey("qp-1") .build()); initModuleIssues(); DefaultIssue issue = new DefaultIssue(project) .at(new DefaultIssueLocation().on(file).at(file.selectLine(3)).message("")) .forRule(SQUID_RULE_KEY); when(filters.accept(any(InputComponent.class), any(ScannerReport.Issue.class))).thenReturn(false); boolean added = moduleIssues.initAndAddIssue(issue); assertThat(added).isFalse(); verifyZeroInteractions(reportPublisher); }
@Test public void should_ignore_lines_commented_with_nosonar() { ruleBuilder.add(SQUID_RULE_KEY).setName(SQUID_RULE_NAME); activeRulesBuilder.addRule(new NewActiveRule.Builder() .setRuleKey(SQUID_RULE_KEY) .setSeverity(Severity.INFO) .setQProfileKey("qp-1") .build()); initModuleIssues(); DefaultIssue issue = new DefaultIssue(project) .at(new DefaultIssueLocation().on(file).at(file.selectLine(3)).message("")) .forRule(SQUID_RULE_KEY); file.noSonarAt(new HashSet<>(Collections.singletonList(3))); boolean added = moduleIssues.initAndAddIssue(issue); assertThat(added).isFalse(); verifyZeroInteractions(reportPublisher); }
/** * SONAR-3164 */ @Test public void param_as_inherited_field() { RuleKey ruleKey = RuleKey.of("squid", "org.sonar.api.batch.rule.CheckWithPrimitiveProperties"); NewActiveRule rule = new NewActiveRule.Builder() .setRuleKey(ruleKey) .setParam("max", "300") .build(); builder.addRule(rule); CheckFactory checkFactory = new CheckFactory(builder.build()); Checks checks = checkFactory.create("squid").addAnnotatedChecks(CheckWithPrimitiveProperties.class); Object check = checks.of(ruleKey); assertThat(check).isInstanceOf(CheckWithPrimitiveProperties.class); assertThat(((CheckWithPrimitiveProperties) check).getMax()).isEqualTo(300); }
@Test public void should_optimize_on_repository() { DefaultSensorDescriptor descriptor = new DefaultSensorDescriptor() .createIssuesForRuleRepositories("squid"); assertThat(optimizer.shouldExecute(descriptor)).isFalse(); ActiveRules activeRules = new ActiveRulesBuilder() .addRule(new NewActiveRule.Builder().setRuleKey(RuleKey.of("repo1", "foo")).build()) .build(); optimizer = new ModuleSensorOptimizer(fs, activeRules, settings.asConfig()); assertThat(optimizer.shouldExecute(descriptor)).isFalse(); activeRules = new ActiveRulesBuilder() .addRule(new NewActiveRule.Builder().setRuleKey(RuleKey.of("repo1", "foo")).build()) .addRule(new NewActiveRule.Builder().setRuleKey(RuleKey.of("squid", "rule")).build()) .build(); optimizer = new ModuleSensorOptimizer(fs, activeRules, settings.asConfig()); assertThat(optimizer.shouldExecute(descriptor)).isTrue(); }
@Test public void param_as_primitive_fields() { RuleKey ruleKey = RuleKey.of("squid", "org.sonar.api.batch.rule.CheckWithPrimitiveProperties"); NewActiveRule rule = new NewActiveRule.Builder() .setRuleKey(ruleKey) .setParam("max", "300") .setParam("ignore", "true") .build(); builder.addRule(rule); CheckFactory checkFactory = new CheckFactory(builder.build()); Checks checks = checkFactory.create("squid").addAnnotatedChecks(CheckWithPrimitiveProperties.class); Object check = checks.of(ruleKey); assertThat(check).isInstanceOf(CheckWithPrimitiveProperties.class); assertThat(((CheckWithPrimitiveProperties) check).getMax()).isEqualTo(300); assertThat(((CheckWithPrimitiveProperties) check).isIgnore()).isTrue(); }
@Test public void use_template_rule_key() { RuleKey ruleKey = RuleKey.of("squid", "S0001_123"); NewActiveRule rule = new NewActiveRule.Builder() .setRuleKey(ruleKey) .setTemplateRuleKey("S0001") .build(); builder.addRule(rule); CheckFactory checkFactory = new CheckFactory(builder.build()); Checks checks = checkFactory.create("squid").addAnnotatedChecks(CheckWithKey.class); Object check = checks.of(ruleKey); assertThat(check).isInstanceOf(CheckWithKey.class); assertThat(checks.of(ruleKey)).isSameAs(check); assertThat(checks.ruleKey(check)).isEqualTo(ruleKey); assertThat(checks.all()).containsOnly(check); }
@Test public void override_field_key() { RuleKey ruleKey = RuleKey.of("squid", "org.sonar.api.batch.rule.CheckWithOverriddenPropertyKey"); NewActiveRule rule = new NewActiveRule.Builder() .setRuleKey(ruleKey) .setParam("maximum", "300") .build(); builder.addRule(rule); CheckFactory checkFactory = new CheckFactory(builder.build()); Checks checks = checkFactory.create("squid").addAnnotatedChecks(CheckWithOverriddenPropertyKey.class); Object check = checks.of(ruleKey); assertThat(check).isInstanceOf(CheckWithOverriddenPropertyKey.class); assertThat(((CheckWithOverriddenPropertyKey) check).getMax()).isEqualTo(300); }
@Test public void class_name_as_check_key() { RuleKey ruleKey = RuleKey.of("squid", "org.sonar.api.batch.rule.CheckWithoutProperties"); NewActiveRule rule = new NewActiveRule.Builder() .setRuleKey(ruleKey) .build(); builder.addRule(rule); CheckFactory checkFactory = new CheckFactory(builder.build()); Checks checks = checkFactory.create("squid").addAnnotatedChecks(CheckWithoutProperties.class); Object check = checks.of(ruleKey); assertThat(check).isInstanceOf(CheckWithoutProperties.class); assertThat(checks.all()).containsOnly(check); assertThat(checks.ruleKey(check)).isEqualTo(ruleKey); }
@Before public void setBuilder() { builder = new NewActiveRule.Builder(); }
@Test public void fail_if_missing_field() { thrown.expect(IllegalStateException.class); thrown.expectMessage("The field 'unknown' does not exist or is not annotated with @RuleProperty in the class org.sonar.api.batch.rule.CheckWithStringProperty"); RuleKey ruleKey = RuleKey.of("squid", "org.sonar.api.batch.rule.CheckWithStringProperty"); NewActiveRule rule = new NewActiveRule.Builder() .setRuleKey(ruleKey) .setParam("unknown", "foo") .build(); builder.addRule(rule); CheckFactory checkFactory = new CheckFactory(builder.build()); checkFactory.create("squid").addAnnotatedChecks(CheckWithStringProperty.class); }