Skip to content

Commit 2884d97

Browse files
JACOCO-74 Import coverage for files whose name and package clashes across different nested modules
Collect sources for every module we traverse in order for the aggregate sensor to re-map properly source files in aggregate reports to files indexed by the scanner. This sharing of information among sensors is necessary because: 1. Aggregate reports contain the name of the module, package and filename but lack the source directories where the package is declared 2. Information about source files and directories is not accessible at the project sensor level
1 parent 7d73d2b commit 2884d97

File tree

32 files changed

+918
-42
lines changed

32 files changed

+918
-42
lines changed

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

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636

3737
import static org.assertj.core.api.Assertions.assertThat;
3838

39-
public class JacocoTest {
39+
class JacocoTest {
4040
private final static String PROJECT_KEY = "jacoco-test-project";
4141
private static final String FILE_KEY = "jacoco-test-project:src/main/java/org/sonarsource/test/Calc.java";
4242
private static final String KOTLIN_FILE_KEY = "org.sonarsource.it.projects:kotlin-jacoco-project:src/main/kotlin/CoverMe.kt";
@@ -223,6 +223,23 @@ void aggregate_and_module_based_reports_complement_each_over_to_build_total_cove
223223
.containsEntry("uncovered_conditions", 0.0)
224224
.containsEntry("coverage", 100.0);
225225

226+
Map<String, Double> measuresForLibraryClash = getCoverageMeasures("org.example:aggregate-and-module-based-mixed-coverage:library-clash/src/main/java/org/example/Library.java");
227+
assertThat(measuresForLibraryClash)
228+
.containsEntry("line_coverage", 100.0)
229+
.containsEntry("lines_to_cover", 2.0)
230+
.containsEntry("uncovered_lines", 0.0)
231+
.containsEntry("coverage", 100.0);
232+
233+
Map<String, Double> measuresForLibraryNested = getCoverageMeasures("org.example:aggregate-and-module-based-mixed-coverage:nested/library/src/main/java/org/example/Library.java");
234+
assertThat(measuresForLibraryNested)
235+
.containsEntry("branch_coverage", 100.0)
236+
.containsEntry("conditions_to_cover", 2.0)
237+
.containsEntry("coverage", 100.0)
238+
.containsEntry("line_coverage", 100.0)
239+
.containsEntry("lines_to_cover", 7.0)
240+
.containsEntry("uncovered_conditions", 0.0)
241+
.containsEntry("uncovered_lines", 0.0);
242+
226243
Map<String, Double> measuresForSquarer = getCoverageMeasures("org.example:aggregate-and-module-based-mixed-coverage:self-covered/src/main/java/org/example/Squarer.java");
227244
assertThat(measuresForSquarer)
228245
.containsEntry("line_coverage", 100.0)

its/src/test/resources/aggregate-and-module-based-mixed-coverage/README.md

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
# Aggregate Maven project
22

3-
A project with 4 modules:
3+
A project with 7 modules with 100% coverage:
44

55
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
6+
2. [library-clash](./library-clash) - containing a class whose name clashes with the one in `library`
7+
3. [nested](./nested) - containing one subproject
8+
4. [library-nested](./nested/library) - a nested subproject containing a class whose name clashes with the one in `library`
9+
5. [library-test](./library.test) - containing test code that uses code from `library`
10+
6. [report](./report) - generating the aggregate coverage report
11+
7[self-covered](./self-covered) - containing code, tests and generating its own module-based coverage report
912

1013

1114
The report can be generated by running the following command:
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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-clash</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+
</project>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package org.example;
2+
3+
public final class Library {
4+
String greet(String name) {
5+
return String.format("Hello, %s!", name);
6+
}
7+
}
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("Hello, World!", library.greet("World"));
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>nested</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
</parent>
11+
12+
<artifactId>library-nested</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: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package org.example;
2+
3+
import java.util.Objects;
4+
5+
/**
6+
* This class contains code that has very little to do with the libraries
7+
* It is also shaped in a way that applying coverage metrics from the other files should not work.
8+
*/
9+
public final class Library {
10+
public final String name;
11+
12+
public Library(String name) {
13+
this.name = name;
14+
}
15+
16+
@Override
17+
public boolean equals(Object o) {
18+
if (!(o instanceof Library)) return false;
19+
Library library = (Library) o;
20+
return Objects.equals(name, library.name);
21+
}
22+
23+
@Override
24+
public int hashCode() {
25+
return Objects.hashCode(name);
26+
}
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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 test() {
9+
Library library = new Library("City");
10+
Assertions.assertEquals("City", library.name);
11+
12+
Library similarLibrary = new Library("City");
13+
Assertions.assertEquals(library, similarLibrary);
14+
Assertions.assertEquals(library.hashCode(), library.hashCode());
15+
Assertions.assertEquals(library.hashCode(), similarLibrary.hashCode());
16+
17+
Assertions.assertNotEquals(library, new Object());
18+
Assertions.assertNotEquals(library, new Library("Other"));
19+
}
20+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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>nested</artifactId>
13+
<packaging>pom</packaging>
14+
15+
<modules>
16+
<module>library</module>
17+
</modules>
18+
</project>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package org.example;
2+
3+
public final class Library {
4+
String greet(String name) {
5+
return String.format("Hello, %s!", name);
6+
}
7+
}

0 commit comments

Comments
 (0)