From: Fabrice Bellingard Date: Wed, 8 Feb 2012 07:33:55 +0000 (+0100) Subject: SONAR-2747 Fix bug if not enough PastSnapshots X-Git-Tag: 2.14~141 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=8792d69a92205563c9f968ea63c12a363a3d03d6;p=sonarqube.git SONAR-2747 Fix bug if not enough PastSnapshots --- 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 b539fdca79f..06a8bf377df 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 @@ -210,8 +210,9 @@ public class NewViolationsDecorator implements Decorator { protected void notifyNewViolations(Project project, DecoratorContext context) { Integer lastAnalysisPeriodIndex = timeMachineConfiguration.getLastAnalysisPeriodIndex(); - if (lastAnalysisPeriodIndex != null) { - PastSnapshot pastSnapshot = timeMachineConfiguration.getProjectPastSnapshots().get(lastAnalysisPeriodIndex - 1); + List projectPastSnapshots = timeMachineConfiguration.getProjectPastSnapshots(); + if (lastAnalysisPeriodIndex != null && projectPastSnapshots.size() >= lastAnalysisPeriodIndex) { + PastSnapshot pastSnapshot = projectPastSnapshots.get(lastAnalysisPeriodIndex - 1); Double newViolationsCount = context.getMeasure(CoreMetrics.NEW_VIOLATIONS).getVariation(lastAnalysisPeriodIndex); // Do not send notification if this is the first analysis or if there's no violation if (pastSnapshot.getTargetDate() != null && newViolationsCount != null && newViolationsCount > 0) { 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 3e77564a3c6..e8695960212 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 @@ -198,6 +198,16 @@ public class NewViolationsDecoratorTest { verify(notificationManager, never()).scheduleForSending(any(Notification.class)); } + @Test + public void shouldNotNotifyIfNoNotEnoughPastSnapshots() throws Exception { + Project project = new Project("key"); + // the #setUp method adds 2 snapshots: if last period analysis is 3, then it's not enough + when(timeMachineConfiguration.getLastAnalysisPeriodIndex()).thenReturn(3); + + decorator.notifyNewViolations(project, context); + verify(notificationManager, never()).scheduleForSending(any(Notification.class)); + } + @Test public void shouldNotNotifyIfNoNewViolations() throws Exception { Project project = new Project("key"); @@ -220,7 +230,7 @@ public class NewViolationsDecoratorTest { Project project = new Project("key").setName("LongName"); project.setId(45); when(timeMachineConfiguration.getLastAnalysisPeriodIndex()).thenReturn(1); - // PastSnaptho with targetDate==null means first analysis + // PastSnapshot 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);