]> source.dussan.org Git - sonarqube.git/blob
2a61210df6d2c4bbc050c232e88a1ac99b4ac722
[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;
21
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;
32
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;
37
38 public class DistributionFormulaTest {
39
40   private static final DistributionFormula BASIC_DISTRIBUTION_FORMULA = new DistributionFormula(FUNCTION_COMPLEXITY_DISTRIBUTION_KEY);
41
42   @Rule
43   public ExpectedException thrown = ExpectedException.none();
44
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));
50
51   @Test
52   public void check_new_counter_class() {
53     assertThat(BASIC_DISTRIBUTION_FORMULA.createNewCounter().getClass()).isEqualTo(DistributionFormula.DistributionCounter.class);
54   }
55
56   @Test
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");
60
61     new DistributionFormula(null);
62   }
63
64   @Test
65   public void check_output_metric_key_is_function_complexity_distribution() {
66     assertThat(BASIC_DISTRIBUTION_FORMULA.getOutputMetricKeys()).containsOnly(FUNCTION_COMPLEXITY_DISTRIBUTION_KEY);
67   }
68
69   @Test
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);
74
75     assertThat(BASIC_DISTRIBUTION_FORMULA.createMeasure(counter, projectCreateMeasureContext).get().getData()).isEqualTo("0=3;3=7;6=10");
76   }
77
78   @Test
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);
83
84     DistributionFormula.DistributionCounter counter = BASIC_DISTRIBUTION_FORMULA.createNewCounter();
85     counter.aggregate(anotherCounter);
86
87     assertThat(BASIC_DISTRIBUTION_FORMULA.createMeasure(counter, projectCreateMeasureContext).get().getData()).isEqualTo("0=3;3=7;6=10");
88   }
89
90   @Test
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);
95
96     Assertions.assertThat(BASIC_DISTRIBUTION_FORMULA.createMeasure(counter, projectCreateMeasureContext)).isAbsent();
97   }
98
99   @Test
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);
104
105     Assertions.assertThat(BASIC_DISTRIBUTION_FORMULA.createMeasure(counter, fileCreateMeasureContext)).isAbsent();
106   }
107
108   private void addMeasure(String metricKey, String value) {
109     when(counterInitializationContext.getMeasure(metricKey)).thenReturn(Optional.of(Measure.newMeasureBuilder().create(value)));
110   }
111
112 }