aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJulien Lancelot <julien.lancelot@gmail.com>2012-11-28 16:32:16 +0100
committerJulien Lancelot <julien.lancelot@gmail.com>2012-11-28 16:32:25 +0100
commit04a58d0b3634fa22820ae89562178e74b43aab5a (patch)
tree4e4d5362c9bd89a3561336a0128f6bbf5182f69e
parentd530754343162316fcfb52a38fb782114ec3fcca (diff)
downloadsonarqube-04a58d0b3634fa22820ae89562178e74b43aab5a.tar.gz
sonarqube-04a58d0b3634fa22820ae89562178e74b43aab5a.zip
SONAR-1352 improve differential alert label
-rw-r--r--plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/sensors/CheckAlertThresholds.java35
-rw-r--r--plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/sensors/CheckAlertThresholdsTest.java43
-rw-r--r--sonar-plugin-api/src/main/java/org/sonar/api/profiles/Alert.java9
3 files changed, 52 insertions, 35 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 d8a3837ae28..17f87796d66 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
@@ -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;
+ }
+
}
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 87b4590b626..d79934da594 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
@@ -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) {
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/profiles/Alert.java b/sonar-plugin-api/src/main/java/org/sonar/api/profiles/Alert.java
index 891e1e955d9..1afbaef3426 100644
--- a/sonar-plugin-api/src/main/java/org/sonar/api/profiles/Alert.java
+++ b/sonar-plugin-api/src/main/java/org/sonar/api/profiles/Alert.java
@@ -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());