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.ce.task.projectanalysis.formula.coverage;
22 import java.util.HashMap;
24 import java.util.Optional;
25 import org.junit.Rule;
26 import org.junit.Test;
27 import org.junit.rules.ExternalResource;
28 import org.sonar.ce.task.projectanalysis.component.Component;
29 import org.sonar.ce.task.projectanalysis.formula.CounterInitializationContext;
30 import org.sonar.ce.task.projectanalysis.measure.Measure;
32 import static com.google.common.base.Preconditions.checkNotNull;
33 import static com.google.common.base.Preconditions.checkState;
34 import static org.assertj.core.api.Assertions.assertThat;
35 import static org.assertj.core.api.Assertions.assertThatThrownBy;
36 import static org.sonar.ce.task.projectanalysis.formula.coverage.CoverageUtils.getLongMeasureValue;
37 import static org.sonar.ce.task.projectanalysis.measure.Measure.newMeasureBuilder;
39 public class CoverageUtilsTest {
41 private static final String SOME_METRIC_KEY = "some key";
42 public static final double DEFAULT_VARIATION = 0d;
45 public CounterInitializationContextRule fileAggregateContext = new CounterInitializationContextRule();
48 public void verify_calculate_coverage() {
49 assertThat(CoverageUtils.calculateCoverage(5, 10)).isEqualTo(50d);
53 public void getLongMeasureValue_returns_0_if_measure_does_not_exist() {
54 assertThat(getLongMeasureValue(fileAggregateContext, SOME_METRIC_KEY)).isZero();
58 public void getLongMeasureValue_returns_0_if_measure_is_NO_VALUE() {
59 fileAggregateContext.put(SOME_METRIC_KEY, newMeasureBuilder().createNoValue());
61 assertThat(getLongMeasureValue(fileAggregateContext, SOME_METRIC_KEY)).isZero();
65 public void getLongMeasureValue_returns_value_if_measure_is_INT() {
66 fileAggregateContext.put(SOME_METRIC_KEY, newMeasureBuilder().create(152));
68 assertThat(getLongMeasureValue(fileAggregateContext, SOME_METRIC_KEY)).isEqualTo(152L);
72 public void getLongMeasureValue_returns_value_if_measure_is_LONG() {
73 fileAggregateContext.put(SOME_METRIC_KEY, newMeasureBuilder().create(152L));
75 assertThat(getLongMeasureValue(fileAggregateContext, SOME_METRIC_KEY)).isEqualTo(152L);
79 public void getLongMeasureValue_throws_ISE_if_measure_is_DOUBLE() {
80 assertThatThrownBy(() -> {
81 fileAggregateContext.put(SOME_METRIC_KEY, newMeasureBuilder().create(152d, 1));
82 getLongMeasureValue(fileAggregateContext, SOME_METRIC_KEY);
84 .isInstanceOf(IllegalStateException.class)
85 .hasMessage("value can not be converted to long because current value type is a DOUBLE");
88 private static class CounterInitializationContextRule extends ExternalResource implements CounterInitializationContext {
89 private final Map<String, Measure> measures = new HashMap<>();
91 public CounterInitializationContextRule put(String metricKey, Measure measure) {
92 checkNotNull(metricKey);
93 checkNotNull(measure);
94 checkState(!measures.containsKey(metricKey));
95 measures.put(metricKey, measure);
100 protected void after() {
105 public Component getLeaf() {
106 throw new UnsupportedOperationException("getFile is not supported");
110 public Optional<Measure> getMeasure(String metricKey) {
111 return Optional.ofNullable(measures.get(metricKey));