]> source.dussan.org Git - sonarqube.git/blob
2116ec61e0573228fbd07b15f24a73f52db9f893
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2016 SonarSource SA
4  * mailto:contact 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.server.computation.task.projectanalysis.formula.coverage;
21
22 import com.google.common.base.Optional;
23 import java.util.HashMap;
24 import java.util.List;
25 import java.util.Map;
26 import org.junit.Rule;
27 import org.junit.Test;
28 import org.junit.rules.ExpectedException;
29 import org.junit.rules.ExternalResource;
30 import org.sonar.server.computation.task.projectanalysis.component.Component;
31 import org.sonar.server.computation.task.projectanalysis.formula.CounterInitializationContext;
32 import org.sonar.server.computation.task.projectanalysis.measure.Measure;
33 import org.sonar.server.computation.task.projectanalysis.measure.MeasureVariations;
34 import org.sonar.server.computation.task.projectanalysis.period.Period;
35
36 import static com.google.common.base.Preconditions.checkNotNull;
37 import static com.google.common.base.Preconditions.checkState;
38 import static org.assertj.core.api.Assertions.assertThat;
39 import static org.sonar.server.computation.task.projectanalysis.formula.coverage.CoverageUtils.getLongMeasureValue;
40 import static org.sonar.server.computation.task.projectanalysis.formula.coverage.CoverageUtils.getLongVariation;
41 import static org.sonar.server.computation.task.projectanalysis.formula.coverage.CoverageUtils.getMeasureVariations;
42 import static org.sonar.server.computation.task.projectanalysis.measure.Measure.newMeasureBuilder;
43
44 public class CoverageUtilsTest {
45
46   private static final String SOME_METRIC_KEY = "some key";
47   public static final MeasureVariations DEFAULT_VARIATIONS = new MeasureVariations(0d, 0d, 0d, 0d, 0d);
48
49   @Rule
50   public CounterInitializationContextRule fileAggregateContext = new CounterInitializationContextRule();
51   @Rule
52   public ExpectedException expectedException = ExpectedException.none();
53
54   @Test
55   public void verify_calculate_coverage() {
56     assertThat(CoverageUtils.calculateCoverage(5, 10)).isEqualTo(50d);
57   }
58
59   @Test
60   public void getLongMeasureValue_returns_0_if_measure_does_not_exist() {
61     assertThat(getLongMeasureValue(fileAggregateContext, SOME_METRIC_KEY)).isEqualTo(0L);
62   }
63
64   @Test
65   public void getLongMeasureValue_returns_0_if_measure_is_NO_VALUE() {
66     fileAggregateContext.put(SOME_METRIC_KEY, newMeasureBuilder().createNoValue());
67
68     assertThat(getLongMeasureValue(fileAggregateContext, SOME_METRIC_KEY)).isEqualTo(0L);
69   }
70
71   @Test
72   public void getLongMeasureValue_returns_value_if_measure_is_INT() {
73     fileAggregateContext.put(SOME_METRIC_KEY, newMeasureBuilder().create(152));
74
75     assertThat(getLongMeasureValue(fileAggregateContext, SOME_METRIC_KEY)).isEqualTo(152L);
76   }
77
78   @Test
79   public void getLongMeasureValue_returns_value_if_measure_is_LONG() {
80     fileAggregateContext.put(SOME_METRIC_KEY, newMeasureBuilder().create(152L));
81
82     assertThat(getLongMeasureValue(fileAggregateContext, SOME_METRIC_KEY)).isEqualTo(152L);
83   }
84
85   @Test
86   public void getLongMeasureValue_throws_ISE_if_measure_is_DOUBLE() {
87     expectedException.expect(IllegalStateException.class);
88     expectedException.expectMessage("value can not be converted to long because current value type is a DOUBLE");
89
90     fileAggregateContext.put(SOME_METRIC_KEY, newMeasureBuilder().create(152d, 1));
91
92     getLongMeasureValue(fileAggregateContext, SOME_METRIC_KEY);
93   }
94
95   @Test
96   public void getMeasureVariations_returns_0_in_all_MeasureVariations_if_there_is_no_measure() {
97     assertThat(getMeasureVariations(fileAggregateContext, SOME_METRIC_KEY)).isEqualTo(DEFAULT_VARIATIONS);
98   }
99
100   @Test
101   public void getMeasureVariations_returns_0_in_all_MeasureVariations_if_there_is_measure_has_no_variations() {
102     fileAggregateContext.put(SOME_METRIC_KEY, newMeasureBuilder().createNoValue());
103
104     assertThat(getMeasureVariations(fileAggregateContext, SOME_METRIC_KEY)).isEqualTo(DEFAULT_VARIATIONS);
105   }
106
107   @Test
108   public void getMeasureVariations_returns_MeasureVariations_of_measure_when_it_has_one() {
109     MeasureVariations measureVariations = new MeasureVariations(null, 5d, null, null);
110     fileAggregateContext.put(SOME_METRIC_KEY, newMeasureBuilder().setVariations(measureVariations).createNoValue());
111
112     assertThat(getMeasureVariations(fileAggregateContext, SOME_METRIC_KEY)).isSameAs(measureVariations);
113   }
114
115   @Test
116   public void getLongVariation_returns_0_if_MeasureVariation_has_none_for_the_specified_period() {
117     MeasureVariations variations = new MeasureVariations(null, 2d, null, null, 5d);
118
119     assertThat(getLongVariation(variations, createPeriod(1))).isEqualTo(0L);
120     assertThat(getLongVariation(variations, createPeriod(2))).isEqualTo(2L);
121     assertThat(getLongVariation(variations, createPeriod(3))).isEqualTo(0L);
122     assertThat(getLongVariation(variations, createPeriod(4))).isEqualTo(0L);
123     assertThat(getLongVariation(variations, createPeriod(5))).isEqualTo(5L);
124   }
125
126   private Period createPeriod(int periodIndex) {
127     return new Period(periodIndex, "mode" + periodIndex, null, 963L + periodIndex, String.valueOf(9865L + periodIndex));
128   }
129
130   private static class CounterInitializationContextRule extends ExternalResource implements CounterInitializationContext {
131     private final Map<String, Measure> measures = new HashMap<>();
132
133     public CounterInitializationContextRule put(String metricKey, Measure measure) {
134       checkNotNull(metricKey);
135       checkNotNull(measure);
136       checkState(!measures.containsKey(metricKey));
137       measures.put(metricKey, measure);
138       return this;
139     }
140
141     @Override
142     protected void after() {
143       measures.clear();
144     }
145
146     @Override
147     public Component getLeaf() {
148       throw new UnsupportedOperationException("getFile is not supported");
149     }
150
151     @Override
152     public Optional<Measure> getMeasure(String metricKey) {
153       return Optional.fromNullable(measures.get(metricKey));
154     }
155
156     @Override
157     public List<Period> getPeriods() {
158       throw new UnsupportedOperationException("getPeriods is not supported");
159     }
160
161     @Override
162     public Period getPeriod() {
163       throw new UnsupportedOperationException("getPeriod is not supported");
164     }
165
166     @Override
167     public boolean hasPeriod() {
168       throw new UnsupportedOperationException("hasPeriod is not supported");
169     }
170   }
171 }