]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-2747 Fix bug if not enough PastSnapshots
authorFabrice Bellingard <bellingard@gmail.com>
Wed, 8 Feb 2012 07:33:55 +0000 (08:33 +0100)
committerFabrice Bellingard <bellingard@gmail.com>
Wed, 8 Feb 2012 07:33:55 +0000 (08:33 +0100)
plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/NewViolationsDecorator.java
plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/timemachine/NewViolationsDecoratorTest.java

index b539fdca79f9f6f0ebaf41a7f66b538082065bfc..06a8bf377df936cb2e004ca2499ca1721a28bfab 100644 (file)
@@ -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) {
index 3e77564a3c6127bae510cf0304574d3f00bce5ae..e869596021240d12d35f042eab7006bb86ce8929 100644 (file)
@@ -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);