From 8792d69a92205563c9f968ea63c12a363a3d03d6 Mon Sep 17 00:00:00 2001 From: Fabrice Bellingard Date: Wed, 8 Feb 2012 08:33:55 +0100 Subject: [PATCH] SONAR-2747 Fix bug if not enough PastSnapshots --- .../core/timemachine/NewViolationsDecorator.java | 5 +++-- .../core/timemachine/NewViolationsDecoratorTest.java | 12 +++++++++++- 2 files changed, 14 insertions(+), 3 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 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); -- 2.39.5