Skip to content

Commit

Permalink
Introduce a common test util for transactions in Hibernate ORM unit t…
Browse files Browse the repository at this point in the history
…ests
  • Loading branch information
yrodiere committed Mar 2, 2022
1 parent ac77f9a commit a4ad840
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 39 deletions.
Original file line number Diff line number Diff line change
@@ -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);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -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,
Expand Down Expand Up @@ -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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,20 @@

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 {

@RegisterExtension
static QuarkusUnitTest runner = new QuarkusUnitTest()
.withApplicationRoot((jar) -> jar
.addClass(TransactionTestUtils.class)
.addPackage(PackageLevelAnnotationTest.class.getPackage())
.addAsResource("application.properties"));

Expand Down Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -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,
Expand Down Expand Up @@ -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")
Expand Down

0 comments on commit a4ad840

Please sign in to comment.