17
17
18
18
import java .util .ArrayList ;
19
19
import java .util .Arrays ;
20
- import java .util .Collection ;
21
20
import java .util .Collections ;
22
- import java .util .HashMap ;
23
21
import java .util .List ;
24
22
import java .util .Map ;
25
23
43
41
import org .springframework .batch .core .step .factory .FaultTolerantStepFactoryBean ;
44
42
import org .springframework .batch .item .ItemReader ;
45
43
import org .springframework .batch .item .support .ListItemReader ;
44
+ import org .springframework .batch .item .support .SynchronizedItemReader ;
46
45
import org .springframework .batch .support .transaction .ResourcelessTransactionManager ;
47
46
import org .springframework .batch .support .transaction .TransactionAwareProxyFactory ;
48
47
import org .springframework .core .task .SimpleAsyncTaskExecutor ;
@@ -79,7 +78,6 @@ class FaultTolerantStepFactoryBeanRollbackTests {
79
78
80
79
private JobRepository repository ;
81
80
82
- @ SuppressWarnings ("unchecked" )
83
81
@ BeforeEach
84
82
void setUp () throws Exception {
85
83
reader = new SkipReaderStub <>();
@@ -103,7 +101,7 @@ void setUp() throws Exception {
103
101
104
102
factory .setSkipLimit (2 );
105
103
106
- factory .setSkippableExceptionClasses (getExceptionMap (Exception .class ));
104
+ factory .setSkippableExceptionClasses (Map . of (Exception .class , true ));
107
105
108
106
EmbeddedDatabase embeddedDatabase = new EmbeddedDatabaseBuilder ()
109
107
.addScript ("/org/springframework/batch/core/schema-drop-hsqldb.sql" )
@@ -177,17 +175,16 @@ void testReaderDefaultNoRollbackOnCheckedException() throws Exception {
177
175
/**
178
176
* Scenario: Exception in reader that should not cause rollback
179
177
*/
180
- @ SuppressWarnings ("unchecked" )
181
178
@ Test
182
179
void testReaderAttributesOverrideSkippableNoRollback () throws Exception {
183
180
reader .setFailures ("2" , "3" );
184
181
reader .setItems ("1" , "2" , "3" , "4" );
185
182
reader .setExceptionType (SkippableException .class );
186
183
187
184
// No skips by default
188
- factory .setSkippableExceptionClasses (getExceptionMap (RuntimeException .class ));
185
+ factory .setSkippableExceptionClasses (Map . of (RuntimeException .class , true ));
189
186
// But this one is explicit in the tx-attrs so it should be skipped
190
- factory .setNoRollbackExceptionClasses (getExceptionList (SkippableException .class ));
187
+ factory .setNoRollbackExceptionClasses (List . of (SkippableException .class ));
191
188
192
189
Step step = factory .getObject ();
193
190
@@ -249,11 +246,8 @@ void testNoRollbackInProcessorWhenSkipExceeded() throws Throwable {
249
246
processor .clear ();
250
247
factory .setItemProcessor (processor );
251
248
252
- List <Class <? extends Throwable >> exceptions = Arrays .asList (Exception .class );
253
- factory .setNoRollbackExceptionClasses (exceptions );
254
- @ SuppressWarnings ("unchecked" )
255
- Map <Class <? extends Throwable >, Boolean > skippable = getExceptionMap (Exception .class );
256
- factory .setSkippableExceptionClasses (skippable );
249
+ factory .setNoRollbackExceptionClasses (List .of (Exception .class ));
250
+ factory .setSkippableExceptionClasses (Map .of (Exception .class , true ));
257
251
258
252
processor .setFailures ("2" );
259
253
@@ -279,7 +273,7 @@ void testProcessSkipWithNoRollbackForCheckedException() throws Exception {
279
273
processor .setFailures ("4" );
280
274
processor .setExceptionType (SkippableException .class );
281
275
282
- factory .setNoRollbackExceptionClasses (getExceptionList (SkippableException .class ));
276
+ factory .setNoRollbackExceptionClasses (List . of (SkippableException .class ));
283
277
284
278
Step step = factory .getObject ();
285
279
@@ -359,7 +353,7 @@ void testWriterNoRollbackOnRuntimeException() throws Exception {
359
353
writer .setFailures ("2" , "3" );
360
354
writer .setExceptionType (SkippableRuntimeException .class );
361
355
362
- factory .setNoRollbackExceptionClasses (getExceptionList (SkippableRuntimeException .class ));
356
+ factory .setNoRollbackExceptionClasses (List . of (SkippableRuntimeException .class ));
363
357
364
358
Step step = factory .getObject ();
365
359
@@ -380,7 +374,7 @@ void testWriterNoRollbackOnCheckedException() throws Exception {
380
374
writer .setFailures ("2" , "3" );
381
375
writer .setExceptionType (SkippableException .class );
382
376
383
- factory .setNoRollbackExceptionClasses (getExceptionList (SkippableException .class ));
377
+ factory .setNoRollbackExceptionClasses (List . of (SkippableException .class ));
384
378
385
379
Step step = factory .getObject ();
386
380
@@ -517,12 +511,7 @@ void testSkipInWriterTransactionalReader() throws Exception {
517
511
518
512
@ Test
519
513
void testMultithreadedSkipInWriter () throws Exception {
520
- factory .setItemReader (new ItemReader <>() {
521
- @ Override
522
- public synchronized String read () throws Exception {
523
- return reader .read ();
524
- }
525
- });
514
+ factory .setItemReader (new SynchronizedItemReader <>(reader ));
526
515
writer .setFailures ("1" , "2" , "3" , "4" , "5" );
527
516
factory .setCommitInterval (3 );
528
517
factory .setSkipLimit (10 );
@@ -575,23 +564,9 @@ void testMultipleSkipsInWriterNonTransactionalProcessor() throws Exception {
575
564
assertEquals ("[1, 2, 3, 4, 5]" , processor .getProcessed ().toString ());
576
565
}
577
566
578
- @ SuppressWarnings ("unchecked" )
579
- private Collection <Class <? extends Throwable >> getExceptionList (Class <? extends Throwable > arg ) {
580
- return Arrays .<Class <? extends Throwable >>asList (arg );
581
- }
582
-
583
- @ SuppressWarnings ("unchecked" )
584
- private Map <Class <? extends Throwable >, Boolean > getExceptionMap (Class <? extends Throwable >... args ) {
585
- Map <Class <? extends Throwable >, Boolean > map = new HashMap <>();
586
- for (Class <? extends Throwable > arg : args ) {
587
- map .put (arg , true );
588
- }
589
- return map ;
590
- }
591
-
592
567
static class ExceptionThrowingChunkListener implements ChunkListener {
593
568
594
- private int phase = - 1 ;
569
+ private final int phase ;
595
570
596
571
public ExceptionThrowingChunkListener (int throwPhase ) {
597
572
this .phase = throwPhase ;
0 commit comments