From: Fabrice Bellingard Date: Tue, 7 Feb 2012 17:17:11 +0000 (+0100) Subject: SONAR-2747 Fix bug with NPE when first analysis X-Git-Tag: 2.14~147 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=ebb549a44bce98a13e9de1569bdcfad52d8ca3db;p=sonarqube.git SONAR-2747 Fix bug with NPE when first analysis --- 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 @@ -215,6 +215,21 @@ public class NewViolationsDecoratorTest { verify(notificationManager, never()).scheduleForSending(any(Notification.class)); } + @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");