Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

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