Skip to content

Support mixed Java/Scala modules using Java annotation processors #159

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ repositories {
group 'org.scoverage'
description = 'gradle-scoverage is a Gradle plugin for calculating code coverage using Scoverage'
if (project.version == 'unspecified') {
version = '3.0.0-SNAPSHOT'
version = '6.0.0-SNAPSHOT'
}
ext {
website = 'http://scoverage.org'
Expand Down
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