]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-2747 Verification is now done on period1
authorFabrice Bellingard <bellingard@gmail.com>
Wed, 8 Feb 2012 09:15:56 +0000 (10:15 +0100)
committerFabrice Bellingard <bellingard@gmail.com>
Wed, 8 Feb 2012 09:15:56 +0000 (10:15 +0100)
And not necessarily 'since last analysis' period.

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
plugins/sonar-email-notifications-plugin/src/main/java/org/sonar/plugins/emailnotifications/newviolations/NewViolationsEmailTemplate.java
plugins/sonar-email-notifications-plugin/src/test/java/org/sonar/plugins/emailnotifications/newviolations/NewViolationsEmailTemplateTest.java
sonar-batch/src/main/java/org/sonar/batch/components/TimeMachineConfiguration.java
sonar-batch/src/test/java/org/sonar/batch/components/TimeMachineConfigurationTest.java

index 06a8bf377df936cb2e004ca2499ca1721a28bfab..d714cada4fac42371343be1b302c3303fa8ce425 100644 (file)
@@ -209,11 +209,11 @@ public class NewViolationsDecorator implements Decorator {
   }
 
   protected void notifyNewViolations(Project project, DecoratorContext context) {
-    Integer lastAnalysisPeriodIndex = timeMachineConfiguration.getLastAnalysisPeriodIndex();
     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);
+    if (projectPastSnapshots.size() >= 1) {
+      // we always check new violations against period1
+      PastSnapshot pastSnapshot = projectPastSnapshots.get(0);
+      Double newViolationsCount = context.getMeasure(CoreMetrics.NEW_VIOLATIONS).getVariation1();
       // 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?
@@ -223,7 +223,6 @@ public class NewViolationsDecorator implements Decorator {
             .setFieldValue("projectName", project.getLongName())
             .setFieldValue("projectKey", project.getKey())
             .setFieldValue("projectId", String.valueOf(project.getId()))
-            .setFieldValue("period", lastAnalysisPeriodIndex.toString())
             .setFieldValue("fromDate", dateformat.format(pastSnapshot.getTargetDate()))
             .setFieldValue("toDate", dateformat.format(new Date()));
         notificationManager.scheduleForSending(notification);
index e869596021240d12d35f042eab7006bb86ce8929..a5ee668fa1c812d61ed60e0be31f4e83dae56928 100644 (file)
@@ -32,6 +32,7 @@ import static org.mockito.Mockito.when;
 
 import java.text.DateFormat;
 import java.text.SimpleDateFormat;
+import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Calendar;
 import java.util.Date;
@@ -70,7 +71,7 @@ public class NewViolationsDecoratorTest {
 
   private NewViolationsDecorator decorator;
   private DecoratorContext context;
-  private Resource resource;
+  private Resource<?> resource;
   private NotificationManager notificationManager;
 
   private Date rightNow;
@@ -188,21 +189,11 @@ public class NewViolationsDecoratorTest {
     verify(notificationManager, never()).scheduleForSending(any(Notification.class));
   }
 
-  @Test
-  public void shouldNotNotifyIfNoPeriodForLastAnalysis() throws Exception {
-    Project project = new Project("key");
-    when(timeMachineConfiguration.getLastAnalysisPeriodIndex()).thenReturn(null);
-
-    decorator.notifyNewViolations(project, context);
-
-    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);
+    when(timeMachineConfiguration.getProjectPastSnapshots()).thenReturn(new ArrayList<PastSnapshot>());
 
     decorator.notifyNewViolations(project, context);
     verify(notificationManager, never()).scheduleForSending(any(Notification.class));
@@ -211,7 +202,6 @@ public class NewViolationsDecoratorTest {
   @Test
   public void shouldNotNotifyIfNoNewViolations() throws Exception {
     Project project = new Project("key");
-    when(timeMachineConfiguration.getLastAnalysisPeriodIndex()).thenReturn(1);
     Measure m = new Measure(CoreMetrics.NEW_VIOLATIONS);
     when(context.getMeasure(CoreMetrics.NEW_VIOLATIONS)).thenReturn(m);
 
@@ -229,7 +219,6 @@ public class NewViolationsDecoratorTest {
   public void shouldNotNotifyUserIfFirstAnalysis() throws Exception {
     Project project = new Project("key").setName("LongName");
     project.setId(45);
-    when(timeMachineConfiguration.getLastAnalysisPeriodIndex()).thenReturn(1);
     // PastSnapshot with targetDate==null means first analysis
     PastSnapshot pastSnapshot = new PastSnapshot("", null);
     when(timeMachineConfiguration.getProjectPastSnapshots()).thenReturn(Lists.newArrayList(pastSnapshot));
@@ -244,11 +233,10 @@ public class NewViolationsDecoratorTest {
   public void shouldNotifyUserAboutNewViolations() throws Exception {
     Project project = new Project("key").setName("LongName");
     project.setId(45);
-    when(timeMachineConfiguration.getLastAnalysisPeriodIndex()).thenReturn(2);
     Calendar pastDate = new GregorianCalendar(2011, 10, 25);
     PastSnapshot pastSnapshot = new PastSnapshot("", pastDate.getTime());
     when(timeMachineConfiguration.getProjectPastSnapshots()).thenReturn(Lists.newArrayList(pastSnapshot, pastSnapshot));
-    Measure m = new Measure(CoreMetrics.NEW_VIOLATIONS).setVariation2(32.0);
+    Measure m = new Measure(CoreMetrics.NEW_VIOLATIONS).setVariation1(32.0);
     when(context.getMeasure(CoreMetrics.NEW_VIOLATIONS)).thenReturn(m);
 
     decorator.decorate(project, context);
@@ -259,7 +247,6 @@ public class NewViolationsDecoratorTest {
         .setFieldValue("projectName", "LongName")
         .setFieldValue("projectKey", "key")
         .setFieldValue("projectId", "45")
-        .setFieldValue("period", "2")
         .setFieldValue("fromDate", dateformat.format(pastDate.getTime()))
         .setFieldValue("toDate", dateformat.format(new Date()));
     verify(notificationManager, times(1)).scheduleForSending(eq(notification));
index 20d590658b9910f6b7305a7fc38c6717284c9108..bfd105102d90475213787d7e9e2d8d6e07837cf6 100644 (file)
@@ -64,10 +64,9 @@ public class NewViolationsEmailTemplate extends EmailTemplate {
 
   private void appendFooter(StringBuilder sb, Notification notification) {
     String projectKey = notification.getFieldValue("projectKey");
-    String period = notification.getFieldValue("period");
     sb.append("\n")
         .append("See it in Sonar: ").append(configuration.getServerBaseURL()).append("/drilldown/measures/").append(projectKey)
-        .append("?metric=new_violations&period=").append(period).append('\n');
+        .append("?metric=new_violations&period=1\n");
   }
 
 }
index c5c46712a946d2f11d1156b0b39e7a189a18529a..4fc6c2209456ac33a0f7a6d62004a3dca801b475 100644 (file)
@@ -57,7 +57,7 @@ public class NewViolationsEmailTemplateTest {
    * Project: Foo
    * 32 new violations on last analysis (introduced between 2012-01-02 and 2012-01-15)
    * 
-   * See it in Sonar: http://nemo.sonarsource.org/drilldown/measures/org.sonar.foo:foo?metric=new_violations&period=2
+   * See it in Sonar: http://nemo.sonarsource.org/drilldown/measures/org.sonar.foo:foo?metric=new_violations&period=1
    * </pre>
    */
   @Test
@@ -67,7 +67,6 @@ public class NewViolationsEmailTemplateTest {
         .setFieldValue("projectName", "Foo")
         .setFieldValue("projectKey", "org.sonar.foo:foo")
         .setFieldValue("projectId", "45")
-        .setFieldValue("period", "2")
         .setFieldValue("fromDate", "2012-01-02")
         .setFieldValue("toDate", "2012-01-15");
 
@@ -78,7 +77,7 @@ public class NewViolationsEmailTemplateTest {
       "Project: Foo\n" +
       "32 new violations on last analysis (introduced between 2012-01-02 and 2012-01-15)\n" +
       "\n" +
-      "See it in Sonar: http://nemo.sonarsource.org/drilldown/measures/org.sonar.foo:foo?metric=new_violations&period=2\n"));
+      "See it in Sonar: http://nemo.sonarsource.org/drilldown/measures/org.sonar.foo:foo?metric=new_violations&period=1\n"));
   }
 
 }
index 59a07f5eaa8ed440c0185bb3883ad44dd8210a08..b5e567ce18de43877e83b35c2b6f7fce3bdbf932 100644 (file)
@@ -112,26 +112,4 @@ public class TimeMachineConfiguration implements BatchExtension {
   public boolean isFileVariationEnabled() {
     return configuration.getBoolean("sonar.enableFileVariation", Boolean.FALSE);
   }
-
-  /**
-   * Returns the index corresponding to the 'previous_analysis' period (which is '1' by default).
-   * 
-   * @return the index of 'previous_analysis' period, or NULL is users have modified the periods and haven't set a 'previous_analysis' one.
-   */
-  public Integer getLastAnalysisPeriodIndex() {
-    // period1 is the default for 'previous_analysis'
-    String period1 = configuration.getString(CoreProperties.TIMEMACHINE_PERIOD_PREFIX + "1");
-    if (StringUtils.isBlank(period1) || CoreProperties.TIMEMACHINE_MODE_PREVIOUS_ANALYSIS.equals(period1)) {
-      return 1;
-    }
-    // else search for the other periods
-    for (int index = 2; index < 6; index++) {
-      if (CoreProperties.TIMEMACHINE_MODE_PREVIOUS_ANALYSIS.equals(configuration
-          .getString(CoreProperties.TIMEMACHINE_PERIOD_PREFIX + index))) {
-        return index;
-      }
-    }
-    // if we're here, this means that we have not found the 'previous_analysis' mode
-    return null;
-  }
 }
index 7d4be9f7802eac4ab5bcc1838536e50ec57a09c0..68468e09e7ef9801c09c599eb34889d0fd3ea3b1 100644 (file)
@@ -21,7 +21,6 @@ package org.sonar.batch.components;
 
 import static org.hamcrest.CoreMatchers.is;
 import static org.hamcrest.MatcherAssert.assertThat;
-import static org.hamcrest.Matchers.nullValue;
 import static org.mockito.Matchers.argThat;
 import static org.mockito.Matchers.eq;
 import static org.mockito.Mockito.mock;
@@ -85,23 +84,4 @@ public class TimeMachineConfigurationTest extends AbstractDbUnitTestCase {
     verifyZeroInteractions(pastSnapshotFinder);
   }
 
-  @Test
-  public void shouldReturnLastAnalysisIndexIfSet() {
-    PropertiesConfiguration conf = new PropertiesConfiguration();
-    TimeMachineConfiguration timeMachineConfiguration = new TimeMachineConfiguration(getSession(), new Project("my:project"), conf,
-        mock(PastSnapshotFinder.class));
-
-    // Nothing set, so period for 'previous_analysis' is 1 by default
-    assertThat(timeMachineConfiguration.getLastAnalysisPeriodIndex(), is(1));
-
-    // period1 has been replaced and 'previous_analysis' not set elsewhere...
-    conf.setProperty(CoreProperties.TIMEMACHINE_PERIOD_PREFIX + 1, "Version 1");
-    conf.setProperty(CoreProperties.TIMEMACHINE_PERIOD_PREFIX + 2, "Version 2");
-    assertThat(timeMachineConfiguration.getLastAnalysisPeriodIndex(), is(nullValue()));
-
-    // 'previous_analysis' has now been set on period 4
-    conf.setProperty(CoreProperties.TIMEMACHINE_PERIOD_PREFIX + 4, CoreProperties.TIMEMACHINE_MODE_PREVIOUS_ANALYSIS);
-    assertThat(timeMachineConfiguration.getLastAnalysisPeriodIndex(), is(4));
-  }
-
 }