-
Notifications
You must be signed in to change notification settings - Fork 698
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add more tests for TransactionSynchronization support
- Loading branch information
Showing
1 changed file
with
96 additions
and
0 deletions.
There are no files selected for viewing
96 changes: 96 additions & 0 deletions
96
...ransaction/src/test/kotlin/org/jetbrains/exposed/spring/TransactionSynchronizationTest.kt
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,96 @@ | ||
package org.jetbrains.exposed.spring | ||
|
||
import junit.framework.TestCase.assertTrue | ||
import org.junit.Before | ||
import org.springframework.transaction.TransactionDefinition | ||
import org.springframework.transaction.support.TransactionSynchronization | ||
import org.springframework.transaction.support.TransactionSynchronizationManager | ||
import kotlin.test.Test | ||
|
||
class TransactionSynchronizationTest : SpringTransactionTestBase() { | ||
|
||
private var synchronization: TestSynchronization = TestSynchronization() | ||
|
||
@Before | ||
fun setUp() { | ||
synchronization = TestSynchronization() | ||
} | ||
|
||
@Test | ||
fun testCommitIsReported() { | ||
transactionManager.execute { | ||
TransactionSynchronizationManager.registerSynchronization(synchronization) | ||
} | ||
|
||
assertTrue(synchronization.committed) | ||
} | ||
|
||
@Test | ||
fun testRollbackIsReported() { | ||
transactionManager.execute { | ||
TransactionSynchronizationManager.registerSynchronization(synchronization) | ||
it.setRollbackOnly() | ||
} | ||
|
||
assertTrue(synchronization.rolledBack) | ||
} | ||
|
||
@Test | ||
fun testNestedRolledBackIsStillCommit() { | ||
transactionManager.execute { | ||
TransactionSynchronizationManager.registerSynchronization(synchronization) | ||
transactionManager.execute(TransactionDefinition.PROPAGATION_NESTED) { | ||
it.setRollbackOnly() | ||
} | ||
} | ||
|
||
assertTrue(synchronization.committed) | ||
} | ||
|
||
@Test | ||
fun testRequiresNewRolledBackIsStillCommit() { | ||
transactionManager.execute { | ||
TransactionSynchronizationManager.registerSynchronization(synchronization) | ||
transactionManager.execute(TransactionDefinition.PROPAGATION_REQUIRES_NEW) { | ||
it.setRollbackOnly() | ||
} | ||
} | ||
|
||
assertTrue(synchronization.committed) | ||
} | ||
|
||
@Test | ||
fun testOuterRollbackPropagatedToNested() { | ||
transactionManager.execute { | ||
it.setRollbackOnly() | ||
transactionManager.execute(TransactionDefinition.PROPAGATION_NESTED) { | ||
TransactionSynchronizationManager.registerSynchronization(synchronization) | ||
} | ||
} | ||
|
||
assertTrue(synchronization.rolledBack) | ||
} | ||
|
||
@Test | ||
fun testOuterRollbackNotPropagatedToRequiresNew() { | ||
transactionManager.execute { | ||
it.setRollbackOnly() | ||
transactionManager.execute(TransactionDefinition.PROPAGATION_REQUIRES_NEW) { | ||
TransactionSynchronizationManager.registerSynchronization(synchronization) | ||
} | ||
} | ||
|
||
assertTrue(synchronization.committed) | ||
} | ||
|
||
class TestSynchronization : TransactionSynchronization { | ||
|
||
var committed: Boolean = false | ||
var rolledBack: Boolean = false | ||
|
||
override fun afterCompletion(status: Int) { | ||
committed = (status == TransactionSynchronization.STATUS_COMMITTED) | ||
rolledBack = (status == TransactionSynchronization.STATUS_ROLLED_BACK) | ||
} | ||
} | ||
} |