Skip to content

Commit

Permalink
Add support for mixed Java/Scala modules that use Java annotation pro…
Browse files Browse the repository at this point in the history
…cessors
  • Loading branch information
grollinger committed Aug 9, 2021
1 parent cbc7657 commit cc77b81
Show file tree
Hide file tree
Showing 13 changed files with 184 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package org.scoverage;

import org.gradle.testkit.runner.TaskOutcome;
import org.junit.Assert;
import org.junit.Test;

import java.io.File;

public class ScalaJavaAnnotationProcessorTest extends ScoverageFunctionalTest {

public ScalaJavaAnnotationProcessorTest() {
super("scala-java-annotation-processor");
}

@Test
public void checkAndAggregateScoverage() throws Exception {

AssertableBuildResult result = run("clean", ScoveragePlugin.getCHECK_NAME(),
ScoveragePlugin.getAGGREGATE_NAME());

result.assertTaskSkipped("java_only:" + ScoveragePlugin.getCOMPILE_NAME());

result.assertTaskSkipped(ScoveragePlugin.getREPORT_NAME());
result.assertTaskSucceeded("mixed_scala_java:" + ScoveragePlugin.getREPORT_NAME());
result.assertTaskSkipped("java_only:" + ScoveragePlugin.getREPORT_NAME());

result.assertTaskSucceeded(ScoveragePlugin.getCHECK_NAME());
result.assertTaskSucceeded("mixed_scala_java:" + ScoveragePlugin.getCHECK_NAME());
result.assertTaskSkipped("java_only:" + ScoveragePlugin.getCHECK_NAME());

result.assertTaskSucceeded(ScoveragePlugin.getAGGREGATE_NAME());

assertAllReportFilesExist();
assertCoverage(100.0);
}

private void assertAllReportFilesExist() {

Assert.assertTrue(resolve(reportDir(), "index.html").exists());

assertMixedScalaJavaReportFilesExist();
assertAggregationFilesExist();
}

private void assertAggregationFilesExist() {

Assert.assertTrue(resolve(reportDir(), "mixed_scala_java/src/main/scala/org/hello/WorldScala.scala.html").exists());
}

private void assertMixedScalaJavaReportFilesExist() {

File reportDir = reportDir(projectDir().toPath().resolve("mixed_scala_java").toFile());
Assert.assertTrue(resolve(reportDir, "index.html").exists());
Assert.assertTrue(resolve(reportDir, "src/main/scala/org/hello/WorldScala.scala.html").exists());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
plugins {
id 'org.scoverage' apply false
}

description = 'a multi-module Scala and Java project using a Java annotation processor'

allprojects {
repositories {
jcenter()
}
}

def lombokVersion = '1.18.20'

subprojects {
apply plugin: 'java'

dependencies {
testImplementation group: 'org.junit.platform', name: 'junit-platform-runner', version: junitPlatformVersion

compileOnly "org.projectlombok:lombok:$lombokVersion"
annotationProcessor "org.projectlombok:lombok:$lombokVersion"

testCompileOnly "org.projectlombok:lombok:$lombokVersion"
testAnnotationProcessor "org.projectlombok:lombok:$lombokVersion"
}

test {
useJUnitPlatform()
}
}

/*
Because the Scala compiler doesn't support annotation processors, Java files using a Java annotation
processor MUST be compiled by the Java compiler. So we can't use the same
configuration as in `scala-java-multi-module` here.
// A common practice in mixed java/scala modules to make Java code able to import Scala code
ext.configureSources = { set, name ->
set.scala.srcDir("src/$name/java")
set.java.srcDirs = []
}
configureSources(sourceSets.main, 'main')
configureSources(sourceSets.test, 'test')
*/

apply plugin: 'org.scoverage'
scoverage {
minimumRate = 0.5
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
dependencies {
testRuntimeOnly group: 'org.junit.vintage', name: 'junit-vintage-engine', version: junitVersion
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package org.hello;

import lombok.Value;

@Value
public class WorldJavaOnly {
private final String foo = "java_only";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.hello;

import org.junit.Test;

public class WorldJavaOnlyTest {

@Test
public void foo() {
new WorldJavaOnly().foo();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
lombok.accessors.fluent=true
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
apply plugin: 'scala'

dependencies {
implementation group: 'org.scala-lang', name: 'scala-library', version: "${scalaVersionMajor}.${scalaVersionMinor}.${scalaVersionBuild}"

testRuntimeOnly group: 'org.junit.vintage', name: 'junit-vintage-engine', version: junitVersion

testImplementation group: 'org.scalatest', name: "scalatest_${scalaVersionMajor}.${scalaVersionMinor}", version: scalatestVersion
}

apply plugin: 'org.scoverage'
scoverage {
minimumRate = 0.5
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package org.hello;

import lombok.Value;

@Value
public class WorldJava {
private final String foo = "java";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package org.hello

class WorldScala {
private val worldJava = new WorldJava()

def foo() = worldJava.foo()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.hello;

import org.junit.Test;

public class WorldJavaTest {

@Test
public void foo() {
new WorldJava().foo();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package org.hello

import org.junit.runner.RunWith
import org.scalatest.FunSuite
import org.scalatest.junit.JUnitRunner

@RunWith(classOf[JUnitRunner])
class WorldScalaSuite extends FunSuite {

test("foo") {
new WorldScala().foo()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include 'java_only', 'mixed_scala_java'
1 change: 1 addition & 0 deletions src/main/groovy/org/scoverage/ScoveragePlugin.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ class ScoveragePlugin implements Plugin<PluginAware> {
java.source(originalSourceSet.java)
scala.source(originalSourceSet.scala)

annotationProcessorPath += originalSourceSet.annotationProcessorPath + project.configurations.scoverage
compileClasspath += originalSourceSet.compileClasspath + project.configurations.scoverage
runtimeClasspath = it.output + project.configurations.scoverage + originalSourceSet.runtimeClasspath
}
Expand Down

0 comments on commit cc77b81

Please sign in to comment.