]> source.dussan.org Git - sonarqube.git/blob
1b82927bcd07324b35d7b5e5cf28ae4b00fc3eeb
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2017 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 org.junit.After;
25 import org.junit.Before;
26 import org.junit.Test;
27 import org.sonar.api.batch.fs.InputFile;
28 import org.sonar.api.measures.CoreMetrics;
29 import org.sonar.scanner.mediumtest.ScannerMediumTester;
30 import org.sonar.scanner.mediumtest.TaskResult;
31 import org.sonar.xoo.XooPlugin;
32
33 import static org.assertj.core.api.Assertions.assertThat;
34 import static org.assertj.core.api.Assertions.tuple;
35
36 public class GenericCoverageMediumTest {
37
38   public ScannerMediumTester tester = ScannerMediumTester.builder()
39     .registerPlugin("xoo", new XooPlugin())
40     .addDefaultQProfile("xoo", "Sonar Way")
41     .build();
42
43   @Before
44   public void prepare() {
45     tester.start();
46   }
47
48   @After
49   public void stop() {
50     tester.stop();
51   }
52
53   @Test
54   public void singleReport() throws IOException {
55
56     File projectDir = new File("src/test/resources/mediumtest/xoo/sample-generic-coverage");
57
58     TaskResult result = tester
59       .newScanTask(new File(projectDir, "sonar-project.properties"))
60       .property("sonar.coverageReportPaths", "coverage.xml")
61       .start();
62
63     InputFile noConditions = result.inputFile("xources/hello/NoConditions.xoo");
64     assertThat(result.coverageFor(noConditions, 6).getHits()).isTrue();
65     assertThat(result.coverageFor(noConditions, 6).getConditions()).isEqualTo(0);
66     assertThat(result.coverageFor(noConditions, 6).getCoveredConditions()).isEqualTo(0);
67
68     assertThat(result.coverageFor(noConditions, 7).getHits()).isFalse();
69
70     assertThat(result.allMeasures().get(noConditions.key())).extracting("metricKey", "intValue.value", "stringValue.value")
71       .containsOnly(
72         tuple(CoreMetrics.LINES_TO_COVER_KEY, 2, ""),
73         tuple(CoreMetrics.UNCOVERED_LINES_KEY, 1, ""),
74         tuple(CoreMetrics.COVERAGE_LINE_HITS_DATA_KEY, 0, "6=1;7=0"));
75
76     InputFile withConditions = result.inputFile("xources/hello/WithConditions.xoo");
77     assertThat(result.coverageFor(withConditions, 3).getHits()).isTrue();
78     assertThat(result.coverageFor(withConditions, 3).getConditions()).isEqualTo(2);
79     assertThat(result.coverageFor(withConditions, 3).getCoveredConditions()).isEqualTo(1);
80
81     assertThat(result.allMeasures().get(withConditions.key())).extracting("metricKey", "intValue.value", "stringValue.value")
82       .containsOnly(
83         tuple(CoreMetrics.LINES_TO_COVER_KEY, 1, ""),
84         tuple(CoreMetrics.UNCOVERED_LINES_KEY, 0, ""),
85         tuple(CoreMetrics.COVERAGE_LINE_HITS_DATA_KEY, 0, "3=1"),
86         tuple(CoreMetrics.CONDITIONS_TO_COVER_KEY, 2, ""),
87         tuple(CoreMetrics.UNCOVERED_CONDITIONS_KEY, 1, ""),
88         tuple(CoreMetrics.CONDITIONS_BY_LINE_KEY, 0, "3=2"),
89         tuple(CoreMetrics.COVERED_CONDITIONS_BY_LINE_KEY, 0, "3=1")
90
91     );
92   }
93
94   @Test
95   public void twoReports() throws IOException {
96
97     File projectDir = new File("src/test/resources/mediumtest/xoo/sample-generic-coverage");
98
99     TaskResult result = tester
100       .newScanTask(new File(projectDir, "sonar-project.properties"))
101       .property("sonar.coverageReportPaths", "coverage.xml,coverage2.xml")
102       .start();
103
104     InputFile noConditions = result.inputFile("xources/hello/NoConditions.xoo");
105     assertThat(result.coverageFor(noConditions, 6).getHits()).isTrue();
106     assertThat(result.coverageFor(noConditions, 6).getConditions()).isEqualTo(0);
107     assertThat(result.coverageFor(noConditions, 6).getCoveredConditions()).isEqualTo(0);
108
109     assertThat(result.coverageFor(noConditions, 7).getHits()).isTrue();
110
111     assertThat(result.allMeasures().get(noConditions.key())).extracting("metricKey", "intValue.value", "stringValue.value")
112       .containsOnly(
113         tuple(CoreMetrics.LINES_TO_COVER_KEY, 2, ""),
114         tuple(CoreMetrics.UNCOVERED_LINES_KEY, 0, ""),
115         tuple(CoreMetrics.COVERAGE_LINE_HITS_DATA_KEY, 0, "6=1;7=1"));
116
117     InputFile withConditions = result.inputFile("xources/hello/WithConditions.xoo");
118     assertThat(result.coverageFor(withConditions, 3).getHits()).isTrue();
119     assertThat(result.coverageFor(withConditions, 3).getConditions()).isEqualTo(2);
120     assertThat(result.coverageFor(withConditions, 3).getCoveredConditions()).isEqualTo(2);
121
122     assertThat(result.allMeasures().get(withConditions.key())).extracting("metricKey", "intValue.value", "stringValue.value")
123       .containsOnly(
124         tuple(CoreMetrics.LINES_TO_COVER_KEY, 1, ""),
125         tuple(CoreMetrics.UNCOVERED_LINES_KEY, 0, ""),
126         tuple(CoreMetrics.COVERAGE_LINE_HITS_DATA_KEY, 0, "3=2"),
127         tuple(CoreMetrics.CONDITIONS_TO_COVER_KEY, 2, ""),
128         tuple(CoreMetrics.UNCOVERED_CONDITIONS_KEY, 0, ""),
129         tuple(CoreMetrics.CONDITIONS_BY_LINE_KEY, 0, "3=2"),
130         tuple(CoreMetrics.COVERED_CONDITIONS_BY_LINE_KEY, 0, "3=2")
131
132     );
133   }
134
135 }