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;
22 import com.google.common.base.Optional;
23 import org.assertj.guava.api.Assertions;
24 import org.junit.Rule;
25 import org.junit.Test;
26 import org.junit.rules.ExpectedException;
27 import org.sonar.server.computation.task.projectanalysis.component.Component;
28 import org.sonar.server.computation.task.projectanalysis.component.ReportComponent;
29 import org.sonar.server.computation.task.projectanalysis.measure.Measure;
30 import org.sonar.server.computation.task.projectanalysis.metric.Metric;
31 import org.sonar.server.computation.task.projectanalysis.period.PeriodsHolder;
33 import static org.assertj.core.api.Assertions.assertThat;
34 import static org.mockito.Mockito.mock;
35 import static org.mockito.Mockito.when;
36 import static org.sonar.api.measures.CoreMetrics.FUNCTION_COMPLEXITY_DISTRIBUTION_KEY;
38 public class DistributionFormulaTest {
40 private static final DistributionFormula BASIC_DISTRIBUTION_FORMULA = new DistributionFormula(FUNCTION_COMPLEXITY_DISTRIBUTION_KEY);
43 public ExpectedException thrown = ExpectedException.none();
45 CounterInitializationContext counterInitializationContext = mock(CounterInitializationContext.class);
46 CreateMeasureContext projectCreateMeasureContext = new DumbCreateMeasureContext(
47 ReportComponent.builder(Component.Type.PROJECT, 1).build(), mock(Metric.class), mock(PeriodsHolder.class));
48 CreateMeasureContext fileCreateMeasureContext = new DumbCreateMeasureContext(
49 ReportComponent.builder(Component.Type.FILE, 1).build(), mock(Metric.class), mock(PeriodsHolder.class));
52 public void check_new_counter_class() {
53 assertThat(BASIC_DISTRIBUTION_FORMULA.createNewCounter().getClass()).isEqualTo(DistributionFormula.DistributionCounter.class);
57 public void fail_with_NPE_when_creating_counter_with_null_metric() {
58 thrown.expect(NullPointerException.class);
59 thrown.expectMessage("Metric key cannot be null");
61 new DistributionFormula(null);
65 public void check_output_metric_key_is_function_complexity_distribution() {
66 assertThat(BASIC_DISTRIBUTION_FORMULA.getOutputMetricKeys()).containsOnly(FUNCTION_COMPLEXITY_DISTRIBUTION_KEY);
70 public void create_measure() {
71 DistributionFormula.DistributionCounter counter = BASIC_DISTRIBUTION_FORMULA.createNewCounter();
72 addMeasure(FUNCTION_COMPLEXITY_DISTRIBUTION_KEY, "0=3;3=7;6=10");
73 counter.initialize(counterInitializationContext);
75 assertThat(BASIC_DISTRIBUTION_FORMULA.createMeasure(counter, projectCreateMeasureContext).get().getData()).isEqualTo("0=3;3=7;6=10");
79 public void create_measure_when_counter_is_aggregating_from_another_counter() {
80 DistributionFormula.DistributionCounter anotherCounter = BASIC_DISTRIBUTION_FORMULA.createNewCounter();
81 addMeasure(FUNCTION_COMPLEXITY_DISTRIBUTION_KEY, "0=3;3=7;6=10");
82 anotherCounter.initialize(counterInitializationContext);
84 DistributionFormula.DistributionCounter counter = BASIC_DISTRIBUTION_FORMULA.createNewCounter();
85 counter.aggregate(anotherCounter);
87 assertThat(BASIC_DISTRIBUTION_FORMULA.createMeasure(counter, projectCreateMeasureContext).get().getData()).isEqualTo("0=3;3=7;6=10");
91 public void create_no_measure_when_no_value() {
92 DistributionFormula.DistributionCounter counter = BASIC_DISTRIBUTION_FORMULA.createNewCounter();
93 when(counterInitializationContext.getMeasure(FUNCTION_COMPLEXITY_DISTRIBUTION_KEY)).thenReturn(Optional.<Measure>absent());
94 counter.initialize(counterInitializationContext);
96 Assertions.assertThat(BASIC_DISTRIBUTION_FORMULA.createMeasure(counter, projectCreateMeasureContext)).isAbsent();
100 public void not_create_measure_when_on_file() {
101 DistributionFormula.DistributionCounter counter = BASIC_DISTRIBUTION_FORMULA.createNewCounter();
102 addMeasure(FUNCTION_COMPLEXITY_DISTRIBUTION_KEY, "0=3;3=7;6=10");
103 counter.initialize(counterInitializationContext);
105 Assertions.assertThat(BASIC_DISTRIBUTION_FORMULA.createMeasure(counter, fileCreateMeasureContext)).isAbsent();
108 private void addMeasure(String metricKey, String value) {
109 when(counterInitializationContext.getMeasure(metricKey)).thenReturn(Optional.of(Measure.newMeasureBuilder().create(value)));