public NewActiveRule build() { return new NewActiveRule(this); } }
public Rules build() { return new DefaultRules(map.values()); } }
public ActiveRules build() { return new DefaultActiveRules(map.values()); } }
@Test public void fail_to_add_twice_the_same_rule() { ActiveRulesBuilder builder = new ActiveRulesBuilder(); NewActiveRule rule = new NewActiveRule.Builder() .setRuleKey(RuleKey.of("squid", "S0001")) .build(); builder.addRule(rule); thrown.expect(IllegalStateException.class); thrown.expectMessage("Rule 'squid:S0001' is already activated"); builder.addRule(rule); } }
@Test public void ignore_null_rule_of_active_rule() { ruleBuilder.add(SQUID_RULE_KEY).setName(SQUID_RULE_NAME); activeRulesBuilder.addRule(new NewActiveRule.Builder().setRuleKey(SQUID_RULE_KEY).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); boolean added = moduleIssues.initAndAddIssue(issue); assertThat(added).isFalse(); verifyZeroInteractions(reportPublisher); }
@Test public void fail_if_field_type_is_not_supported() { thrown.expect(SonarException.class); RuleKey ruleKey = RuleKey.of("squid", "org.sonar.api.batch.rule.CheckWithUnsupportedPropertyType"); NewActiveRule rule = new NewActiveRule.Builder() .setRuleKey(ruleKey) .setParam("max", "300") .build(); builder.addRule(rule); CheckFactory checkFactory = new CheckFactory(builder.build()); checkFactory.create("squid").addAnnotatedChecks(CheckWithUnsupportedPropertyType.class); }
private static Rules load(RulesLoader ref) { Profiler profiler = Profiler.create(LOG).startInfo(LOG_MSG); List<Rule> loadedRules = ref.load(); RulesBuilder builder = new RulesBuilder(); for (Rule r : loadedRules) { NewRule newRule = builder.add(RuleKey.of(r.getRepository(), r.getKey())); newRule.setName(r.getName()); newRule.setInternalKey(r.getInternalKey()); } profiler.stopInfo(); return builder.build(); } }
@Test public void fail_to_add_twice_the_same_param() { RulesBuilder builder = new RulesBuilder(); NewRule newRule = builder.add(RuleKey.of("squid", "S0001")); newRule.addParam("min"); newRule.addParam("max"); thrown.expect(IllegalStateException.class); thrown.expectMessage("Parameter 'min' already exists on rule 'squid:S0001'"); newRule.addParam("min"); } }
@Test public void fail_to_add_twice_the_same_rule() { RulesBuilder builder = new RulesBuilder(); builder.add(RuleKey.of("squid", "S0001")); thrown.expect(IllegalStateException.class); thrown.expectMessage("Rule 'squid:S0001' already exists"); builder.add(RuleKey.of("squid", "S0001")); }
/** * Every rules and active rules has to be added in builders before creating IssuePublisher */ private void initModuleIssues() { moduleIssues = new IssuePublisher(activeRulesBuilder.build(), filters, reportPublisher); }
private NewRule createRule(String key, String repo, String internalKey) { RuleKey ruleKey = RuleKey.of(repo, key); NewRule newRule = new NewRule(ruleKey); newRule.setInternalKey(internalKey); return newRule; } }
@Test public void testRepeatedInternalKey() { List<NewRule> newRules = new LinkedList<>(); newRules.add(createRule("key1", "repo", "internal")); newRules.add(createRule("key2", "repo", "internal")); DefaultRules rules = new DefaultRules(newRules); assertThat(rules.findByInternalKey("repo", "internal")).hasSize(2); assertThat(rules.find(RuleKey.of("repo", "key1"))).isNotNull(); assertThat(rules.find(RuleKey.of("repo", "key2"))).isNotNull(); assertThat(rules.findByRepository("repo")).hasSize(2); }
@Test public void testRepeatedRule() { List<NewRule> newRules = new LinkedList<>(); newRules.add(createRule("key", "repo", "internal")); newRules.add(createRule("key", "repo", "internal")); DefaultRules rules = new DefaultRules(newRules); assertThat(rules.find(RuleKey.of("repo", "key"))).isNotNull(); }
@Before public void setBuilder() { builder = new NewActiveRule.Builder(); }
public NewRule add(RuleKey key) { if (map.containsKey(key)) { throw new IllegalStateException(String.format("Rule '%s' already exists", key)); } NewRule newRule = new NewRule(key); map.put(key, newRule); return newRule; }
public NewRuleParam addParam(String paramKey) { if (params.containsKey(paramKey)) { throw new IllegalStateException(String.format("Parameter '%s' already exists on rule '%s'", paramKey, key)); } NewRuleParam param = new NewRuleParam(paramKey); params.put(paramKey, param); return param; } }
@Test public void testActiveRules() { NewActiveRule activeRule = new NewActiveRule.Builder() .setRuleKey(RuleKey.of("foo", "bar")) .build(); ActiveRules activeRules = new ActiveRulesBuilder().addRule(activeRule).build(); tester.setActiveRules(activeRules); assertThat(tester.activeRules().findAll()).hasSize(1); }
@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); }
@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); }
@Test public void testNonExistingKey() { List<NewRule> newRules = new LinkedList<>(); newRules.add(createRule("key1", "repo", "internal")); newRules.add(createRule("key2", "repo", "internal")); DefaultRules rules = new DefaultRules(newRules); assertThat(rules.findByInternalKey("xx", "xx")).hasSize(0); assertThat(rules.find(RuleKey.of("xxx", "xx"))).isNull(); assertThat(rules.findByRepository("xxxx")).hasSize(0); }