]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-1450: Prevent NPE with Natural and VB plugins
authorGodin <mandrikov@gmail.com>
Fri, 3 Dec 2010 13:38:45 +0000 (13:38 +0000)
committerGodin <mandrikov@gmail.com>
Fri, 3 Dec 2010 13:38:45 +0000 (13:38 +0000)
plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/PastViolationsLoader.java
plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/timemachine/PastViolationsLoaderTest.java

index c6a8a503256b80e030f096c6a2ae32ac9a491b4a..30810cef19989dc1f5a35f04ec64f5ccd8832a16 100644 (file)
@@ -23,6 +23,9 @@ public class PastViolationsLoader implements BatchExtension {
 
   public List<RuleFailureModel> getPastViolations(Resource resource) {
     Snapshot snapshot = resourcePersister.getSnapshot(resource);
+    if (snapshot == null) { // TODO Godin: Prevent NPE with Natural and VB plugins
+      return Collections.emptyList();
+    }
     Snapshot previousLastSnapshot = resourcePersister.getLastSnapshot(snapshot, true);
     if (previousLastSnapshot == null) {
       return Collections.emptyList();
index 5f7bc8e0b34ab21aeb6f5db38d421f982bd862d3..341b5107c4c02a421e39bcab7191fbd3d39d2a96 100644 (file)
@@ -5,6 +5,7 @@ import org.junit.Test;
 import org.sonar.api.database.model.RuleFailureModel;
 import org.sonar.api.database.model.Snapshot;
 import org.sonar.api.resources.JavaFile;
+import org.sonar.api.resources.Resource;
 import org.sonar.batch.index.ResourcePersister;
 import org.sonar.jpa.test.AbstractDbUnitTestCase;
 
@@ -33,6 +34,8 @@ public class PastViolationsLoaderTest extends AbstractDbUnitTestCase {
   @Test
   public void shouldGetPastResourceViolations() {
     Snapshot snapshot = getSession().getSingleResult(Snapshot.class, "id", 1000);
+    doReturn(snapshot).when(resourcePersister)
+        .getSnapshot(any(Resource.class));
     doReturn(snapshot).when(resourcePersister)
         .getLastSnapshot(any(Snapshot.class), anyBoolean());