From f4f780f6b0484ef9cfc71704087a61d2124f2f52 Mon Sep 17 00:00:00 2001 From: Harsha R Date: Fri, 25 Sep 2020 19:13:06 -0700 Subject: [PATCH] Incorporate review comments * Use a constant for sharing data between the two processes * Raise an exception if the mapping data is invalid. * Include a functional-test for verifying the plugin behavior --- devtools/gradle/build.gradle | 43 ++++++++++++++- .../test/AdditionalSourceSetsTest.java | 47 ++++++++++++++++ .../additional-source-sets/build.gradle | 55 +++++++++++++++++++ .../additional-source-sets/settings.gradle | 1 + .../src/funcTest/java/ft/SimpleBeanTest.java | 19 +++++++ .../src/main/java/ft/SimpleBean.java | 11 ++++ .../src/main/resources/META-INF/beans.xml | 6 ++ .../gradle/QuarkusPluginExtension.java | 2 +- .../quarkus/bootstrap/BootstrapConstants.java | 6 ++ .../quarkus/test/common/PathTestHelper.java | 5 +- 10 files changed, 189 insertions(+), 6 deletions(-) create mode 100644 devtools/gradle/src/funcTest/java/io/quarkus/gradle/functional/test/AdditionalSourceSetsTest.java create mode 100644 devtools/gradle/src/funcTest/resources/additional-source-sets/build.gradle create mode 100644 devtools/gradle/src/funcTest/resources/additional-source-sets/settings.gradle create mode 100644 devtools/gradle/src/funcTest/resources/additional-source-sets/src/funcTest/java/ft/SimpleBeanTest.java create mode 100644 devtools/gradle/src/funcTest/resources/additional-source-sets/src/main/java/ft/SimpleBean.java create mode 100644 devtools/gradle/src/funcTest/resources/additional-source-sets/src/main/resources/META-INF/beans.xml diff --git a/devtools/gradle/build.gradle b/devtools/gradle/build.gradle index 5bae6368b05dc..0a7a34a167449 100644 --- a/devtools/gradle/build.gradle +++ b/devtools/gradle/build.gradle @@ -27,6 +27,17 @@ repositories { mavenCentral() } +sourceSets { + // Functional Tests + funcTest { + java.srcDir 'src/funcTest/java' + resources.srcDir 'src/funcTest/resources' + + compileClasspath += sourceSets.main.output + configurations.testRuntimeClasspath + configurations.compileOnly + runtimeClasspath += output + compileClasspath + } +} + configurations.all { exclude group: 'io.quarkus', module: 'quarkus-bootstrap-maven-resolver' } @@ -64,9 +75,9 @@ javadoc { } pluginBundle { - website = 'https://quarkus.io/' - vcsUrl = 'https://github.com/quarkusio/quarkus' - tags = ['quarkus', 'quarkusio', 'graalvm'] + website = 'https://quarkus.io/' + vcsUrl = 'https://github.com/quarkusio/quarkus' + tags = ['quarkus', 'quarkusio', 'graalvm'] } gradlePlugin { @@ -83,3 +94,29 @@ gradlePlugin { wrapper { distributionType = Wrapper.DistributionType.ALL } + +/* + * Functional tests for this module. + */ +task functionalTest(type: Test, description: 'Functional tests', dependsOn: [processResources, classes]) { + description = 'Run the functional tests.' + group = 'verification' + + useJUnitPlatform() + testClassesDirs = sourceSets.funcTest.output.classesDirs + classpath = sourceSets.funcTest.runtimeClasspath + environment 'QUARKUS_VERSION', "${version}" + mustRunAfter test + + // propagate the custom local maven repo, in case it's configured + if (System.properties.containsKey('maven.repo.local')) { + systemProperty 'maven.repo.local', System.properties.get('maven.repo.local') + } + testLogging { + events "passed", "skipped", "failed" + } + // Circumvent issue: https://github.com/gradle/gradle/issues/1675 + systemProperty 'org.gradle.testkit.dir', file("${buildDir}/tmp/test-kit") +} + +check.dependsOn functionalTest diff --git a/devtools/gradle/src/funcTest/java/io/quarkus/gradle/functional/test/AdditionalSourceSetsTest.java b/devtools/gradle/src/funcTest/java/io/quarkus/gradle/functional/test/AdditionalSourceSetsTest.java new file mode 100644 index 0000000000000..bf9755e52c4a4 --- /dev/null +++ b/devtools/gradle/src/funcTest/java/io/quarkus/gradle/functional/test/AdditionalSourceSetsTest.java @@ -0,0 +1,47 @@ +package io.quarkus.gradle.functional.test; + +import org.gradle.testkit.runner.BuildResult; +import org.gradle.testkit.runner.GradleRunner; +import org.junit.jupiter.api.Test; + +import java.io.File; +import java.net.URISyntaxException; +import java.net.URL; + +import static org.junit.jupiter.api.Assertions.assertNotNull; + +/** + * Functional test for verifying that the plugin can work with test sources which are included as part of a custom + * source set. + */ +public class AdditionalSourceSetsTest { + + private static final String BUILD_FILE = "additional-source-sets/build.gradle"; + + @Test + public void executeFunctionalTest() throws URISyntaxException { + File buildFile = getBuildFile(BUILD_FILE); + GradleRunner runner = GradleRunner.create() + .withPluginClasspath() + .withProjectDir(buildFile.getParentFile()); + BuildResult result = runner.withArguments( + "-s", + "-DQUARKUS_VERSION=" + System.getenv("QUARKUS_VERSION"), + "functionalTest") + .build(); + String buildOutput = result.getOutput(); + System.out.println(buildOutput); + } + + /** + * Get the build file by searching for it on the classpath. + * + * @param path the path to the build file. + * @return the build file reference. + */ + private File getBuildFile(String path) throws URISyntaxException { + URL resource = Thread.currentThread().getContextClassLoader().getResource(BUILD_FILE); + assertNotNull(resource, "Build file not found."); + return new File(resource.toURI()); + } +} diff --git a/devtools/gradle/src/funcTest/resources/additional-source-sets/build.gradle b/devtools/gradle/src/funcTest/resources/additional-source-sets/build.gradle new file mode 100644 index 0000000000000..0a5aac7f4c548 --- /dev/null +++ b/devtools/gradle/src/funcTest/resources/additional-source-sets/build.gradle @@ -0,0 +1,55 @@ +plugins { + id 'java' + id 'io.quarkus' +} + +repositories { + mavenLocal() + mavenCentral() +} + +String quarkusVersion = System.getProperty('QUARKUS_VERSION') +dependencies { + implementation "io.quarkus:quarkus-arc:${quarkusVersion}" + testImplementation "io.quarkus:quarkus-junit5:${quarkusVersion}" +} + +sourceSets { + // Functional Tests + funcTest { + java.srcDir 'src/funcTest/java' + if (file('src/funcTest/resources').exists()) { + resources.srcDir 'src/funcTest/resources' + } + + compileClasspath += sourceSets.main.output + configurations.testRuntimeClasspath + configurations.compileOnly + runtimeClasspath += output + compileClasspath + } +} + +compileJava { + options.compilerArgs << '-parameters' +} + +java { + sourceCompatibility = JavaVersion.VERSION_1_8 + targetCompatibility = JavaVersion.VERSION_1_8 +} + +/* + * Functional tests for this module. + */ +task functionalTest(type: Test, description: 'Functional tests', dependsOn: [processResources, classes]) { + description = 'Run the functional tests.' + group = 'verification' + + useJUnitPlatform() + testClassesDirs = sourceSets.funcTest.output.classesDirs + classpath = sourceSets.funcTest.runtimeClasspath + + // Show standard streams on the console. + testLogging { + showStandardStreams = true + } + systemProperty 'quarkus.http.host', 'localhost' +} diff --git a/devtools/gradle/src/funcTest/resources/additional-source-sets/settings.gradle b/devtools/gradle/src/funcTest/resources/additional-source-sets/settings.gradle new file mode 100644 index 0000000000000..b0ee600fb218b --- /dev/null +++ b/devtools/gradle/src/funcTest/resources/additional-source-sets/settings.gradle @@ -0,0 +1 @@ +rootProject.name = 'functional-test' \ No newline at end of file diff --git a/devtools/gradle/src/funcTest/resources/additional-source-sets/src/funcTest/java/ft/SimpleBeanTest.java b/devtools/gradle/src/funcTest/resources/additional-source-sets/src/funcTest/java/ft/SimpleBeanTest.java new file mode 100644 index 0000000000000..a8a62620dd554 --- /dev/null +++ b/devtools/gradle/src/funcTest/resources/additional-source-sets/src/funcTest/java/ft/SimpleBeanTest.java @@ -0,0 +1,19 @@ +package ft; + +import javax.inject.Inject; +import org.junit.jupiter.api.Test; +import io.quarkus.test.junit.QuarkusTest; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +@QuarkusTest +public class SimpleBeanTest { + + @Inject + SimpleBean bean; + + @Test + public void verify() { + assertEquals("hello", bean.hello(), "Did the implementation of the SimpleBean change?"); + } +} diff --git a/devtools/gradle/src/funcTest/resources/additional-source-sets/src/main/java/ft/SimpleBean.java b/devtools/gradle/src/funcTest/resources/additional-source-sets/src/main/java/ft/SimpleBean.java new file mode 100644 index 0000000000000..7dbf40c55c6a6 --- /dev/null +++ b/devtools/gradle/src/funcTest/resources/additional-source-sets/src/main/java/ft/SimpleBean.java @@ -0,0 +1,11 @@ +package ft; + +import javax.enterprise.context.ApplicationScoped; + +@ApplicationScoped +public class SimpleBean { + + public String hello() { + return "hello"; + } +} diff --git a/devtools/gradle/src/funcTest/resources/additional-source-sets/src/main/resources/META-INF/beans.xml b/devtools/gradle/src/funcTest/resources/additional-source-sets/src/main/resources/META-INF/beans.xml new file mode 100644 index 0000000000000..29595ff4907bc --- /dev/null +++ b/devtools/gradle/src/funcTest/resources/additional-source-sets/src/main/resources/META-INF/beans.xml @@ -0,0 +1,6 @@ + + + diff --git a/devtools/gradle/src/main/java/io/quarkus/gradle/QuarkusPluginExtension.java b/devtools/gradle/src/main/java/io/quarkus/gradle/QuarkusPluginExtension.java index a980610c203bc..6c3c110517851 100644 --- a/devtools/gradle/src/main/java/io/quarkus/gradle/QuarkusPluginExtension.java +++ b/devtools/gradle/src/main/java/io/quarkus/gradle/QuarkusPluginExtension.java @@ -73,7 +73,7 @@ void beforeTest(Test task) { project.relativePath(outputDirectory())); joiner.add(mapping); }); - task.environment("TEST_TO_MAIN_MAPPINGS", joiner.toString()); + task.environment(BootstrapConstants.TEST_TO_MAIN_MAPPINGS, joiner.toString()); final String nativeRunner = task.getProject().getBuildDir().toPath().resolve(finalName() + "-runner") .toAbsolutePath() diff --git a/independent-projects/bootstrap/app-model/src/main/java/io/quarkus/bootstrap/BootstrapConstants.java b/independent-projects/bootstrap/app-model/src/main/java/io/quarkus/bootstrap/BootstrapConstants.java index 79c9c5d61a1ee..dae51d8e0892d 100644 --- a/independent-projects/bootstrap/app-model/src/main/java/io/quarkus/bootstrap/BootstrapConstants.java +++ b/independent-projects/bootstrap/app-model/src/main/java/io/quarkus/bootstrap/BootstrapConstants.java @@ -11,6 +11,12 @@ public interface BootstrapConstants { String SERIALIZED_APP_MODEL = "quarkus-internal.serialized-app-model.path"; String DESCRIPTOR_FILE_NAME = "quarkus-extension.properties"; + /** + * Constant for sharing the additional mappings between test-sources and the corresponding application-sources. + * The Gradle plugin populates this data which is then read by the PathTestHelper when executing tests. + */ + String TEST_TO_MAIN_MAPPINGS = "TEST_TO_MAIN_MAPPINGS"; + @Deprecated String EXTENSION_PROPS_JSON_FILE_NAME = "quarkus-extension.json"; diff --git a/test-framework/common/src/main/java/io/quarkus/test/common/PathTestHelper.java b/test-framework/common/src/main/java/io/quarkus/test/common/PathTestHelper.java index ae4935bb5fe8b..16f3b7b4ee18f 100644 --- a/test-framework/common/src/main/java/io/quarkus/test/common/PathTestHelper.java +++ b/test-framework/common/src/main/java/io/quarkus/test/common/PathTestHelper.java @@ -11,6 +11,7 @@ import java.util.Map; import java.util.stream.Stream; +import io.quarkus.bootstrap.BootstrapConstants; import io.quarkus.runtime.util.ClassPathUtils; /** @@ -101,7 +102,7 @@ public final class PathTestHelper { File.separator + "classes"); //endregion - String mappings = System.getenv("TEST_TO_MAIN_MAPPINGS"); + String mappings = System.getenv(BootstrapConstants.TEST_TO_MAIN_MAPPINGS); if (mappings != null) { Stream.of(mappings.split(",")) .filter(s -> !s.isEmpty()) @@ -110,7 +111,7 @@ public final class PathTestHelper { if (entry.length == 2) { TEST_TO_MAIN_DIR_FRAGMENTS.put(entry[0], entry[1]); } else { - System.err.println("Unable to parse additional test-to-main mapping: " + s); + throw new IllegalStateException("Unable to parse additional test-to-main mapping: " + s); } }); }