forked from quarkusio/quarkus
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This allows you to run tests in a rollback only transaction. Fixes quarkusio#6463
- Loading branch information
1 parent
180b01f
commit 84d816c
Showing
8 changed files
with
188 additions
and
0 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
21 changes: 21 additions & 0 deletions
21
.../main/java/io/quarkus/hibernate/orm/deployment/test/HibernateTestTransactionCallback.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 |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package io.quarkus.hibernate.orm.deployment.test; | ||
|
||
import javax.enterprise.inject.spi.CDI; | ||
import javax.persistence.EntityManager; | ||
|
||
import io.quarkus.narayana.jta.runtime.test.TestTransactionCallback; | ||
|
||
public class HibernateTestTransactionCallback implements TestTransactionCallback { | ||
@Override | ||
public void postBegin() { | ||
|
||
} | ||
|
||
@Override | ||
public void preRollback() { | ||
for (EntityManager i : CDI.current().select(EntityManager.class)) { | ||
i.flush(); | ||
} | ||
|
||
} | ||
} |
1 change: 1 addition & 0 deletions
1
.../resources/META-INF/services/io.quarkus.narayana.jta.runtime.test.TestTransactionCallback
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 |
---|---|---|
@@ -0,0 +1 @@ | ||
io.quarkus.hibernate.orm.deployment.test.HibernateTestTransactionCallback |
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
41 changes: 41 additions & 0 deletions
41
...src/main/java/io/quarkus/narayana/jta/runtime/interceptor/TestTransactionInterceptor.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 |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package io.quarkus.narayana.jta.runtime.interceptor; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.ServiceLoader; | ||
|
||
import javax.interceptor.InvocationContext; | ||
import javax.transaction.UserTransaction; | ||
|
||
import io.quarkus.narayana.jta.runtime.test.TestTransactionCallback; | ||
|
||
public class TestTransactionInterceptor { | ||
|
||
static final List<TestTransactionCallback> CALLBACKS; | ||
|
||
static { | ||
List<TestTransactionCallback> callbacks = new ArrayList<>(); | ||
for (TestTransactionCallback i : ServiceLoader.load(TestTransactionCallback.class)) { | ||
callbacks.add(i); | ||
} | ||
CALLBACKS = callbacks; | ||
} | ||
|
||
public static Object intercept(UserTransaction userTransaction, InvocationContext context) throws Exception { | ||
try { | ||
userTransaction.begin(); | ||
for (TestTransactionCallback i : CALLBACKS) { | ||
i.postBegin(); | ||
} | ||
Object result = context.proceed(); | ||
for (TestTransactionCallback i : CALLBACKS) { | ||
i.preRollback(); | ||
} | ||
return result; | ||
} finally { | ||
userTransaction.rollback(); | ||
} | ||
|
||
} | ||
|
||
} |
9 changes: 9 additions & 0 deletions
9
...a/runtime/src/main/java/io/quarkus/narayana/jta/runtime/test/TestTransactionCallback.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 |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package io.quarkus.narayana.jta.runtime.test; | ||
|
||
public interface TestTransactionCallback { | ||
|
||
void postBegin(); | ||
|
||
void preRollback(); | ||
|
||
} |
33 changes: 33 additions & 0 deletions
33
...-tests/hibernate-orm-panache/src/test/java/io/quarkus/it/panache/TestTransactionTest.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 |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package io.quarkus.it.panache; | ||
|
||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import io.quarkus.test.TestTransaction; | ||
import io.quarkus.test.junit.QuarkusTest; | ||
|
||
/** | ||
* Tests that @TestTransaction works as expected | ||
*/ | ||
@QuarkusTest | ||
@TestTransaction | ||
public class TestTransactionTest { | ||
|
||
@Test | ||
@TestTransaction | ||
public void test1() { | ||
Assertions.assertEquals(0, Beer.find("name", "Lager").count()); | ||
Beer b = new Beer(); | ||
b.name = "Lager"; | ||
Beer.persist(b); | ||
} | ||
|
||
@Test | ||
@TestTransaction | ||
public void test2() { | ||
Assertions.assertEquals(0, Beer.find("name", "Lager").count()); | ||
Beer b = new Beer(); | ||
b.name = "Lager"; | ||
Beer.persist(b); | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
test-framework/common/src/main/java/io/quarkus/test/TestTransaction.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 |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package io.quarkus.test; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
import javax.interceptor.InterceptorBinding; | ||
|
||
/** | ||
* Indicates that this method should be run in a rollback only transaction. | ||
* | ||
* This allows the test method to modify the database as required, and then have | ||
* these changes reverted at the end of the method. | ||
* | ||
*/ | ||
@InterceptorBinding | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Target({ ElementType.TYPE, ElementType.METHOD }) | ||
public @interface TestTransaction { | ||
} |