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.period.Period;
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;
42 public class CoverageUtilsTest {
44 private static final String SOME_METRIC_KEY = "some key";
45 public static final double DEFAULT_VARIATION = 0d;
48 public CounterInitializationContextRule fileAggregateContext = new CounterInitializationContextRule();
50 public ExpectedException expectedException = ExpectedException.none();
53 public void verify_calculate_coverage() {
54 assertThat(CoverageUtils.calculateCoverage(5, 10)).isEqualTo(50d);
58 public void getLongMeasureValue_returns_0_if_measure_does_not_exist() {
59 assertThat(getLongMeasureValue(fileAggregateContext, SOME_METRIC_KEY)).isEqualTo(0L);
63 public void getLongMeasureValue_returns_0_if_measure_is_NO_VALUE() {
64 fileAggregateContext.put(SOME_METRIC_KEY, newMeasureBuilder().createNoValue());
66 assertThat(getLongMeasureValue(fileAggregateContext, SOME_METRIC_KEY)).isEqualTo(0L);
70 public void getLongMeasureValue_returns_value_if_measure_is_INT() {
71 fileAggregateContext.put(SOME_METRIC_KEY, newMeasureBuilder().create(152));
73 assertThat(getLongMeasureValue(fileAggregateContext, SOME_METRIC_KEY)).isEqualTo(152L);
77 public void getLongMeasureValue_returns_value_if_measure_is_LONG() {
78 fileAggregateContext.put(SOME_METRIC_KEY, newMeasureBuilder().create(152L));
80 assertThat(getLongMeasureValue(fileAggregateContext, SOME_METRIC_KEY)).isEqualTo(152L);
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");
88 fileAggregateContext.put(SOME_METRIC_KEY, newMeasureBuilder().create(152d, 1));
90 getLongMeasureValue(fileAggregateContext, SOME_METRIC_KEY);
94 public void getMeasureVariations_returns_0_in_all_MeasureVariations_if_there_is_no_measure() {
95 assertThat(getMeasureVariations(fileAggregateContext, SOME_METRIC_KEY)).isEqualTo(DEFAULT_VARIATION);
99 public void getMeasureVariations_returns_0_in_all_MeasureVariations_if_there_is_measure_has_no_variations() {
100 fileAggregateContext.put(SOME_METRIC_KEY, newMeasureBuilder().createNoValue());
102 assertThat(getMeasureVariations(fileAggregateContext, SOME_METRIC_KEY)).isEqualTo(DEFAULT_VARIATION);
106 public void getMeasureVariations_returns_MeasureVariations_of_measure_when_it_has_one() {
107 fileAggregateContext.put(SOME_METRIC_KEY, newMeasureBuilder().setVariation(5d).createNoValue());
109 assertThat(getMeasureVariations(fileAggregateContext, SOME_METRIC_KEY)).isEqualTo(5d);
112 private static class CounterInitializationContextRule extends ExternalResource implements CounterInitializationContext {
113 private final Map<String, Measure> measures = new HashMap<>();
115 public CounterInitializationContextRule put(String metricKey, Measure measure) {
116 checkNotNull(metricKey);
117 checkNotNull(measure);
118 checkState(!measures.containsKey(metricKey));
119 measures.put(metricKey, measure);
124 protected void after() {
129 public Component getLeaf() {
130 throw new UnsupportedOperationException("getFile is not supported");
134 public Optional<Measure> getMeasure(String metricKey) {
135 return Optional.fromNullable(measures.get(metricKey));
139 public List<Period> getPeriods() {
140 throw new UnsupportedOperationException("getPeriods is not supported");
144 public Period getPeriod() {
145 throw new UnsupportedOperationException("getPeriod is not supported");
149 public boolean hasPeriod() {
150 throw new UnsupportedOperationException("hasPeriod is not supported");