]> source.dussan.org Git - sonarqube.git/blob
7102575159c98ff5dcb1602fcc2cba6ee9efd508
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2024 SonarSource SA
4  * mailto:info AT sonarsource DOT com
5  *
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.
10  *
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.
15  *
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.
19  */
20 package org.sonar.ce.task.projectanalysis.formula.coverage;
21
22 import java.util.HashMap;
23 import java.util.Map;
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;
31
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;
38
39 public class CoverageUtilsTest {
40
41   private static final String SOME_METRIC_KEY = "some key";
42   public static final double DEFAULT_VARIATION = 0d;
43
44   @Rule
45   public CounterInitializationContextRule fileAggregateContext = new CounterInitializationContextRule();
46
47   @Test
48   public void verify_calculate_coverage() {
49     assertThat(CoverageUtils.calculateCoverage(5, 10)).isEqualTo(50d);
50   }
51
52   @Test
53   public void getLongMeasureValue_returns_0_if_measure_does_not_exist() {
54     assertThat(getLongMeasureValue(fileAggregateContext, SOME_METRIC_KEY)).isZero();
55   }
56
57   @Test
58   public void getLongMeasureValue_returns_0_if_measure_is_NO_VALUE() {
59     fileAggregateContext.put(SOME_METRIC_KEY, newMeasureBuilder().createNoValue());
60
61     assertThat(getLongMeasureValue(fileAggregateContext, SOME_METRIC_KEY)).isZero();
62   }
63
64   @Test
65   public void getLongMeasureValue_returns_value_if_measure_is_INT() {
66     fileAggregateContext.put(SOME_METRIC_KEY, newMeasureBuilder().create(152));
67
68     assertThat(getLongMeasureValue(fileAggregateContext, SOME_METRIC_KEY)).isEqualTo(152L);
69   }
70
71   @Test
72   public void getLongMeasureValue_returns_value_if_measure_is_LONG() {
73     fileAggregateContext.put(SOME_METRIC_KEY, newMeasureBuilder().create(152L));
74
75     assertThat(getLongMeasureValue(fileAggregateContext, SOME_METRIC_KEY)).isEqualTo(152L);
76   }
77
78   @Test
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);
83     })
84       .isInstanceOf(IllegalStateException.class)
85       .hasMessage("value can not be converted to long because current value type is a DOUBLE");
86   }
87
88   private static class CounterInitializationContextRule extends ExternalResource implements CounterInitializationContext {
89     private final Map<String, Measure> measures = new HashMap<>();
90
91     public CounterInitializationContextRule put(String metricKey, Measure measure) {
92       checkNotNull(metricKey);
93       checkNotNull(measure);
94       checkState(!measures.containsKey(metricKey));
95       measures.put(metricKey, measure);
96       return this;
97     }
98
99     @Override
100     protected void after() {
101       measures.clear();
102     }
103
104     @Override
105     public Component getLeaf() {
106       throw new UnsupportedOperationException("getFile is not supported");
107     }
108
109     @Override
110     public Optional<Measure> getMeasure(String metricKey) {
111       return Optional.ofNullable(measures.get(metricKey));
112     }
113
114   }
115 }