1
1
/*
2
- * Copyright 2002-2018 the original author or authors.
2
+ * Copyright 2002-2019 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
16
16
17
17
package org .springframework .transaction .annotation ;
18
18
19
+ import io .vavr .control .Try ;
19
20
import org .junit .Test ;
20
21
21
22
import org .springframework .aop .framework .ProxyFactory ;
@@ -123,12 +124,12 @@ public void withMultiMethodOverride() {
123
124
}
124
125
125
126
@ Test
126
- public void withRollback () {
127
+ public void withRollbackOnRuntimeException () {
127
128
ProxyFactory proxyFactory = new ProxyFactory ();
128
- proxyFactory .setTarget (new TestWithRollback ());
129
+ proxyFactory .setTarget (new TestWithExceptions ());
129
130
proxyFactory .addAdvice (this .ti );
130
131
131
- TestWithRollback proxy = (TestWithRollback ) proxyFactory .getProxy ();
132
+ TestWithExceptions proxy = (TestWithExceptions ) proxyFactory .getProxy ();
132
133
133
134
try {
134
135
proxy .doSomethingErroneous ();
@@ -147,6 +148,88 @@ public void withRollback() {
147
148
}
148
149
}
149
150
151
+ @ Test
152
+ public void withCommitOnCheckedException () {
153
+ ProxyFactory proxyFactory = new ProxyFactory ();
154
+ proxyFactory .setTarget (new TestWithExceptions ());
155
+ proxyFactory .addAdvice (this .ti );
156
+
157
+ TestWithExceptions proxy = (TestWithExceptions ) proxyFactory .getProxy ();
158
+
159
+ try {
160
+ proxy .doSomethingElseWithCheckedException ();
161
+ fail ("Should throw Exception" );
162
+ }
163
+ catch (Exception ex ) {
164
+ assertGetTransactionAndCommitCount (1 );
165
+ }
166
+ }
167
+
168
+ @ Test
169
+ public void withRollbackOnCheckedExceptionAndRollbackRule () {
170
+ ProxyFactory proxyFactory = new ProxyFactory ();
171
+ proxyFactory .setTarget (new TestWithExceptions ());
172
+ proxyFactory .addAdvice (this .ti );
173
+
174
+ TestWithExceptions proxy = (TestWithExceptions ) proxyFactory .getProxy ();
175
+
176
+ try {
177
+ proxy .doSomethingElseWithCheckedExceptionAndRollbackRule ();
178
+ fail ("Should throw Exception" );
179
+ }
180
+ catch (Exception ex ) {
181
+ assertGetTransactionAndRollbackCount (1 );
182
+ }
183
+ }
184
+
185
+ @ Test
186
+ public void withVavrTrySuccess () {
187
+ ProxyFactory proxyFactory = new ProxyFactory ();
188
+ proxyFactory .setTarget (new TestWithVavrTry ());
189
+ proxyFactory .addAdvice (this .ti );
190
+
191
+ TestWithVavrTry proxy = (TestWithVavrTry ) proxyFactory .getProxy ();
192
+
193
+ proxy .doSomething ();
194
+ assertGetTransactionAndCommitCount (1 );
195
+ }
196
+
197
+ @ Test
198
+ public void withVavrTryRuntimeException () {
199
+ ProxyFactory proxyFactory = new ProxyFactory ();
200
+ proxyFactory .setTarget (new TestWithVavrTry ());
201
+ proxyFactory .addAdvice (this .ti );
202
+
203
+ TestWithVavrTry proxy = (TestWithVavrTry ) proxyFactory .getProxy ();
204
+
205
+ proxy .doSomethingErroneous ();
206
+ assertGetTransactionAndRollbackCount (1 );
207
+ }
208
+
209
+ @ Test
210
+ public void withVavrTryCheckedException () {
211
+ ProxyFactory proxyFactory = new ProxyFactory ();
212
+ proxyFactory .setTarget (new TestWithVavrTry ());
213
+ proxyFactory .addAdvice (this .ti );
214
+
215
+ TestWithVavrTry proxy = (TestWithVavrTry ) proxyFactory .getProxy ();
216
+
217
+ proxy .doSomethingErroneousWithCheckedException ();
218
+ assertGetTransactionAndCommitCount (1 );
219
+ }
220
+
221
+ @ Test
222
+ public void withVavrTryCheckedExceptionAndRollbackRule () {
223
+ ProxyFactory proxyFactory = new ProxyFactory ();
224
+ proxyFactory .setTarget (new TestWithVavrTry ());
225
+ proxyFactory .addAdvice (this .ti );
226
+
227
+ TestWithVavrTry proxy = (TestWithVavrTry ) proxyFactory .getProxy ();
228
+
229
+ proxy .doSomethingErroneousWithCheckedExceptionAndRollbackRule ();
230
+ assertGetTransactionAndRollbackCount (1 );
231
+ }
232
+
150
233
@ Test
151
234
public void withInterface () {
152
235
ProxyFactory proxyFactory = new ProxyFactory ();
@@ -352,21 +435,64 @@ public void doSomethingCompletelyElse() {
352
435
}
353
436
354
437
355
- @ Transactional ( rollbackFor = IllegalStateException . class )
356
- public static class TestWithRollback {
438
+ @ Transactional
439
+ public static class TestWithExceptions {
357
440
358
441
public void doSomethingErroneous () {
359
442
assertTrue (TransactionSynchronizationManager .isActualTransactionActive ());
360
443
assertFalse (TransactionSynchronizationManager .isCurrentTransactionReadOnly ());
361
444
throw new IllegalStateException ();
362
445
}
363
446
364
- @ Transactional (rollbackFor = IllegalArgumentException .class )
365
447
public void doSomethingElseErroneous () {
366
448
assertTrue (TransactionSynchronizationManager .isActualTransactionActive ());
367
449
assertFalse (TransactionSynchronizationManager .isCurrentTransactionReadOnly ());
368
450
throw new IllegalArgumentException ();
369
451
}
452
+
453
+ @ Transactional
454
+ public void doSomethingElseWithCheckedException () throws Exception {
455
+ assertTrue (TransactionSynchronizationManager .isActualTransactionActive ());
456
+ assertFalse (TransactionSynchronizationManager .isCurrentTransactionReadOnly ());
457
+ throw new Exception ();
458
+ }
459
+
460
+ @ Transactional (rollbackFor = Exception .class )
461
+ public void doSomethingElseWithCheckedExceptionAndRollbackRule () throws Exception {
462
+ assertTrue (TransactionSynchronizationManager .isActualTransactionActive ());
463
+ assertFalse (TransactionSynchronizationManager .isCurrentTransactionReadOnly ());
464
+ throw new Exception ();
465
+ }
466
+ }
467
+
468
+
469
+ @ Transactional
470
+ public static class TestWithVavrTry {
471
+
472
+ public Try <String > doSomething () {
473
+ assertTrue (TransactionSynchronizationManager .isActualTransactionActive ());
474
+ assertFalse (TransactionSynchronizationManager .isCurrentTransactionReadOnly ());
475
+ return Try .success ("ok" );
476
+ }
477
+
478
+ public Try <String > doSomethingErroneous () {
479
+ assertTrue (TransactionSynchronizationManager .isActualTransactionActive ());
480
+ assertFalse (TransactionSynchronizationManager .isCurrentTransactionReadOnly ());
481
+ return Try .failure (new IllegalStateException ());
482
+ }
483
+
484
+ public Try <String > doSomethingErroneousWithCheckedException () {
485
+ assertTrue (TransactionSynchronizationManager .isActualTransactionActive ());
486
+ assertFalse (TransactionSynchronizationManager .isCurrentTransactionReadOnly ());
487
+ return Try .failure (new Exception ());
488
+ }
489
+
490
+ @ Transactional (rollbackFor = Exception .class )
491
+ public Try <String > doSomethingErroneousWithCheckedExceptionAndRollbackRule () {
492
+ assertTrue (TransactionSynchronizationManager .isActualTransactionActive ());
493
+ assertFalse (TransactionSynchronizationManager .isCurrentTransactionReadOnly ());
494
+ return Try .failure (new Exception ());
495
+ }
370
496
}
371
497
372
498
0 commit comments