summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSimon Brandhof <simon.brandhof@gmail.com>2013-09-30 14:28:53 +0200
committerSimon Brandhof <simon.brandhof@gmail.com>2013-09-30 14:29:28 +0200
commit1ed170a10d9e45037feba2bb40f3a3c23b74cf89 (patch)
treed64e8f0c9f8ac9a5b933faaa6b7b965b159a6b56
parent9cc09fb5dc0e99d0fc1b33f07102409589bf860b (diff)
downloadsonarqube-1ed170a10d9e45037feba2bb40f3a3c23b74cf89.tar.gz
sonarqube-1ed170a10d9e45037feba2bb40f3a3c23b74cf89.zip
SONAR-4733 Metric label is missing in alert messages
-rw-r--r--plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/sensors/CheckAlertThresholds.java2
-rw-r--r--plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/sensors/CheckAlertThresholdsTest.java39
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
@@ -175,6 +176,34 @@ public class CheckAlertThresholdsTest {
}
@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.<Alert>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.<Alert>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);
measureCoverage.setVariation2(50d);
@@ -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(