diff options
author | Fabrice Bellingard <bellingard@gmail.com> | 2012-02-08 08:33:55 +0100 |
---|---|---|
committer | Fabrice Bellingard <bellingard@gmail.com> | 2012-02-08 08:33:55 +0100 |
commit | 8792d69a92205563c9f968ea63c12a363a3d03d6 (patch) | |
tree | 55ab518252ce22be8e930c31c0d73a0cc1216b59 | |
parent | 2b1cc324fd5e31c529baac594797dee545f7fae0 (diff) | |
download | sonarqube-8792d69a92205563c9f968ea63c12a363a3d03d6.tar.gz sonarqube-8792d69a92205563c9f968ea63c12a363a3d03d6.zip |
SONAR-2747 Fix bug if not enough PastSnapshots
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<PastSnapshot> 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 @@ -199,6 +199,16 @@ public class NewViolationsDecoratorTest { } @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"); when(timeMachineConfiguration.getLastAnalysisPeriodIndex()).thenReturn(1); @@ -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); |