]> source.dussan.org Git - sonarqube.git/blob
6ddde6255350cd7fde710802544611667082ede5
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2019 SonarSource SA
4  * mailto:info 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.ce.task.projectanalysis.step;
21
22 import org.junit.Before;
23 import org.junit.Rule;
24 import org.junit.Test;
25 import org.sonar.api.measures.CoreMetrics;
26 import org.sonar.ce.task.projectanalysis.component.FileAttributes;
27 import org.sonar.ce.task.projectanalysis.component.TreeRootHolderRule;
28 import org.sonar.ce.task.projectanalysis.formula.coverage.LinesAndConditionsWithUncoveredMetricKeys;
29 import org.sonar.ce.task.projectanalysis.measure.MeasureRepoEntry;
30 import org.sonar.ce.task.projectanalysis.measure.MeasureRepositoryRule;
31 import org.sonar.ce.task.projectanalysis.metric.MetricRepositoryRule;
32 import org.sonar.ce.task.step.TestComputationStepContext;
33
34 import static org.assertj.core.api.Assertions.assertThat;
35 import static org.sonar.ce.task.projectanalysis.component.Component.Type.DIRECTORY;
36 import static org.sonar.ce.task.projectanalysis.component.Component.Type.FILE;
37 import static org.sonar.ce.task.projectanalysis.component.Component.Type.PROJECT;
38 import static org.sonar.ce.task.projectanalysis.component.ReportComponent.builder;
39 import static org.sonar.ce.task.projectanalysis.measure.Measure.newMeasureBuilder;
40 import static org.sonar.ce.task.projectanalysis.measure.MeasureRepoEntry.entryOf;
41 import static org.sonar.ce.task.projectanalysis.measure.MeasureRepoEntry.toEntries;
42
43 public class ReportCoverageMeasuresStepTest {
44   private static final int ROOT_REF = 1;
45   private static final int DIRECTORY_REF = 1234;
46   private static final int FILE_1_REF = 12341;
47   private static final int UNIT_TEST_FILE_REF = 12342;
48   private static final int FILE_2_REF = 12343;
49
50   @Rule
51   public TreeRootHolderRule treeRootHolder = new TreeRootHolderRule();
52
53   @Rule
54   public MetricRepositoryRule metricRepository = new MetricRepositoryRule()
55     .add(CoreMetrics.LINES_TO_COVER)
56     .add(CoreMetrics.CONDITIONS_TO_COVER)
57     .add(CoreMetrics.UNCOVERED_LINES)
58     .add(CoreMetrics.UNCOVERED_CONDITIONS)
59     .add(CoreMetrics.COVERAGE)
60     .add(CoreMetrics.BRANCH_COVERAGE)
61     .add(CoreMetrics.LINE_COVERAGE);
62   @Rule
63   public MeasureRepositoryRule measureRepository = MeasureRepositoryRule.create(treeRootHolder, metricRepository);
64
65   CoverageMeasuresStep underTest = new CoverageMeasuresStep(treeRootHolder, metricRepository, measureRepository);
66
67   @Before
68   public void setUp() throws Exception {
69     treeRootHolder.setRoot(
70       builder(PROJECT, ROOT_REF)
71         .addChildren(
72           builder(DIRECTORY, DIRECTORY_REF)
73             .addChildren(
74               builder(FILE, FILE_1_REF).build(),
75               builder(FILE, UNIT_TEST_FILE_REF).setFileAttributes(new FileAttributes(true, "some language", 1)).build(),
76               builder(FILE, FILE_2_REF).build())
77             .build())
78         .build());
79   }
80
81   @Test
82   public void verify_aggregates_values_for_ut_lines_and_conditions() {
83     LinesAndConditionsWithUncoveredMetricKeys metricKeys = new LinesAndConditionsWithUncoveredMetricKeys(
84       CoreMetrics.LINES_TO_COVER_KEY, CoreMetrics.CONDITIONS_TO_COVER_KEY,
85       CoreMetrics.UNCOVERED_LINES_KEY, CoreMetrics.UNCOVERED_CONDITIONS_KEY);
86     verify_lines_and_conditions_aggregates_values(metricKeys);
87   }
88
89   private void verify_lines_and_conditions_aggregates_values(LinesAndConditionsWithUncoveredMetricKeys metricKeys) {
90     measureRepository
91       .addRawMeasure(FILE_1_REF, metricKeys.getLines(), newMeasureBuilder().create(3000))
92       .addRawMeasure(FILE_1_REF, metricKeys.getConditions(), newMeasureBuilder().create(300))
93       .addRawMeasure(FILE_1_REF, metricKeys.getUncoveredLines(), newMeasureBuilder().create(30))
94       .addRawMeasure(FILE_1_REF, metricKeys.getUncoveredConditions(), newMeasureBuilder().create(9))
95
96       .addRawMeasure(FILE_2_REF, metricKeys.getLines(), newMeasureBuilder().create(2000))
97       .addRawMeasure(FILE_2_REF, metricKeys.getConditions(), newMeasureBuilder().create(400))
98       .addRawMeasure(FILE_2_REF, metricKeys.getUncoveredLines(), newMeasureBuilder().create(200))
99       .addRawMeasure(FILE_2_REF, metricKeys.getUncoveredConditions(), newMeasureBuilder().create(16));
100
101     underTest.execute(new TestComputationStepContext());
102
103     MeasureRepoEntry[] nonFileRepoEntries = {
104       entryOf(metricKeys.getLines(), newMeasureBuilder().create(5000)),
105       entryOf(metricKeys.getConditions(), newMeasureBuilder().create(700)),
106       entryOf(metricKeys.getUncoveredLines(), newMeasureBuilder().create(230)),
107       entryOf(metricKeys.getUncoveredConditions(), newMeasureBuilder().create(25))
108     };
109
110     assertThat(toEntries(measureRepository.getAddedRawMeasures(DIRECTORY_REF))).contains(nonFileRepoEntries);
111     assertThat(toEntries(measureRepository.getAddedRawMeasures(ROOT_REF))).contains(nonFileRepoEntries);
112   }
113
114   @Test
115   public void verify_aggregates_values_for_code_line_and_branch_coverage() {
116     LinesAndConditionsWithUncoveredMetricKeys metricKeys = new LinesAndConditionsWithUncoveredMetricKeys(
117       CoreMetrics.LINES_TO_COVER_KEY, CoreMetrics.CONDITIONS_TO_COVER_KEY,
118       CoreMetrics.UNCOVERED_LINES_KEY, CoreMetrics.UNCOVERED_CONDITIONS_KEY);
119     String codeCoverageKey = CoreMetrics.COVERAGE_KEY;
120     String lineCoverageKey = CoreMetrics.LINE_COVERAGE_KEY;
121     String branchCoverageKey = CoreMetrics.BRANCH_COVERAGE_KEY;
122
123     verify_coverage_aggregates_values(metricKeys, codeCoverageKey, lineCoverageKey, branchCoverageKey);
124   }
125
126   private void verify_coverage_aggregates_values(LinesAndConditionsWithUncoveredMetricKeys metricKeys, String codeCoverageKey, String lineCoverageKey, String branchCoverageKey) {
127     measureRepository
128       .addRawMeasure(FILE_1_REF, metricKeys.getLines(), newMeasureBuilder().create(3000))
129       .addRawMeasure(FILE_1_REF, metricKeys.getConditions(), newMeasureBuilder().create(300))
130       .addRawMeasure(FILE_1_REF, metricKeys.getUncoveredLines(), newMeasureBuilder().create(30))
131       .addRawMeasure(FILE_1_REF, metricKeys.getUncoveredConditions(), newMeasureBuilder().create(9))
132
133       .addRawMeasure(FILE_2_REF, metricKeys.getLines(), newMeasureBuilder().create(2000))
134       .addRawMeasure(FILE_2_REF, metricKeys.getConditions(), newMeasureBuilder().create(400))
135       .addRawMeasure(FILE_2_REF, metricKeys.getUncoveredLines(), newMeasureBuilder().create(200))
136       .addRawMeasure(FILE_2_REF, metricKeys.getUncoveredConditions(), newMeasureBuilder().create(16));
137
138     underTest.execute(new TestComputationStepContext());
139
140     assertThat(toEntries(measureRepository.getAddedRawMeasures(FILE_1_REF))).contains(
141       entryOf(codeCoverageKey, newMeasureBuilder().create(98.8d, 1)),
142       entryOf(lineCoverageKey, newMeasureBuilder().create(99d, 1)),
143       entryOf(branchCoverageKey, newMeasureBuilder().create(97d, 1)));
144     assertThat(toEntries(measureRepository.getAddedRawMeasures(FILE_2_REF))).contains(
145       entryOf(codeCoverageKey, newMeasureBuilder().create(91d, 1)),
146       entryOf(lineCoverageKey, newMeasureBuilder().create(90d, 1)),
147       entryOf(branchCoverageKey, newMeasureBuilder().create(96d, 1)));
148     assertThat(toEntries(measureRepository.getAddedRawMeasures(UNIT_TEST_FILE_REF))).isEmpty();
149
150     MeasureRepoEntry[] nonFileRepoEntries = {
151       entryOf(codeCoverageKey, newMeasureBuilder().create(95.5d, 1)),
152       entryOf(lineCoverageKey, newMeasureBuilder().create(95.4d, 1)),
153       entryOf(branchCoverageKey, newMeasureBuilder().create(96.4d, 1))
154     };
155
156     assertThat(toEntries(measureRepository.getAddedRawMeasures(DIRECTORY_REF))).contains(nonFileRepoEntries);
157     assertThat(toEntries(measureRepository.getAddedRawMeasures(ROOT_REF))).contains(nonFileRepoEntries);
158   }
159
160 }