Skip to content

Commit

Permalink
Merge pull request #8930 from aloubyansky/gradle-inject-test-tesources
Browse files Browse the repository at this point in the history
Gradle: inject test resources before the main resources for testing
  • Loading branch information
geoand authored May 1, 2020
2 parents c0386bb + 8f9bcec commit 06b631e
Show file tree
Hide file tree
Showing 14 changed files with 323 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package io.quarkus.gradle;

import java.io.File;
import java.nio.file.Path;

import org.gradle.testkit.runner.BuildResult;
import org.gradle.testkit.runner.GradleRunner;
import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;


public class TestResourcesVsMainResourcesTest extends QuarkusGradleTestBase {

@Test
public void test() throws Exception {

final File projectDir = getProjectDir("test-resources-vs-main-resources");

BuildResult build = GradleRunner.create()
.forwardOutput()
.withPluginClasspath()
.withArguments(arguments("test"))
.withProjectDir(projectDir)
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
plugins {
id 'java'
id 'io.quarkus'
}

repositories {
mavenLocal()
mavenCentral()
}

dependencies {
implementation enforcedPlatform("${quarkusPlatformGroupId}:${quarkusPlatformArtifactId}:${quarkusPlatformVersion}")
implementation 'io.quarkus:quarkus-resteasy'

testImplementation 'io.quarkus:quarkus-junit5'
testImplementation 'io.rest-assured:rest-assured'
}

group 'org.acme'
version '1.0.0-SNAPSHOT'

compileJava {
options.encoding = 'UTF-8'
options.compilerArgs << '-parameters'
}

compileTestJava {
options.encoding = 'UTF-8'
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
quarkusPlatformArtifactId=quarkus-bom
quarkusPlatformGroupId=io.quarkus
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
pluginManagement {
repositories {
mavenLocal()
mavenCentral()
gradlePluginPortal()
}
plugins {
id 'io.quarkus' version "${quarkusPluginVersion}"
}
}
rootProject.name='code-with-quarkus'
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package org.acme;

import io.quarkus.arc.config.ConfigProperties;

@ConfigProperties(prefix = "example")
public class Config {
public String message;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package org.acme;

import javax.inject.Inject;
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 ExampleResource {

@Inject
Config config;

@GET
@Produces(MediaType.TEXT_PLAIN)
public String hello() {
return config.message;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package org.acme;

import javax.inject.Inject;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import org.eclipse.microprofile.config.inject.ConfigProperty;

@Path("/test-only")
public class TestOnlyResource {

@ConfigProperty(name = "test-only")
String testOnly;

@GET
@Produces(MediaType.TEXT_PLAIN)
public String hello() {
return testOnly;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>code-with-quarkus - 1.0.0-SNAPSHOT</title>
<style>
h1, h2, h3, h4, h5, h6 {
margin-bottom: 0.5rem;
font-weight: 400;
line-height: 1.5;
}

h1 {
font-size: 2.5rem;
}

h2 {
font-size: 2rem
}

h3 {
font-size: 1.75rem
}

h4 {
font-size: 1.5rem
}

h5 {
font-size: 1.25rem
}

h6 {
font-size: 1rem
}

.lead {
font-weight: 300;
font-size: 2rem;
}

.banner {
font-size: 2.7rem;
margin: 0;
padding: 2rem 1rem;
background-color: #00A1E2;
color: white;
}

body {
margin: 0;
font-family: -apple-system, system-ui, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol", "Noto Color Emoji";
}

code {
font-family: SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace;
font-size: 87.5%;
color: #e83e8c;
word-break: break-word;
}

.left-column {
padding: .75rem;
max-width: 75%;
min-width: 55%;
}

.right-column {
padding: .75rem;
max-width: 25%;
}

.container {
display: flex;
width: 100%;
}

li {
margin: 0.75rem;
}

.right-section {
margin-left: 1rem;
padding-left: 0.5rem;
}

.right-section h3 {
padding-top: 0;
font-weight: 200;
}

.right-section ul {
border-left: 0.3rem solid #00A1E2;
list-style-type: none;
padding-left: 0;
}

</style>
</head>
<body>

<div class="banner lead">
Your new Cloud-Native application is ready!
</div>

<div class="container">
<div class="left-column">
<p class="lead"> Congratulations, you have created a new Quarkus application.</p>

<h2>Why do you see this?</h2>

<p>This page is served by Quarkus. The source is in
<code>src/main/resources/META-INF/resources/index.html</code>.</p>

<h2>What can I do from here?</h2>

<p>If not already done, run the application in <em>dev mode</em> using: <code>mvn compile quarkus:dev</code>.
</p>
<ul>
<li>Add REST resources, Servlets, functions and other services in <code>src/main/java</code>.</li>
<li>Your static assets are located in <code>src/main/resources/META-INF/resources</code>.</li>
<li>Configure your application in <code>src/main/resources/application.properties</code>.
</li>
</ul>

<h2>How do I get rid of this page?</h2>
<p>Just delete the <code>src/main/resources/META-INF/resources/index.html</code> file.</p>
</div>
<div class="right-column">
<div class="right-section">
<h3>Application</h3>
<ul>
<li>GroupId: org.acme</li>
<li>ArtifactId: code-with-quarkus</li>
<li>Version: 1.0.0-SNAPSHOT</li>
<li>Quarkus Version: 1.3.2.Final</li>
</ul>
</div>
<div class="right-section">
<h3>Next steps</h3>
<ul>
<li><a href="https://quarkus.io/guides/maven-tooling.html" target="_blank">Setup your IDE</a></li>
<li><a href="https://quarkus.io/guides/getting-started.html" target="_blank">Getting started</a></li>
<li><a href="https://quarkus.io" target="_blank">Quarkus Web Site</a></li>
</ul>
</div>
</div>
</div>


</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Configuration file
# key = value
example.message=${MY_PROD_ENV_VAR}
app-only=App only
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
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 ExampleResourceTest {

@Test
public void testHelloEndpoint() {
given()
.when().get("/hello")
.then()
.statusCode(200)
.body(is("Hello from Test"));
}

@Test
public void testTestOnly() {
given()
.when().get("test-only")
.then()
.statusCode(200)
.body(is("Test only"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Configuration file
# key = value
example.message=Hello from Test
test-only=Test only

Original file line number Diff line number Diff line change
Expand Up @@ -191,11 +191,11 @@ public AppModel resolveModel(AppArtifact appArtifact) throws AppModelResolverExc
JavaPluginConvention javaConvention = convention.findPlugin(JavaPluginConvention.class);
if (javaConvention != null) {
final SourceSet mainSourceSet = javaConvention.getSourceSets().getByName(SourceSet.MAIN_SOURCE_SET_NAME);
final List<Path> list = new ArrayList<>(1);
PathsCollection.Builder paths = PathsCollection.builder();
mainSourceSet.getOutput().filter(s -> s.exists()).forEach(f -> {
list.add(f.toPath());
paths.add(f.toPath());
});
appArtifact.setPaths(PathsCollection.of(list.toArray(new Path[0])));
appArtifact.setPaths(paths.build());
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ void beforeTest(Test task) {
try {
final Map<String, Object> props = task.getSystemProperties();

final AppModel deploymentDeps = getAppModelResolver(LaunchMode.TEST)
final AppModel appModel = getAppModelResolver(LaunchMode.TEST)
.resolveModel(getAppArtifact());
final Path serializedModel = QuarkusGradleUtils.serializeAppModel(deploymentDeps, task);
final Path serializedModel = QuarkusGradleUtils.serializeAppModel(appModel, task);
props.put(BootstrapConstants.SERIALIZED_APP_MODEL, serializedModel.toString());

final String nativeRunner = task.getProject().getBuildDir().toPath().resolve(finalName() + "-runner")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,15 @@ private ExtensionState doJavaStart(ExtensionContext context) throws Throwable {

if (!appClassLocation.equals(testClassLocation)) {
runnerBuilder.addAdditionalApplicationArchive(AdditionalDependency.test(testClassLocation));
// if test classes is a dir, we should also check whether test resources dir exists as a separate dir (gradle)
// TODO: this whole app/test path resolution logic is pretty dumb, it needs be re-worked using proper workspace discovery
if (Files.isDirectory(testClassLocation)) {
final Path testResourcesLocation = testClassLocation.getParent().getParent().getParent()
.resolve("resources").resolve("test");
if (Files.exists(testResourcesLocation)) {
runnerBuilder.addAdditionalApplicationArchive(AdditionalDependency.test(testResourcesLocation));
}
}
}

CuratedApplication curatedApplication = runnerBuilder
Expand Down

0 comments on commit 06b631e

Please sign in to comment.