3 * Copyright (C) 2009-2019 SonarSource SA
4 * mailto:info 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.scanner.mediumtest.coverage;
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;
34 import static org.assertj.core.api.Assertions.assertThat;
35 import static org.assertj.core.api.Assertions.tuple;
37 public class GenericCoverageMediumTest {
38 private final List<String> logs = new ArrayList<>();
41 public ScannerMediumTester tester = new ScannerMediumTester()
42 .registerPlugin("xoo", new XooPlugin())
43 .addDefaultQProfile("xoo", "Sonar Way");
46 public void singleReport() throws IOException {
48 File projectDir = new File("test-resources/mediumtest/xoo/sample-generic-coverage");
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")
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);
61 assertThat(result.coverageFor(noConditions, 7).getHits()).isFalse();
63 assertThat(result.allMeasures().get(noConditions.key())).extracting("metricKey", "intValue.value", "stringValue.value")
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"));
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);
74 assertThat(result.allMeasures().get(withConditions.key())).extracting("metricKey", "intValue.value", "stringValue.value")
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")
84 assertThat(logs).noneMatch(l -> l.contains("Please use 'sonar.coverageReportPaths'"));
89 public void warnAboutDeprecatedProperty() {
90 File projectDir = new File("test-resources/mediumtest/xoo/sample-generic-coverage");
93 .setLogOutput((msg, level) -> logs.add(msg))
94 .newAnalysis(new File(projectDir, "sonar-project.properties"))
95 .property("sonar.genericcoverage.reportPath", "coverage.xml")
99 assertThat(logs).anyMatch(l -> l.contains("Please use 'sonar.coverageReportPaths'"));
103 public void twoReports() throws IOException {
105 File projectDir = new File("test-resources/mediumtest/xoo/sample-generic-coverage");
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")
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);
118 assertThat(result.coverageFor(noConditions, 7).getHits()).isTrue();
120 assertThat(result.allMeasures().get(noConditions.key())).extracting("metricKey", "intValue.value", "stringValue.value")
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"));
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);
131 assertThat(result.allMeasures().get(withConditions.key())).extracting("metricKey", "intValue.value", "stringValue.value")
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")
142 assertThat(logs).noneMatch(l -> l.contains("Please use 'sonar.coverageReportPaths'"));