*/
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;
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);
}
}
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;
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));
}
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()
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');
}
--- /dev/null
+/*
+ * 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"));
+ }
+
+}
+++ /dev/null
-/*
- * 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"));
- }
-
-}