Skip to content

Commit

Permalink
Test for APT processor support in DevMojo
Browse files Browse the repository at this point in the history
Related to quarkusio#1502
  • Loading branch information
FroMage committed Jan 15, 2024
1 parent 78035ad commit b1f9947
Show file tree
Hide file tree
Showing 19 changed files with 634 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1468,4 +1468,139 @@ public void testExternalReloadableArtifacts() throws Exception {
.atMost(TestUtils.getDefaultTimeout(), TimeUnit.MINUTES)
.until(() -> devModeClient.getHttpResponse("/hello").contains("BONJOUR!"));
}

@Test
public void testThatAptInClasspathWorks() throws MavenInvocationException, IOException {
testDir = initProject("projects/apt-in-classpath", "projects/project-apt-in-classpath");
run(true);

// wait until app is compiled and started
await()
.pollDelay(100, TimeUnit.MILLISECONDS)
.atMost(TestUtils.getDefaultTimeout(), TimeUnit.MINUTES)
.until(() -> devModeClient.getHttpResponse("/hello").contains("field/myEntity.field"));

// make sure annotations go to the right place
File entityMetamodelSourceFile = new File(testDir, "target/generated-sources/annotations/org/acme/MyEntity_.java");
assertThat(entityMetamodelSourceFile).exists().content().contains("FIELD");
File entityQuerySourceFile = new File(testDir, "target/generated-sources/annotations/org/acme/QMyEntity.java");
assertThat(entityQuerySourceFile).exists().content().contains("field");
File entityMetamodelClassFile = new File(testDir, "target/classes/org/acme/MyEntity_.class");
assertThat(entityMetamodelClassFile).exists();
File entityQueryClassFile = new File(testDir, "target/classes/org/acme/QMyEntity.class");
assertThat(entityQueryClassFile).exists();

// Edit the entity to change the field name
File source = new File(testDir, "src/main/java/org/acme/MyEntity.java");
filter(source, Collections.singletonMap("String field;", "String field2;"));

// Edit the "Hello" message for the new field.
source = new File(testDir, "src/main/java/org/acme/HelloResource.java");
filter(source, Collections.singletonMap("return MyEntity_.FIELD+\"/\"+QMyEntity.myEntity.field;",
"return MyEntity_.FIELD2+\"/\"+QMyEntity.myEntity.field2;"));

// Wait until we get "field2/field2"
await()
.pollDelay(100, TimeUnit.MILLISECONDS)
.atMost(TestUtils.getDefaultTimeout(), TimeUnit.MINUTES)
.until(() -> devModeClient.getHttpResponse("/hello").contains("field2/myEntity.field2"));

// make sure annotations go to the right place
assertThat(entityMetamodelSourceFile).exists().content().contains("FIELD2");
assertThat(entityQuerySourceFile).exists().content().contains("field2");
assertThat(entityMetamodelClassFile).exists();
assertThat(entityQueryClassFile).exists();
}

@Test
public void testThatAptInAnnotationProcessorPathsWorks() throws MavenInvocationException, IOException {
testDir = initProject("projects/apt-in-annotation-processor-paths",
"projects/project-apt-in-annotation-processor-paths");
run(true);

// same expectations as the classpath one: two APT plugins

// wait until app is compiled and started
await()
.pollDelay(100, TimeUnit.MILLISECONDS)
.atMost(TestUtils.getDefaultTimeout(), TimeUnit.MINUTES)
.until(() -> devModeClient.getHttpResponse("/hello").contains("field/myEntity.field"));

// make sure annotations go to the right place
File entityMetamodelSourceFile = new File(testDir, "target/generated-sources/annotations/org/acme/MyEntity_.java");
assertThat(entityMetamodelSourceFile).exists().content().contains("FIELD");
File entityQuerySourceFile = new File(testDir, "target/generated-sources/annotations/org/acme/QMyEntity.java");
assertThat(entityQuerySourceFile).exists().content().contains("field");
File entityMetamodelClassFile = new File(testDir, "target/classes/org/acme/MyEntity_.class");
assertThat(entityMetamodelClassFile).exists();
File entityQueryClassFile = new File(testDir, "target/classes/org/acme/QMyEntity.class");
assertThat(entityQueryClassFile).exists();

// Edit the entity to change the field name
File source = new File(testDir, "src/main/java/org/acme/MyEntity.java");
filter(source, Collections.singletonMap("String field;", "String field2;"));

// Edit the "Hello" message for the new field.
source = new File(testDir, "src/main/java/org/acme/HelloResource.java");
filter(source, Collections.singletonMap("return MyEntity_.FIELD+\"/\"+QMyEntity.myEntity.field;",
"return MyEntity_.FIELD2+\"/\"+QMyEntity.myEntity.field2;"));

// Wait until we get "field2/field2"
await()
.pollDelay(100, TimeUnit.MILLISECONDS)
.atMost(TestUtils.getDefaultTimeout(), TimeUnit.MINUTES)
.until(() -> devModeClient.getHttpResponse("/hello").contains("field2/myEntity.field2"));

// make sure annotations go to the right place
assertThat(entityMetamodelSourceFile).exists().content().contains("FIELD2");
assertThat(entityQuerySourceFile).exists().content().contains("field2");
assertThat(entityMetamodelClassFile).exists();
assertThat(entityQueryClassFile).exists();
}

@Test
public void testThatAptInAnnotationProcessorsWorks() throws MavenInvocationException, IOException {
testDir = initProject("projects/apt-in-annotation-processors",
"projects/project-apt-in-annotation-processors");
run(true);

// NOT the same expectations as the classpath one: only the Hibernate APT plugin

// wait until app is compiled and started
await()
.pollDelay(100, TimeUnit.MILLISECONDS)
.atMost(TestUtils.getDefaultTimeout(), TimeUnit.MINUTES)
.until(() -> devModeClient.getHttpResponse("/hello").contains("field"));

// make sure annotations go to the right place
File entityMetamodelSourceFile = new File(testDir, "target/generated-sources/annotations/org/acme/MyEntity_.java");
assertThat(entityMetamodelSourceFile).exists().content().contains("FIELD");
File entityQuerySourceFile = new File(testDir, "target/generated-sources/annotations/org/acme/QMyEntity.java");
assertThat(entityQuerySourceFile).doesNotExist();
File entityMetamodelClassFile = new File(testDir, "target/classes/org/acme/MyEntity_.class");
assertThat(entityMetamodelClassFile).exists();
File entityQueryClassFile = new File(testDir, "target/classes/org/acme/QMyEntity.class");
assertThat(entityQueryClassFile).doesNotExist();

// Edit the entity to change the field name
File source = new File(testDir, "src/main/java/org/acme/MyEntity.java");
filter(source, Collections.singletonMap("String field;", "String field2;"));

// Edit the "Hello" message for the new field.
source = new File(testDir, "src/main/java/org/acme/HelloResource.java");
filter(source, Collections.singletonMap("return MyEntity_.FIELD;",
"return MyEntity_.FIELD2;"));

// Wait until we get "field2"
await()
.pollDelay(100, TimeUnit.MILLISECONDS)
.atMost(TestUtils.getDefaultTimeout(), TimeUnit.MINUTES)
.until(() -> devModeClient.getHttpResponse("/hello").contains("field2"));

// make sure annotations go to the right place
assertThat(entityMetamodelSourceFile).exists().content().contains("FIELD2");
assertThat(entityQuerySourceFile).doesNotExist();
assertThat(entityMetamodelClassFile).exists();
assertThat(entityQueryClassFile).doesNotExist();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
OTHER_GREETING=Hola
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<groupId>org.acme</groupId>
<artifactId>acme</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<quarkus.platform.group-id>io.quarkus</quarkus.platform.group-id>
<quarkus.platform.artifact-id>quarkus-bom</quarkus.platform.artifact-id>
<quarkus.platform.version>@project.version@</quarkus.platform.version>
<quarkus-plugin.version>@project.version@</quarkus-plugin.version>
<compiler-plugin.version>${compiler-plugin.version}</compiler-plugin.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>${maven.compiler.source}</maven.compiler.source>
<maven.compiler.target>${maven.compiler.target}</maven.compiler.target>
<querydsl.version>5.0.0</querydsl.version>
</properties>
<dependencyManagement>
<dependencies>
<!-- insert managed dependencies here -->
<dependency>
<groupId>\${quarkus.platform.group-id}</groupId>
<artifactId>\${quarkus.platform.artifact-id}</artifactId>
<version>\${quarkus.platform.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<!-- insert test dependencies here -->
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-resteasy-reactive</artifactId>
</dependency>
<!-- Use this rather than quarkus-hibernate-orm which would force us to define a datasource we don't need -->
<dependency>
<groupId>jakarta.persistence</groupId>
<artifactId>jakarta.persistence-api</artifactId>
</dependency>
<dependency>
<groupId>com.querydsl</groupId>
<artifactId>querydsl-jpa</artifactId>
<version>\${querydsl.version}</version>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-junit5</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>\${compiler-plugin.version}</version>
<configuration>
<annotationProcessorPaths>
<path>
<groupId>org.hibernate.orm</groupId>
<artifactId>hibernate-jpamodelgen</artifactId>
</path>
<path>
<groupId>com.querydsl</groupId>
<artifactId>querydsl-apt</artifactId>
<version>\${querydsl.version}</version>
<classifier>jakarta</classifier>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
<plugin>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-maven-plugin</artifactId>
<version>\${quarkus-plugin.version}</version>
<executions>
<execution>
<goals>
<goal>generate-code</goal>
<goal>generate-code-tests</goal>
<goal>build</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<profiles>
<profile>
<id>native</id>
<properties>
<quarkus.package.type>native</quarkus.package.type>
</properties>
</profile>
<profile>
<id>customOutputDir</id>
<build>
<directory>target-other</directory>
</build>
</profile>
</profiles>
</project>
Original file line number Diff line number Diff line change
@@ -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 MyEntity_.FIELD+"/"+QMyEntity.myEntity.field;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.acme;

import jakarta.persistence.Id;
import jakarta.persistence.Entity;

@Entity
public class MyEntity {
public String field;
@Id
public String id;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Configuration file
quarkus.log.level=INFO
quarkus.log.file.enable=false
quarkus.log.console.format=%d{yyyy-MM-dd HH:mm:ss,SSS} %-5p [%c{3.}] (%t) %s%e%n
quarkus.log.file.format=%d{yyyy-MM-dd HH:mm:ss,SSS} %h %N[%i] %-5p [%c{3.}] (%t) %s%e%n
quarkus.log.category."io.quarkus".level=INFO
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 HelloResourceTest {

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

}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
OTHER_GREETING=Hola
Loading

0 comments on commit b1f9947

Please sign in to comment.