From 4b79122029c097460a612cf97067921ed577c183 Mon Sep 17 00:00:00 2001 From: Guillaume Le Floch Date: Thu, 31 Mar 2022 11:26:55 +0200 Subject: [PATCH] Export application name and version in tests --- .../jaxrs/OpenApiDefaultPathTestCase.java | 2 +- .../OpenApiHttpRootDefaultPathTestCase.java | 2 +- ...steasyPathHttpRootDefaultPathTestCase.java | 2 +- .../OpenApiWithResteasyPathTestCase.java | 2 +- .../vertx/OpenApiDefaultPathTestCase.java | 2 +- .../OpenApiHttpRootDefaultPathTestCase.java | 2 +- .../dev/KafkaDevServicesDevModeTestCase.java | 3 ++- .../bootstrap/app/QuarkusBootstrap.java | 16 ++++++++++-- .../org/acme/ApplicationConfigResource.java | 25 +++++++++++++++++++ .../acme/ApplicationConfigResourceTest.java | 21 ++++++++++++++++ .../gradle/ApplicationConfigurationTest.java | 20 +++++++++++++++ .../web/openapi/OpenApiDefaultPathPMT.java | 2 +- 12 files changed, 89 insertions(+), 10 deletions(-) create mode 100644 integration-tests/gradle/src/main/resources/basic-java-library-module/application/src/main/java/org/acme/ApplicationConfigResource.java create mode 100644 integration-tests/gradle/src/main/resources/basic-java-library-module/application/src/test/java/org/acme/ApplicationConfigResourceTest.java create mode 100644 integration-tests/gradle/src/test/java/io/quarkus/gradle/ApplicationConfigurationTest.java diff --git a/extensions/smallrye-openapi/deployment/src/test/java/io/quarkus/smallrye/openapi/test/jaxrs/OpenApiDefaultPathTestCase.java b/extensions/smallrye-openapi/deployment/src/test/java/io/quarkus/smallrye/openapi/test/jaxrs/OpenApiDefaultPathTestCase.java index d3e7b87978a2e..b15d78db8ba31 100644 --- a/extensions/smallrye-openapi/deployment/src/test/java/io/quarkus/smallrye/openapi/test/jaxrs/OpenApiDefaultPathTestCase.java +++ b/extensions/smallrye-openapi/deployment/src/test/java/io/quarkus/smallrye/openapi/test/jaxrs/OpenApiDefaultPathTestCase.java @@ -34,7 +34,7 @@ public void testOpenApiPathAccessResource() { .then() .header("Content-Type", "application/json;charset=UTF-8") .body("openapi", Matchers.startsWith("3.0")) - .body("info.title", Matchers.equalTo("Generated API")) + .body("info.title", Matchers.equalTo("quarkus-smallrye-openapi-deployment API")) .body("tags.name[0]", Matchers.equalTo("test")) .body("paths.'/resource'.get.servers[0]", Matchers.hasKey("url")) .body("paths.'/resource'.get.security[0]", Matchers.hasKey("securityRequirement")) diff --git a/extensions/smallrye-openapi/deployment/src/test/java/io/quarkus/smallrye/openapi/test/jaxrs/OpenApiHttpRootDefaultPathTestCase.java b/extensions/smallrye-openapi/deployment/src/test/java/io/quarkus/smallrye/openapi/test/jaxrs/OpenApiHttpRootDefaultPathTestCase.java index b2b896eddd90f..1ccb3a7991fb1 100644 --- a/extensions/smallrye-openapi/deployment/src/test/java/io/quarkus/smallrye/openapi/test/jaxrs/OpenApiHttpRootDefaultPathTestCase.java +++ b/extensions/smallrye-openapi/deployment/src/test/java/io/quarkus/smallrye/openapi/test/jaxrs/OpenApiHttpRootDefaultPathTestCase.java @@ -33,7 +33,7 @@ public void testOpenApiPathAccessResource() { .then() .header("Content-Type", "application/json;charset=UTF-8") .body("openapi", Matchers.startsWith("3.0")) - .body("info.title", Matchers.equalTo("Generated API")) + .body("info.title", Matchers.equalTo("quarkus-smallrye-openapi-deployment API")) .body("paths", Matchers.hasKey("/foo/resource")); } } diff --git a/extensions/smallrye-openapi/deployment/src/test/java/io/quarkus/smallrye/openapi/test/jaxrs/OpenApiWithResteasyPathHttpRootDefaultPathTestCase.java b/extensions/smallrye-openapi/deployment/src/test/java/io/quarkus/smallrye/openapi/test/jaxrs/OpenApiWithResteasyPathHttpRootDefaultPathTestCase.java index 30aaf20833815..324811c1ac319 100644 --- a/extensions/smallrye-openapi/deployment/src/test/java/io/quarkus/smallrye/openapi/test/jaxrs/OpenApiWithResteasyPathHttpRootDefaultPathTestCase.java +++ b/extensions/smallrye-openapi/deployment/src/test/java/io/quarkus/smallrye/openapi/test/jaxrs/OpenApiWithResteasyPathHttpRootDefaultPathTestCase.java @@ -26,7 +26,7 @@ public void testOpenApiResteasyPathHttpRootDefaultPath() { .then() .header("Content-Type", "application/json;charset=UTF-8") .body("openapi", Matchers.startsWith("3.0")) - .body("info.title", Matchers.equalTo("Generated API")) + .body("info.title", Matchers.equalTo("quarkus-smallrye-openapi-deployment API")) .body("paths", Matchers.hasKey("/http-root-path/resteasy-path/resource")); } } diff --git a/extensions/smallrye-openapi/deployment/src/test/java/io/quarkus/smallrye/openapi/test/jaxrs/OpenApiWithResteasyPathTestCase.java b/extensions/smallrye-openapi/deployment/src/test/java/io/quarkus/smallrye/openapi/test/jaxrs/OpenApiWithResteasyPathTestCase.java index a8b113c025c7f..6f530cc49ce25 100644 --- a/extensions/smallrye-openapi/deployment/src/test/java/io/quarkus/smallrye/openapi/test/jaxrs/OpenApiWithResteasyPathTestCase.java +++ b/extensions/smallrye-openapi/deployment/src/test/java/io/quarkus/smallrye/openapi/test/jaxrs/OpenApiWithResteasyPathTestCase.java @@ -25,7 +25,7 @@ public void testOpenApiResteasyPath() { .then() .header("Content-Type", "application/json;charset=UTF-8") .body("openapi", Matchers.startsWith("3.0")) - .body("info.title", Matchers.equalTo("Generated API")) + .body("info.title", Matchers.equalTo("quarkus-smallrye-openapi-deployment API")) .body("paths", Matchers.hasKey("/foo/bar/resource")); } } diff --git a/extensions/smallrye-openapi/deployment/src/test/java/io/quarkus/smallrye/openapi/test/vertx/OpenApiDefaultPathTestCase.java b/extensions/smallrye-openapi/deployment/src/test/java/io/quarkus/smallrye/openapi/test/vertx/OpenApiDefaultPathTestCase.java index 258a9781194ea..1efed83abe595 100644 --- a/extensions/smallrye-openapi/deployment/src/test/java/io/quarkus/smallrye/openapi/test/vertx/OpenApiDefaultPathTestCase.java +++ b/extensions/smallrye-openapi/deployment/src/test/java/io/quarkus/smallrye/openapi/test/vertx/OpenApiDefaultPathTestCase.java @@ -31,7 +31,7 @@ public void testOpenApiPathAccessResource() { .then() .header("Content-Type", "application/json;charset=UTF-8") .body("openapi", Matchers.startsWith("3.0")) - .body("info.title", Matchers.equalTo("Generated API")) + .body("info.title", Matchers.equalTo("quarkus-smallrye-openapi-deployment API")) .body("paths", Matchers.hasKey("/resource")); } } diff --git a/extensions/smallrye-openapi/deployment/src/test/java/io/quarkus/smallrye/openapi/test/vertx/OpenApiHttpRootDefaultPathTestCase.java b/extensions/smallrye-openapi/deployment/src/test/java/io/quarkus/smallrye/openapi/test/vertx/OpenApiHttpRootDefaultPathTestCase.java index 337370cca6f79..1f9b5e59f651c 100644 --- a/extensions/smallrye-openapi/deployment/src/test/java/io/quarkus/smallrye/openapi/test/vertx/OpenApiHttpRootDefaultPathTestCase.java +++ b/extensions/smallrye-openapi/deployment/src/test/java/io/quarkus/smallrye/openapi/test/vertx/OpenApiHttpRootDefaultPathTestCase.java @@ -33,7 +33,7 @@ public void testOpenApiPathAccessResource() { .then() .header("Content-Type", "application/json;charset=UTF-8") .body("openapi", Matchers.startsWith("3.0")) - .body("info.title", Matchers.equalTo("Generated API")) + .body("info.title", Matchers.equalTo("quarkus-smallrye-openapi-deployment API")) .body("paths", Matchers.hasKey("/foo/resource")); } } diff --git a/extensions/smallrye-reactive-messaging-kafka/deployment/src/test/java/io/quarkus/smallrye/reactivemessaging/kafka/deployment/dev/KafkaDevServicesDevModeTestCase.java b/extensions/smallrye-reactive-messaging-kafka/deployment/src/test/java/io/quarkus/smallrye/reactivemessaging/kafka/deployment/dev/KafkaDevServicesDevModeTestCase.java index 7502113b40c51..887a01b0f04e5 100644 --- a/extensions/smallrye-reactive-messaging-kafka/deployment/src/test/java/io/quarkus/smallrye/reactivemessaging/kafka/deployment/dev/KafkaDevServicesDevModeTestCase.java +++ b/extensions/smallrye-reactive-messaging-kafka/deployment/src/test/java/io/quarkus/smallrye/reactivemessaging/kafka/deployment/dev/KafkaDevServicesDevModeTestCase.java @@ -28,7 +28,8 @@ public class KafkaDevServicesDevModeTestCase { "mp.messaging.outgoing.generated-price.topic=prices\n" + "mp.messaging.incoming.prices.connector=smallrye-kafka\n" + "mp.messaging.incoming.prices.health-readiness-enabled=false\n" + - "mp.messaging.incoming.prices.topic=prices\n"; + "mp.messaging.incoming.prices.topic=prices\n" + + "quarkus.application.name=\n"; @RegisterExtension public static QuarkusDevModeTest test = new QuarkusDevModeTest() diff --git a/independent-projects/bootstrap/core/src/main/java/io/quarkus/bootstrap/app/QuarkusBootstrap.java b/independent-projects/bootstrap/core/src/main/java/io/quarkus/bootstrap/app/QuarkusBootstrap.java index f4e5ab0a46a2a..32e55ddb209d7 100644 --- a/independent-projects/bootstrap/core/src/main/java/io/quarkus/bootstrap/app/QuarkusBootstrap.java +++ b/independent-projects/bootstrap/core/src/main/java/io/quarkus/bootstrap/app/QuarkusBootstrap.java @@ -102,7 +102,7 @@ private QuarkusBootstrap(Builder builder, ConfiguredClassLoading classLoadingCon this.additionalApplicationArchives = new ArrayList<>(builder.additionalApplicationArchives); this.excludeFromClassPath = new ArrayList<>(builder.excludeFromClassPath); this.projectRoot = builder.projectRoot != null ? builder.projectRoot.normalize() : null; - this.buildSystemProperties = builder.buildSystemProperties; + this.buildSystemProperties = builder.buildSystemProperties != null ? builder.buildSystemProperties : new Properties(); this.mode = builder.mode; this.offline = builder.offline; this.test = builder.test; @@ -159,7 +159,19 @@ public CuratedApplication bootstrap() throws BootstrapException { appModelFactory.setEnableClasspathCache(true); } } - return new CuratedApplication(this, appModelFactory.resolveAppModel(), classLoadingConfig); + CurationResult curationResult = appModelFactory.resolveAppModel(); + if (curationResult.getApplicationModel().getAppArtifact() != null) { + if (curationResult.getApplicationModel().getAppArtifact().getArtifactId() != null) { + buildSystemProperties.putIfAbsent("quarkus.application.name", + curationResult.getApplicationModel().getAppArtifact().getArtifactId()); + } + if (curationResult.getApplicationModel().getAppArtifact().getVersion() != null) { + buildSystemProperties.putIfAbsent("quarkus.application.version", + curationResult.getApplicationModel().getAppArtifact().getVersion()); + } + } + + return new CuratedApplication(this, curationResult, classLoadingConfig); } public static ConfiguredClassLoading createClassLoadingConfig(PathCollection applicationRoot, Mode mode, diff --git a/integration-tests/gradle/src/main/resources/basic-java-library-module/application/src/main/java/org/acme/ApplicationConfigResource.java b/integration-tests/gradle/src/main/resources/basic-java-library-module/application/src/main/java/org/acme/ApplicationConfigResource.java new file mode 100644 index 0000000000000..ca578869780f9 --- /dev/null +++ b/integration-tests/gradle/src/main/resources/basic-java-library-module/application/src/main/java/org/acme/ApplicationConfigResource.java @@ -0,0 +1,25 @@ +package org.acme; + + +import org.eclipse.microprofile.config.inject.ConfigProperty; + +import javax.ws.rs.GET; +import javax.ws.rs.Path; +import javax.ws.rs.Produces; +import javax.ws.rs.core.MediaType; + +@Path("/app-config") +public class ApplicationConfigResource { + + @ConfigProperty(name = "quarkus.application.name") + String name; + + @ConfigProperty(name = "quarkus.application.version") + String version; + + @GET + @Produces(MediaType.TEXT_PLAIN) + public String hello() { + return String.format("%s:%s", name, version); + } +} diff --git a/integration-tests/gradle/src/main/resources/basic-java-library-module/application/src/test/java/org/acme/ApplicationConfigResourceTest.java b/integration-tests/gradle/src/main/resources/basic-java-library-module/application/src/test/java/org/acme/ApplicationConfigResourceTest.java new file mode 100644 index 0000000000000..0e54e2de32b43 --- /dev/null +++ b/integration-tests/gradle/src/main/resources/basic-java-library-module/application/src/test/java/org/acme/ApplicationConfigResourceTest.java @@ -0,0 +1,21 @@ +package org.acme; + +import io.quarkus.test.junit.QuarkusTest; +import org.junit.jupiter.api.Test; + +import static io.restassured.RestAssured.given; +import static org.hamcrest.CoreMatchers.is; + +@QuarkusTest +public class ApplicationConfigResourceTest { + + @Test + public void testAppConfigEndpoint() { + given() + .when().get("/app-config") + .then() + .statusCode(200) + .body(is("application:1.0.0-SNAPSHOT")); + } + +} \ No newline at end of file diff --git a/integration-tests/gradle/src/test/java/io/quarkus/gradle/ApplicationConfigurationTest.java b/integration-tests/gradle/src/test/java/io/quarkus/gradle/ApplicationConfigurationTest.java new file mode 100644 index 0000000000000..f44453718537c --- /dev/null +++ b/integration-tests/gradle/src/test/java/io/quarkus/gradle/ApplicationConfigurationTest.java @@ -0,0 +1,20 @@ +package io.quarkus.gradle; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.io.File; + +import org.junit.jupiter.api.Test; + +public class ApplicationConfigurationTest extends QuarkusGradleWrapperTestBase { + + @Test + public void shouldSuccessfullyInjectApplicationConfigInTest() throws Exception { + File projectDir = getProjectDir("basic-java-library-module"); + + BuildResult testResult = runGradleWrapper(projectDir, "clean", ":application:test"); + + assertThat(testResult.getTasks().get(":application:test")).isEqualTo(BuildResult.SUCCESS_OUTCOME); + } + +} diff --git a/integration-tests/spring-web/src/test/java/io/quarkus/it/spring/web/openapi/OpenApiDefaultPathPMT.java b/integration-tests/spring-web/src/test/java/io/quarkus/it/spring/web/openapi/OpenApiDefaultPathPMT.java index 55b6613ee7954..fa87ce69da8da 100644 --- a/integration-tests/spring-web/src/test/java/io/quarkus/it/spring/web/openapi/OpenApiDefaultPathPMT.java +++ b/integration-tests/spring-web/src/test/java/io/quarkus/it/spring/web/openapi/OpenApiDefaultPathPMT.java @@ -34,7 +34,7 @@ public void testOpenApiPathAccessResource() { .then() .header("Content-Type", "application/json;charset=UTF-8") .body("openapi", Matchers.startsWith("3.0")) - .body("info.title", Matchers.equalTo("Generated API")) + .body("info.title", Matchers.equalTo("quarkus-integration-test-spring-web API")) .body("paths", Matchers.hasKey("/resource")); } }