Skip to content

Commit

Permalink
Migrate all JPA tests to RestEasy Reactive
Browse files Browse the repository at this point in the history
Because:

1. We recommend that people use RestEasy Reactive, so we might as well
   take our own advice.
2. I've had enough with copy/pasted, manual exception handling
   ("reportException(...)"), especially since it's buggy (it doesn't
   take into account the fact that exceptions may have a null message,
   e.g. NPEs);
3. I don't want to deal with servlets on a daily basis, thank you very
   much.
  • Loading branch information
yrodiere committed Nov 28, 2023
1 parent 7d4f0c6 commit 81a2cce
Show file tree
Hide file tree
Showing 42 changed files with 345 additions and 766 deletions.
8 changes: 4 additions & 4 deletions integration-tests/jpa-db2/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@
<dependencies>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-undertow</artifactId>
<artifactId>quarkus-hibernate-orm</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-hibernate-orm</artifactId>
<artifactId>quarkus-jdbc-db2</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-jdbc-db2</artifactId>
<artifactId>quarkus-resteasy-reactive</artifactId>
</dependency>

<dependency>
Expand Down Expand Up @@ -81,7 +81,7 @@
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-undertow-deployment</artifactId>
<artifactId>quarkus-resteasy-reactive-deployment</artifactId>
<version>${project.version}</version>
<type>pom</type>
<scope>test</scope>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package io.quarkus.it.jpa.db2;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.UUID;

Expand All @@ -13,36 +12,23 @@
import jakarta.persistence.criteria.CriteriaBuilder;
import jakarta.persistence.criteria.CriteriaQuery;
import jakarta.persistence.criteria.Root;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.core.MediaType;

/**
* Various tests covering JPA functionality. All tests should work in both standard JVM and native mode.
*/
@SuppressWarnings("serial")
@WebServlet(name = "JPATestBootstrapEndpoint", urlPatterns = "/jpa/testfunctionality")
public class JPAFunctionalityTestEndpoint extends HttpServlet {
@Path("/jpa/testfunctionality")
@Produces(MediaType.TEXT_PLAIN)
public class JPAFunctionalityTestEndpoint {

@Inject
EntityManagerFactory entityManagerFactory;

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
try {
doStuffWithHibernate(entityManagerFactory);
} catch (Exception e) {
reportException("An error occurred while performing Hibernate operations", e, resp);
}
resp.getWriter().write("OK");
}

/**
* Lists the various operations we want to test for:
*/
private static void doStuffWithHibernate(EntityManagerFactory entityManagerFactory) {

@GET
public String test() throws IOException {
//Cleanup any existing data:
deleteAllPerson(entityManagerFactory);

Expand All @@ -57,6 +43,7 @@ private static void doStuffWithHibernate(EntityManagerFactory entityManagerFacto

deleteAllPerson(entityManagerFactory);

return "OK";
}

private static void verifyJPANamedQuery(final EntityManagerFactory emf) {
Expand Down Expand Up @@ -143,16 +130,4 @@ private static String randomName() {
return UUID.randomUUID().toString();
}

private void reportException(String errorMessage, final Exception e, final HttpServletResponse resp) throws IOException {
final PrintWriter writer = resp.getWriter();
if (errorMessage != null) {
writer.write(errorMessage);
writer.write(" ");
}
writer.write(e.toString());
writer.append("\n\t");
e.printStackTrace(writer);
writer.append("\n\t");
}

}
4 changes: 2 additions & 2 deletions integration-tests/jpa-derby/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
<!-- .. and some REST endpoints -->
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-undertow</artifactId>
<artifactId>quarkus-resteasy-reactive</artifactId>
</dependency>

<!-- test dependencies -->
Expand Down Expand Up @@ -85,7 +85,7 @@
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-undertow-deployment</artifactId>
<artifactId>quarkus-resteasy-reactive-deployment</artifactId>
<version>${project.version}</version>
<type>pom</type>
<scope>test</scope>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package io.quarkus.it.jpa.derby;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.UUID;

Expand All @@ -13,36 +11,24 @@
import jakarta.persistence.criteria.CriteriaBuilder;
import jakarta.persistence.criteria.CriteriaQuery;
import jakarta.persistence.criteria.Root;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.core.MediaType;

/**
* Basic test running JPA with the Derby database.
* The application can work in either standard JVM or in native mode, while we run H2 as a separate JVM process.
*/
@WebServlet(name = "JPATestBootstrapEndpoint", urlPatterns = "/jpa-derby/testfunctionality")
public class JPAFunctionalityTestEndpoint extends HttpServlet {
@Path("/jpa-derby/testfunctionality")
@Produces(MediaType.TEXT_PLAIN)
public class JPAFunctionalityTestEndpoint {

@Inject
EntityManagerFactory entityManagerFactory;

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
try {
doStuffWithHibernate(entityManagerFactory);
} catch (Exception e) {
reportException("An error occurred while performing Hibernate operations", e, resp);
}
resp.getWriter().write("OK");
}

/**
* Lists the various operations we want to test for:
*/
private static void doStuffWithHibernate(EntityManagerFactory entityManagerFactory) {

@GET
public String test() {
//Cleanup any existing data:
deleteAllPerson(entityManagerFactory);

Expand All @@ -59,11 +45,11 @@ private static void doStuffWithHibernate(EntityManagerFactory entityManagerFacto

deleteAllPerson(entityManagerFactory);

return "OK";
}

private static void verifyHqlFetch(EntityManagerFactory emf) {
EntityManager em = emf.createEntityManager();
try {
try (EntityManager em = emf.createEntityManager()) {
EntityTransaction transaction = em.getTransaction();
try {
transaction.begin();
Expand All @@ -77,8 +63,6 @@ private static void verifyHqlFetch(EntityManagerFactory emf) {
}
throw e;
}
} finally {
em.close();
}
}

Expand Down Expand Up @@ -161,16 +145,4 @@ private static String randomName() {
return UUID.randomUUID().toString();
}

private void reportException(String errorMessage, final Exception e, final HttpServletResponse resp) throws IOException {
final PrintWriter writer = resp.getWriter();
if (errorMessage != null) {
writer.write(errorMessage);
writer.write(" ");
}
writer.write(e.toString());
writer.append("\n\t");
e.printStackTrace(writer);
writer.append("\n\t");
}

}
4 changes: 2 additions & 2 deletions integration-tests/jpa-h2-embedded/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
<!-- .. and some REST endpoints -->
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-undertow</artifactId>
<artifactId>quarkus-resteasy-reactive</artifactId>
</dependency>

<!-- test dependencies -->
Expand Down Expand Up @@ -80,7 +80,7 @@
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-undertow-deployment</artifactId>
<artifactId>quarkus-resteasy-reactive-deployment</artifactId>
<version>${project.version}</version>
<type>pom</type>
<scope>test</scope>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,44 +1,28 @@
package io.quarkus.it.jpa.h2;

import java.io.IOException;
import java.io.PrintWriter;

import jakarta.inject.Inject;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.core.MediaType;

import org.hibernate.SessionFactory;
import org.hibernate.engine.spi.SessionFactoryImplementor;

import io.quarkus.hibernate.orm.runtime.config.DialectVersions;

@WebServlet(name = "DialectEndpoint", urlPatterns = "/dialect/version")
public class DialectEndpoint extends HttpServlet {
@Path("/dialect/version")
@Produces(MediaType.TEXT_PLAIN)
public class DialectEndpoint {
@Inject
SessionFactory sessionFactory;

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
try {
var version = sessionFactory.unwrap(SessionFactoryImplementor.class).getJdbcServices().getDialect().getVersion();
resp.getWriter().write(DialectVersions.toString(version));
} catch (Exception e) {
reportException("Failed to retrieve dialect version", e, resp);
}
}

private void reportException(String errorMessage, final Exception e, final HttpServletResponse resp) throws IOException {
final PrintWriter writer = resp.getWriter();
if (errorMessage != null) {
writer.write(errorMessage);
writer.write(" ");
}
writer.write(e.toString());
writer.append("\n\t");
e.printStackTrace(writer);
writer.append("\n\t");
@GET
public String test() throws IOException {
var version = sessionFactory.unwrap(SessionFactoryImplementor.class).getJdbcServices().getDialect().getVersion();
return DialectVersions.toString(version);
}

}
Loading

0 comments on commit 81a2cce

Please sign in to comment.