]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-2107 Fix NPE when saving a violation on a null rule
authorEvgeny Mandrikov <mandrikov@gmail.com>
Fri, 25 Mar 2011 08:45:29 +0000 (11:45 +0300)
committerEvgeny Mandrikov <mandrikov@gmail.com>
Tue, 29 Mar 2011 21:22:49 +0000 (01:22 +0400)
sonar-batch/src/main/java/org/sonar/batch/index/DefaultIndex.java
sonar-batch/src/test/java/org/sonar/batch/index/DefaultIndexTest.java

index 75e1f03ca78de19129a981ad9f2a2cc1bb360bb0..9bfc83f71f56844b2164a1440713351296e5773e 100644 (file)
@@ -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);
index 4083aaccb18a611e8d63ee6c6bdb0944f19b8493..39080fe6a5b1063fb7b3bbfa31aad2844a246002 100644 (file)
  */
 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));
+  }
 }