From 43781d1e254457b22ac74560cbbff56082d2d59d Mon Sep 17 00:00:00 2001 From: Holly Cummins Date: Wed, 22 May 2024 12:08:49 +0100 Subject: [PATCH 1/2] Rename test template project to better reflect what it is testing --- ...java => TestTemplateCanSeeByteCodeChangesDevModeIT.java} | 6 +++--- ...ava => TestTemplateCanSeeByteCodeChangesTestModeIT.java} | 2 +- .../pom.xml | 0 .../src/main/resources/META-INF/resources/index.html | 0 .../src/main/resources/application.properties | 0 .../src/test/java/org/acme/NormalQuarkusTest.java | 0 .../src/test/java/org/acme/TemplatedNormalTest.java | 0 .../src/test/java/org/acme/TemplatedQuarkusTest.java | 0 8 files changed, 4 insertions(+), 4 deletions(-) rename integration-tests/test-extension/tests/src/test/java/io/quarkus/it/extension/it/{TestTemplateDevModeIT.java => TestTemplateCanSeeByteCodeChangesDevModeIT.java} (92%) rename integration-tests/test-extension/tests/src/test/java/io/quarkus/it/extension/it/{TestTemplateTestModeIT.java => TestTemplateCanSeeByteCodeChangesTestModeIT.java} (96%) rename integration-tests/test-extension/tests/src/test/resources-filtered/projects/{project-using-test-template-from-extension => project-using-test-template-from-extension-with-bytecode-changes}/pom.xml (100%) rename integration-tests/test-extension/tests/src/test/resources-filtered/projects/{project-using-test-template-from-extension => project-using-test-template-from-extension-with-bytecode-changes}/src/main/resources/META-INF/resources/index.html (100%) rename integration-tests/test-extension/tests/src/test/resources-filtered/projects/{project-using-test-template-from-extension => project-using-test-template-from-extension-with-bytecode-changes}/src/main/resources/application.properties (100%) rename integration-tests/test-extension/tests/src/test/resources-filtered/projects/{project-using-test-template-from-extension => project-using-test-template-from-extension-with-bytecode-changes}/src/test/java/org/acme/NormalQuarkusTest.java (100%) rename integration-tests/test-extension/tests/src/test/resources-filtered/projects/{project-using-test-template-from-extension => project-using-test-template-from-extension-with-bytecode-changes}/src/test/java/org/acme/TemplatedNormalTest.java (100%) rename integration-tests/test-extension/tests/src/test/resources-filtered/projects/{project-using-test-template-from-extension => project-using-test-template-from-extension-with-bytecode-changes}/src/test/java/org/acme/TemplatedQuarkusTest.java (100%) diff --git a/integration-tests/test-extension/tests/src/test/java/io/quarkus/it/extension/it/TestTemplateDevModeIT.java b/integration-tests/test-extension/tests/src/test/java/io/quarkus/it/extension/it/TestTemplateCanSeeByteCodeChangesDevModeIT.java similarity index 92% rename from integration-tests/test-extension/tests/src/test/java/io/quarkus/it/extension/it/TestTemplateDevModeIT.java rename to integration-tests/test-extension/tests/src/test/java/io/quarkus/it/extension/it/TestTemplateCanSeeByteCodeChangesDevModeIT.java index db8a2f637092e..993fdbaf83130 100644 --- a/integration-tests/test-extension/tests/src/test/java/io/quarkus/it/extension/it/TestTemplateDevModeIT.java +++ b/integration-tests/test-extension/tests/src/test/java/io/quarkus/it/extension/it/TestTemplateCanSeeByteCodeChangesDevModeIT.java @@ -23,7 +23,7 @@ */ @DisabledIfSystemProperty(named = "quarkus.test.native", matches = "true") @Disabled // Tracked by #27821 -public class TestTemplateDevModeIT extends RunAndCheckMojoTestBase { +public class TestTemplateCanSeeByteCodeChangesDevModeIT extends RunAndCheckMojoTestBase { /* * We have a few tests that will run in parallel, so set a unique port @@ -50,8 +50,8 @@ protected void runAndCheck(boolean performCompile, String... options) @Test public void testThatTheTestsPassed() throws MavenInvocationException, IOException { //we also check continuous testing - String executionDir = "projects/project-using-test-template-from-extension-processed"; - testDir = initProject("projects/project-using-test-template-from-extension", executionDir); + String executionDir = "projects/project-using-test-template-from-extension-with-bytecode-changes-processed"; + testDir = initProject("projects/project-using-test-template-from-extension-with-bytecode-changes", executionDir); runAndCheck(); ContinuousTestingMavenTestUtils testingTestUtils = new ContinuousTestingMavenTestUtils(getPort()); diff --git a/integration-tests/test-extension/tests/src/test/java/io/quarkus/it/extension/it/TestTemplateTestModeIT.java b/integration-tests/test-extension/tests/src/test/java/io/quarkus/it/extension/it/TestTemplateCanSeeByteCodeChangesTestModeIT.java similarity index 96% rename from integration-tests/test-extension/tests/src/test/java/io/quarkus/it/extension/it/TestTemplateTestModeIT.java rename to integration-tests/test-extension/tests/src/test/java/io/quarkus/it/extension/it/TestTemplateCanSeeByteCodeChangesTestModeIT.java index 594bab163a6d1..2cc02f9485e62 100644 --- a/integration-tests/test-extension/tests/src/test/java/io/quarkus/it/extension/it/TestTemplateTestModeIT.java +++ b/integration-tests/test-extension/tests/src/test/java/io/quarkus/it/extension/it/TestTemplateCanSeeByteCodeChangesTestModeIT.java @@ -21,7 +21,7 @@ */ @Disabled // Tracked by #27821 @DisabledIfSystemProperty(named = "quarkus.test.native", matches = "true") -public class TestTemplateTestModeIT extends RunAndCheckMojoTestBase { +public class TestTemplateCanSeeByteCodeChangesTestModeIT extends RunAndCheckMojoTestBase { /* * We have a few tests that will run in parallel, so set a unique port diff --git a/integration-tests/test-extension/tests/src/test/resources-filtered/projects/project-using-test-template-from-extension/pom.xml b/integration-tests/test-extension/tests/src/test/resources-filtered/projects/project-using-test-template-from-extension-with-bytecode-changes/pom.xml similarity index 100% rename from integration-tests/test-extension/tests/src/test/resources-filtered/projects/project-using-test-template-from-extension/pom.xml rename to integration-tests/test-extension/tests/src/test/resources-filtered/projects/project-using-test-template-from-extension-with-bytecode-changes/pom.xml diff --git a/integration-tests/test-extension/tests/src/test/resources-filtered/projects/project-using-test-template-from-extension/src/main/resources/META-INF/resources/index.html b/integration-tests/test-extension/tests/src/test/resources-filtered/projects/project-using-test-template-from-extension-with-bytecode-changes/src/main/resources/META-INF/resources/index.html similarity index 100% rename from integration-tests/test-extension/tests/src/test/resources-filtered/projects/project-using-test-template-from-extension/src/main/resources/META-INF/resources/index.html rename to integration-tests/test-extension/tests/src/test/resources-filtered/projects/project-using-test-template-from-extension-with-bytecode-changes/src/main/resources/META-INF/resources/index.html diff --git a/integration-tests/test-extension/tests/src/test/resources-filtered/projects/project-using-test-template-from-extension/src/main/resources/application.properties b/integration-tests/test-extension/tests/src/test/resources-filtered/projects/project-using-test-template-from-extension-with-bytecode-changes/src/main/resources/application.properties similarity index 100% rename from integration-tests/test-extension/tests/src/test/resources-filtered/projects/project-using-test-template-from-extension/src/main/resources/application.properties rename to integration-tests/test-extension/tests/src/test/resources-filtered/projects/project-using-test-template-from-extension-with-bytecode-changes/src/main/resources/application.properties diff --git a/integration-tests/test-extension/tests/src/test/resources-filtered/projects/project-using-test-template-from-extension/src/test/java/org/acme/NormalQuarkusTest.java b/integration-tests/test-extension/tests/src/test/resources-filtered/projects/project-using-test-template-from-extension-with-bytecode-changes/src/test/java/org/acme/NormalQuarkusTest.java similarity index 100% rename from integration-tests/test-extension/tests/src/test/resources-filtered/projects/project-using-test-template-from-extension/src/test/java/org/acme/NormalQuarkusTest.java rename to integration-tests/test-extension/tests/src/test/resources-filtered/projects/project-using-test-template-from-extension-with-bytecode-changes/src/test/java/org/acme/NormalQuarkusTest.java diff --git a/integration-tests/test-extension/tests/src/test/resources-filtered/projects/project-using-test-template-from-extension/src/test/java/org/acme/TemplatedNormalTest.java b/integration-tests/test-extension/tests/src/test/resources-filtered/projects/project-using-test-template-from-extension-with-bytecode-changes/src/test/java/org/acme/TemplatedNormalTest.java similarity index 100% rename from integration-tests/test-extension/tests/src/test/resources-filtered/projects/project-using-test-template-from-extension/src/test/java/org/acme/TemplatedNormalTest.java rename to integration-tests/test-extension/tests/src/test/resources-filtered/projects/project-using-test-template-from-extension-with-bytecode-changes/src/test/java/org/acme/TemplatedNormalTest.java diff --git a/integration-tests/test-extension/tests/src/test/resources-filtered/projects/project-using-test-template-from-extension/src/test/java/org/acme/TemplatedQuarkusTest.java b/integration-tests/test-extension/tests/src/test/resources-filtered/projects/project-using-test-template-from-extension-with-bytecode-changes/src/test/java/org/acme/TemplatedQuarkusTest.java similarity index 100% rename from integration-tests/test-extension/tests/src/test/resources-filtered/projects/project-using-test-template-from-extension/src/test/java/org/acme/TemplatedQuarkusTest.java rename to integration-tests/test-extension/tests/src/test/resources-filtered/projects/project-using-test-template-from-extension-with-bytecode-changes/src/test/java/org/acme/TemplatedQuarkusTest.java From da65f40dc91feb5ba974ded2a3674a22ac8bf9c2 Mon Sep 17 00:00:00 2001 From: Holly Cummins Date: Wed, 22 May 2024 12:16:03 +0100 Subject: [PATCH 2/2] Add new tests to cover JUnit @TestTemplate --- .../java/io/quarkus/maven/it/DevMojoIT.java | 52 ++++++ .../projects/test-template/pom.xml | 121 ++++++++++++++ .../src/main/java/org/acme/HelloResource.java | 16 ++ .../src/main/java/org/acme/MyApplication.java | 9 + .../src/main/resources/META-INF/beans.xml | 0 .../resources/META-INF/resources/index.html | 154 ++++++++++++++++++ .../src/main/resources/application.properties | 1 + .../src/test/java/com/acme/TemplatedTest.java | 23 +++ .../com/acme/UserIdGeneratorTestCase.java | 14 ++ ...eneratorTestInvocationContextProvider.java | 67 ++++++++ .../extension/it/TestTemplateDevModeIT.java | 65 ++++++++ .../pom.xml | 127 +++++++++++++++ .../resources/META-INF/resources/index.html | 16 ++ .../src/main/resources/application.properties | 1 + .../test/java/org/acme/NormalQuarkusTest.java | 18 ++ .../java/org/acme/TemplatedNormalTest.java | 23 +++ .../java/org/acme/TemplatedQuarkusTest.java | 19 +++ 17 files changed, 726 insertions(+) create mode 100644 integration-tests/maven/src/test/resources-filtered/projects/test-template/pom.xml create mode 100644 integration-tests/maven/src/test/resources-filtered/projects/test-template/src/main/java/org/acme/HelloResource.java create mode 100644 integration-tests/maven/src/test/resources-filtered/projects/test-template/src/main/java/org/acme/MyApplication.java create mode 100644 integration-tests/maven/src/test/resources-filtered/projects/test-template/src/main/resources/META-INF/beans.xml create mode 100644 integration-tests/maven/src/test/resources-filtered/projects/test-template/src/main/resources/META-INF/resources/index.html create mode 100644 integration-tests/maven/src/test/resources-filtered/projects/test-template/src/main/resources/application.properties create mode 100644 integration-tests/maven/src/test/resources-filtered/projects/test-template/src/test/java/com/acme/TemplatedTest.java create mode 100644 integration-tests/maven/src/test/resources-filtered/projects/test-template/src/test/java/com/acme/UserIdGeneratorTestCase.java create mode 100644 integration-tests/maven/src/test/resources-filtered/projects/test-template/src/test/java/com/acme/UserIdGeneratorTestInvocationContextProvider.java create mode 100644 integration-tests/test-extension/tests/src/test/java/io/quarkus/it/extension/it/TestTemplateDevModeIT.java create mode 100644 integration-tests/test-extension/tests/src/test/resources-filtered/projects/project-using-test-template-from-extension/pom.xml create mode 100644 integration-tests/test-extension/tests/src/test/resources-filtered/projects/project-using-test-template-from-extension/src/main/resources/META-INF/resources/index.html create mode 100644 integration-tests/test-extension/tests/src/test/resources-filtered/projects/project-using-test-template-from-extension/src/main/resources/application.properties create mode 100644 integration-tests/test-extension/tests/src/test/resources-filtered/projects/project-using-test-template-from-extension/src/test/java/org/acme/NormalQuarkusTest.java create mode 100644 integration-tests/test-extension/tests/src/test/resources-filtered/projects/project-using-test-template-from-extension/src/test/java/org/acme/TemplatedNormalTest.java create mode 100644 integration-tests/test-extension/tests/src/test/resources-filtered/projects/project-using-test-template-from-extension/src/test/java/org/acme/TemplatedQuarkusTest.java diff --git a/integration-tests/maven/src/test/java/io/quarkus/maven/it/DevMojoIT.java b/integration-tests/maven/src/test/java/io/quarkus/maven/it/DevMojoIT.java index 262bc40ceab09..f9a1957dd172a 100644 --- a/integration-tests/maven/src/test/java/io/quarkus/maven/it/DevMojoIT.java +++ b/integration-tests/maven/src/test/java/io/quarkus/maven/it/DevMojoIT.java @@ -34,6 +34,7 @@ import org.apache.commons.io.FileUtils; import org.apache.maven.shared.invoker.MavenInvocationException; import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.condition.DisabledOnOs; import org.junit.jupiter.api.condition.OS; @@ -588,6 +589,57 @@ public void testRestClientCustomHeadersExtension() throws MavenInvocationExcepti assertThat(devModeClient.getHttpResponse("/app/frontend")).isEqualTo("CustomValue1 CustomValue2"); } + @Test + public void testThatJUnitTestTemplatesWork() throws MavenInvocationException, IOException { + //we also check continuous testing + testDir = initProject("projects/test-template", "projects/test-template-processed"); + runAndCheck(); + + ContinuousTestingMavenTestUtils testingTestUtils = new ContinuousTestingMavenTestUtils(); + ContinuousTestingMavenTestUtils.TestStatus results = testingTestUtils.waitForNextCompletion(); + + //check that the tests in both modules run + Assertions.assertEquals(2, results.getTestsPassed()); + + // Re-running the tests when changes happen is covered by testThatChangesTriggerRerunsOfJUnitTestTemplates + } + + @Disabled("Not working; tracked by #40770") + @Test + public void testThatChangesTriggerRerunsOfJUnitTestTemplates() throws MavenInvocationException, IOException { + //we also check continuous testing + testDir = initProject("projects/test-template", "projects/test-template-processed"); + runAndCheck(); + + ContinuousTestingMavenTestUtils testingTestUtils = new ContinuousTestingMavenTestUtils(); + ContinuousTestingMavenTestUtils.TestStatus results = testingTestUtils.waitForNextCompletion(); + + //check that the tests in both modules run + Assertions.assertEquals(2, results.getTestsPassed()); + + // Edit the "Hello" message. + File source = new File(testDir, "src/main/java/org/acme/HelloResource.java"); + final String uuid = UUID.randomUUID().toString(); + filter(source, Collections.singletonMap("return \"hello\";", "return \"" + uuid + "\";")); + + // Wait until we get "uuid" + await() + .pollDelay(100, TimeUnit.MILLISECONDS) + .atMost(TestUtils.getDefaultTimeout(), TimeUnit.MINUTES) + .until(() -> devModeClient.getHttpResponse("/app/hello").contains(uuid)); + + await() + .pollDelay(100, TimeUnit.MILLISECONDS) + .pollInterval(1, TimeUnit.SECONDS) + .until(source::isFile); + + results = testingTestUtils.waitForNextCompletion(); + + //make sure the test is failing now + Assertions.assertEquals(0, results.getTestsPassed()); + Assertions.assertEquals(2, results.getTestsFailed()); + } + @Test public void testThatTheApplicationIsReloadedMultiModule() throws MavenInvocationException, IOException { //we also check continuous testing diff --git a/integration-tests/maven/src/test/resources-filtered/projects/test-template/pom.xml b/integration-tests/maven/src/test/resources-filtered/projects/test-template/pom.xml new file mode 100644 index 0000000000000..d41a45783c2b2 --- /dev/null +++ b/integration-tests/maven/src/test/resources-filtered/projects/test-template/pom.xml @@ -0,0 +1,121 @@ + + + 4.0.0 + + org.acme + quarkus-test-template + 1.0-SNAPSHOT + + + io.quarkus + quarkus-bom + @project.version@ + @project.version@ + ${compiler-plugin.version} + ${version.surefire.plugin} + ${maven.compiler.source} + ${maven.compiler.target} + UTF-8 + + + + + + \${quarkus.platform.group-id} + \${quarkus.platform.artifact-id} + \${quarkus.platform.version} + pom + import + + + + + + + io.quarkus + quarkus-resteasy + + + io.quarkus + quarkus-junit5 + test + + + io.rest-assured + rest-assured + test + + + + + + + maven-surefire-plugin + \${surefire-plugin.version} + + + org.jboss.logmanager.LogManager + \${maven.home} + + + + + io.quarkus + quarkus-maven-plugin + \${quarkus-plugin.version} + + + + build + + + + + + + + + + + native + + + native + + + + true + + + + + org.apache.maven.plugins + maven-surefire-plugin + + \${native.surefire.skip} + + + + maven-failsafe-plugin + \${surefire-plugin.version} + + + + integration-test + verify + + + + \${project.build.directory}/\${project.build.finalName}-runner + org.jboss.logmanager.LogManager + \${maven.home} + + + + + + + + + + diff --git a/integration-tests/maven/src/test/resources-filtered/projects/test-template/src/main/java/org/acme/HelloResource.java b/integration-tests/maven/src/test/resources-filtered/projects/test-template/src/main/java/org/acme/HelloResource.java new file mode 100644 index 0000000000000..fbb27b57b28fc --- /dev/null +++ b/integration-tests/maven/src/test/resources-filtered/projects/test-template/src/main/java/org/acme/HelloResource.java @@ -0,0 +1,16 @@ +package org.acme; + +import jakarta.ws.rs.GET; +import jakarta.ws.rs.Path; +import jakarta.ws.rs.Produces; +import jakarta.ws.rs.core.MediaType; + +@Path("/hello") +public class HelloResource { + + @GET + @Produces(MediaType.TEXT_PLAIN) + public String hello() { + return "hello"; + } +} diff --git a/integration-tests/maven/src/test/resources-filtered/projects/test-template/src/main/java/org/acme/MyApplication.java b/integration-tests/maven/src/test/resources-filtered/projects/test-template/src/main/java/org/acme/MyApplication.java new file mode 100644 index 0000000000000..a6d66f8b9eda2 --- /dev/null +++ b/integration-tests/maven/src/test/resources-filtered/projects/test-template/src/main/java/org/acme/MyApplication.java @@ -0,0 +1,9 @@ +package org.acme; + +import jakarta.ws.rs.ApplicationPath; +import jakarta.ws.rs.core.Application; + +@ApplicationPath("/app") +public class MyApplication extends Application { + +} diff --git a/integration-tests/maven/src/test/resources-filtered/projects/test-template/src/main/resources/META-INF/beans.xml b/integration-tests/maven/src/test/resources-filtered/projects/test-template/src/main/resources/META-INF/beans.xml new file mode 100644 index 0000000000000..e69de29bb2d1d diff --git a/integration-tests/maven/src/test/resources-filtered/projects/test-template/src/main/resources/META-INF/resources/index.html b/integration-tests/maven/src/test/resources-filtered/projects/test-template/src/main/resources/META-INF/resources/index.html new file mode 100644 index 0000000000000..9eee1d163a6f2 --- /dev/null +++ b/integration-tests/maven/src/test/resources-filtered/projects/test-template/src/main/resources/META-INF/resources/index.html @@ -0,0 +1,154 @@ + + + + + getting-started - 1.0-SNAPSHOT + + + + + + +
+
+

Congratulations, you have created a new Quarkus application.

+ +

Why do you see this?

+ +

This page is served by Quarkus. The source is in + src/main/resources/META-INF/resources/index.html.

+ +

What can I do from here?

+ +

If not already done, run the application in dev mode using: mvn compile quarkus:dev. +

+
    +
  • Add REST resources, Servlets, functions and other services in src/main/java.
  • +
  • Your static assets are located in src/main/resources/META-INF/resources.
  • +
  • Configure your application in src/main/resources/META-INF/microprofile-config.properties. +
  • +
+ +

Do you like Quarkus?

+

Go give it a star on GitHub.

+ +

How do I get rid of this page?

+

Just delete the src/main/resources/META-INF/resources/index.html file.

+
+
+
+

Application

+
    +
  • GroupId: org.acme
  • +
  • ArtifactId: getting-started
  • +
  • Version: 1.0-SNAPSHOT
  • +
+
+ +
+
+ + + + \ No newline at end of file diff --git a/integration-tests/maven/src/test/resources-filtered/projects/test-template/src/main/resources/application.properties b/integration-tests/maven/src/test/resources-filtered/projects/test-template/src/main/resources/application.properties new file mode 100644 index 0000000000000..07a519e0e7928 --- /dev/null +++ b/integration-tests/maven/src/test/resources-filtered/projects/test-template/src/main/resources/application.properties @@ -0,0 +1 @@ +quarkus.test.continuous-testing=enabled diff --git a/integration-tests/maven/src/test/resources-filtered/projects/test-template/src/test/java/com/acme/TemplatedTest.java b/integration-tests/maven/src/test/resources-filtered/projects/test-template/src/test/java/com/acme/TemplatedTest.java new file mode 100644 index 0000000000000..3da0b8f6fdfc7 --- /dev/null +++ b/integration-tests/maven/src/test/resources-filtered/projects/test-template/src/test/java/com/acme/TemplatedTest.java @@ -0,0 +1,23 @@ +package com.acme; + +import static io.restassured.RestAssured.given; +import static org.hamcrest.CoreMatchers.is; + +import org.junit.jupiter.api.TestTemplate; +import org.junit.jupiter.api.extension.ExtendWith; + +import io.quarkus.test.junit.QuarkusTest; + +@QuarkusTest +public class TemplatedTest { + + @TestTemplate + @ExtendWith(UserIdGeneratorTestInvocationContextProvider.class) + public void testHelloEndpoint(UserIdGeneratorTestCase testCase) { + given() + .when().get("/app/hello") + .then() + .statusCode(200) + .body(is(testCase.getExpectedBody())); + } +} diff --git a/integration-tests/maven/src/test/resources-filtered/projects/test-template/src/test/java/com/acme/UserIdGeneratorTestCase.java b/integration-tests/maven/src/test/resources-filtered/projects/test-template/src/test/java/com/acme/UserIdGeneratorTestCase.java new file mode 100644 index 0000000000000..d8c44acc15b88 --- /dev/null +++ b/integration-tests/maven/src/test/resources-filtered/projects/test-template/src/test/java/com/acme/UserIdGeneratorTestCase.java @@ -0,0 +1,14 @@ +package com.acme; + +public class UserIdGeneratorTestCase { + private static int GLOBAL_ID = 0; + private final int id = GLOBAL_ID++; + + public Object getExpectedBody() { + return "hello"; + } + + public String getDisplayName() { + return "simple test template test case" + id; + } +} diff --git a/integration-tests/maven/src/test/resources-filtered/projects/test-template/src/test/java/com/acme/UserIdGeneratorTestInvocationContextProvider.java b/integration-tests/maven/src/test/resources-filtered/projects/test-template/src/test/java/com/acme/UserIdGeneratorTestInvocationContextProvider.java new file mode 100644 index 0000000000000..ffa75399c9300 --- /dev/null +++ b/integration-tests/maven/src/test/resources-filtered/projects/test-template/src/test/java/com/acme/UserIdGeneratorTestInvocationContextProvider.java @@ -0,0 +1,67 @@ +package com.acme; + +import java.util.Arrays; +import java.util.List; +import java.util.stream.Stream; + +import org.junit.jupiter.api.extension.AfterTestExecutionCallback; +import org.junit.jupiter.api.extension.BeforeTestExecutionCallback; +import org.junit.jupiter.api.extension.Extension; +import org.junit.jupiter.api.extension.ExtensionContext; +import org.junit.jupiter.api.extension.ParameterContext; +import org.junit.jupiter.api.extension.ParameterResolver; +import org.junit.jupiter.api.extension.TestTemplateInvocationContext; +import org.junit.jupiter.api.extension.TestTemplateInvocationContextProvider; + +public class UserIdGeneratorTestInvocationContextProvider implements TestTemplateInvocationContextProvider { + @Override + public boolean supportsTestTemplate(ExtensionContext extensionContext) { + return true; + } + + @Override + public Stream provideTestTemplateInvocationContexts(ExtensionContext extensionContext) { + return Stream.of( + genericContext(new UserIdGeneratorTestCase()), + genericContext(new UserIdGeneratorTestCase())); + } + + private TestTemplateInvocationContext genericContext( + UserIdGeneratorTestCase userIdGeneratorTestCase) { + return new TestTemplateInvocationContext() { + @Override + public String getDisplayName(int invocationIndex) { + return userIdGeneratorTestCase.getDisplayName(); + } + + @Override + public List getAdditionalExtensions() { + return Arrays.asList(parameterResolver(), preProcessor(), postProcessor()); + } + + private BeforeTestExecutionCallback preProcessor() { + return context -> System.out.println("Pre-process parameter: " + userIdGeneratorTestCase.getDisplayName()); + } + + private AfterTestExecutionCallback postProcessor() { + return context -> System.out.println("Post-process parameter: " + userIdGeneratorTestCase.getDisplayName()); + } + + private ParameterResolver parameterResolver() { + return new ParameterResolver() { + @Override + public boolean supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext) { + return parameterContext.getParameter() + .getType() + .equals(UserIdGeneratorTestCase.class); + } + + @Override + public Object resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext) { + return userIdGeneratorTestCase; + } + }; + } + }; + } +} diff --git a/integration-tests/test-extension/tests/src/test/java/io/quarkus/it/extension/it/TestTemplateDevModeIT.java b/integration-tests/test-extension/tests/src/test/java/io/quarkus/it/extension/it/TestTemplateDevModeIT.java new file mode 100644 index 0000000000000..f9b3ec9475d53 --- /dev/null +++ b/integration-tests/test-extension/tests/src/test/java/io/quarkus/it/extension/it/TestTemplateDevModeIT.java @@ -0,0 +1,65 @@ +package io.quarkus.it.extension.it; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.io.FileNotFoundException; +import java.io.IOException; + +import org.apache.maven.shared.invoker.MavenInvocationException; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Disabled; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.condition.DisabledIfSystemProperty; + +import io.quarkus.maven.it.RunAndCheckMojoTestBase; +import io.quarkus.maven.it.continuoustesting.ContinuousTestingMavenTestUtils; + +/** + * Be aware! This test will not run if the name does not start with 'Test'. + *

+ * NOTE to anyone diagnosing failures in this test, to run a single method use: + *

+ * mvn install -Dit.test=TestTemplateDevModeIT#methodName + */ +@Disabled("NPE in JUnit stack; See discussion in https://github.com/quarkiverse/quarkiverse/issues/94, should be re-enabled when https://github.com/quarkusio/quarkus/pull/40751 is merged") +@DisabledIfSystemProperty(named = "quarkus.test.native", matches = "true") +public class TestTemplateDevModeIT extends RunAndCheckMojoTestBase { + + /* + * We have a few tests that will run in parallel, so set a unique port + */ + protected int getPort() { + return 8092; + } + + protected void runAndCheck(boolean performCompile, String... options) + throws MavenInvocationException, FileNotFoundException { + run(performCompile, options); + + try { + String resp = devModeClient.getHttpResponse(); + assertThat(resp).containsIgnoringCase("ready").containsIgnoringCase("application") + .containsIgnoringCase("SNAPSHOT"); + } catch (Exception e) { + e.printStackTrace(); + } + + // There's no json endpoints, so nothing else to check + } + + @Test + public void testThatTheTestsPassed() throws MavenInvocationException, IOException { + //we also check continuous testing + String executionDir = "projects/project-using-test-template-from-extension-with-bytecode-changes-processed"; + testDir = initProject("projects/project-using-test-template-from-extension-with-bytecode-changes", executionDir); + runAndCheck(); + + ContinuousTestingMavenTestUtils testingTestUtils = new ContinuousTestingMavenTestUtils(getPort()); + ContinuousTestingMavenTestUtils.TestStatus results = testingTestUtils.waitForNextCompletion(); + // This is a bit brittle when we add tests, but failures are often so catastrophic they're not even reported as failures, + // so we need to check the pass count explicitly + Assertions.assertEquals(0, results.getTestsFailed()); + Assertions.assertEquals(3, results.getTestsPassed()); + } + +} diff --git a/integration-tests/test-extension/tests/src/test/resources-filtered/projects/project-using-test-template-from-extension/pom.xml b/integration-tests/test-extension/tests/src/test/resources-filtered/projects/project-using-test-template-from-extension/pom.xml new file mode 100644 index 0000000000000..77a81d2ea5993 --- /dev/null +++ b/integration-tests/test-extension/tests/src/test/resources-filtered/projects/project-using-test-template-from-extension/pom.xml @@ -0,0 +1,127 @@ + + + 4.0.0 + org.acme + project-using-test-template-from-extension + 999-SNAPSHOT + + ${compiler-plugin.version} + false + quarkus-bom + 17 + UTF-8 + UTF-8 + 3.2.5 + @project.version@ + + + + + io.quarkus + ${quarkus.bom.artifact-id} + ${quarkus.version} + pom + import + + + + + + io.quarkus + quarkus-vertx-http + + + + io.quarkus + quarkus-rest-client-jackson + + + io.quarkus + quarkus-junit5 + test + + + + io.quarkus + integration-test-extension-that-defines-junit-test-extensions + ${quarkus.version} + test + + + + + + + io.quarkus + quarkus-maven-plugin + ${quarkus.version} + true + + + + build + generate-code + generate-code-tests + + + + + + maven-compiler-plugin + \${compiler-plugin.version} + + + -parameters + + + + + maven-surefire-plugin + \${surefire-plugin.version} + + + org.jboss.logmanager.LogManager + \${maven.home} + + + + + + + + native + + + native + + + + + + maven-failsafe-plugin + \${surefire-plugin.version} + + + + integration-test + verify + + + + \${project.build.directory}/\${project.build.finalName}-runner + org.jboss.logmanager.LogManager + \${maven.home} + + + + + + + + + true + + + + diff --git a/integration-tests/test-extension/tests/src/test/resources-filtered/projects/project-using-test-template-from-extension/src/main/resources/META-INF/resources/index.html b/integration-tests/test-extension/tests/src/test/resources-filtered/projects/project-using-test-template-from-extension/src/main/resources/META-INF/resources/index.html new file mode 100644 index 0000000000000..b80fe3dc62642 --- /dev/null +++ b/integration-tests/test-extension/tests/src/test/resources-filtered/projects/project-using-test-template-from-extension/src/main/resources/META-INF/resources/index.html @@ -0,0 +1,16 @@ + + + + + + getting-started - 1.0-SNAPSHOT + + + +

+ The application is ready. +
+ + + + \ No newline at end of file diff --git a/integration-tests/test-extension/tests/src/test/resources-filtered/projects/project-using-test-template-from-extension/src/main/resources/application.properties b/integration-tests/test-extension/tests/src/test/resources-filtered/projects/project-using-test-template-from-extension/src/main/resources/application.properties new file mode 100644 index 0000000000000..442095ca8410c --- /dev/null +++ b/integration-tests/test-extension/tests/src/test/resources-filtered/projects/project-using-test-template-from-extension/src/main/resources/application.properties @@ -0,0 +1 @@ +quarkus.test.continuous-testing=enabled \ No newline at end of file diff --git a/integration-tests/test-extension/tests/src/test/resources-filtered/projects/project-using-test-template-from-extension/src/test/java/org/acme/NormalQuarkusTest.java b/integration-tests/test-extension/tests/src/test/resources-filtered/projects/project-using-test-template-from-extension/src/test/java/org/acme/NormalQuarkusTest.java new file mode 100644 index 0000000000000..41be0f283f6b0 --- /dev/null +++ b/integration-tests/test-extension/tests/src/test/resources-filtered/projects/project-using-test-template-from-extension/src/test/java/org/acme/NormalQuarkusTest.java @@ -0,0 +1,18 @@ +package org.acme; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +import io.quarkus.test.junit.QuarkusTest; + +/** + * Sense check - do we see the added annotation without parameterization? + */ +@QuarkusTest +public class NormalQuarkusTest { + + @Test + void executionAnnotationCheckingTestTemplate() { + Assertions.assertTrue(true); + } +} diff --git a/integration-tests/test-extension/tests/src/test/resources-filtered/projects/project-using-test-template-from-extension/src/test/java/org/acme/TemplatedNormalTest.java b/integration-tests/test-extension/tests/src/test/resources-filtered/projects/project-using-test-template-from-extension/src/test/java/org/acme/TemplatedNormalTest.java new file mode 100644 index 0000000000000..273a628ce214e --- /dev/null +++ b/integration-tests/test-extension/tests/src/test/resources-filtered/projects/project-using-test-template-from-extension/src/test/java/org/acme/TemplatedNormalTest.java @@ -0,0 +1,23 @@ +package org.acme; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.TestTemplate; +import org.junit.jupiter.api.extension.ExtendWith; +import org.junit.jupiter.api.extension.ExtensionContext; + +// No QuarkusTest annotation + +/** + * It's likely we would never expect this to work; unit tests which don't have a @QuarkusTest + * annotation would not be able to + * benefit from bytecode manipulations from extensions. + */ +public class TemplatedNormalTest { + + @TestTemplate + @ExtendWith(MyContextProvider.class) + void trivialTestTemplate(ExtensionContext context) { + Assertions.assertTrue(context != null); + + } +} diff --git a/integration-tests/test-extension/tests/src/test/resources-filtered/projects/project-using-test-template-from-extension/src/test/java/org/acme/TemplatedQuarkusTest.java b/integration-tests/test-extension/tests/src/test/resources-filtered/projects/project-using-test-template-from-extension/src/test/java/org/acme/TemplatedQuarkusTest.java new file mode 100644 index 0000000000000..735f61ce0c751 --- /dev/null +++ b/integration-tests/test-extension/tests/src/test/resources-filtered/projects/project-using-test-template-from-extension/src/test/java/org/acme/TemplatedQuarkusTest.java @@ -0,0 +1,19 @@ +package org.acme; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.TestTemplate; +import org.junit.jupiter.api.extension.ExtendWith; +import org.junit.jupiter.api.extension.ExtensionContext; + +import io.quarkus.test.junit.QuarkusTest; + +@QuarkusTest +public class TemplatedQuarkusTest { + + @TestTemplate + @ExtendWith(MyContextProvider.class) + void trivialTestTemplate(ExtensionContext context) { + Assertions.assertTrue(context != null); + } + +}