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.genericcoverage;
23 import java.nio.charset.StandardCharsets;
24 import org.apache.commons.io.FileUtils;
25 import org.junit.Before;
26 import org.junit.Rule;
27 import org.junit.Test;
28 import org.junit.rules.TemporaryFolder;
29 import org.sonar.api.batch.fs.InputFile;
30 import org.sonar.api.utils.MessageException;
31 import org.sonar.api.impl.fs.DefaultInputFile;
32 import org.sonar.api.impl.fs.TestInputFileBuilder;
33 import org.sonar.api.impl.sensor.SensorContextTester;
35 import static org.assertj.core.api.Assertions.assertThat;
37 public class GenericCoverageReportParserTest {
40 public TemporaryFolder temp = new TemporaryFolder();
42 private DefaultInputFile fileWithBranches;
43 private DefaultInputFile fileWithoutBranch;
44 private DefaultInputFile emptyFile;
45 private SensorContextTester context;
48 public void before() {
49 context = SensorContextTester.create(new File(""));
50 fileWithBranches = setupFile("src/main/java/com/example/ClassWithBranches.java");
51 fileWithoutBranch = setupFile("src/main/java/com/example/ClassWithoutBranch.java");
52 emptyFile = setupFile("src/main/java/com/example/EmptyClass.java");
56 public void empty_file() throws Exception {
57 addFileToFs(emptyFile);
58 GenericCoverageReportParser parser = new GenericCoverageReportParser();
59 parser.parse(new File(this.getClass().getResource("coverage.xml").toURI()), context);
60 assertThat(parser.numberOfMatchedFiles()).isEqualTo(1);
61 assertThat(parser.numberOfUnknownFiles()).isEqualTo(3);
62 assertThat(parser.firstUnknownFiles()).hasSize(3);
66 public void file_without_branch() throws Exception {
67 addFileToFs(fileWithoutBranch);
68 GenericCoverageReportParser parser = new GenericCoverageReportParser();
69 parser.parse(new File(this.getClass().getResource("coverage.xml").toURI()), context);
70 assertThat(parser.numberOfMatchedFiles()).isEqualTo(1);
72 assertThat(context.lineHits(fileWithoutBranch.key(), 2)).isEqualTo(0);
73 assertThat(context.lineHits(fileWithoutBranch.key(), 3)).isEqualTo(1);
74 assertThat(context.lineHits(fileWithoutBranch.key(), 4)).isNull();
75 assertThat(context.lineHits(fileWithoutBranch.key(), 5)).isEqualTo(1);
76 assertThat(context.lineHits(fileWithoutBranch.key(), 6)).isEqualTo(0);
80 public void file_with_branches() throws Exception {
81 addFileToFs(fileWithBranches);
82 GenericCoverageReportParser parser = new GenericCoverageReportParser();
83 parser.parse(new File(this.getClass().getResource("coverage.xml").toURI()), context);
84 assertThat(parser.numberOfMatchedFiles()).isEqualTo(1);
86 assertThat(context.lineHits(fileWithBranches.key(), 3)).isEqualTo(1);
87 assertThat(context.lineHits(fileWithBranches.key(), 4)).isEqualTo(1);
89 assertThat(context.conditions(fileWithBranches.key(), 3)).isEqualTo(8);
90 assertThat(context.conditions(fileWithBranches.key(), 4)).isEqualTo(2);
92 assertThat(context.coveredConditions(fileWithBranches.key(), 3)).isEqualTo(5);
93 assertThat(context.coveredConditions(fileWithBranches.key(), 4)).isEqualTo(0);
96 @Test(expected = MessageException.class)
97 public void coverage_invalid_root_node_name() throws Exception {
98 parseCoverageReport("<mycoverage version=\"1\"></mycoverage>");
101 @Test(expected = MessageException.class)
102 public void coverage_invalid_report_version() throws Exception {
103 parseCoverageReport("<coverage version=\"2\"></coverage>");
106 @Test(expected = MessageException.class)
107 public void coverage_no_report_version() throws Exception {
108 parseCoverageReport("<coverage></coverage>");
111 @Test(expected = MessageException.class)
112 public void coverage_invalid_file_node_name() throws Exception {
113 parseCoverageReport("<coverage version=\"1\"><xx></xx></coverage>");
116 @Test(expected = MessageException.class)
117 public void unitTest_invalid_file_node_name() throws Exception {
118 parseCoverageReport("<unitTest version=\"1\"><xx></xx></unitTest>");
121 @Test(expected = MessageException.class)
122 public void coverage_missing_path_attribute() throws Exception {
123 parseCoverageReport("<coverage version=\"1\"><file></file></coverage>");
126 @Test(expected = MessageException.class)
127 public void unitTest_missing_path_attribute() throws Exception {
128 parseCoverageReport("<unitTest version=\"1\"><file></file></unitTest>");
131 @Test(expected = MessageException.class)
132 public void coverage_invalid_lineToCover_node_name() throws Exception {
133 addFileToFs(setupFile("file1"));
134 parseCoverageReport("<coverage version=\"1\"><file path=\"file1\"><xx/></file></coverage>");
137 @Test(expected = MessageException.class)
138 public void coverage_missing_lineNumber_in_lineToCover() throws Exception {
139 addFileToFs(setupFile("file1"));
140 parseCoverageReport("<coverage version=\"1\"><file path=\"file1\"><lineToCover covered=\"true\"/></file></coverage>");
143 @Test(expected = MessageException.class)
144 public void coverage_lineNumber_in_lineToCover_should_be_a_number() throws Exception {
145 addFileToFs(setupFile("file1"));
146 parseCoverageReport("<coverage version=\"1\"><file path=\"file1\"><lineToCover lineNumber=\"x\" covered=\"true\"/></file></coverage>");
149 @Test(expected = MessageException.class)
150 public void coverage_lineNumber_in_lineToCover_should_be_positive() throws Exception {
151 addFileToFs(setupFile("file1"));
152 parseCoverageReport("<coverage version=\"1\"><file path=\"file1\"><lineToCover lineNumber=\"0\" covered=\"true\"/></file></coverage>");
156 public void coverage_lineNumber_in_lineToCover_can_appear_several_times_for_same_file() throws Exception {
157 addFileToFs(setupFile("file1"));
158 parseCoverageReport("<coverage version=\"1\"><file path=\"file1\">"
159 + "<lineToCover lineNumber=\"1\" covered=\"true\"/>"
160 + "<lineToCover lineNumber=\"1\" covered=\"true\"/></file></coverage>");
163 @Test(expected = MessageException.class)
164 public void coverage_missing_covered_in_lineToCover() throws Exception {
165 addFileToFs(setupFile("file1"));
166 parseCoverageReport("<coverage version=\"1\"><file path=\"file1\"><lineToCover lineNumber=\"3\"/></file></coverage>");
169 @Test(expected = MessageException.class)
170 public void coverage_covered_in_lineToCover_should_be_a_boolean() throws Exception {
171 addFileToFs(setupFile("file1"));
172 parseCoverageReport("<coverage version=\"1\"><file path=\"file1\"><lineToCover lineNumber=\"3\" covered=\"x\"/></file></coverage>");
175 @Test(expected = MessageException.class)
176 public void coverage_branchesToCover_in_lineToCover_should_be_a_number() throws Exception {
177 addFileToFs(setupFile("file1"));
178 parseCoverageReport("<coverage version=\"1\"><file path=\"file1\">"
179 + "<lineToCover lineNumber=\"1\" covered=\"true\" branchesToCover=\"x\"/></file></coverage>");
182 @Test(expected = MessageException.class)
183 public void coverage_branchesToCover_in_lineToCover_should_not_be_negative() throws Exception {
184 addFileToFs(setupFile("file1"));
185 parseCoverageReport("<coverage version=\"1\"><file path=\"file1\">"
186 + "<lineToCover lineNumber=\"1\" covered=\"true\" branchesToCover=\"-1\"/></file></coverage>");
189 @Test(expected = MessageException.class)
190 public void coverage_coveredBranches_in_lineToCover_should_be_a_number() throws Exception {
191 addFileToFs(setupFile("file1"));
192 parseCoverageReport("<coverage version=\"1\"><file path=\"file1\">"
193 + "<lineToCover lineNumber=\"1\" covered=\"true\" branchesToCover=\"2\" coveredBranches=\"x\"/></file></coverage>");
196 @Test(expected = MessageException.class)
197 public void coverage_coveredBranches_in_lineToCover_should_not_be_negative() throws Exception {
198 addFileToFs(setupFile("file1"));
199 parseCoverageReport("<coverage version=\"1\"><file path=\"file1\">"
200 + "<lineToCover lineNumber=\"1\" covered=\"true\" branchesToCover=\"2\" coveredBranches=\"-1\"/></file></coverage>");
203 @Test(expected = MessageException.class)
204 public void coverage_coveredBranches_should_not_be_greater_than_branchesToCover() throws Exception {
205 addFileToFs(setupFile("file1"));
206 parseCoverageReport("<coverage version=\"1\"><file path=\"file1\">"
207 + "<lineToCover lineNumber=\"1\" covered=\"true\" branchesToCover=\"2\" coveredBranches=\"3\"/></file></coverage>");
210 @Test(expected = MessageException.class)
211 public void testUnknownFile() throws Exception {
212 parseCoverageReportFile("xxx.xml");
215 private void addFileToFs(DefaultInputFile inputFile) {
216 context.fileSystem().add(inputFile);
219 private void parseCoverageReport(String string) throws Exception {
220 File report = temp.newFile();
221 FileUtils.write(report, string, StandardCharsets.UTF_8);
222 new GenericCoverageReportParser().parse(report, context);
225 private void parseCoverageReportFile(String reportLocation) throws Exception {
226 new GenericCoverageReportParser().parse(new File(reportLocation), context);
229 private DefaultInputFile setupFile(String path) {
230 return new TestInputFileBuilder(context.module().key(), path)
232 .setType(InputFile.Type.TEST)
233 .initMetadata("1\n2\n3\n4\n5\n6")