]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-1352 Fix issue when analysing project for the first time and an alert with...
authorJulien Lancelot <julien.lancelot@gmail.com>
Thu, 29 Nov 2012 13:09:26 +0000 (14:09 +0100)
committerJulien Lancelot <julien.lancelot@gmail.com>
Thu, 29 Nov 2012 13:09:46 +0000 (14:09 +0100)
plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/sensors/AlertUtils.java
plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/sensors/CheckAlertThresholdsTest.java

index cec2e22be177385be0bd5704af40d1fcb670db51..5d5e6c89a2c83c87587481db39ca05a02425f1d6 100644 (file)
@@ -50,14 +50,16 @@ public final class AlertUtils {
     }
 
     Comparable criteriaValue = getValueForComparison(alert.getMetric(), valueToEval);
-    Comparable metricValue = getMeasureValue(alert, measure);
-
-    int comparison = metricValue.compareTo(criteriaValue);
-    return !(// NOSONAR complexity of this boolean expression is under control
-        (alert.isNotEqualsOperator() && comparison == 0)
-            || (alert.isGreaterOperator() && comparison != 1)
-            || (alert.isSmallerOperator() && comparison != -1)
-            || (alert.isEqualsOperator() && comparison != 0));
+    Comparable measureValue = getMeasureValue(alert, measure);
+    if (measureValue != null) {
+      int comparison = measureValue.compareTo(criteriaValue);
+      return !(// NOSONAR complexity of this boolean expression is under control
+          (alert.isNotEqualsOperator() && comparison == 0)
+              || (alert.isGreaterOperator() && comparison != 1)
+              || (alert.isSmallerOperator() && comparison != -1)
+              || (alert.isEqualsOperator() && comparison != 0));
+    }
+    return false;
   }
 
   private static String getValueToEval(Alert alert, Metric.Level alertLevel) {
@@ -100,7 +102,8 @@ public final class AlertUtils {
     }
     if (metric.getType() == Metric.ValueType.INT ||
         metric.getType() == Metric.ValueType.MILLISEC) {
-      return getValue(alert, measure).intValue();
+      Double value = getValue(alert, measure);
+      return value != null ? value.intValue() : null;
     }
     if (alert.getPeriod() == null) {
       if (metric.getType() == Metric.ValueType.STRING ||
index 90d1ce756e5cc64f745aa5a59fd1e7bca7de1bf7..0d1d5295b3be2335c0694b6dd8f9c9e763b65441 100644 (file)
@@ -197,6 +197,20 @@ public class CheckAlertThresholdsTest {
     verify(context).saveMeasure(argThat(hasLevel(measureComplexity, Metric.Level.WARN)));
   }
 
+  @Test
+  public void shouldBeOkIfVariationIsNull() {
+    measureClasses.setVariation1(null);
+
+    when(profile.getAlerts()).thenReturn(Arrays.asList(
+        new Alert(null, CoreMetrics.CLASSES, Alert.OPERATOR_GREATER, null, "10", 1)
+    ));
+
+    decorator.decorate(project, context);
+
+    verify(context).saveMeasure(argThat(matchesMetric(CoreMetrics.ALERT_STATUS, Metric.Level.OK, null)));
+    verify(context).saveMeasure(argThat(hasLevel(measureClasses, Metric.Level.OK)));
+  }
+
   @Test
   public void shouldVariationPeriodValueCouldBeUsedForRatingMetric() {
     Metric ratingMetric = new Metric.Builder("key.rating", "name.rating", Metric.ValueType.RATING).create();