import org.sonar.api.SonarPlugin;
import org.sonar.api.checks.NoSonarFilter;
import org.sonar.api.resources.Java;
+import org.sonar.core.timemachine.Periods;
import org.sonar.plugins.core.batch.ExcludedResourceFilter;
import org.sonar.plugins.core.batch.IndexProjectPostJob;
import org.sonar.plugins.core.batch.MavenInitializer;
import org.sonar.plugins.core.timemachine.NewItCoverageFileAnalyzer;
import org.sonar.plugins.core.timemachine.NewOverallCoverageFileAnalyzer;
import org.sonar.plugins.core.timemachine.NewViolationsDecorator;
-import org.sonar.plugins.core.timemachine.Periods;
import org.sonar.plugins.core.timemachine.ReferenceAnalysis;
import org.sonar.plugins.core.timemachine.TendencyDecorator;
import org.sonar.plugins.core.timemachine.TimeMachineConfigurationPersister;
import org.sonar.plugins.core.widgets.CustomMeasuresWidget;
import org.sonar.plugins.core.widgets.DescriptionWidget;
import org.sonar.plugins.core.widgets.EventsWidget;
-import org.sonar.plugins.core.widgets.MeasureFilterListWidget;
import org.sonar.plugins.core.widgets.HotspotMetricWidget;
import org.sonar.plugins.core.widgets.HotspotMostViolatedResourcesWidget;
import org.sonar.plugins.core.widgets.HotspotMostViolatedRulesWidget;
import org.sonar.plugins.core.widgets.ItCoverageWidget;
+import org.sonar.plugins.core.widgets.MeasureFilterListWidget;
import org.sonar.plugins.core.widgets.MeasureFilterTreemapWidget;
import org.sonar.plugins.core.widgets.RulesWidget;
import org.sonar.plugins.core.widgets.SizeWidget;
import org.sonar.api.batch.DecoratorContext;
import org.sonar.api.batch.DependedUpon;
import org.sonar.api.batch.DependsUpon;
+import org.sonar.api.database.model.Snapshot;
import org.sonar.api.i18n.I18n;
import org.sonar.api.measures.CoreMetrics;
import org.sonar.api.measures.Measure;
import org.sonar.api.resources.Project;
import org.sonar.api.resources.Resource;
import org.sonar.api.resources.ResourceUtils;
-import org.sonar.plugins.core.timemachine.Periods;
+import org.sonar.core.timemachine.Periods;
import java.util.List;
import java.util.Locale;
public class CheckAlertThresholds implements Decorator {
+ private final Snapshot snapshot;
private final RulesProfile profile;
private final Periods periods;
private final I18n i18n;
- public CheckAlertThresholds(RulesProfile profile, Periods periods, I18n i18n) {
+ public CheckAlertThresholds(Snapshot snapshot, RulesProfile profile, Periods periods, I18n i18n) {
+ this.snapshot = snapshot;
this.profile = profile;
this.periods = periods;
this.i18n = i18n;
.append(level.equals(Metric.Level.ERROR) ? alert.getValueError() : alert.getValueWarning());
if (alertPeriod != null){
- stringBuilder.append(" ").append(periods.getLabel(alertPeriod));
+ stringBuilder.append(" ").append(periods.label(snapshot, alertPeriod));
}
return stringBuilder.toString();
+++ /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.core.timemachine;
-
-import org.sonar.api.BatchExtension;
-import org.sonar.api.CoreProperties;
-import org.sonar.api.database.model.Snapshot;
-import org.sonar.api.i18n.I18n;
-
-import java.text.SimpleDateFormat;
-import java.util.Date;
-import java.util.Locale;
-
-public class Periods implements BatchExtension {
-
- private final Snapshot snapshot;
- private final I18n i18n;
-
- public Periods(Snapshot snapshot, I18n i18n) {
- this.snapshot = snapshot;
- this.i18n = i18n;
- }
-
- public String getLabel(int periodIndex) {
- String mode = snapshot.getPeriodMode(periodIndex);
- String param = snapshot.getPeriodModeParameter(periodIndex);
- Date date = snapshot.getPeriodDate(periodIndex);
-
- String label = "";
- if (mode.equals(CoreProperties.TIMEMACHINE_MODE_DAYS)) {
- label = message("over_x_days", param);
- } else if (mode.equals(CoreProperties.TIMEMACHINE_MODE_VERSION)) {
- label = message("since_version", param);
- if (date != null) {
- label = message("since_version_detailed", param, convertDate(date));
- }
- } else if (mode.equals(CoreProperties.TIMEMACHINE_MODE_PREVIOUS_ANALYSIS)) {
- label = message("since_previous_analysis");
- if (date != null) {
- label = message("since_previous_analysis_detailed", convertDate(date));
- }
- } else if (mode.equals(CoreProperties.TIMEMACHINE_MODE_PREVIOUS_VERSION)) {
- label = message("since_previous_version");
- if (param != null) {
- label = message("since_previous_version_detailed", param);
- }
- } else if (mode.equals(CoreProperties.TIMEMACHINE_MODE_DATE)) {
- label = message("since_x", convertDate(date));
- } else {
- throw new IllegalStateException("This mode is not supported : " + mode);
- }
- return label;
- }
-
- private String message(String key, Object... parameters) {
- return i18n.message(getLocale(), key, null, parameters);
- }
-
- private String convertDate(Date date){
- SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy MMM dd");
- return dateFormat.format(date);
- }
-
- private Locale getLocale() {
- return Locale.ENGLISH;
- }
-
-}
deactivate_all=Deactivate all
default_severity=Default severity
default_sort_on=Default sort on
-delta_since_previous_analysis=Δ since previous analysis
-delta_since_previous_version=Δ since previous version
-delta_over_x_days=Δ over {0} days
-delta_since=Δ since {0}
-delta_since_version=Δ since version {0}
disable_treemap=Disable treemap
enable_treemap=Enable treemap
equals=Equals
import org.mockito.ArgumentMatcher;
import org.mockito.Mockito;
import org.sonar.api.batch.DecoratorContext;
+import org.sonar.api.database.model.Snapshot;
import org.sonar.api.i18n.I18n;
import org.sonar.api.measures.CoreMetrics;
import org.sonar.api.measures.Measure;
import org.sonar.api.resources.Qualifiers;
import org.sonar.api.resources.Resource;
import org.sonar.api.test.IsMeasure;
-import org.sonar.plugins.core.timemachine.Periods;
+import org.sonar.core.timemachine.Periods;
import java.util.ArrayList;
import java.util.Arrays;
private CheckAlertThresholds decorator;
private DecoratorContext context;
private RulesProfile profile;
+
private Measure measureClasses;
private Measure measureCoverage;
private Measure measureComplexity;
+
private Resource project;
+ private Snapshot snapshot;
private Periods periods;
private I18n i18n;
when(context.getMeasure(CoreMetrics.COVERAGE)).thenReturn(measureCoverage);
when(context.getMeasure(CoreMetrics.COMPLEXITY)).thenReturn(measureComplexity);
+ snapshot = mock(Snapshot.class);
profile = mock(RulesProfile.class);
- decorator = new CheckAlertThresholds(profile, periods, i18n);
+ decorator = new CheckAlertThresholds(snapshot, profile, periods, i18n);
project = mock(Resource.class);
when(project.getQualifier()).thenReturn(Qualifiers.PROJECT);
}
measureClasses.setVariation1(40d);
when(i18n.message(Mockito.any(Locale.class), Mockito.eq("metric.classes.name"), Mockito.isNull(String.class))).thenReturn("Classes");
- when(periods.getLabel(1)).thenReturn("since someday");
+ when(periods.label(snapshot, 1)).thenReturn("since someday");
when(profile.getAlerts()).thenReturn(Arrays.asList(
new Alert(null, CoreMetrics.CLASSES, Alert.OPERATOR_GREATER, null, "30", 1) // generates warning because classes increases of 40, which is greater than 30
+++ /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.core.timemachine;
-
-import org.junit.Before;
-import org.junit.Test;
-import org.mockito.Mockito;
-import org.sonar.api.CoreProperties;
-import org.sonar.api.database.model.Snapshot;
-import org.sonar.api.i18n.I18n;
-
-import java.util.Date;
-import java.util.Locale;
-
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.verify;
-import static org.mockito.Mockito.when;
-
-public class PeriodsTest {
-
- private Periods periods;
-
- private Snapshot snapshot;
- private I18n i18n;
-
- private int periodIndex;
- private String param;
-
- @Before
- public void before() {
- periodIndex = 1;
- param = "10";
-
- snapshot = mock(Snapshot.class);
- i18n = mock(I18n.class);
-
- when(snapshot.getPeriodModeParameter(periodIndex)).thenReturn(param);
-
- periods = new Periods(snapshot, i18n);
- }
-
- @Test
- public void shouldReturnLabelInModeDays() {
- when(snapshot.getPeriodMode(periodIndex)).thenReturn(CoreProperties.TIMEMACHINE_MODE_DAYS);
- when(snapshot.getPeriodDate(periodIndex)).thenReturn(new Date());
- when(snapshot.getPeriodModeParameter(periodIndex)).thenReturn(param);
-
- periods.getLabel(periodIndex);
- verify(i18n).message(Mockito.any(Locale.class), Mockito.eq("over_x_days"), Mockito.isNull(String.class), Mockito.eq(param));
- }
-
- @Test
- public void shouldReturnLabelInModeVersion() {
- when(snapshot.getPeriodMode(periodIndex)).thenReturn(CoreProperties.TIMEMACHINE_MODE_VERSION);
- when(snapshot.getPeriodDate(periodIndex)).thenReturn(new Date());
- when(snapshot.getPeriodModeParameter(periodIndex)).thenReturn(param);
-
- periods.getLabel(periodIndex);
-
- verify(i18n).message(Mockito.any(Locale.class), Mockito.eq("since_version_detailed"), Mockito.isNull(String.class), Mockito.eq(param), Mockito.anyString());
- }
-
- @Test
- public void shouldReturnLabelInModePreviousAnalysisWithDateNotNull() {
- when(snapshot.getPeriodMode(periodIndex)).thenReturn(CoreProperties.TIMEMACHINE_MODE_VERSION);
- when(snapshot.getPeriodDate(periodIndex)).thenReturn(new Date());
- when(snapshot.getPeriodModeParameter(periodIndex)).thenReturn(param);
-
- periods.getLabel(periodIndex);
-
- verify(i18n).message(Mockito.any(Locale.class), Mockito.eq("since_version_detailed"), Mockito.isNull(String.class), Mockito.eq(param), Mockito.anyString());
- }
-
- @Test
- public void shouldReturnLabelInModePreviousAnalysisWithNullDate() {
- when(snapshot.getPeriodMode(periodIndex)).thenReturn(CoreProperties.TIMEMACHINE_MODE_VERSION);
- when(snapshot.getPeriodDate(periodIndex)).thenReturn(null);
- when(snapshot.getPeriodModeParameter(periodIndex)).thenReturn(param);
-
- periods.getLabel(periodIndex);
-
- verify(i18n).message(Mockito.any(Locale.class), Mockito.eq("since_version"), Mockito.isNull(String.class), Mockito.eq(param));
- }
-
- @Test
- public void shouldReturnLabelInModePreviousVersionWithParamNotNull() {
- when(snapshot.getPeriodMode(periodIndex)).thenReturn(CoreProperties.TIMEMACHINE_MODE_PREVIOUS_VERSION);
- when(snapshot.getPeriodModeParameter(periodIndex)).thenReturn(param);
-
- periods.getLabel(periodIndex);
-
- verify(i18n).message(Mockito.any(Locale.class), Mockito.eq("since_previous_version_detailed"), Mockito.isNull(String.class), Mockito.eq(param));
- }
-
- @Test
- public void shouldReturnLabelInModePreviousVersionWithParamNull() {
- when(snapshot.getPeriodMode(periodIndex)).thenReturn(CoreProperties.TIMEMACHINE_MODE_PREVIOUS_VERSION);
- when(snapshot.getPeriodModeParameter(periodIndex)).thenReturn(null);
-
- periods.getLabel(periodIndex);
-
- verify(i18n).message(Mockito.any(Locale.class), Mockito.eq("since_previous_version"), Mockito.isNull(String.class));
- }
-
- @Test
- public void shouldReturnLabelInModeDate() {
- when(snapshot.getPeriodMode(periodIndex)).thenReturn(CoreProperties.TIMEMACHINE_MODE_DATE);
- when(snapshot.getPeriodDate(periodIndex)).thenReturn(new Date());
-
- periods.getLabel(periodIndex);
-
- verify(i18n).message(Mockito.any(Locale.class), Mockito.eq("since_x"), Mockito.isNull(String.class), Mockito.anyString());
- }
-
- @Test(expected = IllegalStateException.class)
- public void shouldNotSupportUnknownMode() {
- when(snapshot.getPeriodMode(periodIndex)).thenReturn("Unknown mode");
-
- periods.getLabel(periodIndex);
- }
-}
--- /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.core.timemachine;
+
+import org.apache.commons.lang.StringUtils;
+import org.sonar.api.BatchExtension;
+import org.sonar.api.CoreProperties;
+import org.sonar.api.config.Settings;
+import org.sonar.api.database.model.Snapshot;
+import org.sonar.api.i18n.I18n;
+
+import java.text.ParseException;
+import java.text.SimpleDateFormat;
+import java.util.Date;
+import java.util.Locale;
+
+public class Periods implements BatchExtension {
+
+ private final Settings settings;
+ private final I18n i18n;
+
+ public Periods(Settings settings, I18n i18n) {
+ this.settings = settings;
+ this.i18n = i18n;
+ }
+
+ public String label(Snapshot snapshot, int periodIndex) {
+ String mode = snapshot.getPeriodMode(periodIndex);
+ String param = snapshot.getPeriodModeParameter(periodIndex);
+ Date date = snapshot.getPeriodDate(periodIndex);
+
+ return label(mode, param, date);
+ }
+
+ public String label(int periodIndex) {
+ String periodProperty = settings.getString(CoreProperties.TIMEMACHINE_PERIOD_PREFIX + periodIndex);
+ PeriodParameters periodParameters = new PeriodParameters(periodProperty);
+
+ String mode = periodParameters.getMode();
+ String param = periodParameters.getParam();
+ Date date = periodParameters.getDate();
+
+ return label(mode, param, date);
+ }
+
+ public String label(String mode, String param, Date date) {
+ String label = "";
+ if (CoreProperties.TIMEMACHINE_MODE_DAYS.equals(mode)) {
+ label = message("over_x_days", param);
+ } else if (CoreProperties.TIMEMACHINE_MODE_VERSION.equals(mode)) {
+ label = message("since_version", param);
+ if (date != null) {
+ label = message("since_version_detailed", param, convertDate(date));
+ }
+ } else if (CoreProperties.TIMEMACHINE_MODE_PREVIOUS_ANALYSIS.equals(mode)) {
+ label = message("since_previous_analysis");
+ if (date != null) {
+ label = message("since_previous_analysis_detailed", convertDate(date));
+ }
+ } else if (CoreProperties.TIMEMACHINE_MODE_PREVIOUS_VERSION.equals(mode)) {
+ label = message("since_previous_version");
+ if (param != null) {
+ label = message("since_previous_version_detailed", param);
+ }
+ } else if (CoreProperties.TIMEMACHINE_MODE_DATE.equals(mode)) {
+ label = message("since_x", convertDate(date));
+ } else {
+ throw new IllegalArgumentException("This mode is not supported : " + mode);
+ }
+ return label;
+ }
+
+ private String message(String key, Object... parameters) {
+ return i18n.message(getLocale(), key, null, parameters);
+ }
+
+ private String convertDate(Date date) {
+ SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy MMM dd");
+ return dateFormat.format(date);
+ }
+
+ private Locale getLocale() {
+ return Locale.ENGLISH;
+ }
+
+ private class PeriodParameters {
+
+ private String mode;
+ private String param;
+ private Date date;
+
+ public PeriodParameters(String periodProperty) {
+ if (CoreProperties.TIMEMACHINE_MODE_PREVIOUS_ANALYSIS.equals(periodProperty) || CoreProperties.TIMEMACHINE_MODE_PREVIOUS_VERSION.equals(periodProperty)) {
+ mode = periodProperty;
+ } else if (findByDays(periodProperty) != null) {
+ mode = CoreProperties.TIMEMACHINE_MODE_DAYS;
+ param = Integer.toString(findByDays(periodProperty));
+ } else if (findByDate(periodProperty) != null) {
+ mode = CoreProperties.TIMEMACHINE_MODE_DATE;
+ date = findByDate(periodProperty);
+ } else if (StringUtils.isNotBlank(periodProperty)) {
+ mode = CoreProperties.TIMEMACHINE_MODE_VERSION;
+ param = periodProperty;
+ } else {
+ throw new IllegalArgumentException("Unknown period property : " + periodProperty);
+ }
+ }
+
+ private Integer findByDays(String property) {
+ try {
+ return Integer.parseInt(property);
+ } catch (NumberFormatException e) {
+ return null;
+ }
+ }
+
+ private Date findByDate(String property) {
+ SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
+ try {
+ return format.parse(property);
+ } catch (ParseException e) {
+ return null;
+ }
+ }
+
+ public String getMode() {
+ return mode;
+ }
+
+ public String getParam() {
+ return param;
+ }
+
+ public Date getDate() {
+ return date;
+ }
+ }
+
+}
--- /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.core.timemachine;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.mockito.Mockito;
+import org.sonar.api.CoreProperties;
+import org.sonar.api.config.Settings;
+import org.sonar.api.database.model.Snapshot;
+import org.sonar.api.i18n.I18n;
+
+import java.util.Date;
+import java.util.Locale;
+
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+public class PeriodsTest {
+
+ private Periods periods;
+
+ private Snapshot snapshot;
+
+ private Settings settings;
+ private I18n i18n;
+
+ private int periodIndex;
+ private String param;
+
+ @Before
+ public void before() {
+ periodIndex = 1;
+ param = "10";
+
+ snapshot = mock(Snapshot.class);
+ when(snapshot.getPeriodModeParameter(periodIndex)).thenReturn(param);
+
+ settings = new Settings();
+ i18n = mock(I18n.class);
+ periods = new Periods(settings, i18n);
+ }
+
+ @Test
+ public void shouldReturnSnapshotLabelInModeDays() {
+ when(snapshot.getPeriodMode(periodIndex)).thenReturn(CoreProperties.TIMEMACHINE_MODE_DAYS);
+ when(snapshot.getPeriodDate(periodIndex)).thenReturn(new Date());
+ when(snapshot.getPeriodModeParameter(periodIndex)).thenReturn(param);
+
+ periods.label(snapshot, periodIndex);
+ verify(i18n).message(Mockito.any(Locale.class), Mockito.eq("over_x_days"), Mockito.isNull(String.class), Mockito.eq(param));
+ }
+
+ @Test
+ public void shouldReturnSnapshotLabelInModeVersion() {
+ when(snapshot.getPeriodMode(periodIndex)).thenReturn(CoreProperties.TIMEMACHINE_MODE_VERSION);
+ when(snapshot.getPeriodDate(periodIndex)).thenReturn(new Date());
+ when(snapshot.getPeriodModeParameter(periodIndex)).thenReturn(param);
+
+ periods.label(snapshot, periodIndex);
+ verify(i18n).message(Mockito.any(Locale.class), Mockito.eq("since_version_detailed"), Mockito.isNull(String.class), Mockito.eq(param), Mockito.anyString());
+ }
+
+ @Test
+ public void shouldReturnSnapshotLabelInModePreviousAnalysisWithDateNotNull() {
+ when(snapshot.getPeriodMode(periodIndex)).thenReturn(CoreProperties.TIMEMACHINE_MODE_PREVIOUS_ANALYSIS);
+ when(snapshot.getPeriodDate(periodIndex)).thenReturn(new Date());
+
+ periods.label(snapshot, periodIndex);
+ verify(i18n).message(Mockito.any(Locale.class), Mockito.eq("since_previous_analysis_detailed"), Mockito.isNull(String.class), Mockito.anyString());
+ }
+
+ @Test
+ public void shouldReturnSnapshotLabelInModePreviousAnalysisWithNullDate() {
+ when(snapshot.getPeriodMode(periodIndex)).thenReturn(CoreProperties.TIMEMACHINE_MODE_PREVIOUS_ANALYSIS);
+ when(snapshot.getPeriodDate(periodIndex)).thenReturn(null);
+ when(snapshot.getPeriodModeParameter(periodIndex)).thenReturn(param);
+
+ periods.label(snapshot, periodIndex);
+ verify(i18n).message(Mockito.any(Locale.class), Mockito.eq("since_previous_analysis"), Mockito.isNull(String.class));
+ }
+
+ @Test
+ public void shouldReturnSnapshotLabelInModePreviousVersionWithParamNotNull() {
+ when(snapshot.getPeriodMode(periodIndex)).thenReturn(CoreProperties.TIMEMACHINE_MODE_PREVIOUS_VERSION);
+ when(snapshot.getPeriodModeParameter(periodIndex)).thenReturn(param);
+
+ periods.label(snapshot, periodIndex);
+ verify(i18n).message(Mockito.any(Locale.class), Mockito.eq("since_previous_version_detailed"), Mockito.isNull(String.class), Mockito.eq(param));
+ }
+
+ @Test
+ public void shouldReturnSnapshotLabelInModePreviousVersionWithParamNull() {
+ when(snapshot.getPeriodMode(periodIndex)).thenReturn(CoreProperties.TIMEMACHINE_MODE_PREVIOUS_VERSION);
+ when(snapshot.getPeriodModeParameter(periodIndex)).thenReturn(null);
+
+ periods.label(snapshot, periodIndex);
+ verify(i18n).message(Mockito.any(Locale.class), Mockito.eq("since_previous_version"), Mockito.isNull(String.class));
+ }
+
+ @Test
+ public void shouldReturnSnapshotLabelInModeDate() {
+ when(snapshot.getPeriodMode(periodIndex)).thenReturn(CoreProperties.TIMEMACHINE_MODE_DATE);
+ when(snapshot.getPeriodDate(periodIndex)).thenReturn(new Date());
+
+ periods.label(snapshot, periodIndex);
+
+ verify(i18n).message(Mockito.any(Locale.class), Mockito.eq("since_x"), Mockito.isNull(String.class), Mockito.anyString());
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ public void shouldNotSupportUnknownModeForSnapshotLabel() {
+ when(snapshot.getPeriodMode(periodIndex)).thenReturn("Unknown mode");
+
+ periods.label(snapshot, periodIndex);
+ }
+
+ @Test
+ public void shouldReturnLabelInModeDays() {
+ int periodIndex = 1;
+ String days = "5";
+ settings.setProperty(CoreProperties.TIMEMACHINE_PERIOD_PREFIX + periodIndex, days);
+
+ periods.label(periodIndex);
+ verify(i18n).message(Mockito.any(Locale.class), Mockito.eq("over_x_days"), Mockito.isNull(String.class), Mockito.eq(days));
+ }
+
+ @Test
+ public void shouldReturnLabelInModeVersion() {
+ int periodIndex = 1;
+ String version = "3.5";
+ settings.setProperty(CoreProperties.TIMEMACHINE_PERIOD_PREFIX + periodIndex, version);
+
+ periods.label(periodIndex);
+ verify(i18n).message(Mockito.any(Locale.class), Mockito.eq("since_version"), Mockito.isNull(String.class), Mockito.eq(version));
+ }
+
+ @Test
+ public void shouldReturnLabelInModePreviousAnalysis() {
+ int periodIndex = 1;
+ settings.setProperty(CoreProperties.TIMEMACHINE_PERIOD_PREFIX + periodIndex, CoreProperties.TIMEMACHINE_MODE_PREVIOUS_ANALYSIS);
+
+ periods.label(periodIndex);
+ verify(i18n).message(Mockito.any(Locale.class), Mockito.eq("since_previous_analysis"), Mockito.isNull(String.class));
+ }
+
+ @Test
+ public void shouldReturnLabelInModePreviousVersion() {
+ int periodIndex = 1;
+ settings.setProperty(CoreProperties.TIMEMACHINE_PERIOD_PREFIX + periodIndex, CoreProperties.TIMEMACHINE_MODE_PREVIOUS_VERSION);
+
+ periods.label(periodIndex);
+ verify(i18n).message(Mockito.any(Locale.class), Mockito.eq("since_previous_version"), Mockito.isNull(String.class));
+ }
+
+ @Test
+ public void shouldReturnLabelInModeDate() {
+ int periodIndex = 1;
+ settings.setProperty(CoreProperties.TIMEMACHINE_PERIOD_PREFIX + periodIndex, "2012-12-12");
+
+ periods.label(periodIndex);
+
+ verify(i18n).message(Mockito.any(Locale.class), Mockito.eq("since_x"), Mockito.isNull(String.class), Mockito.anyString());
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ public void shouldNotSupportUnknownModeForLabel() {
+ int periodIndex = 1;
+ settings.setProperty(CoreProperties.TIMEMACHINE_PERIOD_PREFIX + periodIndex, "");
+
+ periods.label(periodIndex);
+ }
+
+
+}
import org.sonar.core.qualitymodel.DefaultModelFinder;
import org.sonar.core.resource.DefaultResourcePermissions;
import org.sonar.core.rule.DefaultRuleFinder;
+import org.sonar.core.timemachine.Periods;
import org.sonar.core.user.DefaultUserFinder;
import org.sonar.core.workflow.ReviewDatabaseStore;
import org.sonar.core.workflow.WorkflowEngine;
servicesContainer.addSingleton(MeasureFilterEngine.class);
servicesContainer.addSingleton(DryRunDatabaseFactory.class);
servicesContainer.addSingleton(DefaultResourcePermissions.class);
+ servicesContainer.addSingleton(Periods.class);
// Notifications
servicesContainer.addSingleton(EmailSettings.class);
import org.sonar.core.purge.PurgeDao;
import org.sonar.core.resource.ResourceIndexerDao;
import org.sonar.core.resource.ResourceKeyUpdaterDao;
+import org.sonar.core.timemachine.Periods;
import org.sonar.core.workflow.WorkflowEngine;
import org.sonar.markdown.Markdown;
import org.sonar.server.configuration.Backup;
import java.net.InetAddress;
import java.sql.Connection;
import java.util.Collection;
+import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;
public byte[] createDatabaseForDryRun() {
return get(DryRunDatabaseFactory.class).createDatabaseForDryRun();
}
+
+ public String getPeriodLabel(int periodIndex) {
+ return get(Periods.class).label(periodIndex);
+ }
+
+ public String getPeriodLabel(String mode, String param, Date date) {
+ return get(Periods.class).label(mode, param, date);
+ }
}
def period_select_options(alert, index)
if index
selected = (alert.period == index ? 'selected' : '')
- "<option value='#{index}' #{selected}>Δ Period #{index}</option>"
+ period_label = Api::Utils.java_facade.getPeriodLabel(index)
+ "<option value='#{index}' #{selected}>Δ #{period_label}</option>"
else
selected = (alert.period ? 'selected' : '')
"<option value='' #{selected}>#{message('absolute_value')}</option>"
def period_label(snapshot, period_index)
return nil if snapshot.nil? || snapshot.project_snapshot.nil?
- mode=snapshot.period_mode(period_index)
- mode_param=snapshot.period_param(period_index)
- date=snapshot.period_datetime(period_index)
-
- label=nil
- if mode
- if mode=='days'
- label = message('over_x_days', :params => mode_param.to_s)
- elsif mode=='version'
- if date
- label = message('since_version_detailed', :params => [mode_param.to_s, date.strftime("%Y %b %d").to_s])
- else
- label = message('since_version', :params => mode_param.to_s)
- end
- elsif mode=='previous_analysis'
- if !date.nil?
- label = message('since_previous_analysis_detailed', :params => date.strftime("%Y %b %d").to_s)
- else
- label = message('since_previous_analysis')
- end
- elsif mode=='previous_version'
- unless mode_param.nil?
- label = message('since_previous_version_detailed', :params => mode_param.to_s)
- else
- label = message('since_previous_version')
- end
- elsif mode=='date'
- label = message('since_x', :params => date.strftime("%Y %b %d").to_s)
- end
- end
- label
+ mode = snapshot.period_mode(period_index)
+ mode_param = snapshot.period_param(period_index)
+ date = snapshot.period_datetime(period_index)
+ Api::Utils.java_facade.getPeriodLabel(mode, mode_param, date) if mode
end
def configuration(key, default = nil)
end
def period_names
- p1=Property.value('sonar.timemachine.period1', nil, 'previous_analysis')
- p2=Property.value('sonar.timemachine.period2', nil, '5')
- p3=Property.value('sonar.timemachine.period3', nil, '30')
- [period_name(p1), period_name(p2), period_name(p3)]
+ period_name1 = Api::Utils.java_facade.getPeriodLabel(1)
+ period_name2 = Api::Utils.java_facade.getPeriodLabel(2)
+ period_name3 = Api::Utils.java_facade.getPeriodLabel(3)
+ [period_name1, period_name2, period_name3]
end
- def period_name(property)
- if property=='previous_analysis'
- message('delta_since_previous_analysis')
- elsif property=='previous_version'
- message('delta_since_previous_version')
- elsif property =~ /^[\d]+(\.[\d]+){0,1}$/
- # is integer
- message('delta_over_x_days', :params => property)
- elsif property =~ /\d{4}-\d{2}-\d{2}/
- message('delta_since', :params => property)
- elsif !property.blank?
- message('delta_since_version', :params => property)
- else
- nil
- end
- end
end