]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-1352 improve differential alert label
authorJulien Lancelot <julien.lancelot@gmail.com>
Wed, 28 Nov 2012 15:32:16 +0000 (16:32 +0100)
committerJulien Lancelot <julien.lancelot@gmail.com>
Wed, 28 Nov 2012 15:32:25 +0000 (16:32 +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
sonar-plugin-api/src/main/java/org/sonar/api/profiles/Alert.java

index d8a3837ae28e4b9dc2810829b8f96eec82d7ed91..17f87796d66e907fef41545b71e4b0e67353ba20 100644 (file)
@@ -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.i18n.I18n;
 import org.sonar.api.measures.CoreMetrics;
 import org.sonar.api.measures.Measure;
 import org.sonar.api.measures.Metric;
@@ -37,16 +38,19 @@ import org.sonar.api.resources.ResourceUtils;
 import org.sonar.plugins.core.timemachine.Periods;
 
 import java.util.List;
+import java.util.Locale;
 
 public class CheckAlertThresholds implements Decorator {
 
   private final RulesProfile profile;
   private final Periods periods;
+  private final I18n i18n;
 
 
-  public CheckAlertThresholds(RulesProfile profile, Periods periods) {
+  public CheckAlertThresholds(RulesProfile profile, Periods periods, I18n i18n) {
     this.profile = profile;
     this.periods = periods;
+    this.i18n = i18n;
   }
 
   @DependedUpon
@@ -123,12 +127,30 @@ public class CheckAlertThresholds implements Decorator {
     if (level == Metric.Level.OK) {
       return null;
     }
-    String alertLabel = alert.getAlertLabel(level);
+    return getAlertLabel(alert, level);
+  }
+
+  private String getAlertLabel(Alert alert, Metric.Level level) {
     Integer alertPeriod = alert.getPeriod();
+    String metric = i18n.message(getLocale(), "metric." + alert.getMetric().getKey() + ".name", null);
+
+    StringBuilder stringBuilder = new StringBuilder();
+    stringBuilder.append(metric);
+
+    if (alertPeriod != null) {
+      String variation = i18n.message(getLocale(), "variation", null).toLowerCase();
+      stringBuilder.append(" ").append(variation);
+    }
+
+    stringBuilder
+        .append(" ").append(alert.getOperator()).append(" ")
+        .append(level.equals(Metric.Level.ERROR) ? alert.getValueError() : alert.getValueWarning());
+
     if (alertPeriod != null){
-      alertLabel += " " + periods.getLabel(alertPeriod);
+      stringBuilder.append(" ").append(periods.getLabel(alertPeriod));
     }
-    return alertLabel;
+
+    return stringBuilder.toString();
   }
 
 
@@ -136,4 +158,9 @@ public class CheckAlertThresholds implements Decorator {
   public String toString() {
     return getClass().getSimpleName();
   }
+
+  private Locale getLocale() {
+    return Locale.ENGLISH;
+  }
+
 }
index 87b4590b6262f9c1ca8cc57cce53d0d2432596bc..d79934da594a988692cfe04ebad2b1080a03b162 100644 (file)
@@ -23,7 +23,9 @@ import org.apache.commons.lang.NotImplementedException;
 import org.junit.Before;
 import org.junit.Test;
 import org.mockito.ArgumentMatcher;
+import org.mockito.Mockito;
 import org.sonar.api.batch.DecoratorContext;
+import org.sonar.api.i18n.I18n;
 import org.sonar.api.measures.CoreMetrics;
 import org.sonar.api.measures.Measure;
 import org.sonar.api.measures.Metric;
@@ -37,6 +39,7 @@ import org.sonar.plugins.core.timemachine.Periods;
 
 import java.util.ArrayList;
 import java.util.Arrays;
+import java.util.Locale;
 
 import static org.junit.Assert.assertFalse;
 import static org.mockito.Matchers.argThat;
@@ -56,11 +59,13 @@ public class CheckAlertThresholdsTest {
   private Measure measureComplexity;
   private Resource project;
   private Periods periods;
+  private I18n i18n;
 
   @Before
   public void setup() {
     context = mock(DecoratorContext.class);
     periods = mock(Periods.class);
+    i18n = mock(I18n.class);
 
     measureClasses = new Measure(CoreMetrics.CLASSES, 20d);
     measureCoverage = new Measure(CoreMetrics.COVERAGE, 35d);
@@ -71,7 +76,7 @@ public class CheckAlertThresholdsTest {
     when(context.getMeasure(CoreMetrics.COMPLEXITY)).thenReturn(measureComplexity);
 
     profile = mock(RulesProfile.class);
-    decorator = new CheckAlertThresholds(profile, periods);
+    decorator = new CheckAlertThresholds(profile, periods, i18n);
     project = mock(Resource.class);
     when(project.getQualifier()).thenReturn(Qualifiers.PROJECT);
   }
@@ -138,22 +143,15 @@ public class CheckAlertThresholdsTest {
 
   @Test
   public void globalLabelShouldAggregateAllLabels() {
-    Alert alert1 = mock(Alert.class);
-    when(alert1.getMetric()).thenReturn(CoreMetrics.CLASSES);
-    when(alert1.getValueError()).thenReturn("10000"); // there are 20 classes, error threshold is higher => alert
-    when(alert1.getAlertLabel(Metric.Level.ERROR)).thenReturn("error classes");
-    when(alert1.getPeriod()).thenReturn(null);
-
-    Alert alert2 = mock(Alert.class);
-    when(alert2.getMetric()).thenReturn(CoreMetrics.COVERAGE);
-    when(alert2.getValueWarning()).thenReturn("80"); // coverage is 35%, warning threshold is higher => alert
-    when(alert2.getAlertLabel(Metric.Level.WARN)).thenReturn("warning coverage");
-    when(alert2.getPeriod()).thenReturn(null);
-
-    when(profile.getAlerts()).thenReturn(Arrays.asList(alert1, alert2));
+    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(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
+
     decorator.decorate(project, context);
 
-    verify(context).saveMeasure(argThat(matchesMetric(CoreMetrics.ALERT_STATUS, Metric.Level.ERROR, "error classes, warning coverage")));
+    verify(context).saveMeasure(argThat(matchesMetric(CoreMetrics.ALERT_STATUS, Metric.Level.ERROR, "Classes < 10000, Coverages < 50.0")));
   }
 
   @Test
@@ -243,16 +241,17 @@ public class CheckAlertThresholdsTest {
   public void shouldLabelAlertContainsPeriod() {
     measureClasses.setVariation1(40d);
 
-    Alert alert = mock(Alert.class);
-    when(alert.getMetric()).thenReturn(CoreMetrics.CLASSES);
-    when(alert.getValueError()).thenReturn("30");
-    when(alert.getAlertLabel(Metric.Level.ERROR)).thenReturn("error classes");
-    when(alert.getPeriod()).thenReturn(1);
+    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("variation"), Mockito.isNull(String.class))).thenReturn("variation");
+    when(periods.getLabel(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
+    ));
 
-    when(profile.getAlerts()).thenReturn(Arrays.asList(alert));
     decorator.decorate(project, context);
 
-    verify(periods).getLabel(1);
+    verify(context).saveMeasure(argThat(matchesMetric(CoreMetrics.ALERT_STATUS, Metric.Level.WARN, "Classes variation > 30 since someday")));
   }
 
   private ArgumentMatcher<Measure> matchesMetric(final Metric metric, final Metric.Level alertStatus, final String alertText) {
index 891e1e955d98bedfbcf80f803b2a056886aa1612..1afbaef3426dbe37a913956e2e8d5371bcb79d94 100644 (file)
@@ -230,15 +230,6 @@ public class Alert extends BaseIdentifiable implements Cloneable {
     return operator.equals(OPERATOR_NOT_EQUALS);
   }
 
-
-  public String getAlertLabel(Metric.Level level) {
-    return new StringBuilder()
-        .append(getMetric().getName())
-        .append(" ").append(getOperator())
-        .append(" ")
-        .append(level.equals(Metric.Level.ERROR) ? getValueError() : getValueWarning()).toString();
-  }
-
   @Override
   public Object clone() {
     return new Alert(getRulesProfile(), getMetric(), getOperator(), getValueError(), getValueWarning(), getPeriod());