Skip to content

Commit 308a86c

Browse files
JACOCO-71 Add a sensor to load aggregate reports (#144)
1 parent 3f39d08 commit 308a86c

File tree

25 files changed

+682
-63
lines changed

25 files changed

+682
-63
lines changed

its/src/test/java/org/sonar/plugins/jacoco/its/JacocoTest.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,41 @@ void no_failure_with_invalid_reports() throws IOException {
193193
assertThat(measures.get("coverage")).isEqualTo(0.0);
194194
}
195195

196+
@Test
197+
void aggregate_and_module_based_reports_complement_each_over_to_build_total_coverage() {
198+
Path project = Path.of("src", "test", "resources", "aggregate-and-module-based-mixed-coverage");
199+
Path rootPom = project.resolve("pom.xml");
200+
Path reportLocation = project.resolve("report")
201+
.resolve("target")
202+
.resolve("site")
203+
.resolve("jacoco-aggregate")
204+
.resolve("jacoco.xml");
205+
MavenBuild build = MavenBuild.create()
206+
.setPom(rootPom.toFile())
207+
.addGoal("clean verify")
208+
.addSonarGoal()
209+
.setProperty("sonar.coverage.jacoco.aggregateXmlReportPath", reportLocation.toAbsolutePath().toString());
210+
211+
orchestrator.executeBuild(build, true);
212+
213+
Map<String, Double> measuresForLibrary = getCoverageMeasures("org.example:aggregate-and-module-based-mixed-coverage:library/src/main/java/org/example/Library.java");
214+
assertThat(measuresForLibrary)
215+
.containsEntry("line_coverage", 100.0)
216+
.containsEntry("lines_to_cover", 4.0)
217+
.containsEntry("uncovered_lines", 0.0)
218+
.containsEntry("branch_coverage", 100.0)
219+
.containsEntry("conditions_to_cover", 2.0)
220+
.containsEntry("uncovered_conditions", 0.0)
221+
.containsEntry("coverage", 100.0);
222+
223+
Map<String, Double> measuresForSquarer = getCoverageMeasures("org.example:aggregate-and-module-based-mixed-coverage:self-covered/src/main/java/org/example/Squarer.java");
224+
assertThat(measuresForSquarer)
225+
.containsEntry("line_coverage", 100.0)
226+
.containsEntry("lines_to_cover", 2.0)
227+
.containsEntry("uncovered_lines", 0.0)
228+
.containsEntry("coverage", 100.0);
229+
}
230+
196231
@Test
197232
void kotlin_files_should_be_located_and_covered() {
198233
Path BASE_DIRECTORY = Paths.get("src/test/resources");
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
**/target/**
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Aggregate Maven project
2+
3+
A project with 4 modules:
4+
5+
1. [library](./library) - containing code but no tests
6+
2. [library.test](./library.test) - containing test code that uses code from `library`
7+
3. [report](./report) - generating the aggregate coverage report
8+
4. [self-covered](./self-covered) - containing code, tests and generating its own module-based coverage report
9+
10+
11+
The report can be generated by running the following command:
12+
```shell
13+
mvn verify --file ./pom.xml
14+
```
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<parent>
7+
<groupId>org.example</groupId>
8+
<artifactId>aggregate-and-module-based-mixed-coverage</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
</parent>
11+
12+
<artifactId>library-test</artifactId>
13+
<packaging>jar</packaging>
14+
15+
<dependencies>
16+
<dependency>
17+
<groupId>org.example</groupId>
18+
<artifactId>library</artifactId>
19+
<version>${project.version}</version>
20+
<scope>test</scope>
21+
</dependency>
22+
<dependency>
23+
<groupId>org.junit.jupiter</groupId>
24+
<artifactId>junit-jupiter-api</artifactId>
25+
<version>6.0.1</version>
26+
<scope>test</scope>
27+
</dependency>
28+
</dependencies>
29+
</project>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package org.example;
2+
3+
import org.junit.jupiter.api.Assertions;
4+
import org.junit.jupiter.api.Test;
5+
6+
class LibraryTest {
7+
@Test
8+
void incompleteTest() {
9+
Library library = new Library();
10+
Assertions.assertEquals(2, library.div(2, 1));
11+
}
12+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<parent>
7+
<groupId>org.example</groupId>
8+
<artifactId>aggregate-and-module-based-mixed-coverage</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
</parent>
11+
12+
<artifactId>library</artifactId>
13+
<packaging>jar</packaging>
14+
15+
<dependencies>
16+
<dependency>
17+
<groupId>org.junit.jupiter</groupId>
18+
<artifactId>junit-jupiter-api</artifactId>
19+
<version>6.0.1</version>
20+
<scope>test</scope>
21+
</dependency>
22+
</dependencies>
23+
24+
</project>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package org.example;
2+
3+
public class Library {
4+
public Integer div(int a, int b) {
5+
if (b == 0) {
6+
return null;
7+
}
8+
return a / b;
9+
}
10+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package org.example;
2+
3+
import org.junit.jupiter.api.Assertions;
4+
import org.junit.jupiter.api.Test;
5+
6+
class LibraryTest {
7+
@Test
8+
void returns_null_when_dividing_by_zero() {
9+
Library library = new Library();
10+
Assertions.assertNull(library.div(2, 0));
11+
}
12+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<groupId>org.example</groupId>
7+
<artifactId>aggregate-and-module-based-mixed-coverage</artifactId>
8+
<version>1.0-SNAPSHOT</version>
9+
10+
<packaging>pom</packaging>
11+
12+
<modules>
13+
<module>library</module>
14+
<module>library-test</module>
15+
<module>report</module>
16+
<module>self-covered</module>
17+
</modules>
18+
19+
<build>
20+
<plugins>
21+
<plugin>
22+
<groupId>org.jacoco</groupId>
23+
<artifactId>jacoco-maven-plugin</artifactId>
24+
<version>0.8.14</version>
25+
<executions>
26+
<execution>
27+
<id>prepare-agent</id>
28+
<goals>
29+
<goal>prepare-agent</goal>
30+
</goals>
31+
</execution>
32+
</executions>
33+
</plugin>
34+
</plugins>
35+
</build>
36+
</project>
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<parent>
7+
<groupId>org.example</groupId>
8+
<artifactId>aggregate-and-module-based-mixed-coverage</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
</parent>
11+
12+
<artifactId>report</artifactId>
13+
<packaging>jar</packaging>
14+
15+
<dependencies>
16+
<dependency>
17+
<groupId>org.example</groupId>
18+
<artifactId>library</artifactId>
19+
<version>${project.version}</version>
20+
<scope>compile</scope>
21+
</dependency>
22+
<dependency>
23+
<groupId>org.example</groupId>
24+
<artifactId>library-test</artifactId>
25+
<version>${project.version}</version>
26+
<scope>test</scope>
27+
</dependency>
28+
</dependencies>
29+
30+
<build>
31+
<plugins>
32+
<plugin>
33+
<groupId>org.jacoco</groupId>
34+
<artifactId>jacoco-maven-plugin</artifactId>
35+
<executions>
36+
<execution>
37+
<id>report-aggregate</id>
38+
<phase>verify</phase>
39+
<goals>
40+
<goal>report-aggregate</goal>
41+
</goals>
42+
</execution>
43+
</executions>
44+
</plugin>
45+
</plugins>
46+
</build>
47+
</project>

0 commit comments

Comments
 (0)