diff options
author | Fabrice Bellingard <fabrice.bellingard@sonarsource.com> | 2013-02-18 10:27:16 +0100 |
---|---|---|
committer | Fabrice Bellingard <fabrice.bellingard@sonarsource.com> | 2013-02-18 10:27:16 +0100 |
commit | 6723d2eacd59bc102b4133b9c8c76e82d9dad7a4 (patch) | |
tree | ab9549e805e5c0f69d2af0ce1e93dbbe0b45eb85 | |
parent | 72bf960a39ec08483e3bf5ab35ea38ab54ee5308 (diff) | |
download | sonarqube-6723d2eacd59bc102b4133b9c8c76e82d9dad7a4.tar.gz sonarqube-6723d2eacd59bc102b4133b9c8c76e82d9dad7a4.zip |
SONAR-3583 Do not save violation if rule does not exist in DB (= no ID)
-rw-r--r-- | sonar-batch/src/main/java/org/sonar/batch/index/DefaultIndex.java | 12 | ||||
-rw-r--r-- | sonar-batch/src/test/java/org/sonar/batch/index/DefaultIndexTest.java | 17 |
2 files changed, 22 insertions, 7 deletions
diff --git a/sonar-batch/src/main/java/org/sonar/batch/index/DefaultIndex.java b/sonar-batch/src/main/java/org/sonar/batch/index/DefaultIndex.java index 26ac1277c22..8e6067ef39b 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/index/DefaultIndex.java +++ b/sonar-batch/src/main/java/org/sonar/batch/index/DefaultIndex.java @@ -352,8 +352,8 @@ public class DefaultIndex extends SonarIndex { throw new IllegalArgumentException("Violations are only supported on files, directories and project"); } - if (violation.getRule() == null) { - LOG.warn("Rule is null, ignoring violation {}", violation); + if (violation.getRule() == null || violation.getRule().getId() == null) { + LOG.warn("Rule does not exist (it is null or its ID is null): ignoring violation {}", violation); return; } @@ -470,10 +470,10 @@ public class DefaultIndex extends SonarIndex { if (!StringUtils.equals(Scopes.PROJECT, resource.getScope())) { // not a project nor a library uid = new StringBuilder(ResourceModel.KEY_SIZE) - .append(project.getKey()) - .append(':') - .append(resource.getKey()) - .toString(); + .append(project.getKey()) + .append(':') + .append(resource.getKey()) + .toString(); } return uid; } diff --git a/sonar-batch/src/test/java/org/sonar/batch/index/DefaultIndexTest.java b/sonar-batch/src/test/java/org/sonar/batch/index/DefaultIndexTest.java index 87cb9361e11..8ea5f04e9c1 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/index/DefaultIndexTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/index/DefaultIndexTest.java @@ -77,8 +77,9 @@ public class DefaultIndexTest { }; RulesProfile rulesProfile = RulesProfile.create(); rule = Rule.create("repoKey", "ruleKey", "Rule"); + rule.setId(1); rulesProfile.activateRule(rule, null); - index.setCurrentProject(project, new ResourceFilters(new ResourceFilter[] { filter }), new ViolationFilters(), rulesProfile); + index.setCurrentProject(project, new ResourceFilters(new ResourceFilter[] {filter}), new ViolationFilters(), rulesProfile); index.doStart(project); } @@ -202,6 +203,20 @@ public class DefaultIndexTest { assertThat(index.getViolations(file).size(), is(0)); } + /** + * See https://jira.codehaus.org/browse/SONAR-3583 + */ + @Test + public void shouldNotFailWhenSavingViolationOnRuleThatDoesNotExistInDB() { + Rule ruleWithoutID = Rule.create("repoKey", "ruleKey", "Rule"); + + File file = new File("org/foo/Bar.java"); + Violation violation = Violation.create(ruleWithoutID, file); + index.addViolation(violation); + + assertThat(index.getViolations(file).size(), is(0)); + } + @Test public void testGetViolations() { File file = new File("org/foo/Bar.java"); |