From 3b6071f59a040a82284a0cae44075bdf10863b6b Mon Sep 17 00:00:00 2001 From: Alexey Loubyansky Date: Fri, 8 Apr 2022 17:56:58 +0200 Subject: [PATCH] Support custom test classes dir under 'target' --- .../java/io/quarkus/maven/it/BuildIT.java | 10 + .../projects/test-source-sets/pom.xml | 234 ++++++++++++++++++ .../acme/GreetingResourceIntegrationTest.java | 22 ++ .../main/java/org/acme/GreetingResource.java | 18 ++ .../java/org/acme/GreetingResourceTest.java | 12 + .../quarkus/test/common/PathTestHelper.java | 56 +++-- .../AbstractJvmQuarkusTestExtension.java | 3 +- 7 files changed, 337 insertions(+), 18 deletions(-) create mode 100644 integration-tests/maven/src/test/resources-filtered/projects/test-source-sets/pom.xml create mode 100644 integration-tests/maven/src/test/resources-filtered/projects/test-source-sets/src/integrationtest/java/org/acme/GreetingResourceIntegrationTest.java create mode 100644 integration-tests/maven/src/test/resources-filtered/projects/test-source-sets/src/main/java/org/acme/GreetingResource.java create mode 100644 integration-tests/maven/src/test/resources-filtered/projects/test-source-sets/src/test/java/org/acme/GreetingResourceTest.java diff --git a/integration-tests/maven/src/test/java/io/quarkus/maven/it/BuildIT.java b/integration-tests/maven/src/test/java/io/quarkus/maven/it/BuildIT.java index 597e24d826111..c3ec32fc7baa7 100644 --- a/integration-tests/maven/src/test/java/io/quarkus/maven/it/BuildIT.java +++ b/integration-tests/maven/src/test/java/io/quarkus/maven/it/BuildIT.java @@ -11,6 +11,7 @@ import java.util.ArrayList; import java.util.Collections; import java.util.List; +import java.util.Map; import java.util.jar.Attributes; import java.util.jar.JarInputStream; import java.util.jar.Manifest; @@ -30,6 +31,15 @@ class BuildIT extends MojoTestBase { private RunningInvoker running; private File testDir; + @Test + void testCustomTestSourceSets() + throws MavenInvocationException, IOException, InterruptedException { + testDir = initProject("projects/test-source-sets"); + running = new RunningInvoker(testDir, false); + MavenProcessInvocationResult result = running.execute(List.of("clean", "verify"), Map.of()); + assertThat(result.getProcess().waitFor()).isZero(); + } + @Test void testConditionalDependencies() throws MavenInvocationException, IOException, InterruptedException { diff --git a/integration-tests/maven/src/test/resources-filtered/projects/test-source-sets/pom.xml b/integration-tests/maven/src/test/resources-filtered/projects/test-source-sets/pom.xml new file mode 100644 index 0000000000000..815fab772adf5 --- /dev/null +++ b/integration-tests/maven/src/test/resources-filtered/projects/test-source-sets/pom.xml @@ -0,0 +1,234 @@ + + + 4.0.0 + de.turing85 + quarkus-source-sets + 1.0.0-SNAPSHOT + + + 3.3.0 + 3.8.1 + 3.2.0 + false + 11 + + + verify + none + + UTF-8 + UTF-8 + + + \${project.basedir}/src/integrationtest + + + \${project.build.integrationtestRoot}/java + + + \${project.build.integrationtestRoot}/resources + + + \${project.build.directory}/integrationtest-classes + + + \${project.build.directory}/generated-integrationtest-sources/test-annotations + + + + quarkus-bom + io.quarkus + @project.version@ + 3.0.0-M5 + + + + + ${quarkus.platform.group-id} + ${quarkus.platform.artifact-id} + ${quarkus.platform.version} + pom + import + + + + + + io.quarkus + quarkus-resteasy-reactive + + + io.quarkus + quarkus-junit5 + test + + + io.rest-assured + rest-assured + test + + + + + + org.apache.maven.plugins + maven-dependency-plugin + ${dependency-plugin.version} + + + + build-classpath + + generate-test-sources + + classpath + + + + + + org.codehaus.mojo + build-helper-maven-plugin + ${build-helper-maven-plugin.version} + + + add-integration-test + generate-test-sources + + add-test-source + add-test-resource + + + + \${project.build.integrationtestSourceDirectory} + + + + \${project.build.integerationtestResoruceDirectory} + \${project.build.integrationtestOutputDirectory} + + + + + + + + ${quarkus.platform.group-id} + quarkus-maven-plugin + ${quarkus.platform.version} + true + + + + build + generate-code + generate-code-tests + + + + + + maven-compiler-plugin + ${compiler-plugin.version} + + + -parameters + + + + + default-testCompile + + testCompile + + test-compile + + + -cp + + \${classpath}\${path.separator}\${project.build.outputDirectory} + + + + \${project.build.testSourceDirectory} + + \${project.build.testOutputDirectory} + + + + compile-integrationtests + + testCompile + + test-compile + + + -cp + + \${classpath}\${path.separator}\${project.build.outputDirectory}\${path.separator}\${project.build.testOutputDirectory} + + + + + \${project.build.integrationtestSourceDirectory} + + + + \${project.build.integrationtestGeneratedSourceDirectory} + + \${project.build.integrationtestOutputDirectory} + + + + + + maven-surefire-plugin + ${surefire-plugin.version} + + + org.jboss.logmanager.LogManager + \${maven.home} + + \${project.build.outputDirectory} + + + + maven-failsafe-plugin + ${surefire-plugin.version} + + + none + + + integration-test + + integration-test + verify + + \${failsafe.it-phase} + + + + \${project.build.testOutputDirectory} + + + + **/*.java + + false + + \${project.build.directory}/\${project.build.finalName}-runner + org.jboss.logmanager.LogManager + \${maven.home} + \${project.build.integrationtestOutputDirectory} + + \${project.build.outputDirectory} + \${project.build.integrationtestOutputDirectory} + + + + + + + diff --git a/integration-tests/maven/src/test/resources-filtered/projects/test-source-sets/src/integrationtest/java/org/acme/GreetingResourceIntegrationTest.java b/integration-tests/maven/src/test/resources-filtered/projects/test-source-sets/src/integrationtest/java/org/acme/GreetingResourceIntegrationTest.java new file mode 100644 index 0000000000000..468ea751f717e --- /dev/null +++ b/integration-tests/maven/src/test/resources-filtered/projects/test-source-sets/src/integrationtest/java/org/acme/GreetingResourceIntegrationTest.java @@ -0,0 +1,22 @@ +package org.acme; + +import static io.restassured.RestAssured.given; +import static org.hamcrest.CoreMatchers.is; + +import io.quarkus.test.junit.QuarkusTest; +import org.junit.jupiter.api.Test; + +@QuarkusTest +public class GreetingResourceIntegrationTest { + + @Test + public void testHelloEndpoint() { + // @formatter: off + given() + .when().get("/hello") + .then() + .statusCode(200) + .body(is(GreetingResource.HELLO)); + // @formatter: on + } +} \ No newline at end of file diff --git a/integration-tests/maven/src/test/resources-filtered/projects/test-source-sets/src/main/java/org/acme/GreetingResource.java b/integration-tests/maven/src/test/resources-filtered/projects/test-source-sets/src/main/java/org/acme/GreetingResource.java new file mode 100644 index 0000000000000..a3b68994dfaa1 --- /dev/null +++ b/integration-tests/maven/src/test/resources-filtered/projects/test-source-sets/src/main/java/org/acme/GreetingResource.java @@ -0,0 +1,18 @@ +package org.acme; + +import javax.ws.rs.GET; +import javax.ws.rs.Path; +import javax.ws.rs.Produces; +import javax.ws.rs.core.MediaType; + +@Path("/hello") +public class GreetingResource { + + static final String HELLO = "Hello"; + + @GET + @Produces(MediaType.TEXT_PLAIN) + public String hello() { + return HELLO; + } +} \ No newline at end of file diff --git a/integration-tests/maven/src/test/resources-filtered/projects/test-source-sets/src/test/java/org/acme/GreetingResourceTest.java b/integration-tests/maven/src/test/resources-filtered/projects/test-source-sets/src/test/java/org/acme/GreetingResourceTest.java new file mode 100644 index 0000000000000..c0819a603bc38 --- /dev/null +++ b/integration-tests/maven/src/test/resources-filtered/projects/test-source-sets/src/test/java/org/acme/GreetingResourceTest.java @@ -0,0 +1,12 @@ +package org.acme; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +public class GreetingResourceTest { + @Test + void shouldReturnExpectedValue() { + String actual = new GreetingResource().hello(); + Assertions.assertEquals(actual, GreetingResource.HELLO); + } +} \ No newline at end of file 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 e26309fb6405d..079f0d7a32db2 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 @@ -9,6 +9,7 @@ import java.nio.file.Paths; import java.util.HashMap; import java.util.Map; +import java.util.Optional; import java.util.stream.Stream; import io.quarkus.bootstrap.BootstrapConstants; @@ -18,6 +19,7 @@ * Maps between builder test and application class directories. */ public final class PathTestHelper { + private static final String TARGET = "target"; private static final Map TEST_TO_MAIN_DIR_FRAGMENTS = new HashMap<>(); static { @@ -141,15 +143,20 @@ public static Path getTestClassesLocation(Class testClass) { throw new RuntimeException("Failed to resolve the location of the JAR containing " + testClass, e); } } + Path path = toPath(resource); + path = path.getRoot().resolve(path.subpath(0, path.getNameCount() - Path.of(classFileName).getNameCount())); - if (!isInTestDir(resource)) { - throw new RuntimeException( - "The test class " + testClass + " is not located in any of the directories " - + TEST_TO_MAIN_DIR_FRAGMENTS.keySet()); + if (!isInTestDir(resource) && !path.getParent().getFileName().toString().equals(TARGET)) { + final StringBuilder msg = new StringBuilder(); + msg.append("The test class ").append(testClass.getName()).append(" is not located in any of the directories "); + var i = TEST_TO_MAIN_DIR_FRAGMENTS.keySet().iterator(); + msg.append(i.next()); + while (i.hasNext()) { + msg.append(", ").append(i.next()); + } + throw new RuntimeException(msg.toString()); } - - Path path = toPath(resource); - return path.getRoot().resolve(path.subpath(0, path.getNameCount() - Paths.get(classFileName).getNameCount())); + return path; } /** @@ -175,13 +182,10 @@ public static Path getAppClassLocationForTestLocation(String testClassLocation) .append(testClassLocation, 0, testClassLocation.length() - "-tests.jar".length()) .append(".jar") .toString()); - } else if (testClassLocation.contains("-rpkgtests")) { - // This is a third party test-jar transformed using rpkgtests-maven-plugin - return Paths.get(testClassLocation.replace("-rpkgtests", "")); } - return Paths.get(testClassLocation); + return Path.of(testClassLocation); } - return TEST_TO_MAIN_DIR_FRAGMENTS.entrySet().stream() + Optional mainClassesDir = TEST_TO_MAIN_DIR_FRAGMENTS.entrySet().stream() .filter(e -> testClassLocation.contains(e.getKey())) .map(e -> { // we should replace only the last occurrence of the fragment @@ -191,10 +195,30 @@ public static Path getAppClassLocationForTestLocation(String testClassLocation) if (i + e.getKey().length() + 1 < testClassLocation.length()) { buf.append(testClassLocation.substring(i + e.getKey().length())); } - return Paths.get(buf.toString()); + return Path.of(buf.toString()); }) - .findFirst() - .orElseThrow(() -> new IllegalStateException("Unable to translate path for " + testClassLocation)); + .findFirst(); + Path p = null; + if (mainClassesDir.isPresent()) { + p = mainClassesDir.get(); + if (Files.exists(p)) { + return p; + } + } + // could be a custom test classes dir under the 'target' dir with the main + // classes still under 'target/classes' + p = Path.of(testClassLocation).getParent(); + if (p != null && p.getFileName().toString().equals(TARGET)) { + p = p.resolve("classes"); + if (Files.exists(p)) { + return p; + } + } + if (mainClassesDir.isPresent()) { + // if it's mapped but doesn't exist, it's still ok to return it + return mainClassesDir.get(); + } + throw new IllegalStateException("Unable to translate path for " + testClassLocation); } /** @@ -246,7 +270,7 @@ public static boolean isTestClass(String className, ClassLoader classLoader, Pat } path = path.substring(5, path.lastIndexOf('!')); - return testLocation.equals(Paths.get(path)); + return testLocation.equals(Path.of(path)); } private static boolean isInTestDir(URL resource) { diff --git a/test-framework/junit5/src/main/java/io/quarkus/test/junit/AbstractJvmQuarkusTestExtension.java b/test-framework/junit5/src/main/java/io/quarkus/test/junit/AbstractJvmQuarkusTestExtension.java index 970242d209c98..92716cb9d47eb 100644 --- a/test-framework/junit5/src/main/java/io/quarkus/test/junit/AbstractJvmQuarkusTestExtension.java +++ b/test-framework/junit5/src/main/java/io/quarkus/test/junit/AbstractJvmQuarkusTestExtension.java @@ -104,7 +104,6 @@ protected PrepareResult createAugmentor(ExtensionContext context, Class