-
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.
Test @TransactionScoped with QuarkusTransaction
- Loading branch information
Showing
6 changed files
with
245 additions
and
16 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
85 changes: 85 additions & 0 deletions
85
.../test/java/io/quarkus/narayana/jta/TransactionScopeQuarkusTransactionBeginCommitTest.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,85 @@ | ||
package io.quarkus.narayana.jta; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
||
import jakarta.enterprise.context.ContextNotActiveException; | ||
import jakarta.inject.Inject; | ||
import jakarta.transaction.Transaction; | ||
import jakarta.transaction.TransactionManager; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import io.quarkus.test.junit.QuarkusTest; | ||
|
||
@QuarkusTest | ||
class TransactionScopeQuarkusTransactionBeginCommitTest { | ||
|
||
@Inject | ||
TransactionManager tm; | ||
|
||
@Inject | ||
TransactionScopedBean beanTransactional; | ||
|
||
@Inject | ||
TransactionBeanWithEvents beanEvents; | ||
|
||
@Test | ||
void transactionScopedInTransaction() throws Exception { | ||
TransactionScopedBean.resetCounters(); | ||
|
||
QuarkusTransaction.begin(); | ||
beanTransactional.setValue(42); | ||
assertEquals(1, TransactionScopedBean.getInitializedCount(), "Expected @PostConstruct to be invoked"); | ||
assertEquals(42, beanTransactional.getValue(), "Transaction scope did not save the value"); | ||
Transaction suspendedTransaction = tm.suspend(); | ||
|
||
assertThrows(ContextNotActiveException.class, () -> { | ||
beanTransactional.getValue(); | ||
}, "Not expecting to have available TransactionScoped bean outside of the transaction"); | ||
|
||
QuarkusTransaction.begin(); | ||
beanTransactional.setValue(1); | ||
assertEquals(2, TransactionScopedBean.getInitializedCount(), "Expected @PostConstruct to be invoked"); | ||
assertEquals(1, beanTransactional.getValue(), "Transaction scope did not save the value"); | ||
QuarkusTransaction.commit(); | ||
assertEquals(1, TransactionScopedBean.getPreDestroyCount(), "Expected @PreDestroy to be invoked"); | ||
|
||
assertThrows(ContextNotActiveException.class, () -> { | ||
beanTransactional.getValue(); | ||
}, "Not expecting to have available TransactionScoped bean outside of the transaction"); | ||
|
||
tm.resume(suspendedTransaction); | ||
assertEquals(42, beanTransactional.getValue(), "Transaction scope did not resumed correctly"); | ||
QuarkusTransaction.rollback(); | ||
assertEquals(2, TransactionScopedBean.getPreDestroyCount(), "Expected @PreDestroy to be invoked"); | ||
} | ||
|
||
@Test | ||
void scopeEventsAreEmitted() { | ||
TransactionBeanWithEvents.cleanCounts(); | ||
|
||
QuarkusTransaction.begin(); | ||
beanEvents.listenToCommitRollback(); | ||
QuarkusTransaction.commit(); | ||
|
||
assertEquals(1, TransactionBeanWithEvents.getInitialized(), "Expected @Initialized to be observed"); | ||
assertEquals(1, TransactionBeanWithEvents.getBeforeDestroyed(), "Expected @BeforeDestroyed to be observed"); | ||
assertEquals(1, TransactionBeanWithEvents.getDestroyed(), "Expected @Destroyed to be observed"); | ||
assertEquals(1, TransactionBeanWithEvents.getCommited(), "Expected commit to be called once"); | ||
assertEquals(0, TransactionBeanWithEvents.getRolledBack(), "Expected no rollback"); | ||
TransactionBeanWithEvents.cleanCounts(); | ||
|
||
QuarkusTransaction.begin(); | ||
beanEvents.listenToCommitRollback(); | ||
QuarkusTransaction.rollback(); | ||
|
||
assertEquals(1, TransactionBeanWithEvents.getInitialized(), "Expected @Initialized to be observed"); | ||
assertEquals(1, TransactionBeanWithEvents.getBeforeDestroyed(), "Expected @BeforeDestroyed to be observed"); | ||
assertEquals(1, TransactionBeanWithEvents.getDestroyed(), "Expected @Destroyed to be observed"); | ||
assertEquals(0, TransactionBeanWithEvents.getCommited(), "Expected no commit"); | ||
assertEquals(1, TransactionBeanWithEvents.getRolledBack(), "Expected rollback to be called once"); | ||
TransactionBeanWithEvents.cleanCounts(); | ||
} | ||
|
||
} |
84 changes: 84 additions & 0 deletions
84
...a/src/test/java/io/quarkus/narayana/jta/TransactionScopeQuarkusTransactionRunnerTest.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,84 @@ | ||
package io.quarkus.narayana.jta; | ||
|
||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
||
import jakarta.enterprise.context.ContextNotActiveException; | ||
import jakarta.inject.Inject; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import io.quarkus.test.junit.QuarkusTest; | ||
|
||
@QuarkusTest | ||
class TransactionScopeQuarkusTransactionRunnerTest { | ||
|
||
@Inject | ||
TransactionScopedBean beanTransactional; | ||
|
||
@Inject | ||
TransactionBeanWithEvents beanEvents; | ||
|
||
@Test | ||
void transactionScopedInTransaction() { | ||
TransactionScopedBean.resetCounters(); | ||
|
||
QuarkusTransaction.requiringNew().run(() -> { | ||
beanTransactional.setValue(42); | ||
assertEquals(1, TransactionScopedBean.getInitializedCount(), "Expected @PostConstruct to be invoked"); | ||
assertEquals(42, beanTransactional.getValue(), "Transaction scope did not save the value"); | ||
|
||
QuarkusTransaction.suspendingExisting().run(() -> { | ||
assertThrows(ContextNotActiveException.class, () -> { | ||
beanTransactional.getValue(); | ||
}, "Not expecting to have available TransactionScoped bean outside of the transaction"); | ||
|
||
QuarkusTransaction.requiringNew().run(() -> { | ||
beanTransactional.setValue(1); | ||
assertEquals(2, TransactionScopedBean.getInitializedCount(), "Expected @PostConstruct to be invoked"); | ||
assertEquals(1, beanTransactional.getValue(), "Transaction scope did not save the value"); | ||
}); | ||
assertEquals(1, TransactionScopedBean.getPreDestroyCount(), "Expected @PreDestroy to be invoked"); | ||
|
||
assertThrows(ContextNotActiveException.class, () -> { | ||
beanTransactional.getValue(); | ||
}, "Not expecting to have available TransactionScoped bean outside of the transaction"); | ||
}); | ||
|
||
assertEquals(42, beanTransactional.getValue(), "Transaction scope did not resumed correctly"); | ||
}); | ||
assertEquals(2, TransactionScopedBean.getPreDestroyCount(), "Expected @PreDestroy to be invoked"); | ||
} | ||
|
||
@Test | ||
void scopeEventsAreEmitted() { | ||
TransactionBeanWithEvents.cleanCounts(); | ||
|
||
QuarkusTransaction.requiringNew().run(() -> { | ||
beanEvents.listenToCommitRollback(); | ||
}); | ||
|
||
assertEquals(1, TransactionBeanWithEvents.getInitialized(), "Expected @Initialized to be observed"); | ||
assertEquals(1, TransactionBeanWithEvents.getBeforeDestroyed(), "Expected @BeforeDestroyed to be observed"); | ||
assertEquals(1, TransactionBeanWithEvents.getDestroyed(), "Expected @Destroyed to be observed"); | ||
assertEquals(1, TransactionBeanWithEvents.getCommited(), "Expected commit to be called once"); | ||
assertEquals(0, TransactionBeanWithEvents.getRolledBack(), "Expected no rollback"); | ||
TransactionBeanWithEvents.cleanCounts(); | ||
|
||
assertThatThrownBy(() -> QuarkusTransaction.requiringNew().run(() -> { | ||
beanEvents.listenToCommitRollback(); | ||
throw new RuntimeException(); | ||
})) | ||
// expect runtime exception to rollback the call | ||
.isInstanceOf(RuntimeException.class); | ||
|
||
assertEquals(1, TransactionBeanWithEvents.getInitialized(), "Expected @Initialized to be observed"); | ||
assertEquals(1, TransactionBeanWithEvents.getBeforeDestroyed(), "Expected @BeforeDestroyed to be observed"); | ||
assertEquals(1, TransactionBeanWithEvents.getDestroyed(), "Expected @Destroyed to be observed"); | ||
assertEquals(0, TransactionBeanWithEvents.getCommited(), "Expected no commit"); | ||
assertEquals(1, TransactionBeanWithEvents.getRolledBack(), "Expected rollback to be called once"); | ||
TransactionBeanWithEvents.cleanCounts(); | ||
} | ||
|
||
} |
42 changes: 42 additions & 0 deletions
42
...narayana-jta/src/test/java/io/quarkus/narayana/jta/TransactionScopeTransactionalTest.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,42 @@ | ||
package io.quarkus.narayana.jta; | ||
|
||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
import jakarta.inject.Inject; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import io.quarkus.test.junit.QuarkusTest; | ||
|
||
@QuarkusTest | ||
class TransactionScopeTransactionalTest { | ||
@Inject | ||
TransactionBeanWithEvents beanEvents; | ||
|
||
@Test | ||
void scopeEventsAreEmitted() { | ||
TransactionBeanWithEvents.cleanCounts(); | ||
|
||
beanEvents.doInTransaction(true); | ||
|
||
assertEquals(1, TransactionBeanWithEvents.getInitialized(), "Expected @Initialized to be observed"); | ||
assertEquals(1, TransactionBeanWithEvents.getBeforeDestroyed(), "Expected @BeforeDestroyed to be observed"); | ||
assertEquals(1, TransactionBeanWithEvents.getDestroyed(), "Expected @Destroyed to be observed"); | ||
assertEquals(1, TransactionBeanWithEvents.getCommited(), "Expected commit to be called once"); | ||
assertEquals(0, TransactionBeanWithEvents.getRolledBack(), "Expected no rollback"); | ||
TransactionBeanWithEvents.cleanCounts(); | ||
|
||
assertThatThrownBy(() -> beanEvents.doInTransaction(false)) | ||
// expect runtime exception to rollback the call | ||
.isInstanceOf(RuntimeException.class); | ||
|
||
assertEquals(1, TransactionBeanWithEvents.getInitialized(), "Expected @Initialized to be observed"); | ||
assertEquals(1, TransactionBeanWithEvents.getBeforeDestroyed(), "Expected @BeforeDestroyed to be observed"); | ||
assertEquals(1, TransactionBeanWithEvents.getDestroyed(), "Expected @Destroyed to be observed"); | ||
assertEquals(0, TransactionBeanWithEvents.getCommited(), "Expected no commit"); | ||
assertEquals(1, TransactionBeanWithEvents.getRolledBack(), "Expected rollback to be called once"); | ||
TransactionBeanWithEvents.cleanCounts(); | ||
} | ||
|
||
} |
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