1
1
/*
2
- * Copyright 2002-2022 the original author or authors.
2
+ * Copyright 2002-2023 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.
17
17
package org .springframework .transaction .annotation ;
18
18
19
19
import java .time .Duration ;
20
+ import java .util .concurrent .CompletableFuture ;
20
21
21
22
import io .vavr .control .Try ;
22
23
import org .junit .jupiter .api .Test ;
@@ -57,7 +58,6 @@ public void classLevelOnly() {
57
58
ProxyFactory proxyFactory = new ProxyFactory ();
58
59
proxyFactory .setTarget (new TestClassLevelOnly ());
59
60
proxyFactory .addAdvice (this .ti );
60
-
61
61
TestClassLevelOnly proxy = (TestClassLevelOnly ) proxyFactory .getProxy ();
62
62
63
63
proxy .doSomething ();
@@ -78,7 +78,6 @@ public void withSingleMethodOverride() {
78
78
ProxyFactory proxyFactory = new ProxyFactory ();
79
79
proxyFactory .setTarget (new TestWithSingleMethodOverride ());
80
80
proxyFactory .addAdvice (this .ti );
81
-
82
81
TestWithSingleMethodOverride proxy = (TestWithSingleMethodOverride ) proxyFactory .getProxy ();
83
82
84
83
proxy .doSomething ();
@@ -99,7 +98,6 @@ public void withSingleMethodOverrideInverted() {
99
98
ProxyFactory proxyFactory = new ProxyFactory ();
100
99
proxyFactory .setTarget (new TestWithSingleMethodOverrideInverted ());
101
100
proxyFactory .addAdvice (this .ti );
102
-
103
101
TestWithSingleMethodOverrideInverted proxy = (TestWithSingleMethodOverrideInverted ) proxyFactory .getProxy ();
104
102
105
103
proxy .doSomething ();
@@ -120,7 +118,6 @@ public void withMultiMethodOverride() {
120
118
ProxyFactory proxyFactory = new ProxyFactory ();
121
119
proxyFactory .setTarget (new TestWithMultiMethodOverride ());
122
120
proxyFactory .addAdvice (this .ti );
123
-
124
121
TestWithMultiMethodOverride proxy = (TestWithMultiMethodOverride ) proxyFactory .getProxy ();
125
122
126
123
proxy .doSomething ();
@@ -141,7 +138,6 @@ public void withRollbackOnRuntimeException() {
141
138
ProxyFactory proxyFactory = new ProxyFactory ();
142
139
proxyFactory .setTarget (new TestWithExceptions ());
143
140
proxyFactory .addAdvice (this .ti );
144
-
145
141
TestWithExceptions proxy = (TestWithExceptions ) proxyFactory .getProxy ();
146
142
147
143
assertThatIllegalStateException ().isThrownBy (
@@ -158,7 +154,6 @@ public void withCommitOnCheckedException() {
158
154
ProxyFactory proxyFactory = new ProxyFactory ();
159
155
proxyFactory .setTarget (new TestWithExceptions ());
160
156
proxyFactory .addAdvice (this .ti );
161
-
162
157
TestWithExceptions proxy = (TestWithExceptions ) proxyFactory .getProxy ();
163
158
164
159
assertThatException ()
@@ -171,7 +166,6 @@ public void withRollbackOnCheckedExceptionAndRollbackRule() {
171
166
ProxyFactory proxyFactory = new ProxyFactory ();
172
167
proxyFactory .setTarget (new TestWithExceptions ());
173
168
proxyFactory .addAdvice (this .ti );
174
-
175
169
TestWithExceptions proxy = (TestWithExceptions ) proxyFactory .getProxy ();
176
170
177
171
assertThatException ()
@@ -184,7 +178,6 @@ public void withMonoSuccess() {
184
178
ProxyFactory proxyFactory = new ProxyFactory ();
185
179
proxyFactory .setTarget (new TestWithReactive ());
186
180
proxyFactory .addAdvice (new TransactionInterceptor (rtm , this .source ));
187
-
188
181
TestWithReactive proxy = (TestWithReactive ) proxyFactory .getProxy ();
189
182
190
183
StepVerifier .withVirtualTime (proxy ::monoSuccess ).thenAwait (Duration .ofSeconds (10 )).verifyComplete ();
@@ -196,7 +189,6 @@ public void withMonoFailure() {
196
189
ProxyFactory proxyFactory = new ProxyFactory ();
197
190
proxyFactory .setTarget (new TestWithReactive ());
198
191
proxyFactory .addAdvice (new TransactionInterceptor (rtm , this .source ));
199
-
200
192
TestWithReactive proxy = (TestWithReactive ) proxyFactory .getProxy ();
201
193
202
194
proxy .monoFailure ().as (StepVerifier ::create ).verifyError ();
@@ -208,7 +200,6 @@ public void withMonoRollback() {
208
200
ProxyFactory proxyFactory = new ProxyFactory ();
209
201
proxyFactory .setTarget (new TestWithReactive ());
210
202
proxyFactory .addAdvice (new TransactionInterceptor (rtm , this .source ));
211
-
212
203
TestWithReactive proxy = (TestWithReactive ) proxyFactory .getProxy ();
213
204
214
205
StepVerifier .withVirtualTime (proxy ::monoSuccess ).thenAwait (Duration .ofSeconds (1 )).thenCancel ().verify ();
@@ -220,7 +211,6 @@ public void withFluxSuccess() {
220
211
ProxyFactory proxyFactory = new ProxyFactory ();
221
212
proxyFactory .setTarget (new TestWithReactive ());
222
213
proxyFactory .addAdvice (new TransactionInterceptor (rtm , this .source ));
223
-
224
214
TestWithReactive proxy = (TestWithReactive ) proxyFactory .getProxy ();
225
215
226
216
StepVerifier .withVirtualTime (proxy ::fluxSuccess ).thenAwait (Duration .ofSeconds (10 )).expectNextCount (1 ).verifyComplete ();
@@ -232,7 +222,6 @@ public void withFluxFailure() {
232
222
ProxyFactory proxyFactory = new ProxyFactory ();
233
223
proxyFactory .setTarget (new TestWithReactive ());
234
224
proxyFactory .addAdvice (new TransactionInterceptor (rtm , this .source ));
235
-
236
225
TestWithReactive proxy = (TestWithReactive ) proxyFactory .getProxy ();
237
226
238
227
proxy .fluxFailure ().as (StepVerifier ::create ).verifyError ();
@@ -244,19 +233,61 @@ public void withFluxRollback() {
244
233
ProxyFactory proxyFactory = new ProxyFactory ();
245
234
proxyFactory .setTarget (new TestWithReactive ());
246
235
proxyFactory .addAdvice (new TransactionInterceptor (rtm , this .source ));
247
-
248
236
TestWithReactive proxy = (TestWithReactive ) proxyFactory .getProxy ();
249
237
250
238
StepVerifier .withVirtualTime (proxy ::fluxSuccess ).thenAwait (Duration .ofSeconds (1 )).thenCancel ().verify ();
251
239
assertReactiveGetTransactionAndRollbackCount (1 );
252
240
}
253
241
242
+ @ Test
243
+ public void withCompletableFutureSuccess () {
244
+ ProxyFactory proxyFactory = new ProxyFactory ();
245
+ proxyFactory .setTarget (new TestWithCompletableFuture ());
246
+ proxyFactory .addAdvice (this .ti );
247
+ TestWithCompletableFuture proxy = (TestWithCompletableFuture ) proxyFactory .getProxy ();
248
+
249
+ proxy .doSomething ();
250
+ assertGetTransactionAndCommitCount (1 );
251
+ }
252
+
253
+ @ Test
254
+ public void withCompletableFutureRuntimeException () {
255
+ ProxyFactory proxyFactory = new ProxyFactory ();
256
+ proxyFactory .setTarget (new TestWithCompletableFuture ());
257
+ proxyFactory .addAdvice (this .ti );
258
+ TestWithCompletableFuture proxy = (TestWithCompletableFuture ) proxyFactory .getProxy ();
259
+
260
+ proxy .doSomethingErroneous ();
261
+ assertGetTransactionAndRollbackCount (1 );
262
+ }
263
+
264
+ @ Test
265
+ public void withCompletableFutureCheckedException () {
266
+ ProxyFactory proxyFactory = new ProxyFactory ();
267
+ proxyFactory .setTarget (new TestWithCompletableFuture ());
268
+ proxyFactory .addAdvice (this .ti );
269
+ TestWithCompletableFuture proxy = (TestWithCompletableFuture ) proxyFactory .getProxy ();
270
+
271
+ proxy .doSomethingErroneousWithCheckedException ();
272
+ assertGetTransactionAndCommitCount (1 );
273
+ }
274
+
275
+ @ Test
276
+ public void withCompletableFutureCheckedExceptionAndRollbackRule () {
277
+ ProxyFactory proxyFactory = new ProxyFactory ();
278
+ proxyFactory .setTarget (new TestWithCompletableFuture ());
279
+ proxyFactory .addAdvice (this .ti );
280
+ TestWithCompletableFuture proxy = (TestWithCompletableFuture ) proxyFactory .getProxy ();
281
+
282
+ proxy .doSomethingErroneousWithCheckedExceptionAndRollbackRule ();
283
+ assertGetTransactionAndRollbackCount (1 );
284
+ }
285
+
254
286
@ Test
255
287
public void withVavrTrySuccess () {
256
288
ProxyFactory proxyFactory = new ProxyFactory ();
257
289
proxyFactory .setTarget (new TestWithVavrTry ());
258
290
proxyFactory .addAdvice (this .ti );
259
-
260
291
TestWithVavrTry proxy = (TestWithVavrTry ) proxyFactory .getProxy ();
261
292
262
293
proxy .doSomething ();
@@ -268,7 +299,6 @@ public void withVavrTryRuntimeException() {
268
299
ProxyFactory proxyFactory = new ProxyFactory ();
269
300
proxyFactory .setTarget (new TestWithVavrTry ());
270
301
proxyFactory .addAdvice (this .ti );
271
-
272
302
TestWithVavrTry proxy = (TestWithVavrTry ) proxyFactory .getProxy ();
273
303
274
304
proxy .doSomethingErroneous ();
@@ -280,7 +310,6 @@ public void withVavrTryCheckedException() {
280
310
ProxyFactory proxyFactory = new ProxyFactory ();
281
311
proxyFactory .setTarget (new TestWithVavrTry ());
282
312
proxyFactory .addAdvice (this .ti );
283
-
284
313
TestWithVavrTry proxy = (TestWithVavrTry ) proxyFactory .getProxy ();
285
314
286
315
proxy .doSomethingErroneousWithCheckedException ();
@@ -292,7 +321,6 @@ public void withVavrTryCheckedExceptionAndRollbackRule() {
292
321
ProxyFactory proxyFactory = new ProxyFactory ();
293
322
proxyFactory .setTarget (new TestWithVavrTry ());
294
323
proxyFactory .addAdvice (this .ti );
295
-
296
324
TestWithVavrTry proxy = (TestWithVavrTry ) proxyFactory .getProxy ();
297
325
298
326
proxy .doSomethingErroneousWithCheckedExceptionAndRollbackRule ();
@@ -305,7 +333,6 @@ public void withInterface() {
305
333
proxyFactory .setTarget (new TestWithInterfaceImpl ());
306
334
proxyFactory .addInterface (TestWithInterface .class );
307
335
proxyFactory .addAdvice (this .ti );
308
-
309
336
TestWithInterface proxy = (TestWithInterface ) proxyFactory .getProxy ();
310
337
311
338
proxy .doSomething ();
@@ -330,7 +357,6 @@ public void crossClassInterfaceMethodLevelOnJdkProxy() {
330
357
proxyFactory .setTarget (new SomeServiceImpl ());
331
358
proxyFactory .addInterface (SomeService .class );
332
359
proxyFactory .addAdvice (this .ti );
333
-
334
360
SomeService someService = (SomeService ) proxyFactory .getProxy ();
335
361
336
362
someService .bar ();
@@ -349,7 +375,6 @@ public void crossClassInterfaceOnJdkProxy() {
349
375
proxyFactory .setTarget (new OtherServiceImpl ());
350
376
proxyFactory .addInterface (OtherService .class );
351
377
proxyFactory .addAdvice (this .ti );
352
-
353
378
OtherService otherService = (OtherService ) proxyFactory .getProxy ();
354
379
355
380
otherService .foo ();
@@ -366,7 +391,6 @@ public void withInterfaceOnTargetJdkProxy() {
366
391
proxyFactory .setTarget (targetFactory .getProxy ());
367
392
proxyFactory .addInterface (TestWithInterface .class );
368
393
proxyFactory .addAdvice (this .ti );
369
-
370
394
TestWithInterface proxy = (TestWithInterface ) proxyFactory .getProxy ();
371
395
372
396
proxy .doSomething ();
@@ -395,7 +419,6 @@ public void withInterfaceOnTargetCglibProxy() {
395
419
proxyFactory .setTarget (targetFactory .getProxy ());
396
420
proxyFactory .addInterface (TestWithInterface .class );
397
421
proxyFactory .addAdvice (this .ti );
398
-
399
422
TestWithInterface proxy = (TestWithInterface ) proxyFactory .getProxy ();
400
423
401
424
proxy .doSomething ();
@@ -544,6 +567,7 @@ public void doSomethingElseWithCheckedExceptionAndRollbackRule() throws Exceptio
544
567
}
545
568
}
546
569
570
+
547
571
@ Transactional
548
572
public static class TestWithReactive {
549
573
@@ -564,6 +588,37 @@ public Flux<Object> fluxFailure() {
564
588
}
565
589
}
566
590
591
+
592
+ @ Transactional
593
+ public static class TestWithCompletableFuture {
594
+
595
+ public CompletableFuture <String > doSomething () {
596
+ assertThat (TransactionSynchronizationManager .isActualTransactionActive ()).isTrue ();
597
+ assertThat (TransactionSynchronizationManager .isCurrentTransactionReadOnly ()).isFalse ();
598
+ return CompletableFuture .completedFuture ("ok" );
599
+ }
600
+
601
+ public CompletableFuture <String > doSomethingErroneous () {
602
+ assertThat (TransactionSynchronizationManager .isActualTransactionActive ()).isTrue ();
603
+ assertThat (TransactionSynchronizationManager .isCurrentTransactionReadOnly ()).isFalse ();
604
+ return CompletableFuture .failedFuture (new IllegalStateException ());
605
+ }
606
+
607
+ public CompletableFuture <String > doSomethingErroneousWithCheckedException () {
608
+ assertThat (TransactionSynchronizationManager .isActualTransactionActive ()).isTrue ();
609
+ assertThat (TransactionSynchronizationManager .isCurrentTransactionReadOnly ()).isFalse ();
610
+ return CompletableFuture .failedFuture (new Exception ());
611
+ }
612
+
613
+ @ Transactional (rollbackFor = Exception .class )
614
+ public CompletableFuture <String > doSomethingErroneousWithCheckedExceptionAndRollbackRule () {
615
+ assertThat (TransactionSynchronizationManager .isActualTransactionActive ()).isTrue ();
616
+ assertThat (TransactionSynchronizationManager .isCurrentTransactionReadOnly ()).isFalse ();
617
+ return CompletableFuture .failedFuture (new Exception ());
618
+ }
619
+ }
620
+
621
+
567
622
@ Transactional
568
623
public static class TestWithVavrTry {
569
624
0 commit comments