3 * Copyright (C) 2009-2024 SonarSource SA
4 * mailto:info AT sonarsource DOT com
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 3 of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 package org.sonar.server.measure.ws;
22 import org.junit.Test;
23 import org.sonar.api.measures.Metric;
24 import org.sonar.db.measure.MeasureDto;
25 import org.sonar.db.metric.MetricDto;
27 import static org.assertj.core.api.Assertions.assertThat;
28 import static org.assertj.core.api.Assertions.assertThatThrownBy;
29 import static org.sonar.api.measures.Metric.ValueType.BOOL;
30 import static org.sonar.api.measures.Metric.ValueType.DATA;
31 import static org.sonar.api.measures.Metric.ValueType.FLOAT;
32 import static org.sonar.api.measures.Metric.ValueType.INT;
33 import static org.sonar.api.measures.Metric.ValueType.MILLISEC;
34 import static org.sonar.api.measures.Metric.ValueType.PERCENT;
35 import static org.sonar.api.measures.Metric.ValueType.WORK_DUR;
36 import static org.sonar.db.metric.MetricTesting.newMetricDto;
37 import static org.sonar.server.measure.ws.MeasureValueFormatter.formatMeasureValue;
38 import static org.sonar.server.measure.ws.MeasureValueFormatter.formatNumericalValue;
40 public class MeasureValueFormatterTest {
44 public void test_formatNumericalValue() {
45 assertThat(formatNumericalValue(-1.0d, newMetric(BOOL))).isEqualTo("false");
46 assertThat(formatNumericalValue(1.0d, newMetric(BOOL))).isEqualTo("true");
47 assertThat(formatNumericalValue(1_000.123d, newMetric(FLOAT))).isEqualTo("1000.123");
48 assertThat(formatNumericalValue(1_000.0d, newMetric(INT))).isEqualTo("1000");
49 assertThat(formatNumericalValue(1_000.0d, newMetric(WORK_DUR))).isEqualTo("1000");
50 assertThat(formatNumericalValue(6_000_000_000_000.0d, newMetric(MILLISEC))).isEqualTo("6000000000000");
54 public void test_formatMeasureValue() {
55 assertThat(formatMeasureValue(newNumericMeasure(-1.0d), newMetric(BOOL))).isEqualTo("false");
56 assertThat(formatMeasureValue(newNumericMeasure(1.0d), newMetric(BOOL))).isEqualTo("true");
57 assertThat(formatMeasureValue(newNumericMeasure(1000.123d), newMetric(PERCENT))).isEqualTo("1000.123");
58 assertThat(formatMeasureValue(newNumericMeasure(1000.0d), newMetric(WORK_DUR))).isEqualTo("1000");
59 assertThat(formatMeasureValue(newNumericMeasure(6_000_000_000_000.0d), newMetric(MILLISEC))).isEqualTo("6000000000000");
60 assertThat(formatMeasureValue(newTextMeasure("text-value"), newMetric(DATA))).isEqualTo("text-value");
64 public void fail_if_text_value_type_for_numeric_formatter() {
65 assertThatThrownBy(() -> formatNumericalValue(42.0d, newMetric(DATA)))
66 .isInstanceOf(IllegalArgumentException.class)
67 .hasMessage("Unsupported metric type 'DATA' for numerical value");
70 private static MetricDto newMetric(Metric.ValueType valueType) {
71 return newMetricDto().setValueType(valueType.name());
74 private static MeasureDto newNumericMeasure(Double value) {
75 return new MeasureDto().setValue(value);
78 private static MeasureDto newTextMeasure(String data) {
79 return new MeasureDto().setData(data);