From 1ed170a10d9e45037feba2bb40f3a3c23b74cf89 Mon Sep 17 00:00:00 2001 From: Simon Brandhof Date: Mon, 30 Sep 2013 14:28:53 +0200 Subject: [PATCH] SONAR-4733 Metric label is missing in alert messages --- .../core/sensors/CheckAlertThresholds.java | 2 +- .../sensors/CheckAlertThresholdsTest.java | 39 ++++++++++++++++--- 2 files changed, 35 insertions(+), 6 deletions(-) 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 197dfb3fd41..2da8eead044 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 @@ -133,7 +133,7 @@ public class CheckAlertThresholds implements Decorator { private String getAlertLabel(Alert alert, Metric.Level level) { Integer alertPeriod = alert.getPeriod(); - String metric = i18n.message(getLocale(), "metric." + alert.getMetric().getKey() + ".name", null); + String metric = i18n.message(getLocale(), "metric." + alert.getMetric().getKey() + ".name", alert.getMetric().getName()); StringBuilder stringBuilder = new StringBuilder(); stringBuilder.append(metric); 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 9a48abfd4c9..46e43b20a41 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 @@ -45,6 +45,7 @@ import java.util.Locale; import static com.google.common.collect.Lists.newArrayList; import static org.fest.assertions.Assertions.assertThat; +import static org.mockito.Matchers.anyString; import static org.mockito.Matchers.argThat; import static org.mockito.Mockito.*; @@ -68,7 +69,7 @@ public class CheckAlertThresholdsTest { context = mock(DecoratorContext.class); periods = mock(Periods.class); i18n = mock(I18n.class); - when(i18n.message(Mockito.any(Locale.class), Mockito.eq("variation"), Mockito.eq("variation"))).thenReturn("variation"); + when(i18n.message(any(Locale.class), eq("variation"), eq("variation"))).thenReturn("variation"); measureClasses = new Measure(CoreMetrics.CLASSES, 20d); measureCoverage = new Measure(CoreMetrics.COVERAGE, 35d); @@ -163,8 +164,8 @@ public class CheckAlertThresholdsTest { @Test public void globalLabelShouldAggregateAllLabels() { - when(i18n.message(Mockito.any(Locale.class), Mockito.eq("metric.classes.name"), Mockito.isNull(String.class))).thenReturn("Classes"); - when(i18n.message(Mockito.any(Locale.class), Mockito.eq("metric.coverage.name"), Mockito.isNull(String.class))).thenReturn("Coverages"); + when(i18n.message(any(Locale.class), eq("metric.classes.name"), anyString())).thenReturn("Classes"); + when(i18n.message(any(Locale.class), eq("metric.coverage.name"), anyString())).thenReturn("Coverages"); when(profile.getAlerts()).thenReturn(Arrays.asList( new Alert(null, CoreMetrics.CLASSES, Alert.OPERATOR_SMALLER, null, "10000"), // there are 20 classes, error threshold is higher => alert new Alert(null, CoreMetrics.COVERAGE, Alert.OPERATOR_SMALLER, "50.0", "80.0"))); // coverage is 35%, warning threshold is higher => alert @@ -174,6 +175,34 @@ public class CheckAlertThresholdsTest { verify(context).saveMeasure(argThat(matchesMetric(CoreMetrics.ALERT_STATUS, Metric.Level.ERROR, "Classes < 10000, Coverages < 50.0"))); } + @Test + public void alertLabelUsesL10nMetricName() { + Metric metric = new Metric.Builder("rating", "Rating", Metric.ValueType.INT).create(); + + // metric name is declared in l10n bundle + when(i18n.message(any(Locale.class), eq("metric.rating.name"), anyString())).thenReturn("THE RATING"); + + when(context.getMeasure(metric)).thenReturn(new Measure(metric, 4d)); + when(profile.getAlerts()).thenReturn(Arrays.asList(new Alert(null, metric, Alert.OPERATOR_SMALLER, "10", null))); + decorator.decorate(project, context); + + verify(context).saveMeasure(argThat(matchesMetric(CoreMetrics.ALERT_STATUS, Metric.Level.ERROR, "THE RATING < 10"))); + } + + @Test + public void alertLabelUsesMetricNameIfMissingL10nBundle() { + // the third argument is Metric#getName() + when(i18n.message(any(Locale.class), eq("metric.classes.name"), eq("Classes"))).thenReturn("Classes"); + when(profile.getAlerts()).thenReturn(Arrays.asList( + // there are 20 classes, error threshold is higher => alert + new Alert(null, CoreMetrics.CLASSES, Alert.OPERATOR_SMALLER, "10000", null) + )); + + decorator.decorate(project, context); + + verify(context).saveMeasure(argThat(matchesMetric(CoreMetrics.ALERT_STATUS, Metric.Level.ERROR, "Classes < 10000"))); + } + @Test public void shouldBeOkIfPeriodVariationIsEnough() { measureClasses.setVariation1(0d); @@ -275,7 +304,7 @@ public class CheckAlertThresholdsTest { public void shouldLabelAlertContainsPeriod() { measureClasses.setVariation1(40d); - when(i18n.message(Mockito.any(Locale.class), Mockito.eq("metric.classes.name"), Mockito.isNull(String.class))).thenReturn("Classes"); + when(i18n.message(any(Locale.class), eq("metric.classes.name"), anyString())).thenReturn("Classes"); when(periods.label(snapshot, 1)).thenReturn("since someday"); when(profile.getAlerts()).thenReturn(Arrays.asList( @@ -295,7 +324,7 @@ public class CheckAlertThresholdsTest { when(context.getMeasure(newMetric)).thenReturn(measure); measureClasses.setVariation1(40d); - when(i18n.message(Mockito.any(Locale.class), Mockito.eq("metric.new_metric_key.name"), Mockito.isNull(String.class))).thenReturn("New Measure"); + when(i18n.message(any(Locale.class), eq("metric.new_metric_key.name"), anyString())).thenReturn("New Measure"); when(periods.label(snapshot, 1)).thenReturn("since someday"); when(profile.getAlerts()).thenReturn(Arrays.asList( -- 2.39.5