Skip to content

Commit

Permalink
Merge pull request #37329 from yrodiere/jpa-test-cleanup
Browse files Browse the repository at this point in the history
JPA test cleanup
  • Loading branch information
yrodiere authored Dec 5, 2023
2 parents 1c4a6a8 + 40e209b commit 45a7c9d
Show file tree
Hide file tree
Showing 69 changed files with 1,298 additions and 1,857 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,158 +1,102 @@
package io.quarkus.it.jpa.db2;

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

import jakarta.inject.Inject;
import jakarta.persistence.EntityManager;
import jakarta.persistence.EntityManagerFactory;
import jakarta.persistence.EntityTransaction;
import jakarta.persistence.TypedQuery;
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;

import io.quarkus.narayana.jta.QuarkusTransaction;

/**
* 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) {
EntityManager em;

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

//Store some well known Person instances we can then test on:
storeTestPersons(entityManagerFactory);
QuarkusTransaction.requiringNew().run(() -> {
persistNewPerson("Gizmo");
persistNewPerson("Quarkus");
persistNewPerson("Hibernate ORM");
});

//Load all persons and run some checks on the query results:
verifyListOfExistingPersons(entityManagerFactory);
QuarkusTransaction.requiringNew().run(() -> {
CriteriaBuilder cb = em.getCriteriaBuilder();

CriteriaQuery<Person> cq = cb.createQuery(Person.class);
Root<Person> from = cq.from(Person.class);
cq.select(from).orderBy(cb.asc(from.get("name")));
TypedQuery<Person> q = em.createQuery(cq);
List<Person> allpersons = q.getResultList();
if (allpersons.size() != 3) {
throw new RuntimeException("Incorrect number of results");
}
if (!allpersons.get(0).getName().equals("Gizmo")) {
throw new RuntimeException("Incorrect order of results");
}
StringBuilder sb = new StringBuilder("list of stored Person names:\n\t");
for (Person p : allpersons) {
p.describeFully(sb);
sb.append("\n\t");
if (p.getStatus() != Status.LIVING) {
throw new RuntimeException("Incorrect status " + p);
}
}
sb.append("\nList complete.\n");
System.out.print(sb);
});

//Try a JPA named query:
verifyJPANamedQuery(entityManagerFactory);

deleteAllPerson(entityManagerFactory);

}
QuarkusTransaction.requiringNew().run(() -> {
TypedQuery<Person> typedQuery = em.createNamedQuery(
"get_person_by_name", Person.class);
typedQuery.setParameter("name", "Quarkus");
final Person singleResult = typedQuery.getSingleResult();

if (!singleResult.getName().equals("Quarkus")) {
throw new RuntimeException("Wrong result from named JPA query");
}
});

private static void verifyJPANamedQuery(final EntityManagerFactory emf) {
EntityManager em = emf.createEntityManager();
EntityTransaction transaction = em.getTransaction();
transaction.begin();
TypedQuery<Person> typedQuery = em.createNamedQuery(
"get_person_by_name", Person.class);
typedQuery.setParameter("name", "Quarkus");
final Person singleResult = typedQuery.getSingleResult();

if (!singleResult.getName().equals("Quarkus")) {
throw new RuntimeException("Wrong result from named JPA query");
}

transaction.commit();
em.close();
}
cleanUpData();

private static void verifyListOfExistingPersons(final EntityManagerFactory emf) {
EntityManager em = emf.createEntityManager();
EntityTransaction transaction = em.getTransaction();
transaction.begin();
listExistingPersons(em);
transaction.commit();
em.close();
return "OK";
}

private static void storeTestPersons(final EntityManagerFactory emf) {
EntityManager em = emf.createEntityManager();
EntityTransaction transaction = em.getTransaction();
transaction.begin();
persistNewPerson(em, "Gizmo");
persistNewPerson(em, "Quarkus");
persistNewPerson(em, "Hibernate ORM");
transaction.commit();
em.close();
private void cleanUpData() {
QuarkusTransaction.requiringNew()
.run(() -> em.createNativeQuery("Delete from Person").executeUpdate());
}

private static void deleteAllPerson(final EntityManagerFactory emf) {
EntityManager em = emf.createEntityManager();
EntityTransaction transaction = em.getTransaction();
transaction.begin();
em.createNativeQuery("Delete from Person").executeUpdate();
transaction.commit();
em.close();
}

private static void listExistingPersons(EntityManager em) {
CriteriaBuilder cb = em.getCriteriaBuilder();

CriteriaQuery<Person> cq = cb.createQuery(Person.class);
Root<Person> from = cq.from(Person.class);
cq.select(from).orderBy(cb.asc(from.get("name")));
TypedQuery<Person> q = em.createQuery(cq);
List<Person> allpersons = q.getResultList();
if (allpersons.size() != 3) {
throw new RuntimeException("Incorrect number of results");
}
if (!allpersons.get(0).getName().equals("Gizmo")) {
throw new RuntimeException("Incorrect order of results");
}
StringBuilder sb = new StringBuilder("list of stored Person names:\n\t");
for (Person p : allpersons) {
p.describeFully(sb);
sb.append("\n\t");
if (p.getStatus() != Status.LIVING) {
throw new RuntimeException("Incorrect status " + p);
}
}
sb.append("\nList complete.\n");
System.out.print(sb);
}

private static void persistNewPerson(EntityManager entityManager, String name) {
private void persistNewPerson(String name) {
Person person = new Person();
person.setName(name);
person.setStatus(Status.LIVING);
person.setAddress(new SequencedAddress("Street " + randomName()));
entityManager.persist(person);
em.persist(person);
}

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
Loading

0 comments on commit 45a7c9d

Please sign in to comment.