]> source.dussan.org Git - sonarqube.git/blob
d8cdd2dc1cf64ce4940692a6f4806ab6828234ea
[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.period.Period;
34
35 import static com.google.common.base.Preconditions.checkNotNull;
36 import static com.google.common.base.Preconditions.checkState;
37 import static org.assertj.core.api.Assertions.assertThat;
38 import static org.sonar.server.computation.task.projectanalysis.formula.coverage.CoverageUtils.getLongMeasureValue;
39 import static org.sonar.server.computation.task.projectanalysis.formula.coverage.CoverageUtils.getMeasureVariations;
40 import static org.sonar.server.computation.task.projectanalysis.measure.Measure.newMeasureBuilder;
41
42 public class CoverageUtilsTest {
43
44   private static final String SOME_METRIC_KEY = "some key";
45   public static final double DEFAULT_VARIATION = 0d;
46
47   @Rule
48   public CounterInitializationContextRule fileAggregateContext = new CounterInitializationContextRule();
49   @Rule
50   public ExpectedException expectedException = ExpectedException.none();
51
52   @Test
53   public void verify_calculate_coverage() {
54     assertThat(CoverageUtils.calculateCoverage(5, 10)).isEqualTo(50d);
55   }
56
57   @Test
58   public void getLongMeasureValue_returns_0_if_measure_does_not_exist() {
59     assertThat(getLongMeasureValue(fileAggregateContext, SOME_METRIC_KEY)).isEqualTo(0L);
60   }
61
62   @Test
63   public void getLongMeasureValue_returns_0_if_measure_is_NO_VALUE() {
64     fileAggregateContext.put(SOME_METRIC_KEY, newMeasureBuilder().createNoValue());
65
66     assertThat(getLongMeasureValue(fileAggregateContext, SOME_METRIC_KEY)).isEqualTo(0L);
67   }
68
69   @Test
70   public void getLongMeasureValue_returns_value_if_measure_is_INT() {
71     fileAggregateContext.put(SOME_METRIC_KEY, newMeasureBuilder().create(152));
72
73     assertThat(getLongMeasureValue(fileAggregateContext, SOME_METRIC_KEY)).isEqualTo(152L);
74   }
75
76   @Test
77   public void getLongMeasureValue_returns_value_if_measure_is_LONG() {
78     fileAggregateContext.put(SOME_METRIC_KEY, newMeasureBuilder().create(152L));
79
80     assertThat(getLongMeasureValue(fileAggregateContext, SOME_METRIC_KEY)).isEqualTo(152L);
81   }
82
83   @Test
84   public void getLongMeasureValue_throws_ISE_if_measure_is_DOUBLE() {
85     expectedException.expect(IllegalStateException.class);
86     expectedException.expectMessage("value can not be converted to long because current value type is a DOUBLE");
87
88     fileAggregateContext.put(SOME_METRIC_KEY, newMeasureBuilder().create(152d, 1));
89
90     getLongMeasureValue(fileAggregateContext, SOME_METRIC_KEY);
91   }
92
93   @Test
94   public void getMeasureVariations_returns_0_in_all_MeasureVariations_if_there_is_no_measure() {
95     assertThat(getMeasureVariations(fileAggregateContext, SOME_METRIC_KEY)).isEqualTo(DEFAULT_VARIATION);
96   }
97
98   @Test
99   public void getMeasureVariations_returns_0_in_all_MeasureVariations_if_there_is_measure_has_no_variations() {
100     fileAggregateContext.put(SOME_METRIC_KEY, newMeasureBuilder().createNoValue());
101
102     assertThat(getMeasureVariations(fileAggregateContext, SOME_METRIC_KEY)).isEqualTo(DEFAULT_VARIATION);
103   }
104
105   @Test
106   public void getMeasureVariations_returns_MeasureVariations_of_measure_when_it_has_one() {
107     fileAggregateContext.put(SOME_METRIC_KEY, newMeasureBuilder().setVariation(5d).createNoValue());
108
109     assertThat(getMeasureVariations(fileAggregateContext, SOME_METRIC_KEY)).isEqualTo(5d);
110   }
111
112   private static class CounterInitializationContextRule extends ExternalResource implements CounterInitializationContext {
113     private final Map<String, Measure> measures = new HashMap<>();
114
115     public CounterInitializationContextRule put(String metricKey, Measure measure) {
116       checkNotNull(metricKey);
117       checkNotNull(measure);
118       checkState(!measures.containsKey(metricKey));
119       measures.put(metricKey, measure);
120       return this;
121     }
122
123     @Override
124     protected void after() {
125       measures.clear();
126     }
127
128     @Override
129     public Component getLeaf() {
130       throw new UnsupportedOperationException("getFile is not supported");
131     }
132
133     @Override
134     public Optional<Measure> getMeasure(String metricKey) {
135       return Optional.fromNullable(measures.get(metricKey));
136     }
137
138     @Override
139     public List<Period> getPeriods() {
140       throw new UnsupportedOperationException("getPeriods is not supported");
141     }
142
143     @Override
144     public Period getPeriod() {
145       throw new UnsupportedOperationException("getPeriod is not supported");
146     }
147
148     @Override
149     public boolean hasPeriod() {
150       throw new UnsupportedOperationException("hasPeriod is not supported");
151     }
152   }
153 }