]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-2464 Metric.Builder must set best and worst values when type percent
authorEvgeny Mandrikov <mandrikov@gmail.com>
Mon, 23 May 2011 19:05:29 +0000 (23:05 +0400)
committerEvgeny Mandrikov <mandrikov@gmail.com>
Mon, 23 May 2011 19:07:09 +0000 (23:07 +0400)
sonar-plugin-api/src/main/java/org/sonar/api/measures/Metric.java
sonar-plugin-api/src/test/java/org/sonar/api/measures/MetricTest.java

index a26fe7a481a405c38944ea9f12c76285cb8b57fa..91210908704e49dac574faa5a9a21fc95c9cbe3d 100644 (file)
@@ -663,6 +663,10 @@ public class Metric implements ServerExtension, BatchExtension {
     }
 
     public Metric create() {
+      if (ValueType.PERCENT.equals(this.type)) {
+        this.bestValue = (direction == DIRECTION_BETTER ? 100.0 : 0.0);
+        this.worstValue = (direction == DIRECTION_BETTER ? 0.0 : 100.0);
+      }
       return new Metric(key, name, type, description, direction, domain, qualitative, worstValue, bestValue, optimizedBestValue, hidden, formula);
     }
   }
index 32cc7c0fa53dc7bb12817daf1cdf0d9056c13cb1..1afef72fdc9af726ed3a6e59355d4e260935515c 100644 (file)
@@ -55,4 +55,20 @@ public class MetricTest {
     assertThat(metric.isHidden(), is(false));
     assertThat(metric.isOptimizedBestValue(), is(false));
   }
+
+  @Test
+  public void shouldCreatePercentMetricWithDefaultValues() {
+    Metric better = new Metric.Builder("foo", "Foo", Metric.ValueType.PERCENT)
+        .setDirection(Metric.DIRECTION_BETTER)
+        .create();
+    Metric worst = new Metric.Builder("foo", "Foo", Metric.ValueType.PERCENT)
+        .setDirection(Metric.DIRECTION_WORST)
+        .create();
+
+    assertThat(better.getBestValue(), is(100.0));
+    assertThat(better.getWorstValue(), is(0.0));
+    assertThat(worst.getBestValue(), is(0.0));
+    assertThat(worst.getWorstValue(), is(100.0));
+  }
+
 }