diff --git a/devtools/common/src/main/java/io/quarkus/generators/rest/BasicRestProjectGenerator.java b/devtools/common/src/main/java/io/quarkus/generators/rest/BasicRestProjectGenerator.java index aea7c3c9ada6f..36c2d8da0a55b 100644 --- a/devtools/common/src/main/java/io/quarkus/generators/rest/BasicRestProjectGenerator.java +++ b/devtools/common/src/main/java/io/quarkus/generators/rest/BasicRestProjectGenerator.java @@ -56,6 +56,7 @@ private class BasicRestProject { private ProjectWriter writer; private String srcMainPath; private String testMainPath; + private String nativeTestMainPath; private SourceType type; private BasicRestProject(final ProjectWriter writer, final Map parameters) { @@ -76,13 +77,19 @@ private boolean initProject() throws IOException { srcMainPath = writer.mkdirs(type.getSrcDir()); testMainPath = writer.mkdirs(type.getTestSrcDir()); + // for gradle we want to place the native tests in under 'src/native-test/java' + // since gradle's idiomatic way of running integration tests is to create a new source set + if (getBuildTool() == BuildTool.GRADLE) { + nativeTestMainPath = writer.mkdirs(type.getTestSrcDir().replace("test", "native-test")); + } else { + nativeTestMainPath = testMainPath; + } return newProject; } private boolean initBuildTool() throws IOException { - BuildFile buildFileManager = get(BUILD_FILE, null); - BuildTool buildTool = buildFileManager == null ? BuildTool.MAVEN : buildFileManager.getBuildTool(); + BuildTool buildTool = getBuildTool(); context.putIfAbsent(ADDITIONAL_GITIGNORE_ENTRIES, buildTool.getGitIgnoreEntries()); boolean newProject = !writer.exists(buildTool.getDependenciesFile()); if (newProject) { @@ -110,6 +117,11 @@ private boolean initBuildTool() throws IOException { return newProject; } + private BuildTool getBuildTool() { + BuildFile buildFileManager = get(BUILD_FILE, null); + return buildFileManager == null ? BuildTool.MAVEN : buildFileManager.getBuildTool(); + } + private void generate(final String templateName, final Map context, final String outputFilePath, final String resourceType) throws IOException { @@ -176,9 +188,18 @@ private void setupContext() throws IOException { if (packageName != null) { String packageDir = srcMainPath + '/' + packageName.replace('.', '/'); + String originalTestMainPath = testMainPath; String testPackageDir = testMainPath + '/' + packageName.replace('.', '/'); srcMainPath = writer.mkdirs(packageDir); testMainPath = writer.mkdirs(testPackageDir); + + if (!originalTestMainPath.equals(nativeTestMainPath)) { + String nativeTestPackageDir = nativeTestMainPath + '/' + packageName.replace('.', '/'); + nativeTestMainPath = writer.mkdirs(nativeTestPackageDir); + } else { + nativeTestMainPath = testMainPath; + } + } else { throw new NullPointerException("Need a non-null package name"); } @@ -192,7 +213,7 @@ private void createClasses() throws IOException { String extension = type.getExtension(); String classFile = srcMainPath + '/' + className + extension; String testClassFile = testMainPath + '/' + className + "Test" + extension; - String itTestClassFile = testMainPath + '/' + "Native" + className + "IT" + extension; + String itTestClassFile = nativeTestMainPath + '/' + "Native" + className + "IT" + extension; String name = getName(); String srcResourceTemplate = type.getSrcResourceTemplate(name); Object isSpring = context.get(IS_SPRING); diff --git a/devtools/common/src/main/resources/templates/basic-rest/java/build.gradle-template.ftl b/devtools/common/src/main/resources/templates/basic-rest/java/build.gradle-template.ftl index 1cb1e962a8e62..6596bc398e527 100644 --- a/devtools/common/src/main/resources/templates/basic-rest/java/build.gradle-template.ftl +++ b/devtools/common/src/main/resources/templates/basic-rest/java/build.gradle-template.ftl @@ -20,12 +20,29 @@ repositories { mavenCentral() } +sourceSets { + nativeTest { + compileClasspath += sourceSets.main.output + compileClasspath += sourceSets.test.output + runtimeClasspath += sourceSets.main.output + runtimeClasspath += sourceSets.test.output + } +} + +configurations { + nativeTestImplementation.extendsFrom implementation + nativeTestRuntimeOnly.extendsFrom runtimeOnly +} + dependencies { implementation enforcedPlatform("io.quarkus:quarkus-bom:${quarkusVersion}") implementation 'io.quarkus:quarkus-resteasy' testImplementation 'io.quarkus:quarkus-junit5' testImplementation 'io.rest-assured:rest-assured' + + nativeTestImplementation 'io.quarkus:quarkus-junit5' + nativeTestImplementation 'io.rest-assured:rest-assured' } group '${project_groupId}' @@ -37,8 +54,20 @@ compileJava { test { useJUnitPlatform() - exclude '**/Native*' } +task testNative(type: Test) { + useJUnitPlatform() + description = 'Runs native image tests' + group = 'verification' + + testClassesDirs = sourceSets.nativeTest.output.classesDirs + classpath = sourceSets.nativeTest.runtimeClasspath + shouldRunAfter test +} + +testNative.dependsOn buildNative +check.dependsOn testNative + diff --git a/devtools/common/src/main/resources/templates/basic-rest/kotlin/build.gradle-template.ftl b/devtools/common/src/main/resources/templates/basic-rest/kotlin/build.gradle-template.ftl index 62ca99e4fa5ba..3494fb18b9d2d 100644 --- a/devtools/common/src/main/resources/templates/basic-rest/kotlin/build.gradle-template.ftl +++ b/devtools/common/src/main/resources/templates/basic-rest/kotlin/build.gradle-template.ftl @@ -20,6 +20,20 @@ repositories { mavenCentral() } +sourceSets { + nativeTest { + compileClasspath += sourceSets.main.output + compileClasspath += sourceSets.test.output + runtimeClasspath += sourceSets.main.output + runtimeClasspath += sourceSets.test.output + } +} + +configurations { + nativeTestImplementation.extendsFrom implementation + nativeTestRuntimeOnly.extendsFrom runtimeOnly +} + dependencies { implementation enforcedPlatform("io.quarkus:quarkus-bom:${quarkusVersion}") implementation 'io.quarkus:quarkus-resteasy' @@ -27,6 +41,9 @@ dependencies { testImplementation 'io.quarkus:quarkus-junit5' testImplementation 'io.rest-assured:rest-assured' + + nativeTestImplementation 'io.quarkus:quarkus-junit5' + nativeTestImplementation 'io.rest-assured:rest-assured' } group '${project_groupId}' @@ -34,9 +51,21 @@ version '${project_version}' test { useJUnitPlatform() - exclude '**/Native*' } +task testNative(type: Test) { + useJUnitPlatform() + description = 'Runs native image tests' + group = 'verification' + + testClassesDirs = sourceSets.nativeTest.output.classesDirs + classpath = sourceSets.nativeTest.runtimeClasspath + shouldRunAfter test +} + +testNative.dependsOn buildNative +check.dependsOn testNative + quarkus { setOutputDirectory("$projectDir/build/classes/kotlin/main") } diff --git a/devtools/common/src/main/resources/templates/basic-rest/scala/build.gradle-template.ftl b/devtools/common/src/main/resources/templates/basic-rest/scala/build.gradle-template.ftl index 303dcaa00debd..beed9cdce922e 100644 --- a/devtools/common/src/main/resources/templates/basic-rest/scala/build.gradle-template.ftl +++ b/devtools/common/src/main/resources/templates/basic-rest/scala/build.gradle-template.ftl @@ -20,12 +20,29 @@ repositories { mavenCentral() } +sourceSets { + nativeTest { + compileClasspath += sourceSets.main.output + compileClasspath += sourceSets.test.output + runtimeClasspath += sourceSets.main.output + runtimeClasspath += sourceSets.test.output + } +} + +configurations { + nativeTestImplementation.extendsFrom implementation + nativeTestRuntimeOnly.extendsFrom runtimeOnly +} + dependencies { implementation enforcedPlatform("io.quarkus:quarkus-bom:${quarkusVersion}") implementation 'io.quarkus:quarkus-resteasy' testImplementation 'io.quarkus:quarkus-junit5' testImplementation 'io.rest-assured:rest-assured' + + nativeTestImplementation 'io.quarkus:quarkus-junit5' + nativeTestImplementation 'io.rest-assured:rest-assured' } group '${project_groupId}' @@ -33,6 +50,18 @@ version '${project_version}' test { useJUnitPlatform() - exclude '**/Native*' } +task testNative(type: Test) { + useJUnitPlatform() + description = 'Runs native image tests' + group = 'verification' + + testClassesDirs = sourceSets.nativeTest.output.classesDirs + classpath = sourceSets.nativeTest.runtimeClasspath + shouldRunAfter test +} + +testNative.dependsOn buildNative +check.dependsOn testNative + 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 4dbe0c95a3dec..156e9fcf5801c 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 @@ -21,12 +21,21 @@ public final class PathTestHelper { "bin" + File.separator + "test", "bin" + File.separator + "main"); // gradle + TEST_TO_MAIN_DIR_FRAGMENTS.put( + "classes" + File.separator + "java" + File.separator + "nativeTest", + "classes" + File.separator + "java" + File.separator + "main"); TEST_TO_MAIN_DIR_FRAGMENTS.put( "classes" + File.separator + "java" + File.separator + "test", "classes" + File.separator + "java" + File.separator + "main"); + TEST_TO_MAIN_DIR_FRAGMENTS.put( + "classes" + File.separator + "kotlin" + File.separator + "nativeTest", + "classes" + File.separator + "kotlin" + File.separator + "main"); TEST_TO_MAIN_DIR_FRAGMENTS.put( "classes" + File.separator + "kotlin" + File.separator + "test", "classes" + File.separator + "kotlin" + File.separator + "main"); + TEST_TO_MAIN_DIR_FRAGMENTS.put( + "classes" + File.separator + "scala" + File.separator + "nativeTest", + "classes" + File.separator + "scala" + File.separator + "main"); TEST_TO_MAIN_DIR_FRAGMENTS.put( "classes" + File.separator + "scala" + File.separator + "test", "classes" + File.separator + "scala" + File.separator + "main");