3 * Copyright (C) 2009-2016 SonarSource SA
4 * mailto:contact 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.computation.task.projectanalysis.formula.coverage;
22 import com.google.common.base.Optional;
23 import java.util.HashMap;
24 import java.util.List;
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;
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;
44 public class CoverageUtilsTest {
46 private static final String SOME_METRIC_KEY = "some key";
47 public static final MeasureVariations DEFAULT_VARIATIONS = new MeasureVariations(0d, 0d, 0d, 0d, 0d);
50 public CounterInitializationContextRule fileAggregateContext = new CounterInitializationContextRule();
52 public ExpectedException expectedException = ExpectedException.none();
55 public void verify_calculate_coverage() {
56 assertThat(CoverageUtils.calculateCoverage(5, 10)).isEqualTo(50d);
60 public void getLongMeasureValue_returns_0_if_measure_does_not_exist() {
61 assertThat(getLongMeasureValue(fileAggregateContext, SOME_METRIC_KEY)).isEqualTo(0L);
65 public void getLongMeasureValue_returns_0_if_measure_is_NO_VALUE() {
66 fileAggregateContext.put(SOME_METRIC_KEY, newMeasureBuilder().createNoValue());
68 assertThat(getLongMeasureValue(fileAggregateContext, SOME_METRIC_KEY)).isEqualTo(0L);
72 public void getLongMeasureValue_returns_value_if_measure_is_INT() {
73 fileAggregateContext.put(SOME_METRIC_KEY, newMeasureBuilder().create(152));
75 assertThat(getLongMeasureValue(fileAggregateContext, SOME_METRIC_KEY)).isEqualTo(152L);
79 public void getLongMeasureValue_returns_value_if_measure_is_LONG() {
80 fileAggregateContext.put(SOME_METRIC_KEY, newMeasureBuilder().create(152L));
82 assertThat(getLongMeasureValue(fileAggregateContext, SOME_METRIC_KEY)).isEqualTo(152L);
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");
90 fileAggregateContext.put(SOME_METRIC_KEY, newMeasureBuilder().create(152d, 1));
92 getLongMeasureValue(fileAggregateContext, SOME_METRIC_KEY);
96 public void getMeasureVariations_returns_0_in_all_MeasureVariations_if_there_is_no_measure() {
97 assertThat(getMeasureVariations(fileAggregateContext, SOME_METRIC_KEY)).isEqualTo(DEFAULT_VARIATIONS);
101 public void getMeasureVariations_returns_0_in_all_MeasureVariations_if_there_is_measure_has_no_variations() {
102 fileAggregateContext.put(SOME_METRIC_KEY, newMeasureBuilder().createNoValue());
104 assertThat(getMeasureVariations(fileAggregateContext, SOME_METRIC_KEY)).isEqualTo(DEFAULT_VARIATIONS);
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());
112 assertThat(getMeasureVariations(fileAggregateContext, SOME_METRIC_KEY)).isSameAs(measureVariations);
116 public void getLongVariation_returns_0_if_MeasureVariation_has_none_for_the_specified_period() {
117 MeasureVariations variations = new MeasureVariations(null, 2d, null, null, 5d);
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);
126 private Period createPeriod(int periodIndex) {
127 return new Period(periodIndex, "mode" + periodIndex, null, 963L + periodIndex, String.valueOf(9865L + periodIndex));
130 private static class CounterInitializationContextRule extends ExternalResource implements CounterInitializationContext {
131 private final Map<String, Measure> measures = new HashMap<>();
133 public CounterInitializationContextRule put(String metricKey, Measure measure) {
134 checkNotNull(metricKey);
135 checkNotNull(measure);
136 checkState(!measures.containsKey(metricKey));
137 measures.put(metricKey, measure);
142 protected void after() {
147 public Component getLeaf() {
148 throw new UnsupportedOperationException("getFile is not supported");
152 public Optional<Measure> getMeasure(String metricKey) {
153 return Optional.fromNullable(measures.get(metricKey));
157 public List<Period> getPeriods() {
158 throw new UnsupportedOperationException("getPeriods is not supported");
162 public Period getPeriod() {
163 throw new UnsupportedOperationException("getPeriod is not supported");
167 public boolean hasPeriod() {
168 throw new UnsupportedOperationException("hasPeriod is not supported");