From a4ad840478008d944c34dc677b8d913a0ffd28e6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yoann=20Rodi=C3=A8re?= Date: Tue, 1 Mar 2022 14:04:25 +0100 Subject: [PATCH] Introduce a common test util for transactions in Hibernate ORM unit tests --- .../hibernate/orm/TransactionTestUtils.java | 32 +++++++++++++++++++ ...HibernateEntityEnhancerFinalFieldTest.java | 16 ++-------- .../packages/PackageLevelAnnotationTest.java | 16 ++-------- .../PublicFieldAccessFinalFieldTest.java | 16 ++-------- 4 files changed, 41 insertions(+), 39 deletions(-) create mode 100644 extensions/hibernate-orm/deployment/src/test/java/io/quarkus/hibernate/orm/TransactionTestUtils.java diff --git a/extensions/hibernate-orm/deployment/src/test/java/io/quarkus/hibernate/orm/TransactionTestUtils.java b/extensions/hibernate-orm/deployment/src/test/java/io/quarkus/hibernate/orm/TransactionTestUtils.java new file mode 100644 index 00000000000000..7f57554a2dbfcf --- /dev/null +++ b/extensions/hibernate-orm/deployment/src/test/java/io/quarkus/hibernate/orm/TransactionTestUtils.java @@ -0,0 +1,32 @@ +package io.quarkus.hibernate.orm; + +import javax.transaction.HeuristicMixedException; +import javax.transaction.HeuristicRollbackException; +import javax.transaction.NotSupportedException; +import javax.transaction.RollbackException; +import javax.transaction.SystemException; +import javax.transaction.UserTransaction; + +public class TransactionTestUtils { + + public static void inTransaction(UserTransaction transaction, Runnable runnable) { + try { + transaction.begin(); + try { + runnable.run(); + transaction.commit(); + } catch (Throwable t) { + try { + transaction.rollback(); + } catch (Throwable t2) { + t.addSuppressed(t2); + } + throw t; + } + } catch (SystemException | NotSupportedException | RollbackException | HeuristicMixedException + | HeuristicRollbackException e) { + throw new IllegalStateException("Transaction exception", e); + } + } + +} diff --git a/extensions/hibernate-orm/deployment/src/test/java/io/quarkus/hibernate/orm/enhancer/HibernateEntityEnhancerFinalFieldTest.java b/extensions/hibernate-orm/deployment/src/test/java/io/quarkus/hibernate/orm/enhancer/HibernateEntityEnhancerFinalFieldTest.java index 9d2df3696772ce..13a2287eec7c6b 100644 --- a/extensions/hibernate-orm/deployment/src/test/java/io/quarkus/hibernate/orm/enhancer/HibernateEntityEnhancerFinalFieldTest.java +++ b/extensions/hibernate-orm/deployment/src/test/java/io/quarkus/hibernate/orm/enhancer/HibernateEntityEnhancerFinalFieldTest.java @@ -20,14 +20,13 @@ import javax.persistence.EntityManager; import javax.persistence.GeneratedValue; import javax.persistence.Id; -import javax.transaction.NotSupportedException; -import javax.transaction.SystemException; import javax.transaction.UserTransaction; import org.hibernate.annotations.Immutable; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.RegisterExtension; +import io.quarkus.hibernate.orm.TransactionTestUtils; import io.quarkus.test.QuarkusUnitTest; /** @@ -41,6 +40,7 @@ public class HibernateEntityEnhancerFinalFieldTest { @RegisterExtension static QuarkusUnitTest runner = new QuarkusUnitTest() .withApplicationRoot((jar) -> jar + .addClass(TransactionTestUtils.class) .addClasses( EntityWithFinalField.class, EntityWithEmbeddedIdWithFinalField.class, EntityWithEmbeddedIdWithFinalField.EmbeddableId.class, @@ -143,17 +143,7 @@ public void embeddableNonIdWithFinalField_smokeTest() { } private void inTransaction(Runnable runnable) { - try { - transaction.begin(); - try { - runnable.run(); - transaction.commit(); - } catch (Exception e) { - transaction.rollback(); - } - } catch (SystemException | NotSupportedException e) { - throw new IllegalStateException("Transaction exception", e); - } + TransactionTestUtils.inTransaction(transaction, runnable); } @Entity(name = "withfinal") diff --git a/extensions/hibernate-orm/deployment/src/test/java/io/quarkus/hibernate/orm/packages/PackageLevelAnnotationTest.java b/extensions/hibernate-orm/deployment/src/test/java/io/quarkus/hibernate/orm/packages/PackageLevelAnnotationTest.java index 20f2da92decbb1..b299c86075d082 100644 --- a/extensions/hibernate-orm/deployment/src/test/java/io/quarkus/hibernate/orm/packages/PackageLevelAnnotationTest.java +++ b/extensions/hibernate-orm/deployment/src/test/java/io/quarkus/hibernate/orm/packages/PackageLevelAnnotationTest.java @@ -4,13 +4,12 @@ import javax.inject.Inject; import javax.persistence.EntityManager; -import javax.transaction.NotSupportedException; -import javax.transaction.SystemException; import javax.transaction.UserTransaction; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.RegisterExtension; +import io.quarkus.hibernate.orm.TransactionTestUtils; import io.quarkus.test.QuarkusUnitTest; public class PackageLevelAnnotationTest { @@ -18,6 +17,7 @@ public class PackageLevelAnnotationTest { @RegisterExtension static QuarkusUnitTest runner = new QuarkusUnitTest() .withApplicationRoot((jar) -> jar + .addClass(TransactionTestUtils.class) .addPackage(PackageLevelAnnotationTest.class.getPackage()) .addAsResource("application.properties")); @@ -56,16 +56,6 @@ public void test() { } private void inTransaction(Runnable runnable) { - try { - transaction.begin(); - try { - runnable.run(); - transaction.commit(); - } catch (Exception e) { - transaction.rollback(); - } - } catch (SystemException | NotSupportedException e) { - throw new IllegalStateException("Transaction exception", e); - } + TransactionTestUtils.inTransaction(transaction, runnable); } } diff --git a/extensions/hibernate-orm/deployment/src/test/java/io/quarkus/hibernate/orm/publicfields/PublicFieldAccessFinalFieldTest.java b/extensions/hibernate-orm/deployment/src/test/java/io/quarkus/hibernate/orm/publicfields/PublicFieldAccessFinalFieldTest.java index 1f7a6ac0a80539..aaed67aa68cadb 100644 --- a/extensions/hibernate-orm/deployment/src/test/java/io/quarkus/hibernate/orm/publicfields/PublicFieldAccessFinalFieldTest.java +++ b/extensions/hibernate-orm/deployment/src/test/java/io/quarkus/hibernate/orm/publicfields/PublicFieldAccessFinalFieldTest.java @@ -20,14 +20,13 @@ import javax.persistence.EntityManager; import javax.persistence.GeneratedValue; import javax.persistence.Id; -import javax.transaction.NotSupportedException; -import javax.transaction.SystemException; import javax.transaction.UserTransaction; import org.hibernate.annotations.Immutable; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.RegisterExtension; +import io.quarkus.hibernate.orm.TransactionTestUtils; import io.quarkus.test.QuarkusUnitTest; /** @@ -41,6 +40,7 @@ public class PublicFieldAccessFinalFieldTest { @RegisterExtension static QuarkusUnitTest runner = new QuarkusUnitTest() .withApplicationRoot((jar) -> jar + .addClass(TransactionTestUtils.class) .addClasses( EntityWithFinalField.class, EntityWithEmbeddedIdWithFinalField.class, EntityWithEmbeddedIdWithFinalField.EmbeddableId.class, @@ -143,17 +143,7 @@ public void embeddableNonIdWithFinalField_smokeTest() { } private void inTransaction(Runnable runnable) { - try { - transaction.begin(); - try { - runnable.run(); - transaction.commit(); - } catch (Exception e) { - transaction.rollback(); - } - } catch (SystemException | NotSupportedException e) { - throw new IllegalStateException("Transaction exception", e); - } + TransactionTestUtils.inTransaction(transaction, runnable); } @Entity(name = "withfinal")