/
TransactionPropertiesUtilTests.groovy
110 lines (80 loc) · 5.92 KB
/
TransactionPropertiesUtilTests.groovy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package grails.plugin.transaction.handling
import org.springframework.transaction.TransactionDefinition
import org.springframework.transaction.interceptor.DefaultTransactionAttribute;
import org.springframework.transaction.interceptor.NoRollbackRuleAttribute;
import org.springframework.transaction.interceptor.RollbackRuleAttribute;
import org.springframework.transaction.interceptor.RuleBasedTransactionAttribute;
import org.springframework.transaction.support.DefaultTransactionDefinition;
import grails.test.*
class TransactionPropertiesUtilTests extends GrailsUnitTestCase {
private TransactionPropertiesUtil txPropsUtil = new TransactionPropertiesUtil()
void testExpandWithoutRollbackRules() {
Map result = null
result = txPropsUtil.expand([:])
assertNotNull result
assertTrue result.isEmpty()
result = txPropsUtil.expand([propagation: 'required'])
assertEquals([propagationBehaviorName: 'PROPAGATION_REQUIRED'], result)
result = txPropsUtil.expand([isolation: 'serializable', timeout: 'default'])
assertEquals([isolationLevelName: 'ISOLATION_SERIALIZABLE', timeout: TransactionDefinition.TIMEOUT_DEFAULT], result)
result = txPropsUtil.expand([propagationBehaviorName: 'PROPAGATION_REQUIRES_NEW', isolationLevel: TransactionDefinition.ISOLATION_REPEATABLE_READ])
assertEquals([propagationBehaviorName: 'PROPAGATION_REQUIRES_NEW', isolationLevel: TransactionDefinition.ISOLATION_REPEATABLE_READ], result)
result = txPropsUtil.expand([timeout: 987, readOnly: true])
assertEquals([timeout: 987, readOnly: true], result)
result = txPropsUtil.expand([propagationBehaviorName: 'PROPAGATION_NEVER', propagation: 'supports'])
assertEquals([propagationBehaviorName: 'PROPAGATION_SUPPORTS'], result)
result = txPropsUtil.expand([isolation: 'default', isolationLevel: TransactionDefinition.ISOLATION_READ_UNCOMMITTED])
assertEquals([isolationLevelName: 'ISOLATION_DEFAULT', isolationLevel: TransactionDefinition.ISOLATION_READ_UNCOMMITTED], result)
}
void testExpandWithRollbackRules() {
Map result = null
result = txPropsUtil.expand([rollbackFor: ['Abc']])
assertEquals([rollbackRules: [new RollbackRuleAttribute('Abc')]], result)
result = txPropsUtil.expand([propagation: 'requiresNew', noRollbackFor: [RuntimeException]])
assertEquals([propagationBehaviorName: 'PROPAGATION_REQUIRES_NEW', rollbackRules: [new NoRollbackRuleAttribute(RuntimeException)]], result)
result = txPropsUtil.expand([isolation: 'default', readOnly: true, rollbackFor: [IllegalStateException, 'B'], noRollbackFor: [RuntimeException, 'A']])
assertEquals([isolationLevelName: 'ISOLATION_DEFAULT', readOnly: true,
rollbackRules: [new RollbackRuleAttribute(IllegalStateException), new RollbackRuleAttribute('B'),
new NoRollbackRuleAttribute(RuntimeException), new NoRollbackRuleAttribute('A')]], result)
}
void testApplyWithoutRollbackRules() {
for (txDefClass in [DefaultTransactionDefinition, DefaultTransactionAttribute, RuleBasedTransactionAttribute]) {
TransactionDefinition txDef = txDefClass.newInstance()
txPropsUtil.applyTo([:], txDef)
assertEquals(TransactionDefinition.PROPAGATION_REQUIRED, txDef.propagationBehavior)
assertEquals(TransactionDefinition.ISOLATION_DEFAULT, txDef.isolationLevel)
assertEquals(TransactionDefinition.TIMEOUT_DEFAULT, txDef.timeout)
assertEquals(false, txDef.readOnly)
txDef = txDefClass.newInstance()
txPropsUtil.applyTo([propagation: 'supports', timeout: 123], txDef)
assertEquals(TransactionDefinition.PROPAGATION_SUPPORTS, txDef.propagationBehavior)
assertEquals(TransactionDefinition.ISOLATION_DEFAULT, txDef.isolationLevel)
assertEquals(123, txDef.timeout)
assertEquals(false, txDef.readOnly)
}
}
void testApplyWithRollbackRules() {
TransactionDefinition txDef = new RuleBasedTransactionAttribute()
txPropsUtil.applyTo([propagation: 'never', readOnly: true, rollbackFor: [IllegalArgumentException], noRollbackFor: [IllegalStateException]], txDef)
assertEquals(TransactionDefinition.PROPAGATION_NEVER, txDef.propagationBehavior)
assertEquals(TransactionDefinition.ISOLATION_DEFAULT, txDef.isolationLevel)
assertEquals(TransactionDefinition.TIMEOUT_DEFAULT, txDef.timeout)
assertEquals(true, txDef.readOnly)
assertEquals([new RollbackRuleAttribute(IllegalArgumentException), new NoRollbackRuleAttribute(IllegalStateException)], txDef.rollbackRules)
}
void testRemovePropagation() {
Map result = null
result = txPropsUtil.removePropagationProperties([:])
assertNotNull result
assertTrue result.isEmpty()
result = txPropsUtil.removePropagationProperties([propagation:'required'])
assertNotNull result
assertTrue result.isEmpty()
result = txPropsUtil.removePropagationProperties([isolation:'default'])
assertEquals([isolation:'default'], result)
result = txPropsUtil.removePropagationProperties([isolation:'default', propagationBehavior: TransactionDefinition.PROPAGATION_MANDATORY, timeout:12])
assertEquals([isolation:'default', timeout: 12], result)
result = txPropsUtil.removePropagationProperties([isolationLevel: TransactionDefinition.ISOLATION_REPEATABLE_READ, propagationBehaviorName: 'PROPAGATION_MANDATORY', rollbackFor: [], noRollbackFor: []])
assertEquals([isolationLevel: TransactionDefinition.ISOLATION_REPEATABLE_READ, rollbackFor: [], noRollbackFor: []], result)
}
}