diff options
author | Evgeny Mandrikov <mandrikov@gmail.com> | 2011-03-25 11:45:29 +0300 |
---|---|---|
committer | Evgeny Mandrikov <mandrikov@gmail.com> | 2011-03-30 01:22:49 +0400 |
commit | 8e7b3b00b4b9eace1f814a35c1b92a09818dbdf9 (patch) | |
tree | c154e32ba2c62ae8a1c6aa72a67540d96f24b502 /sonar-batch | |
parent | 8eb8b248e31554e00b0eb695f09c7a9f2bdbeba8 (diff) | |
download | sonarqube-8e7b3b00b4b9eace1f814a35c1b92a09818dbdf9.tar.gz sonarqube-8e7b3b00b4b9eace1f814a35c1b92a09818dbdf9.zip |
SONAR-2107 Fix NPE when saving a violation on a null rule
Diffstat (limited to 'sonar-batch')
-rw-r--r-- | sonar-batch/src/main/java/org/sonar/batch/index/DefaultIndex.java | 5 | ||||
-rw-r--r-- | sonar-batch/src/test/java/org/sonar/batch/index/DefaultIndexTest.java | 31 |
2 files changed, 26 insertions, 10 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 75e1f03ca78..9bfc83f71f5 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 @@ -299,6 +299,11 @@ 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); + return; + } + Bucket bucket = checkIndexed(resource); if (bucket != null && !bucket.isExcluded()) { boolean isIgnored = !force && violationFilters != null && violationFilters.isIgnored(violation); 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 4083aaccb18..39080fe6a5b 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 @@ -19,9 +19,14 @@ */ package org.sonar.batch.index; +import static org.hamcrest.Matchers.nullValue; +import static org.hamcrest.core.Is.is; +import static org.junit.Assert.assertThat; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + import org.apache.commons.lang.StringUtils; import org.junit.Before; -import org.junit.Ignore; import org.junit.Test; import org.sonar.api.batch.ResourceFilter; import org.sonar.api.measures.CoreMetrics; @@ -30,18 +35,14 @@ import org.sonar.api.measures.MeasuresFilters; import org.sonar.api.measures.MetricFinder; import org.sonar.api.profiles.RulesProfile; import org.sonar.api.resources.*; +import org.sonar.api.rules.Rule; +import org.sonar.api.rules.Violation; import org.sonar.api.utils.SonarException; import org.sonar.batch.DefaultResourceCreationLock; import org.sonar.batch.ProjectTree; import org.sonar.batch.ResourceFilters; import org.sonar.batch.ViolationFilters; -import static org.hamcrest.Matchers.nullValue; -import static org.hamcrest.core.Is.is; -import static org.junit.Assert.assertThat; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; - public class DefaultIndexTest { private DefaultIndex index = null; @@ -61,11 +62,10 @@ public class DefaultIndexTest { return StringUtils.containsIgnoreCase(resource.getKey(), "excluded"); } }; - index.setCurrentProject(project, new ResourceFilters(new ResourceFilter[]{filter}), new ViolationFilters(), RulesProfile.create()); + index.setCurrentProject(project, new ResourceFilters(new ResourceFilter[] { filter }), new ViolationFilters(), RulesProfile.create()); index.doStart(project); } - @Test public void shouldCreateUID() { Project project = new Project("my_project"); @@ -135,7 +135,6 @@ public class DefaultIndexTest { assertThat(index.getParent(fileRef), nullValue()); } - /** * Only a warning is logged when index is locked. */ @@ -174,4 +173,16 @@ public class DefaultIndexTest { assertThat(index.isIndexed(dir, true), is(true)); assertThat(index.getMeasures(dir, MeasuresFilters.metric("ncloc")).getIntValue(), is(50)); } + + /** + * See http://jira.codehaus.org/browse/SONAR-2107 + */ + @Test + public void shouldNotFailWhenSavingViolationOnNullRule() { + File file = new File("org/foo/Bar.java"); + Violation violation = Violation.create((Rule) null, file); + index.addViolation(violation); + + assertThat(index.getViolations(file).size(), is(0)); + } } |