From: Godin Date: Fri, 3 Dec 2010 13:38:45 +0000 (+0000) Subject: SONAR-1450: Prevent NPE with Natural and VB plugins X-Git-Tag: 2.6~423 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=555287b5a0fd21e19f26afa7b9f683a244c39dad;p=sonarqube.git SONAR-1450: Prevent NPE with Natural and VB plugins --- diff --git a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/PastViolationsLoader.java b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/PastViolationsLoader.java index c6a8a503256..30810cef199 100644 --- a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/PastViolationsLoader.java +++ b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/PastViolationsLoader.java @@ -23,6 +23,9 @@ public class PastViolationsLoader implements BatchExtension { public List 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(); diff --git a/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/timemachine/PastViolationsLoaderTest.java b/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/timemachine/PastViolationsLoaderTest.java index 5f7bc8e0b34..341b5107c4c 100644 --- a/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/timemachine/PastViolationsLoaderTest.java +++ b/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/timemachine/PastViolationsLoaderTest.java @@ -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());