From 70eac6ea0e564da3735a3d80065326cee10c7c48 Mon Sep 17 00:00:00 2001 From: Julien Lancelot Date: Fri, 7 Dec 2012 14:55:15 +0100 Subject: [PATCH] SONAR-1352 Do not display "variation" on alerts on new metrics --- .../core/sensors/CheckAlertThresholds.java | 4 +++- .../sensors/CheckAlertThresholdsTest.java | 22 ++++++++++++++++++- 2 files changed, 24 insertions(+), 2 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 63e0efc328f..992c524e806 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 @@ -43,6 +43,8 @@ import java.util.Locale; public class CheckAlertThresholds implements Decorator { + private static final String VARIATION_METRIC_PREFIX = "new_"; + private final Snapshot snapshot; private final RulesProfile profile; private final Periods periods; @@ -140,7 +142,7 @@ public class CheckAlertThresholds implements Decorator { StringBuilder stringBuilder = new StringBuilder(); stringBuilder.append(metric); - if (alertPeriod != null) { + if (alertPeriod != null && !alert.getMetric().getKey().startsWith(VARIATION_METRIC_PREFIX)) { String variation = i18n.message(getLocale(), "variation", null).toLowerCase(); stringBuilder.append(" ").append(variation); } 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 13af0bd4450..196cec3d98d 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 @@ -218,7 +218,7 @@ public class CheckAlertThresholdsTest { @Test public void shouldVariationPeriodValueCouldBeUsedForRatingMetric() { - Metric ratingMetric = new Metric.Builder("key.rating", "name.rating", Metric.ValueType.RATING).create(); + Metric ratingMetric = new Metric.Builder("key_rating_metric", "Rating metric", Metric.ValueType.RATING).create(); Measure measureRatingMetric = new Measure(ratingMetric, 150d); measureRatingMetric.setVariation1(50d); when(context.getMeasure(ratingMetric)).thenReturn(measureRatingMetric); @@ -273,6 +273,26 @@ public class CheckAlertThresholdsTest { verify(context).saveMeasure(argThat(matchesMetric(CoreMetrics.ALERT_STATUS, Metric.Level.WARN, "Classes variation > 30 since someday"))); } + @Test + public void shouldLabelAlertForNewMetricDoNotContainsVariationWord() { + Metric newMetric = new Metric.Builder("new_metric_key", "New Metric", Metric.ValueType.INT).create(); + Measure measure = new Measure(newMetric, 15d); + measure.setVariation1(50d); + 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(periods.label(snapshot, 1)).thenReturn("since someday"); + + when(profile.getAlerts()).thenReturn(Arrays.asList( + new Alert(null, newMetric, Alert.OPERATOR_GREATER, null, "30", 1) // generates warning because classes increases of 40, which is greater than 30 + )); + + decorator.decorate(project, context); + + verify(context).saveMeasure(argThat(matchesMetric(CoreMetrics.ALERT_STATUS, Metric.Level.WARN, "New Measure > 30 since someday"))); + } + private ArgumentMatcher matchesMetric(final Metric metric, final Metric.Level alertStatus, final String alertText) { return new ArgumentMatcher() { @Override -- 2.39.5