-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #37329 from yrodiere/jpa-test-cleanup
JPA test cleanup
- Loading branch information
Showing
69 changed files
with
1,298 additions
and
1,857 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
176 changes: 60 additions & 116 deletions
176
...ation-tests/jpa-db2/src/main/java/io/quarkus/it/jpa/db2/JPAFunctionalityTestEndpoint.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.