aboutsummaryrefslogtreecommitdiffstats
path: root/plugins
diff options
context:
space:
mode:
authorFabrice Bellingard <bellingard@gmail.com>2012-02-07 18:17:11 +0100
committerFabrice Bellingard <bellingard@gmail.com>2012-02-07 18:18:29 +0100
commitebb549a44bce98a13e9de1569bdcfad52d8ca3db (patch)
tree22183068915cb9cba7a15862a800e8d58f4d859f /plugins
parent45fc5038ad1a362e7006dde7dfcdf624de59022f (diff)
downloadsonarqube-ebb549a44bce98a13e9de1569bdcfad52d8ca3db.tar.gz
sonarqube-ebb549a44bce98a13e9de1569bdcfad52d8ca3db.zip
SONAR-2747 Fix bug with NPE when first analysis
Diffstat (limited to 'plugins')
-rw-r--r--plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/NewViolationsDecorator.java3
-rw-r--r--plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/timemachine/NewViolationsDecoratorTest.java15
2 files changed, 17 insertions, 1 deletions
diff --git a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/NewViolationsDecorator.java b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/NewViolationsDecorator.java
index 23b731b25c2..b539fdca79f 100644
--- a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/NewViolationsDecorator.java
+++ b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/NewViolationsDecorator.java
@@ -213,7 +213,8 @@ public class NewViolationsDecorator implements Decorator {
if (lastAnalysisPeriodIndex != null) {
PastSnapshot pastSnapshot = timeMachineConfiguration.getProjectPastSnapshots().get(lastAnalysisPeriodIndex - 1);
Double newViolationsCount = context.getMeasure(CoreMetrics.NEW_VIOLATIONS).getVariation(lastAnalysisPeriodIndex);
- if (newViolationsCount != null && newViolationsCount > 0) {
+ // Do not send notification if this is the first analysis or if there's no violation
+ if (pastSnapshot.getTargetDate() != null && newViolationsCount != null && newViolationsCount > 0) {
// Maybe we should check if this is the first analysis or not?
DateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd");
Notification notification = new Notification("new-violations")
diff --git a/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/timemachine/NewViolationsDecoratorTest.java b/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/timemachine/NewViolationsDecoratorTest.java
index bb60b3cf32a..3e77564a3c6 100644
--- a/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/timemachine/NewViolationsDecoratorTest.java
+++ b/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/timemachine/NewViolationsDecoratorTest.java
@@ -216,6 +216,21 @@ public class NewViolationsDecoratorTest {
}
@Test
+ public void shouldNotNotifyUserIfFirstAnalysis() throws Exception {
+ Project project = new Project("key").setName("LongName");
+ project.setId(45);
+ when(timeMachineConfiguration.getLastAnalysisPeriodIndex()).thenReturn(1);
+ // PastSnaptho with targetDate==null means first analysis
+ PastSnapshot pastSnapshot = new PastSnapshot("", null);
+ when(timeMachineConfiguration.getProjectPastSnapshots()).thenReturn(Lists.newArrayList(pastSnapshot));
+ Measure m = new Measure(CoreMetrics.NEW_VIOLATIONS).setVariation1(0.0);
+ when(context.getMeasure(CoreMetrics.NEW_VIOLATIONS)).thenReturn(m);
+
+ decorator.decorate(project, context);
+ verify(notificationManager, never()).scheduleForSending(any(Notification.class));
+ }
+
+ @Test
public void shouldNotifyUserAboutNewViolations() throws Exception {
Project project = new Project("key").setName("LongName");
project.setId(45);