Skip to content

Commit

Permalink
Export application name and version in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
glefloch committed Apr 6, 2022
1 parent c393835 commit 5d07ce8
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 = new Properties(builder.buildSystemProperties);
this.mode = builder.mode;
this.offline = builder.offline;
this.test = builder.test;
Expand Down Expand Up @@ -159,7 +159,20 @@ public CuratedApplication bootstrap() throws BootstrapException {
appModelFactory.setEnableClasspathCache(true);
}
}
return new CuratedApplication(this, appModelFactory.resolveAppModel(), classLoadingConfig);
CurationResult curationResult = appModelFactory.resolveAppModel();
if (curationResult.getApplicationModel().getApplicationModule() != null
&& curationResult.getApplicationModel().getApplicationModule().getId() != null) {
if (curationResult.getApplicationModel().getApplicationModule().getId().getArtifactId() != null) {
buildSystemProperties.putIfAbsent("quarkus.application.name",
curationResult.getApplicationModel().getApplicationModule().getId().getArtifactId());
}
if (curationResult.getApplicationModel().getApplicationModule().getId().getVersion() != null) {
buildSystemProperties.putIfAbsent("quarkus.application.version",
curationResult.getApplicationModel().getApplicationModule().getId().getVersion());
}
}

return new CuratedApplication(this, curationResult, classLoadingConfig);
}

public static ConfiguredClassLoading createClassLoadingConfig(PathCollection applicationRoot, Mode mode,
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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"));
}

}
Original file line number Diff line number Diff line change
@@ -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);
}

}

0 comments on commit 5d07ce8

Please sign in to comment.