diff options
author | Sébastien Lesaint <sebastien.lesaint@sonarsource.com> | 2015-06-26 13:04:23 +0200 |
---|---|---|
committer | Sébastien Lesaint <sebastien.lesaint@sonarsource.com> | 2015-06-26 15:10:27 +0200 |
commit | 01992f79d99fab567f780b8e2594f4ef73b50f5b (patch) | |
tree | 92fbd1f91b45f801c82eb8adc9c817eb849df8d2 | |
parent | bfbdeee2868f4b8e0ac4716670919d69aae61fa0 (diff) | |
download | sonarqube-01992f79d99fab567f780b8e2594f4ef73b50f5b.tar.gz sonarqube-01992f79d99fab567f780b8e2594f4ef73b50f5b.zip |
double values of Measures must have only 1 decimal (rounded up)
7 files changed, 69 insertions, 43 deletions
diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/measure/Measure.java b/server/sonar-server/src/main/java/org/sonar/server/computation/measure/Measure.java index ace346b0a19..42fdfcda244 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/computation/measure/Measure.java +++ b/server/sonar-server/src/main/java/org/sonar/server/computation/measure/Measure.java @@ -20,16 +20,24 @@ package org.sonar.server.computation.measure; import com.google.common.base.Optional; +import java.math.BigDecimal; +import java.math.RoundingMode; import java.util.Locale; import java.util.Objects; import javax.annotation.CheckForNull; import javax.annotation.Nullable; +import static com.google.common.base.Preconditions.checkArgument; import static com.google.common.base.Preconditions.checkState; import static java.util.Objects.requireNonNull; public final class Measure { + /** + * Default precision when saving a double value type + */ + private static final int DEFAULT_PRECISION = 1; + public enum ValueType { NO_VALUE, BOOLEAN, INT, LONG, DOUBLE, STRING, LEVEL } @@ -86,7 +94,7 @@ public final class Measure { this.valueType = valueType; this.ruleId = ruleId; this.characteristicId = characteristicId; - this.value = value; + this.value = scale(value); this.data = data; this.dataLevel = dataLevel; this.description = description; @@ -94,6 +102,16 @@ public final class Measure { this.variations = variations; } + @CheckForNull + private static Double scale(@Nullable Double value) { + if (value == null) { + return null; + } + checkArgument(!Double.isNaN(value), "Measure value can not be NaN"); + BigDecimal bd = BigDecimal.valueOf(value); + return bd.setScale(DEFAULT_PRECISION, RoundingMode.HALF_UP).doubleValue(); + } + public static NewMeasureBuilder newMeasureBuilder() { return new NewMeasureBuilder(); } diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/measure/BatchMeasureToMeasureTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/measure/BatchMeasureToMeasureTest.java index 68d24e8931e..fbf88dbdf0d 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/computation/measure/BatchMeasureToMeasureTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/computation/measure/BatchMeasureToMeasureTest.java @@ -250,7 +250,7 @@ public class BatchMeasureToMeasureTest { assertThat(measure.isPresent()); assertThat(measure.get().getValueType()).isEqualTo(Measure.ValueType.DOUBLE); - assertThat(measure.get().getDoubleValue()).isEqualTo(10.6395d); + assertThat(measure.get().getDoubleValue()).isEqualTo(10.6d); assertThat(measure.get().getData()).isEqualTo(SOME_DATA); assertThat(measure.get().getQualityGateStatus().getStatus()).isEqualTo(Measure.Level.OK); assertThat(measure.get().getQualityGateStatus().getText()).isEqualTo(SOME_ALERT_TEXT); diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/measure/BestValueOptimizationTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/measure/BestValueOptimizationTest.java index c2d02d91d03..389a9387201 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/computation/measure/BestValueOptimizationTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/computation/measure/BestValueOptimizationTest.java @@ -147,7 +147,7 @@ public class BestValueOptimizationTest { assertThat(underTest.apply(newMeasureBuilder().create(36.5d))).isTrue(); assertThat(underTest.apply(newMeasureBuilder().setVariations(SOME_EMPTY_VARIATIONS).create(36.5d))).isTrue(); - assertThat(underTest.apply(newMeasureBuilder().create(36.52d))).isFalse(); + assertThat(underTest.apply(newMeasureBuilder().create(36.6d))).isFalse(); } @Test diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/measure/MeasureDtoToMeasureTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/measure/MeasureDtoToMeasureTest.java index a95aea561fd..85fa5722ddf 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/computation/measure/MeasureDtoToMeasureTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/computation/measure/MeasureDtoToMeasureTest.java @@ -241,7 +241,7 @@ public class MeasureDtoToMeasureTest { assertThat(measure.isPresent()); assertThat(measure.get().getValueType()).isEqualTo(Measure.ValueType.DOUBLE); - assertThat(measure.get().getDoubleValue()).isEqualTo(10.6395d); + assertThat(measure.get().getDoubleValue()).isEqualTo(10.6d); assertThat(measure.get().getData()).isEqualTo(SOME_DATA); assertThat(measure.get().getQualityGateStatus().getStatus()).isEqualTo(Level.OK); assertThat(measure.get().getQualityGateStatus().getText()).isEqualTo(SOME_ALERT_TEXT); diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/measure/MeasureTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/measure/MeasureTest.java index 268078e87d2..93032cefc4a 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/computation/measure/MeasureTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/computation/measure/MeasureTest.java @@ -36,18 +36,19 @@ import org.sonar.server.computation.measure.Measure.ValueType; import static com.google.common.collect.FluentIterable.from; import static org.assertj.core.api.Assertions.assertThat; +import static org.sonar.server.computation.measure.Measure.newMeasureBuilder; @RunWith(DataProviderRunner.class) public class MeasureTest { - private static final Measure INT_MEASURE = Measure.newMeasureBuilder().create((int) 1); - private static final Measure LONG_MEASURE = Measure.newMeasureBuilder().create(1l); - private static final Measure DOUBLE_MEASURE = Measure.newMeasureBuilder().create(1d); - private static final Measure STRING_MEASURE = Measure.newMeasureBuilder().create("some_sT ring"); - private static final Measure TRUE_MEASURE = Measure.newMeasureBuilder().create(true); - private static final Measure FALSE_MEASURE = Measure.newMeasureBuilder().create(false); - private static final Measure LEVEL_MEASURE = Measure.newMeasureBuilder().create(Measure.Level.OK); - private static final Measure NO_VALUE_MEASURE = Measure.newMeasureBuilder().createNoValue(); + private static final Measure INT_MEASURE = newMeasureBuilder().create((int) 1); + private static final Measure LONG_MEASURE = newMeasureBuilder().create(1l); + private static final Measure DOUBLE_MEASURE = newMeasureBuilder().create(1d); + private static final Measure STRING_MEASURE = newMeasureBuilder().create("some_sT ring"); + private static final Measure TRUE_MEASURE = newMeasureBuilder().create(true); + private static final Measure FALSE_MEASURE = newMeasureBuilder().create(false); + private static final Measure LEVEL_MEASURE = newMeasureBuilder().create(Measure.Level.OK); + private static final Measure NO_VALUE_MEASURE = newMeasureBuilder().createNoValue(); private static final List<Measure> MEASURES = ImmutableList.of( INT_MEASURE, LONG_MEASURE, DOUBLE_MEASURE, STRING_MEASURE, TRUE_MEASURE, FALSE_MEASURE, NO_VALUE_MEASURE, LEVEL_MEASURE @@ -109,7 +110,7 @@ public class MeasureTest { executionException.expect(UnsupportedOperationException.class); executionException.expectMessage("A measure can not be associated to both a Characteristic and a Rule"); - Measure.newMeasureBuilder().forCharacteristic(SOME_CHARACTERISTIC_ID).forRule(SOME_RULE_ID); + newMeasureBuilder().forCharacteristic(SOME_CHARACTERISTIC_ID).forRule(SOME_RULE_ID); } @Test @@ -117,34 +118,34 @@ public class MeasureTest { executionException.expect(UnsupportedOperationException.class); executionException.expectMessage("A measure can not be associated to both a Characteristic and a Rule"); - Measure.newMeasureBuilder().forRule(SOME_RULE_ID).forCharacteristic(SOME_CHARACTERISTIC_ID); + newMeasureBuilder().forRule(SOME_RULE_ID).forCharacteristic(SOME_CHARACTERISTIC_ID); } @Test public void getRuleId_returns_null_when_ruleKey_has_not_been_set() { - assertThat(Measure.newMeasureBuilder().createNoValue().getRuleId()).isNull(); - assertThat(Measure.newMeasureBuilder().forCharacteristic(SOME_CHARACTERISTIC_ID).createNoValue().getRuleId()).isNull(); + assertThat(newMeasureBuilder().createNoValue().getRuleId()).isNull(); + assertThat(newMeasureBuilder().forCharacteristic(SOME_CHARACTERISTIC_ID).createNoValue().getRuleId()).isNull(); } @Test public void getRuleId_returns_key_set_in_builder() { - assertThat(Measure.newMeasureBuilder().forRule(SOME_RULE_ID).createNoValue().getRuleId()).isEqualTo(SOME_RULE_ID); + assertThat(newMeasureBuilder().forRule(SOME_RULE_ID).createNoValue().getRuleId()).isEqualTo(SOME_RULE_ID); } @Test public void getCharacteristicId_returns_null_when_ruleKey_has_not_been_set() { - assertThat(Measure.newMeasureBuilder().createNoValue().getCharacteristicId()).isNull(); - assertThat(Measure.newMeasureBuilder().forRule(SOME_RULE_ID).createNoValue().getCharacteristicId()).isNull(); + assertThat(newMeasureBuilder().createNoValue().getCharacteristicId()).isNull(); + assertThat(newMeasureBuilder().forRule(SOME_RULE_ID).createNoValue().getCharacteristicId()).isNull(); } @Test public void getCharacteristicId_returns_id_set_in_builder() { - assertThat(Measure.newMeasureBuilder().forCharacteristic(SOME_CHARACTERISTIC_ID).createNoValue().getCharacteristicId()).isEqualTo(SOME_CHARACTERISTIC_ID); + assertThat(newMeasureBuilder().forCharacteristic(SOME_CHARACTERISTIC_ID).createNoValue().getCharacteristicId()).isEqualTo(SOME_CHARACTERISTIC_ID); } @Test(expected = NullPointerException.class) public void create_from_String_throws_NPE_if_arg_is_null() { - Measure.newMeasureBuilder().create((String) null); + newMeasureBuilder().create((String) null); } @Test @@ -262,52 +263,52 @@ public class MeasureTest { public void getAlertStatus_returns_argument_from_setQualityGateStatus() { QualityGateStatus someStatus = new QualityGateStatus(Measure.Level.OK); - assertThat(Measure.newMeasureBuilder().setQualityGateStatus(someStatus).create(true, null).getQualityGateStatus()).isEqualTo(someStatus); - assertThat(Measure.newMeasureBuilder().setQualityGateStatus(someStatus).create(false, null).getQualityGateStatus()).isEqualTo(someStatus); - assertThat(Measure.newMeasureBuilder().setQualityGateStatus(someStatus).create((int) 1, null).getQualityGateStatus()).isEqualTo(someStatus); - assertThat(Measure.newMeasureBuilder().setQualityGateStatus(someStatus).create((long) 1, null).getQualityGateStatus()).isEqualTo(someStatus); - assertThat(Measure.newMeasureBuilder().setQualityGateStatus(someStatus).create((double) 1, null).getQualityGateStatus()).isEqualTo(someStatus); - assertThat(Measure.newMeasureBuilder().setQualityGateStatus(someStatus).create("str").getQualityGateStatus()).isEqualTo(someStatus); - assertThat(Measure.newMeasureBuilder().setQualityGateStatus(someStatus).create(Measure.Level.OK).getQualityGateStatus()).isEqualTo(someStatus); + assertThat(newMeasureBuilder().setQualityGateStatus(someStatus).create(true, null).getQualityGateStatus()).isEqualTo(someStatus); + assertThat(newMeasureBuilder().setQualityGateStatus(someStatus).create(false, null).getQualityGateStatus()).isEqualTo(someStatus); + assertThat(newMeasureBuilder().setQualityGateStatus(someStatus).create((int) 1, null).getQualityGateStatus()).isEqualTo(someStatus); + assertThat(newMeasureBuilder().setQualityGateStatus(someStatus).create((long) 1, null).getQualityGateStatus()).isEqualTo(someStatus); + assertThat(newMeasureBuilder().setQualityGateStatus(someStatus).create((double) 1, null).getQualityGateStatus()).isEqualTo(someStatus); + assertThat(newMeasureBuilder().setQualityGateStatus(someStatus).create("str").getQualityGateStatus()).isEqualTo(someStatus); + assertThat(newMeasureBuilder().setQualityGateStatus(someStatus).create(Measure.Level.OK).getQualityGateStatus()).isEqualTo(someStatus); } @Test(expected = NullPointerException.class) public void newMeasureBuilder_setQualityGateStatus_throws_NPE_if_arg_is_null() { - Measure.newMeasureBuilder().setQualityGateStatus(null); + newMeasureBuilder().setQualityGateStatus(null); } @Test(expected = NullPointerException.class) public void newMeasureBuilder_setVariations_throws_NPE_if_arg_is_null() { - Measure.newMeasureBuilder().setVariations(null); + newMeasureBuilder().setVariations(null); } @Test(expected = NullPointerException.class) public void newMeasureBuilder_setDescription_throws_NPE_if_arg_is_null() { - Measure.newMeasureBuilder().setDescription(null); + newMeasureBuilder().setDescription(null); } @Test(expected = NullPointerException.class) public void updateMeasureBuilder_setQualityGateStatus_throws_NPE_if_arg_is_null() { - Measure.updatedMeasureBuilder(Measure.newMeasureBuilder().createNoValue()).setQualityGateStatus(null); + Measure.updatedMeasureBuilder(newMeasureBuilder().createNoValue()).setQualityGateStatus(null); } @Test(expected = NullPointerException.class) public void updateMeasureBuilder_setVariations_throws_NPE_if_arg_is_null() { - Measure.updatedMeasureBuilder(Measure.newMeasureBuilder().createNoValue()).setVariations(null); + Measure.updatedMeasureBuilder(newMeasureBuilder().createNoValue()).setVariations(null); } @Test(expected = UnsupportedOperationException.class) public void updateMeasureBuilder_setQualityGateStatus_throws_USO_if_measure_already_has_a_QualityGateStatus() { QualityGateStatus qualityGateStatus = new QualityGateStatus(Measure.Level.ERROR); - Measure.updatedMeasureBuilder(Measure.newMeasureBuilder().setQualityGateStatus(qualityGateStatus).createNoValue()).setQualityGateStatus(qualityGateStatus); + Measure.updatedMeasureBuilder(newMeasureBuilder().setQualityGateStatus(qualityGateStatus).createNoValue()).setQualityGateStatus(qualityGateStatus); } @Test(expected = UnsupportedOperationException.class) public void updateMeasureBuilder_setVariations_throws_USO_if_measure_already_has_Variations() { MeasureVariations variations = new MeasureVariations(1d); - Measure.updatedMeasureBuilder(Measure.newMeasureBuilder().setVariations(variations).createNoValue()).setVariations(variations); + Measure.updatedMeasureBuilder(newMeasureBuilder().setVariations(variations).createNoValue()).setVariations(variations); } @Test @@ -327,11 +328,11 @@ public class MeasureTest { public void getData_returns_argument_from_factory_method() { String someData = "lololool"; - assertThat(Measure.newMeasureBuilder().create(true, someData).getData()).isEqualTo(someData); - assertThat(Measure.newMeasureBuilder().create(false, someData).getData()).isEqualTo(someData); - assertThat(Measure.newMeasureBuilder().create((int) 1, someData).getData()).isEqualTo(someData); - assertThat(Measure.newMeasureBuilder().create((long) 1, someData).getData()).isEqualTo(someData); - assertThat(Measure.newMeasureBuilder().create((double) 1, someData).getData()).isEqualTo(someData); + assertThat(newMeasureBuilder().create(true, someData).getData()).isEqualTo(someData); + assertThat(newMeasureBuilder().create(false, someData).getData()).isEqualTo(someData); + assertThat(newMeasureBuilder().create((int) 1, someData).getData()).isEqualTo(someData); + assertThat(newMeasureBuilder().create((long) 1, someData).getData()).isEqualTo(someData); + assertThat(newMeasureBuilder().create((double) 1, someData).getData()).isEqualTo(someData); } @Test @@ -339,6 +340,14 @@ public class MeasureTest { assertThat(LEVEL_MEASURE.getData()).isNull(); } + @Test + public void double_values_are_scaled_to_1_digit_and_round() { + assertThat(newMeasureBuilder().create(30.27777d).getDoubleValue()).isEqualTo(30.3d); + assertThat(newMeasureBuilder().create(30d).getDoubleValue()).isEqualTo(30d); + assertThat(newMeasureBuilder().create(30.01d).getDoubleValue()).isEqualTo(30d); + assertThat(newMeasureBuilder().create(30.1d).getDoubleValue()).isEqualTo(30.1d); + } + private enum WrapInArray implements Function<Measure, Measure[]> { INSTANCE; diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/step/CustomMeasuresCopyStepTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/step/CustomMeasuresCopyStepTest.java index 1176757999c..5ba5dd55d3a 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/computation/step/CustomMeasuresCopyStepTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/computation/step/CustomMeasuresCopyStepTest.java @@ -19,7 +19,6 @@ */ package org.sonar.server.computation.step; -import org.assertj.core.data.Offset; import org.junit.Before; import org.junit.ClassRule; import org.junit.Rule; @@ -93,7 +92,7 @@ public class CustomMeasuresCopyStepTest { ArgumentCaptor<Measure> measureCaptor = ArgumentCaptor.forClass(Measure.class); verify(measureRepository).add(eq(file1), eq(floatMetric), measureCaptor.capture()); Measure fileMeasure = measureCaptor.getValue(); - assertThat(fileMeasure.getDoubleValue()).isEqualTo(3.14, Offset.offset(0.001)); + assertThat(fileMeasure.getDoubleValue()).isEqualTo(3.1d); verify(measureRepository).add(eq(project), eq(stringMetric), measureCaptor.capture()); Measure projectMeasure = measureCaptor.getValue(); diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/step/PersistMeasuresStepTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/step/PersistMeasuresStepTest.java index ac7cb109e20..98d83419134 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/computation/step/PersistMeasuresStepTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/computation/step/PersistMeasuresStepTest.java @@ -188,7 +188,7 @@ public class PersistMeasuresStepTest extends BaseStepTest { assertThat(dto.get("metricId")).isEqualTo(doubleMetric.getId().longValue()); assertThat(dto.get("ruleId")).isEqualTo(rule.getId().longValue()); assertThat(dto.get("characteristicId")).isNull(); - assertThat(dto.get("value")).isEqualTo(123.123d); + assertThat(dto.get("value")).isEqualTo(123.1d); assertThat(dto.get("severity")).isNull(); } |