From 676770e8e1bf0a9ae13ec57373a6cca887f4e45c Mon Sep 17 00:00:00 2001 From: Evgeny Mandrikov Date: Mon, 23 May 2011 23:05:29 +0400 Subject: [PATCH] SONAR-2464 Metric.Builder must set best and worst values when type percent --- .../main/java/org/sonar/api/measures/Metric.java | 4 ++++ .../java/org/sonar/api/measures/MetricTest.java | 16 ++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/measures/Metric.java b/sonar-plugin-api/src/main/java/org/sonar/api/measures/Metric.java index a26fe7a481a..91210908704 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/measures/Metric.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/measures/Metric.java @@ -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); } } diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/measures/MetricTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/measures/MetricTest.java index 32cc7c0fa53..1afef72fdc9 100644 --- a/sonar-plugin-api/src/test/java/org/sonar/api/measures/MetricTest.java +++ b/sonar-plugin-api/src/test/java/org/sonar/api/measures/MetricTest.java @@ -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)); + } + } -- 2.39.5