From 8e264cd428bc6ebcc987340f0521858dd41e1056 Mon Sep 17 00:00:00 2001 From: Julien Lancelot Date: Wed, 5 Dec 2012 15:23:52 +0100 Subject: [PATCH] Periods is now used to display label periods for global settings and for snapshot periods --- .../org/sonar/plugins/core/CorePlugin.java | 4 +- .../core/sensors/CheckAlertThresholds.java | 9 +- .../plugins/core/timemachine/Periods.java | 85 ---------- .../resources/org/sonar/l10n/core.properties | 5 - .../sensors/CheckAlertThresholdsTest.java | 11 +- .../org/sonar/core/timemachine/Periods.java | 156 ++++++++++++++++++ .../sonar}/core/timemachine/PeriodsTest.java | 117 +++++++++---- .../org/sonar/server/platform/Platform.java | 2 + .../java/org/sonar/server/ui/JRubyFacade.java | 10 ++ .../WEB-INF/app/helpers/alerts_helper.rb | 3 +- .../WEB-INF/app/helpers/application_helper.rb | 35 +--- .../WEB-INF/app/helpers/measures_helper.rb | 24 +-- 12 files changed, 280 insertions(+), 181 deletions(-) delete mode 100644 plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/Periods.java create mode 100644 sonar-core/src/main/java/org/sonar/core/timemachine/Periods.java rename {plugins/sonar-core-plugin/src/test/java/org/sonar/plugins => sonar-core/src/test/java/org/sonar}/core/timemachine/PeriodsTest.java (55%) diff --git a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/CorePlugin.java b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/CorePlugin.java index 1cbc4f387fc..baedbbfb19c 100644 --- a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/CorePlugin.java +++ b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/CorePlugin.java @@ -27,6 +27,7 @@ import org.sonar.api.PropertyType; 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; @@ -76,7 +77,6 @@ import org.sonar.plugins.core.timemachine.NewCoverageFileAnalyzer; 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; @@ -92,11 +92,11 @@ import org.sonar.plugins.core.widgets.CoverageWidget; 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; diff --git a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/sensors/CheckAlertThresholds.java b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/sensors/CheckAlertThresholds.java index 17f87796d66..63e0efc328f 100644 --- a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/sensors/CheckAlertThresholds.java +++ b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/sensors/CheckAlertThresholds.java @@ -26,6 +26,7 @@ import org.sonar.api.batch.DecoratorBarriers; 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; @@ -35,19 +36,21 @@ import org.sonar.api.profiles.RulesProfile; 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; @@ -147,7 +150,7 @@ public class CheckAlertThresholds implements Decorator { .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(); diff --git a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/Periods.java b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/Periods.java deleted file mode 100644 index 74d47c624e6..00000000000 --- a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/Periods.java +++ /dev/null @@ -1,85 +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.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; - } - -} diff --git a/plugins/sonar-core-plugin/src/main/resources/org/sonar/l10n/core.properties b/plugins/sonar-core-plugin/src/main/resources/org/sonar/l10n/core.properties index 5ed67de4d59..349b5346f3c 100644 --- a/plugins/sonar-core-plugin/src/main/resources/org/sonar/l10n/core.properties +++ b/plugins/sonar-core-plugin/src/main/resources/org/sonar/l10n/core.properties @@ -174,11 +174,6 @@ created_by=Created by 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 diff --git a/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/sensors/CheckAlertThresholdsTest.java b/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/sensors/CheckAlertThresholdsTest.java index 0d1d5295b3b..13af0bd4450 100644 --- a/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/sensors/CheckAlertThresholdsTest.java +++ b/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/sensors/CheckAlertThresholdsTest.java @@ -25,6 +25,7 @@ import org.junit.Test; 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; @@ -35,7 +36,7 @@ import org.sonar.api.resources.Project; 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; @@ -54,10 +55,13 @@ public class CheckAlertThresholdsTest { 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; @@ -76,8 +80,9 @@ public class CheckAlertThresholdsTest { 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); } @@ -257,7 +262,7 @@ public class CheckAlertThresholdsTest { 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 diff --git a/sonar-core/src/main/java/org/sonar/core/timemachine/Periods.java b/sonar-core/src/main/java/org/sonar/core/timemachine/Periods.java new file mode 100644 index 00000000000..4d1c34ace3a --- /dev/null +++ b/sonar-core/src/main/java/org/sonar/core/timemachine/Periods.java @@ -0,0 +1,156 @@ +/* + * 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; + } + } + +} diff --git a/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/timemachine/PeriodsTest.java b/sonar-core/src/test/java/org/sonar/core/timemachine/PeriodsTest.java similarity index 55% rename from plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/timemachine/PeriodsTest.java rename to sonar-core/src/test/java/org/sonar/core/timemachine/PeriodsTest.java index b8a3f04905a..5fd891c4128 100644 --- a/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/timemachine/PeriodsTest.java +++ b/sonar-core/src/test/java/org/sonar/core/timemachine/PeriodsTest.java @@ -17,12 +17,13 @@ * 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; +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; @@ -38,6 +39,8 @@ public class PeriodsTest { private Periods periods; private Snapshot snapshot; + + private Settings settings; private I18n i18n; private int periodIndex; @@ -49,90 +52,142 @@ public class PeriodsTest { param = "10"; snapshot = mock(Snapshot.class); - i18n = mock(I18n.class); - when(snapshot.getPeriodModeParameter(periodIndex)).thenReturn(param); - periods = new Periods(snapshot, i18n); + settings = new Settings(); + i18n = mock(I18n.class); + periods = new Periods(settings, i18n); } @Test - public void shouldReturnLabelInModeDays() { + 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.getLabel(periodIndex); + 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 shouldReturnLabelInModeVersion() { + 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.getLabel(periodIndex); - + 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 shouldReturnLabelInModePreviousAnalysisWithDateNotNull() { - when(snapshot.getPeriodMode(periodIndex)).thenReturn(CoreProperties.TIMEMACHINE_MODE_VERSION); + public void shouldReturnSnapshotLabelInModePreviousAnalysisWithDateNotNull() { + when(snapshot.getPeriodMode(periodIndex)).thenReturn(CoreProperties.TIMEMACHINE_MODE_PREVIOUS_ANALYSIS); 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()); + 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 shouldReturnLabelInModePreviousAnalysisWithNullDate() { - when(snapshot.getPeriodMode(periodIndex)).thenReturn(CoreProperties.TIMEMACHINE_MODE_VERSION); + 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.getLabel(periodIndex); - - verify(i18n).message(Mockito.any(Locale.class), Mockito.eq("since_version"), Mockito.isNull(String.class), Mockito.eq(param)); + periods.label(snapshot, periodIndex); + verify(i18n).message(Mockito.any(Locale.class), Mockito.eq("since_previous_analysis"), Mockito.isNull(String.class)); } @Test - public void shouldReturnLabelInModePreviousVersionWithParamNotNull() { + public void shouldReturnSnapshotLabelInModePreviousVersionWithParamNotNull() { when(snapshot.getPeriodMode(periodIndex)).thenReturn(CoreProperties.TIMEMACHINE_MODE_PREVIOUS_VERSION); when(snapshot.getPeriodModeParameter(periodIndex)).thenReturn(param); - periods.getLabel(periodIndex); - + 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 shouldReturnLabelInModePreviousVersionWithParamNull() { + public void shouldReturnSnapshotLabelInModePreviousVersionWithParamNull() { when(snapshot.getPeriodMode(periodIndex)).thenReturn(CoreProperties.TIMEMACHINE_MODE_PREVIOUS_VERSION); when(snapshot.getPeriodModeParameter(periodIndex)).thenReturn(null); - periods.getLabel(periodIndex); - + periods.label(snapshot, periodIndex); verify(i18n).message(Mockito.any(Locale.class), Mockito.eq("since_previous_version"), Mockito.isNull(String.class)); } @Test - public void shouldReturnLabelInModeDate() { + public void shouldReturnSnapshotLabelInModeDate() { when(snapshot.getPeriodMode(periodIndex)).thenReturn(CoreProperties.TIMEMACHINE_MODE_DATE); when(snapshot.getPeriodDate(periodIndex)).thenReturn(new Date()); - periods.getLabel(periodIndex); + periods.label(snapshot, 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() { + @Test(expected = IllegalArgumentException.class) + public void shouldNotSupportUnknownModeForSnapshotLabel() { when(snapshot.getPeriodMode(periodIndex)).thenReturn("Unknown mode"); - periods.getLabel(periodIndex); + 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); + } + + } diff --git a/sonar-server/src/main/java/org/sonar/server/platform/Platform.java b/sonar-server/src/main/java/org/sonar/server/platform/Platform.java index c316a4a6082..9809d4816c6 100644 --- a/sonar-server/src/main/java/org/sonar/server/platform/Platform.java +++ b/sonar-server/src/main/java/org/sonar/server/platform/Platform.java @@ -54,6 +54,7 @@ import org.sonar.core.persistence.MyBatis; 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; @@ -245,6 +246,7 @@ public final class Platform { servicesContainer.addSingleton(MeasureFilterEngine.class); servicesContainer.addSingleton(DryRunDatabaseFactory.class); servicesContainer.addSingleton(DefaultResourcePermissions.class); + servicesContainer.addSingleton(Periods.class); // Notifications servicesContainer.addSingleton(EmailSettings.class); diff --git a/sonar-server/src/main/java/org/sonar/server/ui/JRubyFacade.java b/sonar-server/src/main/java/org/sonar/server/ui/JRubyFacade.java index 18ee0d05a65..fbeb700e60e 100644 --- a/sonar-server/src/main/java/org/sonar/server/ui/JRubyFacade.java +++ b/sonar-server/src/main/java/org/sonar/server/ui/JRubyFacade.java @@ -57,6 +57,7 @@ import org.sonar.core.persistence.DryRunDatabaseFactory; 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; @@ -81,6 +82,7 @@ import javax.annotation.Nullable; 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; @@ -524,4 +526,12 @@ public final class JRubyFacade { 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); + } } diff --git a/sonar-server/src/main/webapp/WEB-INF/app/helpers/alerts_helper.rb b/sonar-server/src/main/webapp/WEB-INF/app/helpers/alerts_helper.rb index d94f56c2d0e..e39cf586981 100644 --- a/sonar-server/src/main/webapp/WEB-INF/app/helpers/alerts_helper.rb +++ b/sonar-server/src/main/webapp/WEB-INF/app/helpers/alerts_helper.rb @@ -91,7 +91,8 @@ module AlertsHelper def period_select_options(alert, index) if index selected = (alert.period == index ? 'selected' : '') - "" + period_label = Api::Utils.java_facade.getPeriodLabel(index) + "" else selected = (alert.period ? 'selected' : '') "" diff --git a/sonar-server/src/main/webapp/WEB-INF/app/helpers/application_helper.rb b/sonar-server/src/main/webapp/WEB-INF/app/helpers/application_helper.rb index 8a42d5e6545..4903166626a 100644 --- a/sonar-server/src/main/webapp/WEB-INF/app/helpers/application_helper.rb +++ b/sonar-server/src/main/webapp/WEB-INF/app/helpers/application_helper.rb @@ -82,37 +82,10 @@ module ApplicationHelper 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) diff --git a/sonar-server/src/main/webapp/WEB-INF/app/helpers/measures_helper.rb b/sonar-server/src/main/webapp/WEB-INF/app/helpers/measures_helper.rb index 8c757d0ab12..5c8ceb97160 100644 --- a/sonar-server/src/main/webapp/WEB-INF/app/helpers/measures_helper.rb +++ b/sonar-server/src/main/webapp/WEB-INF/app/helpers/measures_helper.rb @@ -92,26 +92,10 @@ module MeasuresHelper 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 -- 2.39.5