]> source.dussan.org Git - sonarqube.git/blob
777c85a924b3548b143f9ab58cf5e88dbcceecfa
[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.genericcoverage;
21
22 import java.io.File;
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;
34
35 import static org.assertj.core.api.Assertions.assertThat;
36
37 public class GenericCoverageReportParserTest {
38
39   @Rule
40   public TemporaryFolder temp = new TemporaryFolder();
41
42   private DefaultInputFile fileWithBranches;
43   private DefaultInputFile fileWithoutBranch;
44   private DefaultInputFile emptyFile;
45   private SensorContextTester context;
46
47   @Before
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");
53   }
54
55   @Test
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);
63   }
64
65   @Test
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);
71
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);
77   }
78
79   @Test
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);
85
86     assertThat(context.lineHits(fileWithBranches.key(), 3)).isEqualTo(1);
87     assertThat(context.lineHits(fileWithBranches.key(), 4)).isEqualTo(1);
88
89     assertThat(context.conditions(fileWithBranches.key(), 3)).isEqualTo(8);
90     assertThat(context.conditions(fileWithBranches.key(), 4)).isEqualTo(2);
91
92     assertThat(context.coveredConditions(fileWithBranches.key(), 3)).isEqualTo(5);
93     assertThat(context.coveredConditions(fileWithBranches.key(), 4)).isEqualTo(0);
94   }
95
96   @Test(expected = MessageException.class)
97   public void coverage_invalid_root_node_name() throws Exception {
98     parseCoverageReport("<mycoverage version=\"1\"></mycoverage>");
99   }
100
101   @Test(expected = MessageException.class)
102   public void coverage_invalid_report_version() throws Exception {
103     parseCoverageReport("<coverage version=\"2\"></coverage>");
104   }
105
106   @Test(expected = MessageException.class)
107   public void coverage_no_report_version() throws Exception {
108     parseCoverageReport("<coverage></coverage>");
109   }
110
111   @Test(expected = MessageException.class)
112   public void coverage_invalid_file_node_name() throws Exception {
113     parseCoverageReport("<coverage version=\"1\"><xx></xx></coverage>");
114   }
115
116   @Test(expected = MessageException.class)
117   public void unitTest_invalid_file_node_name() throws Exception {
118     parseCoverageReport("<unitTest version=\"1\"><xx></xx></unitTest>");
119   }
120
121   @Test(expected = MessageException.class)
122   public void coverage_missing_path_attribute() throws Exception {
123     parseCoverageReport("<coverage version=\"1\"><file></file></coverage>");
124   }
125
126   @Test(expected = MessageException.class)
127   public void unitTest_missing_path_attribute() throws Exception {
128     parseCoverageReport("<unitTest version=\"1\"><file></file></unitTest>");
129   }
130
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>");
135   }
136
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>");
141   }
142
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>");
147   }
148
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>");
153   }
154
155   @Test
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>");
161   }
162
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>");
167   }
168
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>");
173   }
174
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>");
180   }
181
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>");
187   }
188
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>");
194   }
195
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>");
201   }
202
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>");
208   }
209
210   @Test(expected = MessageException.class)
211   public void testUnknownFile() throws Exception {
212     parseCoverageReportFile("xxx.xml");
213   }
214
215   private void addFileToFs(DefaultInputFile inputFile) {
216     context.fileSystem().add(inputFile);
217   }
218
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);
223   }
224
225   private void parseCoverageReportFile(String reportLocation) throws Exception {
226     new GenericCoverageReportParser().parse(new File(reportLocation), context);
227   }
228
229   private DefaultInputFile setupFile(String path) {
230     return new TestInputFileBuilder(context.module().key(), path)
231       .setLanguage("bla")
232       .setType(InputFile.Type.TEST)
233       .initMetadata("1\n2\n3\n4\n5\n6")
234       .build();
235   }
236 }