Skip to content

Commit ee291c2

Browse files
authored
Merge pull request #159 from grollinger/annotation-processors
Support mixed Java/Scala modules using Java annotation processors
2 parents bbc81ea + cc77b81 commit ee291c2

File tree

14 files changed

+185
-1
lines changed

14 files changed

+185
-1
lines changed

Diff for: build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ repositories {
1111
group 'org.scoverage'
1212
description = 'gradle-scoverage is a Gradle plugin for calculating code coverage using Scoverage'
1313
if (project.version == 'unspecified') {
14-
version = '3.0.0-SNAPSHOT'
14+
version = '6.0.0-SNAPSHOT'
1515
}
1616
ext {
1717
website = 'http://scoverage.org'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package org.scoverage;
2+
3+
import org.gradle.testkit.runner.TaskOutcome;
4+
import org.junit.Assert;
5+
import org.junit.Test;
6+
7+
import java.io.File;
8+
9+
public class ScalaJavaAnnotationProcessorTest extends ScoverageFunctionalTest {
10+
11+
public ScalaJavaAnnotationProcessorTest() {
12+
super("scala-java-annotation-processor");
13+
}
14+
15+
@Test
16+
public void checkAndAggregateScoverage() throws Exception {
17+
18+
AssertableBuildResult result = run("clean", ScoveragePlugin.getCHECK_NAME(),
19+
ScoveragePlugin.getAGGREGATE_NAME());
20+
21+
result.assertTaskSkipped("java_only:" + ScoveragePlugin.getCOMPILE_NAME());
22+
23+
result.assertTaskSkipped(ScoveragePlugin.getREPORT_NAME());
24+
result.assertTaskSucceeded("mixed_scala_java:" + ScoveragePlugin.getREPORT_NAME());
25+
result.assertTaskSkipped("java_only:" + ScoveragePlugin.getREPORT_NAME());
26+
27+
result.assertTaskSucceeded(ScoveragePlugin.getCHECK_NAME());
28+
result.assertTaskSucceeded("mixed_scala_java:" + ScoveragePlugin.getCHECK_NAME());
29+
result.assertTaskSkipped("java_only:" + ScoveragePlugin.getCHECK_NAME());
30+
31+
result.assertTaskSucceeded(ScoveragePlugin.getAGGREGATE_NAME());
32+
33+
assertAllReportFilesExist();
34+
assertCoverage(100.0);
35+
}
36+
37+
private void assertAllReportFilesExist() {
38+
39+
Assert.assertTrue(resolve(reportDir(), "index.html").exists());
40+
41+
assertMixedScalaJavaReportFilesExist();
42+
assertAggregationFilesExist();
43+
}
44+
45+
private void assertAggregationFilesExist() {
46+
47+
Assert.assertTrue(resolve(reportDir(), "mixed_scala_java/src/main/scala/org/hello/WorldScala.scala.html").exists());
48+
}
49+
50+
private void assertMixedScalaJavaReportFilesExist() {
51+
52+
File reportDir = reportDir(projectDir().toPath().resolve("mixed_scala_java").toFile());
53+
Assert.assertTrue(resolve(reportDir, "index.html").exists());
54+
Assert.assertTrue(resolve(reportDir, "src/main/scala/org/hello/WorldScala.scala.html").exists());
55+
}
56+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
plugins {
2+
id 'org.scoverage' apply false
3+
}
4+
5+
description = 'a multi-module Scala and Java project using a Java annotation processor'
6+
7+
allprojects {
8+
repositories {
9+
jcenter()
10+
}
11+
}
12+
13+
def lombokVersion = '1.18.20'
14+
15+
subprojects {
16+
apply plugin: 'java'
17+
18+
dependencies {
19+
testImplementation group: 'org.junit.platform', name: 'junit-platform-runner', version: junitPlatformVersion
20+
21+
compileOnly "org.projectlombok:lombok:$lombokVersion"
22+
annotationProcessor "org.projectlombok:lombok:$lombokVersion"
23+
24+
testCompileOnly "org.projectlombok:lombok:$lombokVersion"
25+
testAnnotationProcessor "org.projectlombok:lombok:$lombokVersion"
26+
}
27+
28+
test {
29+
useJUnitPlatform()
30+
}
31+
}
32+
33+
/*
34+
Because the Scala compiler doesn't support annotation processors, Java files using a Java annotation
35+
processor MUST be compiled by the Java compiler. So we can't use the same
36+
configuration as in `scala-java-multi-module` here.
37+
38+
// A common practice in mixed java/scala modules to make Java code able to import Scala code
39+
ext.configureSources = { set, name ->
40+
set.scala.srcDir("src/$name/java")
41+
set.java.srcDirs = []
42+
}
43+
configureSources(sourceSets.main, 'main')
44+
configureSources(sourceSets.test, 'test')
45+
*/
46+
47+
apply plugin: 'org.scoverage'
48+
scoverage {
49+
minimumRate = 0.5
50+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
dependencies {
2+
testRuntimeOnly group: 'org.junit.vintage', name: 'junit-vintage-engine', version: junitVersion
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package org.hello;
2+
3+
import lombok.Value;
4+
5+
@Value
6+
public class WorldJavaOnly {
7+
private final String foo = "java_only";
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package org.hello;
2+
3+
import org.junit.Test;
4+
5+
public class WorldJavaOnlyTest {
6+
7+
@Test
8+
public void foo() {
9+
new WorldJavaOnly().foo();
10+
}
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
lombok.accessors.fluent=true
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
apply plugin: 'scala'
2+
3+
dependencies {
4+
implementation group: 'org.scala-lang', name: 'scala-library', version: "${scalaVersionMajor}.${scalaVersionMinor}.${scalaVersionBuild}"
5+
6+
testRuntimeOnly group: 'org.junit.vintage', name: 'junit-vintage-engine', version: junitVersion
7+
8+
testImplementation group: 'org.scalatest', name: "scalatest_${scalaVersionMajor}.${scalaVersionMinor}", version: scalatestVersion
9+
}
10+
11+
apply plugin: 'org.scoverage'
12+
scoverage {
13+
minimumRate = 0.5
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package org.hello;
2+
3+
import lombok.Value;
4+
5+
@Value
6+
public class WorldJava {
7+
private final String foo = "java";
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package org.hello
2+
3+
class WorldScala {
4+
private val worldJava = new WorldJava()
5+
6+
def foo() = worldJava.foo()
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package org.hello;
2+
3+
import org.junit.Test;
4+
5+
public class WorldJavaTest {
6+
7+
@Test
8+
public void foo() {
9+
new WorldJava().foo();
10+
}
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package org.hello
2+
3+
import org.junit.runner.RunWith
4+
import org.scalatest.FunSuite
5+
import org.scalatest.junit.JUnitRunner
6+
7+
@RunWith(classOf[JUnitRunner])
8+
class WorldScalaSuite extends FunSuite {
9+
10+
test("foo") {
11+
new WorldScala().foo()
12+
}
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
include 'java_only', 'mixed_scala_java'

Diff for: src/main/groovy/org/scoverage/ScoveragePlugin.groovy

+1
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ class ScoveragePlugin implements Plugin<PluginAware> {
8787
java.source(originalSourceSet.java)
8888
scala.source(originalSourceSet.scala)
8989

90+
annotationProcessorPath += originalSourceSet.annotationProcessorPath + project.configurations.scoverage
9091
compileClasspath += originalSourceSet.compileClasspath + project.configurations.scoverage
9192
runtimeClasspath = it.output + project.configurations.scoverage + originalSourceSet.runtimeClasspath
9293
}

0 commit comments

Comments
 (0)