@Test public void getDeprecatedKeys_returns_keys_in_order_of_addDeprecatedKeys_calls() { Set<RuleKey> ruleKeys = ImmutableSet.of(RuleKey.of("foo", "AAA"), RuleKey.of("bar", "CCCC"), RuleKey.of("doh", "CCCC"), RuleKey.of("foo", "BBBBBBBBBB")); List<RuleKey> sortedRuleKeys = ruleKeys.stream().sorted(Ordering.natural().onResultOf(RuleKey::toString)).collect(Collectors.toList()); // ensure we don't have the same order Assume.assumeTrue(!ImmutableList.copyOf(ruleKeys).equals(sortedRuleKeys)); String repositoryKey = "foo"; String ruleKey = "doh"; RulesDefinition.NewRepository newRepository = context.createRepository(repositoryKey, "bar"); RulesDefinition.NewRule newRule = newRepository.createRule(ruleKey) .setName("doh rule") .setHtmlDescription("doh description"); sortedRuleKeys.forEach(r -> newRule.addDeprecatedRuleKey(r.repository(), r.rule())); newRepository.done(); RulesDefinition.Repository repository = context.repository(repositoryKey); RulesDefinition.Rule rule = repository.rule(ruleKey); assertThat(ImmutableList.copyOf(rule.deprecatedRuleKeys())) .isEqualTo(sortedRuleKeys); }
.setName("Name2") .setHtmlDescription("Description") .addDeprecatedRuleKey(repository1, ruleKey); repo.done(); });
.setName("Name2") .setHtmlDescription("Description") .addDeprecatedRuleKey(repository, ruleKey1); repo.done(); });
.setType(RuleType.CODE_SMELL) .setStatus(RuleStatus.BETA) .addDeprecatedRuleKey("fake", "rule1") .addDeprecatedRuleKey("fake", "rule2"); repo.done(); });
.setName("Name2") .setHtmlDescription("Description") .addDeprecatedRuleKey("foo", "bar") .addDeprecatedRuleKey(repository1, ruleKey1) .addDeprecatedRuleKey("some", "noise"); repo.done(); });
.setName(name) .setHtmlDescription(description) .addDeprecatedRuleKey(repo1, ruleKey1); repo.done(); });
.setType(RuleType.CODE_SMELL) .setStatus(RuleStatus.BETA) .addDeprecatedRuleKey("fake", "rule1") .addDeprecatedRuleKey("fake", "rule2"); repo.done(); });
@Test public void declaring_two_rules_with_same_deprecated_RuleKey_should_throw_ISE() { expectedException.expect(IllegalStateException.class); expectedException.expectMessage("The following deprecated rule keys are declared at least twice [fake:old]"); execute(context -> { NewRepository repo = context.createRepository("fake", "java"); repo.createRule("newKey1") .setName("One") .setHtmlDescription("Description of One") .setSeverity(BLOCKER) .setInternalKey("config1") .setTags("tag1", "tag2", "tag3") .setType(RuleType.CODE_SMELL) .addDeprecatedRuleKey("fake", "old") .setStatus(RuleStatus.BETA); repo.createRule("newKey2") .setName("One") .setHtmlDescription("Description of One") .setSeverity(BLOCKER) .setInternalKey("config1") .setTags("tag1", "tag2", "tag3") .setType(RuleType.CODE_SMELL) .addDeprecatedRuleKey("fake", "old") .setStatus(RuleStatus.BETA); repo.done(); }); }
@Test public void declaring_a_rule_with_an_existing_RuleKey_still_used_should_throw_IAE() { expectedException.expect(IllegalArgumentException.class); expectedException.expectMessage("The rule 'newKey1' of repository 'fake' is declared several times"); execute(context -> { NewRepository repo = context.createRepository("fake", "java"); repo.createRule("newKey1") .setName("One") .setHtmlDescription("Description of One") .setSeverity(BLOCKER) .setInternalKey("config1") .setTags("tag1", "tag2", "tag3") .setType(RuleType.CODE_SMELL) .setStatus(RuleStatus.BETA); repo.createRule("newKey1") .setName("One") .setHtmlDescription("Description of One") .setSeverity(BLOCKER) .setInternalKey("config1") .setTags("tag1", "tag2", "tag3") .setType(RuleType.CODE_SMELL) .addDeprecatedRuleKey("fake", "newKey1") .setStatus(RuleStatus.BETA); repo.done(); }); }
@Test public void declaring_a_rule_with_a_deprecated_RuleKey_still_used_should_throw_ISE() { expectedException.expect(IllegalStateException.class); expectedException.expectMessage("The following rule keys are declared both as deprecated and used key [fake:newKey1]"); execute(context -> { NewRepository repo = context.createRepository("fake", "java"); repo.createRule("newKey1") .setName("One") .setHtmlDescription("Description of One") .setSeverity(BLOCKER) .setInternalKey("config1") .setTags("tag1", "tag2", "tag3") .setType(RuleType.CODE_SMELL) .setStatus(RuleStatus.BETA); repo.createRule("newKey2") .setName("One") .setHtmlDescription("Description of One") .setSeverity(BLOCKER) .setInternalKey("config1") .setTags("tag1", "tag2", "tag3") .setType(RuleType.CODE_SMELL) .addDeprecatedRuleKey("fake", "newKey1") .setStatus(RuleStatus.BETA); repo.done(); }); }
@Test public void getDeprecatedKeys_does_not_return_the_same_key_more_than_once() { RuleKey duplicatedRuleKey = RuleKey.of("foo", "AAA"); RuleKey ruleKey2 = RuleKey.of("bar", "CCCC"); RuleKey ruleKey3 = RuleKey.of("foo", "BBBBBBBBBB"); List<RuleKey> ruleKeys = ImmutableList.of(duplicatedRuleKey, ruleKey2, duplicatedRuleKey, duplicatedRuleKey, ruleKey3); String repositoryKey = "foo"; String ruleKey = "doh"; RulesDefinition.NewRepository newRepository = context.createRepository(repositoryKey, "bar"); RulesDefinition.NewRule newRule = newRepository.createRule(ruleKey) .setName("doh rule") .setHtmlDescription("doh description"); ruleKeys.forEach(r -> newRule.addDeprecatedRuleKey(r.repository(), r.rule())); newRepository.done(); RulesDefinition.Repository repository = context.repository(repositoryKey); RulesDefinition.Rule rule = repository.rule(ruleKey); assertThat(rule.deprecatedRuleKeys()) .containsExactly(ruleKey2, duplicatedRuleKey, ruleKey3); }
@Test @UseDataProvider("nullOrEmpty") public void addDeprecatedRuleKey_fails_with_IAE_if_key_is_null_or_empty(String nullOrEmpty) { RulesDefinition.NewRepository newRepository = context.createRepository("foo", "bar"); RulesDefinition.NewRule newRule = newRepository.createRule("doh"); expectedException.expect(IllegalArgumentException.class); expectedException.expectMessage("Rule must be set"); newRule.addDeprecatedRuleKey("oldRepo", nullOrEmpty); }
@Test public void updating_the_deprecated_to_a_new_ruleKey_should_throw_an_ISE() { // On this new rule add a deprecated key execute(context -> createRule(context, "javascript", "javascript", "s103", r -> r.addDeprecatedRuleKey("javascript", "linelength"))); expectedException.expect(IllegalStateException.class); expectedException.expectMessage("An incorrect state of deprecated rule keys has been detected.\n " + "The deprecated rule key [javascript:linelength] was previously deprecated by [javascript:s103]. [javascript:s103] should be a deprecated key of [sonarjs:s103],"); // This rule should have been moved to another repository execute(context -> createRule(context, "javascript", "sonarjs", "s103", r -> r.addDeprecatedRuleKey("javascript", "linelength"))); }
@Test @UseDataProvider("nullOrEmpty") public void addDeprecatedRuleKey_fails_with_IAE_if_repository_is_null_or_empty(String nullOrEmpty) { RulesDefinition.NewRepository newRepository = context.createRepository("foo", "bar"); RulesDefinition.NewRule newRule = newRepository.createRule("doh"); expectedException.expect(IllegalArgumentException.class); expectedException.expectMessage("Repository must be set"); newRule.addDeprecatedRuleKey(nullOrEmpty, "oldKey"); }