]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-2747 Improve email rendering
authorFabrice Bellingard <bellingard@gmail.com>
Tue, 7 Feb 2012 15:32:00 +0000 (16:32 +0100)
committerFabrice Bellingard <bellingard@gmail.com>
Tue, 7 Feb 2012 15:34:35 +0000 (16:34 +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
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 [new file with mode: 0644]
plugins/sonar-email-notifications-plugin/src/test/java/org/sonar/plugins/emailnotifications/newviolations/NewViolationsTemplateTest.java [deleted file]

index 96c4e6aa5890efceb146d67d5da0b22d4aa1b5b6..23b731b25c24c467a591689738de3e92773486ed 100644 (file)
@@ -19,6 +19,8 @@
  */
 package org.sonar.plugins.core.timemachine;
 
+import java.text.DateFormat;
+import java.text.SimpleDateFormat;
 import java.util.Arrays;
 import java.util.Collection;
 import java.util.Date;
@@ -209,15 +211,19 @@ 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);
       Double newViolationsCount = context.getMeasure(CoreMetrics.NEW_VIOLATIONS).getVariation(lastAnalysisPeriodIndex);
       if (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")
             .setFieldValue("count", String.valueOf(newViolationsCount.intValue()))
             .setFieldValue("projectName", project.getLongName())
             .setFieldValue("projectKey", project.getKey())
             .setFieldValue("projectId", String.valueOf(project.getId()))
-            .setFieldValue("period", lastAnalysisPeriodIndex.toString());
+            .setFieldValue("period", lastAnalysisPeriodIndex.toString())
+            .setFieldValue("fromDate", dateformat.format(pastSnapshot.getTargetDate()))
+            .setFieldValue("toDate", dateformat.format(new Date()));
         notificationManager.scheduleForSending(notification);
       }
     }
index d38f59e30ebea3d600dcfcfed6a2f2ac248c02a9..bb60b3cf32aa1f1d18fec0e626b11c36e8337f6e 100644 (file)
@@ -30,8 +30,12 @@ import static org.mockito.Mockito.times;
 import static org.mockito.Mockito.verify;
 import static org.mockito.Mockito.when;
 
+import java.text.DateFormat;
+import java.text.SimpleDateFormat;
 import java.util.Arrays;
+import java.util.Calendar;
 import java.util.Date;
+import java.util.GregorianCalendar;
 import java.util.List;
 
 import org.apache.commons.lang.ObjectUtils;
@@ -216,17 +220,23 @@ public class NewViolationsDecoratorTest {
     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);
     when(context.getMeasure(CoreMetrics.NEW_VIOLATIONS)).thenReturn(m);
 
     decorator.decorate(project, context);
 
+    DateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd");
     Notification notification = new Notification("new-violations")
         .setFieldValue("count", "32")
         .setFieldValue("projectName", "LongName")
         .setFieldValue("projectKey", "key")
         .setFieldValue("projectId", "45")
-        .setFieldValue("period", "2");
+        .setFieldValue("period", "2")
+        .setFieldValue("fromDate", dateformat.format(pastDate.getTime()))
+        .setFieldValue("toDate", dateformat.format(new Date()));
     verify(notificationManager, times(1)).scheduleForSending(eq(notification));
   }
 
index d0a874f943f550693f3b016f5018c437809a89d1..b719c4e336d10368eea53f208ccf24bfd1c0308e 100644 (file)
@@ -45,8 +45,13 @@ public class NewViolationsEmailTemplate extends EmailTemplate {
     StringBuilder sb = new StringBuilder();
 
     String projectName = notification.getFieldValue("projectName");
-    appendLine(sb, "Project", projectName);
-    appendLine(sb, "New violations on last analysis", notification.getFieldValue("count"));
+    String violationsCount = notification.getFieldValue("count");
+    String fromDate = notification.getFieldValue("fromDate");
+    String toDate = notification.getFieldValue("toDate");
+
+    sb.append("Project: ").append(projectName).append('\n');
+    sb.append(violationsCount).append(" new violations on last analysis");
+    sb.append(" (introduced between ").append(fromDate).append(" and ").append(toDate).append(")").append('\n');
     appendFooter(sb, notification);
 
     EmailMessage message = new EmailMessage()
@@ -64,7 +69,7 @@ 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--\n")
+    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');
   }
diff --git a/plugins/sonar-email-notifications-plugin/src/test/java/org/sonar/plugins/emailnotifications/newviolations/NewViolationsEmailTemplateTest.java b/plugins/sonar-email-notifications-plugin/src/test/java/org/sonar/plugins/emailnotifications/newviolations/NewViolationsEmailTemplateTest.java
new file mode 100644 (file)
index 0000000..c5c4671
--- /dev/null
@@ -0,0 +1,84 @@
+/*
+ * Sonar, open source software quality management tool.
+ * Copyright (C) 2008-2012 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * Sonar is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * Sonar is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with Sonar; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
+ */
+package org.sonar.plugins.emailnotifications.newviolations;
+
+import static org.hamcrest.Matchers.is;
+import static org.hamcrest.Matchers.nullValue;
+import static org.junit.Assert.assertThat;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.sonar.api.notifications.Notification;
+import org.sonar.plugins.emailnotifications.EmailConfiguration;
+import org.sonar.plugins.emailnotifications.api.EmailMessage;
+
+public class NewViolationsEmailTemplateTest {
+
+  private NewViolationsEmailTemplate template;
+
+  @Before
+  public void setUp() {
+    EmailConfiguration configuration = mock(EmailConfiguration.class);
+    when(configuration.getServerBaseURL()).thenReturn("http://nemo.sonarsource.org");
+    template = new NewViolationsEmailTemplate(configuration);
+  }
+
+  @Test
+  public void shouldNotFormatIfNotCorrectNotification() {
+    Notification notification = new Notification("other-notif");
+    EmailMessage message = template.format(notification);
+    assertThat(message, nullValue());
+  }
+
+  /**
+   * <pre>
+   * Subject: New violations for project Foo
+   * From: Sonar
+   * 
+   * 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
+   * </pre>
+   */
+  @Test
+  public void shouldFormatCommentAdded() {
+    Notification notification = new Notification("new-violations")
+        .setFieldValue("count", "32")
+        .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");
+
+    EmailMessage message = template.format(notification);
+    assertThat(message.getMessageId(), is("new-violations/45"));
+    assertThat(message.getSubject(), is("New violations for project Foo"));
+    assertThat(message.getMessage(), is("" +
+      "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"));
+  }
+
+}
diff --git a/plugins/sonar-email-notifications-plugin/src/test/java/org/sonar/plugins/emailnotifications/newviolations/NewViolationsTemplateTest.java b/plugins/sonar-email-notifications-plugin/src/test/java/org/sonar/plugins/emailnotifications/newviolations/NewViolationsTemplateTest.java
deleted file mode 100644 (file)
index 3e46276..0000000
+++ /dev/null
@@ -1,84 +0,0 @@
-/*
- * Sonar, open source software quality management tool.
- * Copyright (C) 2008-2012 SonarSource
- * mailto:contact AT sonarsource DOT com
- *
- * Sonar is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * Sonar is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with Sonar; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
- */
-package org.sonar.plugins.emailnotifications.newviolations;
-
-import static org.hamcrest.Matchers.is;
-import static org.hamcrest.Matchers.nullValue;
-import static org.junit.Assert.assertThat;
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.when;
-
-import org.junit.Before;
-import org.junit.Test;
-import org.sonar.api.notifications.Notification;
-import org.sonar.plugins.emailnotifications.EmailConfiguration;
-import org.sonar.plugins.emailnotifications.api.EmailMessage;
-
-public class NewViolationsTemplateTest {
-
-  private NewViolationsEmailTemplate template;
-
-  @Before
-  public void setUp() {
-    EmailConfiguration configuration = mock(EmailConfiguration.class);
-    when(configuration.getServerBaseURL()).thenReturn("http://nemo.sonarsource.org");
-    template = new NewViolationsEmailTemplate(configuration);
-  }
-
-  @Test
-  public void shouldNotFormatIfNotCorrectNotification() {
-    Notification notification = new Notification("other-notif");
-    EmailMessage message = template.format(notification);
-    assertThat(message, nullValue());
-  }
-
-  /**
-   * <pre>
-   * Subject: New violations for project Foo
-   * From: Sonar
-   * 
-   * Project: Foo
-   * New violations on last analysis: 32
-   * 
-   * --
-   * See it in Sonar: http://nemo.sonarsource.org/drilldown/measures/org.sonar.foo:foo?metric=new_violations&period=2
-   * </pre>
-   */
-  @Test
-  public void shouldFormatCommentAdded() {
-    Notification notification = new Notification("new-violations")
-        .setFieldValue("count", "32")
-        .setFieldValue("projectName", "Foo")
-        .setFieldValue("projectKey", "org.sonar.foo:foo")
-        .setFieldValue("projectId", "45")
-        .setFieldValue("period", "2");
-
-    EmailMessage message = template.format(notification);
-    assertThat(message.getMessageId(), is("new-violations/45"));
-    assertThat(message.getSubject(), is("New violations for project Foo"));
-    assertThat(message.getMessage(), is("" +
-      "Project: Foo\n" +
-      "New violations on last analysis: 32\n" +
-      "\n" +
-      "--\n" +
-      "See it in Sonar: http://nemo.sonarsource.org/drilldown/measures/org.sonar.foo:foo?metric=new_violations&period=2\n"));
-  }
-
-}