]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-1352 Do not display "variation" on alerts on new metrics
authorJulien Lancelot <julien.lancelot@gmail.com>
Fri, 7 Dec 2012 13:55:15 +0000 (14:55 +0100)
committerJulien Lancelot <julien.lancelot@gmail.com>
Fri, 7 Dec 2012 13:55:15 +0000 (14:55 +0100)
plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/sensors/CheckAlertThresholds.java
plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/sensors/CheckAlertThresholdsTest.java

index 63e0efc328f7e932c50e2279a7fe17db61728999..992c524e806429adf744f4f84d8a992bb3696846 100644 (file)
@@ -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);
     }
index 13af0bd445045fac0529e9fede400285ff6ab020..196cec3d98dd23d212fb1a4b0cab52fd0ecaef5a 100644 (file)
@@ -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<Measure> matchesMetric(final Metric metric, final Metric.Level alertStatus, final String alertText) {
     return new ArgumentMatcher<Measure>() {
       @Override