]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-2747 Fix bug with NPE when first analysis
authorFabrice Bellingard <bellingard@gmail.com>
Tue, 7 Feb 2012 17:17:11 +0000 (18:17 +0100)
committerFabrice Bellingard <bellingard@gmail.com>
Tue, 7 Feb 2012 17:18:29 +0000 (18:18 +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 23b731b25c24c467a591689738de3e92773486ed..b539fdca79f9f6f0ebaf41a7f66b538082065bfc 100644 (file)
@@ -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")
index bb60b3cf32aa1f1d18fec0e626b11c36e8337f6e..3e77564a3c6127bae510cf0304574d3f00bce5ae 100644 (file)
@@ -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");