]> source.dussan.org Git - sonarqube.git/blob
e41b31e8d601c85363fd05d3fe6d123b7078c9e7
[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.scanner.mediumtest.coverage;
21
22 import java.io.File;
23 import java.io.IOException;
24 import java.util.ArrayList;
25 import java.util.List;
26 import org.junit.Rule;
27 import org.junit.Test;
28 import org.sonar.api.batch.fs.InputFile;
29 import org.sonar.api.measures.CoreMetrics;
30 import org.sonar.scanner.mediumtest.ScannerMediumTester;
31 import org.sonar.scanner.mediumtest.AnalysisResult;
32 import org.sonar.xoo.XooPlugin;
33
34 import static org.assertj.core.api.Assertions.assertThat;
35 import static org.assertj.core.api.Assertions.tuple;
36
37 public class GenericCoverageMediumTest {
38   private final List<String> logs = new ArrayList<>();
39   
40   @Rule
41   public ScannerMediumTester tester = new ScannerMediumTester()
42     .registerPlugin("xoo", new XooPlugin())
43     .addDefaultQProfile("xoo", "Sonar Way");
44
45   @Test
46   public void singleReport() throws IOException {
47
48     File projectDir = new File("test-resources/mediumtest/xoo/sample-generic-coverage");
49
50     AnalysisResult result = tester
51       .setLogOutput((msg, level) -> logs.add(msg))
52       .newAnalysis(new File(projectDir, "sonar-project.properties"))
53       .property("sonar.coverageReportPaths", "coverage.xml")
54       .execute();
55
56     InputFile noConditions = result.inputFile("xources/hello/NoConditions.xoo");
57     assertThat(result.coverageFor(noConditions, 6).getHits()).isTrue();
58     assertThat(result.coverageFor(noConditions, 6).getConditions()).isEqualTo(0);
59     assertThat(result.coverageFor(noConditions, 6).getCoveredConditions()).isEqualTo(0);
60
61     assertThat(result.coverageFor(noConditions, 7).getHits()).isFalse();
62
63     assertThat(result.allMeasures().get(noConditions.key())).extracting("metricKey", "intValue.value", "stringValue.value")
64       .containsOnly(
65         tuple(CoreMetrics.LINES_TO_COVER_KEY, 2, ""),
66         tuple(CoreMetrics.UNCOVERED_LINES_KEY, 1, ""),
67         tuple(CoreMetrics.COVERAGE_LINE_HITS_DATA_KEY, 0, "6=1;7=0"));
68
69     InputFile withConditions = result.inputFile("xources/hello/WithConditions.xoo");
70     assertThat(result.coverageFor(withConditions, 3).getHits()).isTrue();
71     assertThat(result.coverageFor(withConditions, 3).getConditions()).isEqualTo(2);
72     assertThat(result.coverageFor(withConditions, 3).getCoveredConditions()).isEqualTo(1);
73
74     assertThat(result.allMeasures().get(withConditions.key())).extracting("metricKey", "intValue.value", "stringValue.value")
75       .containsOnly(
76         tuple(CoreMetrics.LINES_TO_COVER_KEY, 1, ""),
77         tuple(CoreMetrics.UNCOVERED_LINES_KEY, 0, ""),
78         tuple(CoreMetrics.COVERAGE_LINE_HITS_DATA_KEY, 0, "3=1"),
79         tuple(CoreMetrics.CONDITIONS_TO_COVER_KEY, 2, ""),
80         tuple(CoreMetrics.UNCOVERED_CONDITIONS_KEY, 1, ""),
81         tuple(CoreMetrics.CONDITIONS_BY_LINE_KEY, 0, "3=2"),
82         tuple(CoreMetrics.COVERED_CONDITIONS_BY_LINE_KEY, 0, "3=1")
83     );
84     assertThat(logs).noneMatch(l -> l.contains("Please use 'sonar.coverageReportPaths'"));
85
86   }
87   
88   @Test
89   public void warnAboutDeprecatedProperty() {
90     File projectDir = new File("test-resources/mediumtest/xoo/sample-generic-coverage");
91
92     tester
93       .setLogOutput((msg, level) -> logs.add(msg))
94       .newAnalysis(new File(projectDir, "sonar-project.properties"))
95       .property("sonar.genericcoverage.reportPath", "coverage.xml")
96       .execute();
97       
98       
99     assertThat(logs).anyMatch(l -> l.contains("Please use 'sonar.coverageReportPaths'"));
100   }
101
102   @Test
103   public void twoReports() throws IOException {
104
105     File projectDir = new File("test-resources/mediumtest/xoo/sample-generic-coverage");
106
107     AnalysisResult result = tester
108       .setLogOutput((msg, level) -> logs.add(msg))
109       .newAnalysis(new File(projectDir, "sonar-project.properties"))
110       .property("sonar.coverageReportPaths", "coverage.xml,coverage2.xml")
111       .execute();
112
113     InputFile noConditions = result.inputFile("xources/hello/NoConditions.xoo");
114     assertThat(result.coverageFor(noConditions, 6).getHits()).isTrue();
115     assertThat(result.coverageFor(noConditions, 6).getConditions()).isEqualTo(0);
116     assertThat(result.coverageFor(noConditions, 6).getCoveredConditions()).isEqualTo(0);
117
118     assertThat(result.coverageFor(noConditions, 7).getHits()).isTrue();
119
120     assertThat(result.allMeasures().get(noConditions.key())).extracting("metricKey", "intValue.value", "stringValue.value")
121       .containsOnly(
122         tuple(CoreMetrics.LINES_TO_COVER_KEY, 2, ""),
123         tuple(CoreMetrics.UNCOVERED_LINES_KEY, 0, ""),
124         tuple(CoreMetrics.COVERAGE_LINE_HITS_DATA_KEY, 0, "6=1;7=1"));
125
126     InputFile withConditions = result.inputFile("xources/hello/WithConditions.xoo");
127     assertThat(result.coverageFor(withConditions, 3).getHits()).isTrue();
128     assertThat(result.coverageFor(withConditions, 3).getConditions()).isEqualTo(2);
129     assertThat(result.coverageFor(withConditions, 3).getCoveredConditions()).isEqualTo(2);
130
131     assertThat(result.allMeasures().get(withConditions.key())).extracting("metricKey", "intValue.value", "stringValue.value")
132       .containsOnly(
133         tuple(CoreMetrics.LINES_TO_COVER_KEY, 1, ""),
134         tuple(CoreMetrics.UNCOVERED_LINES_KEY, 0, ""),
135         tuple(CoreMetrics.COVERAGE_LINE_HITS_DATA_KEY, 0, "3=2"),
136         tuple(CoreMetrics.CONDITIONS_TO_COVER_KEY, 2, ""),
137         tuple(CoreMetrics.UNCOVERED_CONDITIONS_KEY, 0, ""),
138         tuple(CoreMetrics.CONDITIONS_BY_LINE_KEY, 0, "3=2"),
139         tuple(CoreMetrics.COVERED_CONDITIONS_BY_LINE_KEY, 0, "3=2")
140     );
141     
142     assertThat(logs).noneMatch(l -> l.contains("Please use 'sonar.coverageReportPaths'"));
143   }
144   
145 }