And not necessarily 'since last analysis' period.
}
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?
.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);
import java.text.DateFormat;
import java.text.SimpleDateFormat;
+import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
private NewViolationsDecorator decorator;
private DecoratorContext context;
- private Resource resource;
+ private Resource<?> resource;
private NotificationManager notificationManager;
private Date rightNow;
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));
@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);
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));
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);
.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));
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");
}
}
* 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
.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");
"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"));
}
}
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;
- }
}
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;
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));
- }
-
}