1
1
/*
2
- * Copyright 2002-2024 the original author or authors.
2
+ * Copyright 2002-2025 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 .orm .jpa ;
18
18
19
- import java .util .ArrayList ;
20
19
import java .util .List ;
21
20
22
21
import jakarta .persistence .EntityManager ;
46
45
import static org .mockito .Mockito .verify ;
47
46
48
47
/**
48
+ * Unit tests for {@link JpaTransactionManager}.
49
49
* @author Costin Leau
50
50
* @author Juergen Hoeller
51
51
* @author Phillip Webb
52
52
*/
53
53
class JpaTransactionManagerTests {
54
54
55
- private EntityManagerFactory factory = mock ( );
55
+ private static final List < String > testList = List . of ( "test" );
56
56
57
- private EntityManager manager = mock ();
58
57
59
- private EntityTransaction tx = mock ();
58
+ private final EntityManagerFactory factory = mock ();
60
59
61
- private JpaTransactionManager tm = new JpaTransactionManager ( factory );
60
+ private final EntityManager manager = mock ( );
62
61
63
- private TransactionTemplate tt = new TransactionTemplate (tm );
62
+ private final EntityTransaction tx = mock ();
63
+
64
+ private final JpaTransactionManager tm = new JpaTransactionManager (factory );
65
+
66
+ private final TransactionTemplate tt = new TransactionTemplate (tm );
64
67
65
68
66
69
@ BeforeEach
@@ -81,18 +84,15 @@ void verifyTransactionSynchronizationManagerState() {
81
84
82
85
@ Test
83
86
void testTransactionCommit () {
84
- final List <String > l = new ArrayList <>();
85
- l .add ("test" );
86
-
87
87
assertThat (TransactionSynchronizationManager .hasResource (factory )).isFalse ();
88
88
assertThat (TransactionSynchronizationManager .isSynchronizationActive ()).isFalse ();
89
89
90
90
Object result = tt .execute (status -> {
91
91
assertThat (TransactionSynchronizationManager .hasResource (factory )).isTrue ();
92
92
EntityManagerFactoryUtils .getTransactionalEntityManager (factory ).flush ();
93
- return l ;
93
+ return testList ;
94
94
});
95
- assertThat (result ).isSameAs (l );
95
+ assertThat (result ).isSameAs (testList );
96
96
97
97
assertThat (TransactionSynchronizationManager .hasResource (factory )).isFalse ();
98
98
assertThat (TransactionSynchronizationManager .isSynchronizationActive ()).isFalse ();
@@ -107,19 +107,16 @@ void testTransactionCommitWithRollbackException() {
107
107
given (tx .getRollbackOnly ()).willReturn (true );
108
108
willThrow (new RollbackException ()).given (tx ).commit ();
109
109
110
- final List <String > l = new ArrayList <>();
111
- l .add ("test" );
112
-
113
110
assertThat (TransactionSynchronizationManager .hasResource (factory )).isFalse ();
114
111
assertThat (TransactionSynchronizationManager .isSynchronizationActive ()).isFalse ();
115
112
116
113
try {
117
114
Object result = tt .execute (status -> {
118
115
assertThat (TransactionSynchronizationManager .hasResource (factory )).isTrue ();
119
116
EntityManagerFactoryUtils .getTransactionalEntityManager (factory ).flush ();
120
- return l ;
117
+ return testList ;
121
118
});
122
- assertThat (result ).isSameAs (l );
119
+ assertThat (result ).isSameAs (testList );
123
120
}
124
121
catch (TransactionSystemException tse ) {
125
122
// expected
@@ -137,9 +134,6 @@ void testTransactionCommitWithRollbackException() {
137
134
void testTransactionRollback () {
138
135
given (tx .isActive ()).willReturn (true );
139
136
140
- final List <String > l = new ArrayList <>();
141
- l .add ("test" );
142
-
143
137
assertThat (TransactionSynchronizationManager .hasResource (factory )).isFalse ();
144
138
assertThat (TransactionSynchronizationManager .isSynchronizationActive ()).isFalse ();
145
139
@@ -159,9 +153,6 @@ void testTransactionRollback() {
159
153
160
154
@ Test
161
155
void testTransactionRollbackWithAlreadyRolledBack () {
162
- final List <String > l = new ArrayList <>();
163
- l .add ("test" );
164
-
165
156
assertThat (TransactionSynchronizationManager .hasResource (factory )).isFalse ();
166
157
assertThat (TransactionSynchronizationManager .isSynchronizationActive ()).isFalse ();
167
158
@@ -182,9 +173,6 @@ void testTransactionRollbackWithAlreadyRolledBack() {
182
173
void testTransactionRollbackOnly () {
183
174
given (tx .isActive ()).willReturn (true );
184
175
185
- final List <String > l = new ArrayList <>();
186
- l .add ("test" );
187
-
188
176
assertThat (TransactionSynchronizationManager .hasResource (factory )).isFalse ();
189
177
assertThat (TransactionSynchronizationManager .isSynchronizationActive ()).isFalse ();
190
178
@@ -194,7 +182,7 @@ void testTransactionRollbackOnly() {
194
182
EntityManagerFactoryUtils .getTransactionalEntityManager (factory ).flush ();
195
183
status .setRollbackOnly ();
196
184
197
- return l ;
185
+ return testList ;
198
186
});
199
187
200
188
assertThat (TransactionSynchronizationManager .hasResource (factory )).isFalse ();
@@ -207,9 +195,6 @@ void testTransactionRollbackOnly() {
207
195
208
196
@ Test
209
197
void testParticipatingTransactionWithCommit () {
210
- final List <String > l = new ArrayList <>();
211
- l .add ("test" );
212
-
213
198
assertThat (TransactionSynchronizationManager .hasResource (factory )).isFalse ();
214
199
assertThat (TransactionSynchronizationManager .isSynchronizationActive ()).isFalse ();
215
200
@@ -218,7 +203,7 @@ void testParticipatingTransactionWithCommit() {
218
203
219
204
return tt .execute (status1 -> {
220
205
EntityManagerFactoryUtils .getTransactionalEntityManager (factory ).flush ();
221
- return l ;
206
+ return testList ;
222
207
});
223
208
});
224
209
@@ -234,9 +219,6 @@ void testParticipatingTransactionWithCommit() {
234
219
void testParticipatingTransactionWithRollback () {
235
220
given (tx .isActive ()).willReturn (true );
236
221
237
- final List <String > l = new ArrayList <>();
238
- l .add ("test" );
239
-
240
222
assertThat (TransactionSynchronizationManager .hasResource (factory )).isFalse ();
241
223
assertThat (TransactionSynchronizationManager .isSynchronizationActive ()).isFalse ();
242
224
@@ -263,9 +245,6 @@ void testParticipatingTransactionWithRollbackOnly() {
263
245
given (tx .getRollbackOnly ()).willReturn (true );
264
246
willThrow (new RollbackException ()).given (tx ).commit ();
265
247
266
- final List <String > l = new ArrayList <>();
267
- l .add ("test" );
268
-
269
248
assertThat (TransactionSynchronizationManager .hasResource (factory )).isFalse ();
270
249
assertThat (TransactionSynchronizationManager .isSynchronizationActive ()).isFalse ();
271
250
@@ -293,20 +272,17 @@ void testParticipatingTransactionWithRollbackOnly() {
293
272
void testParticipatingTransactionWithRequiresNew () {
294
273
tt .setPropagationBehavior (TransactionDefinition .PROPAGATION_REQUIRES_NEW );
295
274
296
- final List <String > l = new ArrayList <>();
297
- l .add ("test" );
298
-
299
275
assertThat (TransactionSynchronizationManager .hasResource (factory )).isFalse ();
300
276
assertThat (TransactionSynchronizationManager .isSynchronizationActive ()).isFalse ();
301
277
302
278
Object result = tt .execute (status -> {
303
279
assertThat (TransactionSynchronizationManager .hasResource (factory )).isTrue ();
304
280
return tt .execute (status1 -> {
305
281
EntityManagerFactoryUtils .getTransactionalEntityManager (factory ).flush ();
306
- return l ;
282
+ return testList ;
307
283
});
308
284
});
309
- assertThat (result ).isSameAs (l );
285
+ assertThat (result ).isSameAs (testList );
310
286
311
287
assertThat (TransactionSynchronizationManager .hasResource (factory )).isFalse ();
312
288
assertThat (TransactionSynchronizationManager .isSynchronizationActive ()).isFalse ();
@@ -320,10 +296,6 @@ void testParticipatingTransactionWithRequiresNew() {
320
296
void testParticipatingTransactionWithRequiresNewAndPrebound () {
321
297
tt .setPropagationBehavior (TransactionDefinition .PROPAGATION_REQUIRES_NEW );
322
298
323
-
324
- final List <String > l = new ArrayList <>();
325
- l .add ("test" );
326
-
327
299
assertThat (TransactionSynchronizationManager .hasResource (factory )).isFalse ();
328
300
assertThat (TransactionSynchronizationManager .isSynchronizationActive ()).isFalse ();
329
301
@@ -336,10 +308,10 @@ void testParticipatingTransactionWithRequiresNewAndPrebound() {
336
308
assertThat (TransactionSynchronizationManager .hasResource (factory )).isTrue ();
337
309
return tt .execute (status1 -> {
338
310
EntityManagerFactoryUtils .getTransactionalEntityManager (factory ).flush ();
339
- return l ;
311
+ return testList ;
340
312
});
341
313
});
342
- assertThat (result ).isSameAs (l );
314
+ assertThat (result ).isSameAs (testList );
343
315
}
344
316
finally {
345
317
TransactionSynchronizationManager .unbindResource (factory );
@@ -358,9 +330,6 @@ void testParticipatingTransactionWithRequiresNewAndPrebound() {
358
330
void testPropagationSupportsAndRequiresNew () {
359
331
tt .setPropagationBehavior (TransactionDefinition .PROPAGATION_SUPPORTS );
360
332
361
- final List <String > l = new ArrayList <>();
362
- l .add ("test" );
363
-
364
333
assertThat (TransactionSynchronizationManager .hasResource (factory )).isFalse ();
365
334
assertThat (TransactionSynchronizationManager .isSynchronizationActive ()).isFalse ();
366
335
@@ -370,10 +339,10 @@ void testPropagationSupportsAndRequiresNew() {
370
339
tt2 .setPropagationBehavior (TransactionDefinition .PROPAGATION_REQUIRES_NEW );
371
340
return tt2 .execute (status1 -> {
372
341
EntityManagerFactoryUtils .getTransactionalEntityManager (factory ).flush ();
373
- return l ;
342
+ return testList ;
374
343
});
375
344
});
376
- assertThat (result ).isSameAs (l );
345
+ assertThat (result ).isSameAs (testList );
377
346
378
347
assertThat (TransactionSynchronizationManager .hasResource (factory )).isFalse ();
379
348
assertThat (TransactionSynchronizationManager .isSynchronizationActive ()).isFalse ();
@@ -387,9 +356,6 @@ void testPropagationSupportsAndRequiresNew() {
387
356
void testPropagationSupportsAndRequiresNewAndEarlyAccess () {
388
357
tt .setPropagationBehavior (TransactionDefinition .PROPAGATION_SUPPORTS );
389
358
390
- final List <String > l = new ArrayList <>();
391
- l .add ("test" );
392
-
393
359
assertThat (TransactionSynchronizationManager .hasResource (factory )).isFalse ();
394
360
assertThat (TransactionSynchronizationManager .isSynchronizationActive ()).isFalse ();
395
361
@@ -401,10 +367,10 @@ void testPropagationSupportsAndRequiresNewAndEarlyAccess() {
401
367
tt2 .setPropagationBehavior (TransactionDefinition .PROPAGATION_REQUIRES_NEW );
402
368
return tt2 .execute (status1 -> {
403
369
EntityManagerFactoryUtils .getTransactionalEntityManager (factory ).flush ();
404
- return l ;
370
+ return testList ;
405
371
});
406
372
});
407
- assertThat (result ).isSameAs (l );
373
+ assertThat (result ).isSameAs (testList );
408
374
409
375
assertThat (TransactionSynchronizationManager .hasResource (factory )).isFalse ();
410
376
assertThat (TransactionSynchronizationManager .isSynchronizationActive ()).isFalse ();
@@ -456,9 +422,6 @@ public void afterCompletion(int status) {
456
422
457
423
@ Test
458
424
void testTransactionCommitWithPropagationSupports () {
459
- final List <String > l = new ArrayList <>();
460
- l .add ("test" );
461
-
462
425
tt .setPropagationBehavior (TransactionDefinition .PROPAGATION_SUPPORTS );
463
426
464
427
assertThat (TransactionSynchronizationManager .hasResource (factory )).isFalse ();
@@ -469,9 +432,9 @@ void testTransactionCommitWithPropagationSupports() {
469
432
assertThat (TransactionSynchronizationManager .isSynchronizationActive ()).isTrue ();
470
433
assertThat (status .isNewTransaction ()).isFalse ();
471
434
EntityManagerFactoryUtils .getTransactionalEntityManager (factory ).flush ();
472
- return l ;
435
+ return testList ;
473
436
});
474
- assertThat (result ).isSameAs (l );
437
+ assertThat (result ).isSameAs (testList );
475
438
476
439
assertThat (TransactionSynchronizationManager .hasResource (factory )).isFalse ();
477
440
assertThat (TransactionSynchronizationManager .isSynchronizationActive ()).isFalse ();
@@ -505,9 +468,6 @@ void testTransactionRollbackWithPropagationSupports() {
505
468
506
469
@ Test
507
470
void testTransactionCommitWithPrebound () {
508
- final List <String > l = new ArrayList <>();
509
- l .add ("test" );
510
-
511
471
assertThat (TransactionSynchronizationManager .hasResource (factory )).isFalse ();
512
472
assertThat (TransactionSynchronizationManager .isSynchronizationActive ()).isFalse ();
513
473
TransactionSynchronizationManager .bindResource (factory , new EntityManagerHolder (manager ));
@@ -517,9 +477,9 @@ void testTransactionCommitWithPrebound() {
517
477
assertThat (TransactionSynchronizationManager .hasResource (factory )).isTrue ();
518
478
assertThat (TransactionSynchronizationManager .isSynchronizationActive ()).isTrue ();
519
479
EntityManagerFactoryUtils .getTransactionalEntityManager (factory );
520
- return l ;
480
+ return testList ;
521
481
});
522
- assertThat (result ).isSameAs (l );
482
+ assertThat (result ).isSameAs (testList );
523
483
524
484
assertThat (TransactionSynchronizationManager .hasResource (factory )).isTrue ();
525
485
assertThat (TransactionSynchronizationManager .isSynchronizationActive ()).isFalse ();
@@ -563,9 +523,6 @@ void testTransactionRollbackWithPrebound() {
563
523
564
524
@ Test
565
525
void testTransactionCommitWithPreboundAndPropagationSupports () {
566
- final List <String > l = new ArrayList <>();
567
- l .add ("test" );
568
-
569
526
tt .setPropagationBehavior (TransactionDefinition .PROPAGATION_SUPPORTS );
570
527
571
528
assertThat (TransactionSynchronizationManager .hasResource (factory )).isFalse ();
@@ -578,9 +535,9 @@ void testTransactionCommitWithPreboundAndPropagationSupports() {
578
535
assertThat (TransactionSynchronizationManager .isSynchronizationActive ()).isTrue ();
579
536
assertThat (status .isNewTransaction ()).isFalse ();
580
537
EntityManagerFactoryUtils .getTransactionalEntityManager (factory ).flush ();
581
- return l ;
538
+ return testList ;
582
539
});
583
- assertThat (result ).isSameAs (l );
540
+ assertThat (result ).isSameAs (testList );
584
541
585
542
assertThat (TransactionSynchronizationManager .hasResource (factory )).isTrue ();
586
543
assertThat (TransactionSynchronizationManager .isSynchronizationActive ()).isFalse ();
0 commit comments