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
a4f910f
commit f7b47c8
Showing
5 changed files
with
131 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
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
18 changes: 18 additions & 0 deletions
18
...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,18 @@ | ||
package io.quarkus.narayana.jta.runtime.interceptor; | ||
|
||
import javax.interceptor.InvocationContext; | ||
import javax.transaction.UserTransaction; | ||
|
||
public class TestTransactionInterceptor { | ||
|
||
public static Object intercept(UserTransaction userTransaction, InvocationContext context) throws Exception { | ||
try { | ||
userTransaction.begin(); | ||
return context.proceed(); | ||
} finally { | ||
userTransaction.rollback(); | ||
} | ||
|
||
} | ||
|
||
} |
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. | ||
* | ||
* The 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 { | ||
} |