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.scanner.mediumtest.AnalysisResult;
30 import org.sonar.scanner.mediumtest.ScannerMediumTester;
31 import org.sonar.xoo.XooPlugin;
33 import static org.assertj.core.api.Assertions.assertThat;
35 public class GenericCoverageMediumTest {
36 private final List<String> logs = new ArrayList<>();
39 public ScannerMediumTester tester = new ScannerMediumTester()
40 .registerPlugin("xoo", new XooPlugin())
41 .addDefaultQProfile("xoo", "Sonar Way");
44 public void singleReport() throws IOException {
46 File projectDir = new File("test-resources/mediumtest/xoo/sample-generic-coverage");
48 AnalysisResult result = tester
49 .setLogOutput((msg, level) -> logs.add(msg))
50 .newAnalysis(new File(projectDir, "sonar-project.properties"))
51 .property("sonar.coverageReportPaths", "coverage.xml")
54 InputFile noConditions = result.inputFile("xources/hello/NoConditions.xoo");
55 assertThat(result.coverageFor(noConditions, 6).getHits()).isTrue();
56 assertThat(result.coverageFor(noConditions, 6).getConditions()).isEqualTo(0);
57 assertThat(result.coverageFor(noConditions, 6).getCoveredConditions()).isEqualTo(0);
59 assertThat(result.coverageFor(noConditions, 7).getHits()).isFalse();
61 InputFile withConditions = result.inputFile("xources/hello/WithConditions.xoo");
62 assertThat(result.coverageFor(withConditions, 3).getHits()).isTrue();
63 assertThat(result.coverageFor(withConditions, 3).getConditions()).isEqualTo(2);
64 assertThat(result.coverageFor(withConditions, 3).getCoveredConditions()).isEqualTo(1);
66 assertThat(logs).noneMatch(l -> l.contains("Please use 'sonar.coverageReportPaths'"));
71 public void warnAboutDeprecatedProperty() {
72 File projectDir = new File("test-resources/mediumtest/xoo/sample-generic-coverage");
75 .setLogOutput((msg, level) -> logs.add(msg))
76 .newAnalysis(new File(projectDir, "sonar-project.properties"))
77 .property("sonar.genericcoverage.reportPath", "coverage.xml")
81 assertThat(logs).anyMatch(l -> l.contains("Please use 'sonar.coverageReportPaths'"));
85 public void twoReports() throws IOException {
87 File projectDir = new File("test-resources/mediumtest/xoo/sample-generic-coverage");
89 AnalysisResult result = tester
90 .setLogOutput((msg, level) -> logs.add(msg))
91 .newAnalysis(new File(projectDir, "sonar-project.properties"))
92 .property("sonar.coverageReportPaths", "coverage.xml,coverage2.xml")
95 InputFile noConditions = result.inputFile("xources/hello/NoConditions.xoo");
96 assertThat(result.coverageFor(noConditions, 6).getHits()).isTrue();
97 assertThat(result.coverageFor(noConditions, 6).getConditions()).isEqualTo(0);
98 assertThat(result.coverageFor(noConditions, 6).getCoveredConditions()).isEqualTo(0);
100 assertThat(result.coverageFor(noConditions, 7).getHits()).isTrue();
102 InputFile withConditions = result.inputFile("xources/hello/WithConditions.xoo");
103 assertThat(result.coverageFor(withConditions, 3).getHits()).isTrue();
104 assertThat(result.coverageFor(withConditions, 3).getConditions()).isEqualTo(2);
105 assertThat(result.coverageFor(withConditions, 3).getCoveredConditions()).isEqualTo(2);
107 assertThat(logs).noneMatch(l -> l.contains("Please use 'sonar.coverageReportPaths'"));