-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
FaultTolerantStepFactoryBeanTests.java
1141 lines (910 loc) · 36.5 KB
/
FaultTolerantStepFactoryBeanTests.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright 2008-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.batch.core.step.item;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.springframework.aop.framework.ProxyFactory;
import org.springframework.batch.core.BatchStatus;
import org.springframework.batch.core.ChunkListener;
import org.springframework.batch.core.ExitStatus;
import org.springframework.batch.core.ItemProcessListener;
import org.springframework.batch.core.ItemReadListener;
import org.springframework.batch.core.ItemWriteListener;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.SkipListener;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.StepExecution;
import org.springframework.batch.core.StepListener;
import org.springframework.batch.core.listener.SkipListenerSupport;
import org.springframework.batch.core.repository.JobRepository;
import org.springframework.batch.core.repository.support.MapJobRepositoryFactoryBean;
import org.springframework.batch.core.scope.context.ChunkContext;
import org.springframework.batch.core.step.factory.FaultTolerantStepFactoryBean;
import org.springframework.batch.core.step.skip.LimitCheckingItemSkipPolicy;
import org.springframework.batch.core.step.skip.SkipLimitExceededException;
import org.springframework.batch.core.step.skip.SkipPolicy;
import org.springframework.batch.item.ExecutionContext;
import org.springframework.batch.item.ItemReader;
import org.springframework.batch.item.ItemStream;
import org.springframework.batch.item.ItemStreamException;
import org.springframework.batch.item.ItemStreamReader;
import org.springframework.batch.item.ItemWriter;
import org.springframework.batch.item.ItemWriterException;
import org.springframework.batch.item.ParseException;
import org.springframework.batch.item.UnexpectedInputException;
import org.springframework.batch.item.WriteFailedException;
import org.springframework.batch.item.WriterNotOpenException;
import org.springframework.batch.item.support.AbstractItemStreamItemReader;
import org.springframework.batch.support.transaction.ResourcelessTransactionManager;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.lang.Nullable;
import org.springframework.scheduling.concurrent.ConcurrentTaskExecutor;
import org.springframework.test.util.ReflectionTestUtils;
import org.springframework.util.StringUtils;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
/**
* Tests for {@link FaultTolerantStepFactoryBean}.
*/
public class FaultTolerantStepFactoryBeanTests {
protected final Log logger = LogFactory.getLog(getClass());
private FaultTolerantStepFactoryBean<String, String> factory;
private SkipReaderStub<String> reader;
private SkipProcessorStub<String> processor;
private SkipWriterStub<String> writer;
private JobExecution jobExecution;
private StepExecution stepExecution;
private JobRepository repository;
private boolean opened = false;
private boolean closed = false;
public FaultTolerantStepFactoryBeanTests() throws Exception {
reader = new SkipReaderStub<>();
processor = new SkipProcessorStub<>();
writer = new SkipWriterStub<>();
}
@SuppressWarnings("unchecked")
@Before
public void setUp() throws Exception {
factory = new FaultTolerantStepFactoryBean<>();
factory.setBeanName("stepName");
factory.setTransactionManager(new ResourcelessTransactionManager());
factory.setCommitInterval(2);
reader.clear();
reader.setItems("1", "2", "3", "4", "5");
factory.setItemReader(reader);
processor.clear();
factory.setItemProcessor(processor);
writer.clear();
factory.setItemWriter(writer);
factory.setSkipLimit(2);
factory
.setSkippableExceptionClasses(getExceptionMap(SkippableException.class, SkippableRuntimeException.class));
MapJobRepositoryFactoryBean repositoryFactory = new MapJobRepositoryFactoryBean();
repositoryFactory.afterPropertiesSet();
repository = repositoryFactory.getObject();
factory.setJobRepository(repository);
jobExecution = repository.createJobExecution("skipJob", new JobParameters());
stepExecution = jobExecution.createStepExecution(factory.getName());
repository.add(stepExecution);
}
@Test
public void testMandatoryReader() {
// given
factory = new FaultTolerantStepFactoryBean<>();
factory.setItemWriter(writer);
// when
final Exception expectedException = Assert.assertThrows(IllegalStateException.class, factory::getObject);
// then
assertEquals("ItemReader must be provided", expectedException.getMessage());
}
@Test
public void testMandatoryWriter() throws Exception {
// given
factory = new FaultTolerantStepFactoryBean<>();
factory.setItemReader(reader);
// when
final Exception expectedException = Assert.assertThrows(IllegalStateException.class, factory::getObject);
// then
assertEquals("ItemWriter must be provided", expectedException.getMessage());
}
/**
* Non-skippable (and non-fatal) exception causes failure immediately.
*
* @throws Exception
*/
@SuppressWarnings("unchecked")
@Test
public void testNonSkippableExceptionOnRead() throws Exception {
reader.setFailures("2");
// nothing is skippable
factory.setSkippableExceptionClasses(getExceptionMap(NonExistentException.class));
Step step = factory.getObject();
step.execute(stepExecution);
assertEquals(BatchStatus.FAILED, stepExecution.getStatus());
assertEquals(ExitStatus.FAILED.getExitCode(), stepExecution.getExitStatus().getExitCode());
assertTrue(stepExecution.getExitStatus().getExitDescription().contains("Non-skippable exception during read"));
assertStepExecutionsAreEqual(stepExecution, repository.getLastStepExecution(jobExecution.getJobInstance(), step
.getName()));
}
@SuppressWarnings("unchecked")
@Test
public void testNonSkippableException() throws Exception {
// nothing is skippable
factory.setSkippableExceptionClasses(getExceptionMap(NonExistentException.class));
factory.setCommitInterval(1);
// no failures on read
reader.setItems("1", "2", "3", "4", "5");
writer.setFailures("1");
Step step = factory.getObject();
step.execute(stepExecution);
assertEquals(BatchStatus.FAILED, stepExecution.getStatus());
assertEquals(1, reader.getRead().size());
assertEquals(ExitStatus.FAILED.getExitCode(), stepExecution.getExitStatus().getExitCode());
assertTrue(stepExecution.getExitStatus().getExitDescription().contains("Intended Failure"));
assertStepExecutionsAreEqual(stepExecution, repository.getLastStepExecution(jobExecution.getJobInstance(), step
.getName()));
}
/**
* Check items causing errors are skipped as expected.
*/
@Test
public void testReadSkip() throws Exception {
reader.setFailures("2");
Step step = factory.getObject();
step.execute(stepExecution);
assertEquals(1, stepExecution.getSkipCount());
assertEquals(1, stepExecution.getReadSkipCount());
assertEquals(4, stepExecution.getReadCount());
assertEquals(0, stepExecution.getWriteSkipCount());
assertEquals(0, stepExecution.getRollbackCount());
// writer did not skip "2" as it never made it to writer, only "4" did
assertTrue(reader.getRead().contains("4"));
assertFalse(reader.getRead().contains("2"));
List<String> expectedOutput = Arrays.asList(StringUtils.commaDelimitedListToStringArray("1,3,4,5"));
assertEquals(expectedOutput, writer.getWritten());
assertEquals(BatchStatus.COMPLETED, stepExecution.getStatus());
assertStepExecutionsAreEqual(stepExecution, repository.getLastStepExecution(jobExecution.getJobInstance(), step
.getName()));
}
/**
* Check items causing errors are skipped as expected.
*/
@Test
public void testReadSkipWithPolicy() throws Exception {
// Should be ignored
factory.setSkipLimit(0);
factory.setSkipPolicy(new LimitCheckingItemSkipPolicy(2, Collections
.<Class<? extends Throwable>, Boolean> singletonMap(Exception.class, true)));
testReadSkip();
}
/**
* Check items causing errors are skipped as expected.
*/
@Test
public void testReadSkipWithPolicyExceptionInReader() throws Exception {
// Should be ignored
factory.setSkipLimit(0);
factory.setSkipPolicy(new SkipPolicy() {
@Override
public boolean shouldSkip(Throwable t, int skipCount) throws SkipLimitExceededException {
throw new RuntimeException("Planned exception in SkipPolicy");
}
});
reader.setFailures("2");
Step step = factory.getObject();
step.execute(stepExecution);
assertEquals(BatchStatus.FAILED, stepExecution.getStatus());
assertEquals(0, stepExecution.getReadSkipCount());
assertEquals(1, stepExecution.getReadCount());
}
/**
* Check items causing errors are skipped as expected.
*/
@Test
public void testReadSkipWithPolicyExceptionInWriter() throws Exception {
// Should be ignored
factory.setSkipLimit(0);
factory.setSkipPolicy(new SkipPolicy() {
@Override
public boolean shouldSkip(Throwable t, int skipCount) throws SkipLimitExceededException {
throw new RuntimeException("Planned exception in SkipPolicy");
}
});
writer.setFailures("2");
Step step = factory.getObject();
step.execute(stepExecution);
assertEquals(BatchStatus.FAILED, stepExecution.getStatus());
assertEquals(0, stepExecution.getWriteSkipCount());
assertEquals(2, stepExecution.getReadCount());
}
/**
* Check to make sure that ItemStreamException can be skipped. (see
* BATCH-915)
*/
@Test
public void testReadSkipItemStreamException() throws Exception {
reader.setFailures("2");
reader.setExceptionType(ItemStreamException.class);
Map<Class<? extends Throwable>, Boolean> map = new HashMap<>();
map.put(ItemStreamException.class, true);
factory.setSkippableExceptionClasses(map);
Step step = factory.getObject();
step.execute(stepExecution);
assertEquals(1, stepExecution.getSkipCount());
assertEquals(1, stepExecution.getReadSkipCount());
assertEquals(4, stepExecution.getReadCount());
assertEquals(0, stepExecution.getWriteSkipCount());
assertEquals(0, stepExecution.getRollbackCount());
// writer did not skip "2" as it never made it to writer, only "4" did
assertTrue(reader.getRead().contains("4"));
assertFalse(reader.getRead().contains("2"));
List<String> expectedOutput = Arrays.asList(StringUtils.commaDelimitedListToStringArray("1,3,4,5"));
assertEquals(expectedOutput, writer.getWritten());
assertEquals(BatchStatus.COMPLETED, stepExecution.getStatus());
assertStepExecutionsAreEqual(stepExecution, repository.getLastStepExecution(jobExecution.getJobInstance(), step
.getName()));
}
/**
* Check items causing errors are skipped as expected.
*/
@Test
public void testProcessSkip() throws Exception {
processor.setFailures("4");
writer.setFailures("4");
Step step = factory.getObject();
step.execute(stepExecution);
assertEquals(1, stepExecution.getSkipCount());
assertEquals(0, stepExecution.getReadSkipCount());
assertEquals(5, stepExecution.getReadCount());
assertEquals(1, stepExecution.getProcessSkipCount());
assertEquals(1, stepExecution.getRollbackCount());
// writer skips "4"
assertTrue(reader.getRead().contains("4"));
assertFalse(writer.getWritten().contains("4"));
List<String> expectedOutput = Arrays.asList(StringUtils.commaDelimitedListToStringArray("1,2,3,5"));
assertEquals(expectedOutput, writer.getWritten());
assertEquals(BatchStatus.COMPLETED, stepExecution.getStatus());
assertStepExecutionsAreEqual(stepExecution, repository.getLastStepExecution(jobExecution.getJobInstance(), step
.getName()));
}
@Test
public void testProcessFilter() throws Exception {
processor.setFailures("4");
processor.setFilter(true);
ItemProcessListenerStub<String, String> listenerStub = new ItemProcessListenerStub<>();
factory.setListeners(new StepListener[] { listenerStub });
Step step = factory.getObject();
step.execute(stepExecution);
assertEquals(0, stepExecution.getSkipCount());
assertEquals(0, stepExecution.getReadSkipCount());
assertEquals(5, stepExecution.getReadCount());
assertEquals(1, stepExecution.getFilterCount());
assertEquals(0, stepExecution.getRollbackCount());
assertTrue(listenerStub.isFilterEncountered());
// writer skips "4"
assertTrue(reader.getRead().contains("4"));
assertFalse(writer.getWritten().contains("4"));
List<String> expectedOutput = Arrays.asList(StringUtils.commaDelimitedListToStringArray("1,2,3,5"));
assertEquals(expectedOutput, writer.getWritten());
assertEquals(BatchStatus.COMPLETED, stepExecution.getStatus());
assertStepExecutionsAreEqual(stepExecution, repository.getLastStepExecution(jobExecution.getJobInstance(), step
.getName()));
}
/**
* Check items causing errors are skipped as expected.
*/
@Test
public void testWriteSkip() throws Exception {
writer.setFailures("4");
Step step = factory.getObject();
step.execute(stepExecution);
assertEquals(1, stepExecution.getSkipCount());
assertEquals(0, stepExecution.getReadSkipCount());
assertEquals(5, stepExecution.getReadCount());
assertEquals(1, stepExecution.getWriteSkipCount());
assertEquals(2, stepExecution.getRollbackCount());
// writer skips "4"
assertTrue(reader.getRead().contains("4"));
assertFalse(writer.getCommitted().contains("4"));
List<String> expectedOutput = Arrays.asList(StringUtils.commaDelimitedListToStringArray("1,2,3,5"));
assertEquals(expectedOutput, writer.getCommitted());
assertEquals(BatchStatus.COMPLETED, stepExecution.getStatus());
assertStepExecutionsAreEqual(stepExecution, repository.getLastStepExecution(jobExecution.getJobInstance(), step
.getName()));
}
/**
* Fatal exception should cause immediate termination provided the exception
* is not skippable (note the fatal exception is also classified as
* rollback).
*/
@Test
public void testFatalException() throws Exception {
reader.setFailures("2");
Map<Class<? extends Throwable>, Boolean> map = new HashMap<>();
map.put(SkippableException.class, true);
map.put(SkippableRuntimeException.class, true);
map.put(FatalRuntimeException.class, false);
factory.setSkippableExceptionClasses(map);
factory.setItemWriter(new ItemWriter<String>() {
@Override
public void write(List<? extends String> items) {
throw new FatalRuntimeException("Ouch!");
}
});
Step step = factory.getObject();
step.execute(stepExecution);
String message = stepExecution.getFailureExceptions().get(0).getCause().getMessage();
assertEquals("Wrong message: ", "Ouch!", message);
assertStepExecutionsAreEqual(stepExecution, repository.getLastStepExecution(jobExecution.getJobInstance(), step
.getName()));
}
/**
* Check items causing errors are skipped as expected.
*/
@Test
public void testSkipOverLimit() throws Exception {
reader.setFailures("2");
writer.setFailures("4");
factory.setSkipLimit(1);
Step step = factory.getObject();
step.execute(stepExecution);
assertEquals(1, stepExecution.getSkipCount());
// writer did not skip "2" as it never made it to writer, only "4" did
assertTrue(reader.getRead().contains("4"));
assertFalse(writer.getCommitted().contains("4"));
// failure on "4" tripped the skip limit so we never got to "5"
List<String> expectedOutput = Arrays.asList(StringUtils.commaDelimitedListToStringArray("1,3"));
assertEquals(expectedOutput, writer.getCommitted());
assertStepExecutionsAreEqual(stepExecution, repository.getLastStepExecution(jobExecution.getJobInstance(), step
.getName()));
}
/**
* Check items causing errors are skipped as expected.
*/
@SuppressWarnings("unchecked")
@Test
public void testSkipOverLimitOnRead() throws Exception {
reader.setItems(StringUtils.commaDelimitedListToStringArray("1,2,3,4,5,6"));
reader.setFailures(StringUtils.commaDelimitedListToStringArray("2,3,5"));
writer.setFailures("4");
factory.setSkipLimit(3);
factory.setSkippableExceptionClasses(getExceptionMap(Exception.class));
Step step = factory.getObject();
step.execute(stepExecution);
assertEquals(BatchStatus.FAILED, stepExecution.getStatus());
assertEquals(3, stepExecution.getSkipCount());
assertEquals(2, stepExecution.getReadSkipCount());
assertEquals(1, stepExecution.getWriteSkipCount());
assertEquals(2, stepExecution.getReadCount());
// writer did not skip "2" as it never made it to writer, only "4" did
assertFalse(reader.getRead().contains("2"));
assertTrue(reader.getRead().contains("4"));
// only "1" was ever committed
List<String> expectedOutput = Arrays.asList(StringUtils.commaDelimitedListToStringArray("1"));
assertEquals(expectedOutput, writer.getCommitted());
assertStepExecutionsAreEqual(stepExecution, repository.getLastStepExecution(jobExecution.getJobInstance(), step
.getName()));
}
/**
* Check items causing errors are skipped as expected.
*/
@Test
public void testSkipOverLimitOnReadWithListener() throws Exception {
reader.setFailures("1", "3", "5");
writer.setFailures();
final List<Throwable> listenerCalls = new ArrayList<>();
factory.setListeners(new StepListener[] { new SkipListenerSupport<String, String>() {
@Override
public void onSkipInRead(Throwable t) {
listenerCalls.add(t);
}
} });
factory.setCommitInterval(2);
factory.setSkipLimit(2);
Step step = factory.getObject();
step.execute(stepExecution);
// 1,3 skipped inside a committed chunk. 5 tripped the skip
// limit but it was skipped in a chunk that rolled back, so
// it will re-appear on a restart and the listener is not called.
assertEquals(2, listenerCalls.size());
assertEquals(2, stepExecution.getReadSkipCount());
assertEquals(BatchStatus.FAILED, stepExecution.getStatus());
}
/**
* Check items causing errors are skipped as expected.
*/
@SuppressWarnings("unchecked")
@Test
public void testSkipListenerFailsOnRead() throws Exception {
reader.setItems(StringUtils.commaDelimitedListToStringArray("1,2,3,4,5,6"));
reader.setFailures(StringUtils.commaDelimitedListToStringArray("2,3,5"));
writer.setFailures("4");
factory.setSkipLimit(3);
factory.setListeners(new StepListener[] { new SkipListenerSupport<String, String>() {
@Override
public void onSkipInRead(Throwable t) {
throw new RuntimeException("oops");
}
} });
factory.setSkippableExceptionClasses(getExceptionMap(Exception.class));
Step step = factory.getObject();
step.execute(stepExecution);
assertEquals(BatchStatus.FAILED, stepExecution.getStatus());
assertEquals("oops", stepExecution.getFailureExceptions().get(0).getCause().getMessage());
// listeners are called only once chunk is about to commit, so
// listener failure does not affect other statistics
assertEquals(2, stepExecution.getReadSkipCount());
// but we didn't get as far as the write skip in the scan:
assertEquals(0, stepExecution.getWriteSkipCount());
assertEquals(2, stepExecution.getSkipCount());
assertStepExecutionsAreEqual(stepExecution, repository.getLastStepExecution(jobExecution.getJobInstance(), step
.getName()));
}
/**
* Check items causing errors are skipped as expected.
*/
@SuppressWarnings("unchecked")
@Test
public void testSkipListenerFailsOnWrite() throws Exception {
reader.setItems(StringUtils.commaDelimitedListToStringArray("1,2,3,4,5,6"));
writer.setFailures("4");
factory.setSkipLimit(3);
factory.setListeners(new StepListener[] { new SkipListenerSupport<String, String>() {
@Override
public void onSkipInWrite(String item, Throwable t) {
throw new RuntimeException("oops");
}
} });
factory.setSkippableExceptionClasses(getExceptionMap(Exception.class));
Step step = factory.getObject();
step.execute(stepExecution);
assertEquals(BatchStatus.FAILED, stepExecution.getStatus());
assertEquals("oops", stepExecution.getFailureExceptions().get(0).getCause().getMessage());
assertEquals(1, stepExecution.getSkipCount());
assertEquals(0, stepExecution.getReadSkipCount());
assertEquals(1, stepExecution.getWriteSkipCount());
assertStepExecutionsAreEqual(stepExecution, repository.getLastStepExecution(jobExecution.getJobInstance(), step
.getName()));
}
/**
* Check items causing errors are skipped as expected.
*/
@Test
public void testSkipOnReadNotDoubleCounted() throws Exception {
reader.setItems(StringUtils.commaDelimitedListToStringArray("1,2,3,4,5,6"));
reader.setFailures(StringUtils.commaDelimitedListToStringArray("2,3,5"));
writer.setFailures("4");
factory.setSkipLimit(4);
Step step = factory.getObject();
step.execute(stepExecution);
assertEquals(4, stepExecution.getSkipCount());
assertEquals(3, stepExecution.getReadSkipCount());
assertEquals(1, stepExecution.getWriteSkipCount());
// skipped 2,3,4,5
List<String> expectedOutput = Arrays.asList(StringUtils.commaDelimitedListToStringArray("1,6"));
assertEquals(expectedOutput, writer.getCommitted());
// reader exceptions should not cause rollback, 1 writer exception
// causes 2 rollbacks
assertEquals(2, stepExecution.getRollbackCount());
assertStepExecutionsAreEqual(stepExecution, repository.getLastStepExecution(jobExecution.getJobInstance(), step
.getName()));
}
/**
* Check items causing errors are skipped as expected.
*/
@Test
public void testSkipOnWriteNotDoubleCounted() throws Exception {
reader.setItems(StringUtils.commaDelimitedListToStringArray("1,2,3,4,5,6,7"));
reader.setFailures(StringUtils.commaDelimitedListToStringArray("2,3"));
writer.setFailures("4", "5");
factory.setSkipLimit(4);
factory.setCommitInterval(3); // includes all expected skips
Step step = factory.getObject();
step.execute(stepExecution);
assertEquals(4, stepExecution.getSkipCount());
assertEquals(2, stepExecution.getReadSkipCount());
assertEquals(2, stepExecution.getWriteSkipCount());
// skipped 2,3,4,5
List<String> expectedOutput = Arrays.asList(StringUtils.commaDelimitedListToStringArray("1,6,7"));
assertEquals(expectedOutput, writer.getCommitted());
assertStepExecutionsAreEqual(stepExecution, repository.getLastStepExecution(jobExecution.getJobInstance(), step
.getName()));
}
@SuppressWarnings("unchecked")
@Test
public void testDefaultSkipPolicy() throws Exception {
reader.setItems("a", "b", "c");
reader.setFailures("b");
factory.setSkippableExceptionClasses(getExceptionMap(Exception.class));
factory.setSkipLimit(1);
Step step = factory.getObject();
step.execute(stepExecution);
assertEquals(1, stepExecution.getSkipCount());
assertEquals("[a, c]", reader.getRead().toString());
assertStepExecutionsAreEqual(stepExecution, repository.getLastStepExecution(jobExecution.getJobInstance(), step
.getName()));
}
/**
* Check items causing errors are skipped as expected.
*/
@SuppressWarnings("unchecked")
@Test
public void testSkipOverLimitOnReadWithAllSkipsAtEnd() throws Exception {
reader.setItems(StringUtils.commaDelimitedListToStringArray("1,2,3,4,5,6,7,8,9,10,11,12,13,14,15"));
reader.setFailures(StringUtils.commaDelimitedListToStringArray("6,12,13,14,15"));
writer.setFailures("4");
factory.setCommitInterval(5);
factory.setSkipLimit(3);
factory.setSkippableExceptionClasses(getExceptionMap(Exception.class));
Step step = factory.getObject();
step.execute(stepExecution);
assertEquals(BatchStatus.FAILED, stepExecution.getStatus());
assertEquals("bad skip count", 3, stepExecution.getSkipCount());
assertEquals("bad read skip count", 2, stepExecution.getReadSkipCount());
assertEquals("bad write skip count", 1, stepExecution.getWriteSkipCount());
// writer did not skip "6" as it never made it to writer, only "4" did
assertFalse(reader.getRead().contains("6"));
assertTrue(reader.getRead().contains("4"));
// only "1" was ever committed
List<String> expectedOutput = Arrays.asList(StringUtils.commaDelimitedListToStringArray("1,2,3,5,7,8,9,10,11"));
assertEquals(expectedOutput, writer.getCommitted());
assertStepExecutionsAreEqual(stepExecution, repository.getLastStepExecution(jobExecution.getJobInstance(), step
.getName()));
}
@Test
public void testReprocessingAfterWriterRollback() throws Exception {
reader.setItems("1", "2", "3", "4");
writer.setFailures("4");
Step step = factory.getObject();
step.execute(stepExecution);
assertEquals(1, stepExecution.getSkipCount());
assertEquals(2, stepExecution.getRollbackCount());
// 1,2,3,4,3,4 - one scan until the item is
// identified and finally skipped on the second attempt
assertEquals("[1, 2, 3, 4, 3, 4]", processor.getProcessed().toString());
assertStepExecutionsAreEqual(stepExecution, repository.getLastStepExecution(jobExecution.getJobInstance(), step
.getName()));
}
@Test
public void testAutoRegisterItemListeners() throws Exception {
reader.setFailures("2");
final List<Integer> listenerCalls = new ArrayList<>();
class TestItemListenerWriter implements ItemWriter<String>, ItemReadListener<String>,
ItemWriteListener<String>, ItemProcessListener<String, String>, SkipListener<String, String>,
ChunkListener {
@Override
public void write(List<? extends String> items) throws Exception {
if (items.contains("4")) {
throw new SkippableException("skippable");
}
}
@Override
public void afterRead(String item) {
listenerCalls.add(1);
}
@Override
public void beforeRead() {
}
@Override
public void onReadError(Exception ex) {
}
@Override
public void afterWrite(List<? extends String> items) {
listenerCalls.add(2);
}
@Override
public void beforeWrite(List<? extends String> items) {
}
@Override
public void onWriteError(Exception exception, List<? extends String> items) {
}
@Override
public void afterProcess(String item, @Nullable String result) {
listenerCalls.add(3);
}
@Override
public void beforeProcess(String item) {
}
@Override
public void onProcessError(String item, Exception e) {
}
@Override
public void afterChunk(ChunkContext context) {
listenerCalls.add(4);
}
@Override
public void beforeChunk(ChunkContext context) {
}
@Override
public void onSkipInProcess(String item, Throwable t) {
}
@Override
public void onSkipInRead(Throwable t) {
listenerCalls.add(6);
}
@Override
public void onSkipInWrite(String item, Throwable t) {
listenerCalls.add(5);
}
@Override
public void afterChunkError(ChunkContext context) {
}
}
factory.setItemWriter(new TestItemListenerWriter());
Step step = factory.getObject();
step.execute(stepExecution);
assertEquals(BatchStatus.COMPLETED, stepExecution.getStatus());
for (int i = 1; i <= 6; i++) {
assertTrue("didn't call listener " + i, listenerCalls.contains(i));
}
}
/**
* Check ItemStream is opened
*/
@Test
public void testItemStreamOpenedEvenWithTaskExecutor() throws Exception {
writer.setFailures("4");
ItemReader<String> reader = new AbstractItemStreamItemReader<String>() {
@Override
public void close() {
super.close();
closed = true;
}
@Override
public void open(ExecutionContext executionContext) {
super.open(executionContext);
opened = true;
}
@Nullable
@Override
public String read() {
return null;
}
};
factory.setItemReader(reader);
factory.setTaskExecutor(new ConcurrentTaskExecutor());
Step step = factory.getObject();
step.execute(stepExecution);
assertTrue(opened);
assertTrue(closed);
assertEquals(BatchStatus.COMPLETED, stepExecution.getStatus());
}
/**
* Check ItemStream is opened
*/
@Test
public void testNestedItemStreamOpened() throws Exception {
writer.setFailures("4");
ItemStreamReader<String> reader = new ItemStreamReader<String>() {
@Override
public void close() throws ItemStreamException {
}
@Override
public void open(ExecutionContext executionContext) throws ItemStreamException {
}
@Override
public void update(ExecutionContext executionContext) throws ItemStreamException {
}
@Nullable
@Override
public String read() throws Exception, UnexpectedInputException, ParseException {
return null;
}
};
ItemStreamReader<String> stream = new ItemStreamReader<String>() {
@Override
public void close() throws ItemStreamException {
closed = true;
}
@Override
public void open(ExecutionContext executionContext) throws ItemStreamException {
opened = true;
}
@Override
public void update(ExecutionContext executionContext) throws ItemStreamException {
}
@Nullable
@Override
public String read() throws Exception, UnexpectedInputException, ParseException {
return null;
}
};
factory.setItemReader(reader);
factory.setStreams(new ItemStream[] { stream, reader });
Step step = factory.getObject();
step.execute(stepExecution);
assertTrue(opened);
assertTrue(closed);
assertEquals(BatchStatus.COMPLETED, stepExecution.getStatus());
}
/**
* Check ItemStream is opened
*/
@SuppressWarnings("unchecked")
@Test
public void testProxiedItemStreamOpened() throws Exception {
writer.setFailures("4");
ItemStreamReader<String> reader = new ItemStreamReader<String>() {
@Override
public void close() throws ItemStreamException {
closed = true;
}
@Override
public void open(ExecutionContext executionContext) throws ItemStreamException {
opened = true;
}
@Override
public void update(ExecutionContext executionContext) throws ItemStreamException {
}
@Nullable
@Override
public String read() throws Exception, UnexpectedInputException, ParseException {
return null;
}
};
ProxyFactory proxy = new ProxyFactory();
proxy.setTarget(reader);
proxy.setInterfaces(new Class<?>[] { ItemReader.class, ItemStream.class });
proxy.addAdvice(new MethodInterceptor() {
@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
return invocation.proceed();
}
});
Object advised = proxy.getProxy();
factory.setItemReader((ItemReader<? extends String>) advised);
factory.setStreams(new ItemStream[] { (ItemStream) advised });
Step step = factory.getObject();
step.execute(stepExecution);
assertTrue(opened);
assertTrue(closed);
assertEquals(BatchStatus.COMPLETED, stepExecution.getStatus());
}
private static class ItemProcessListenerStub<T, S> implements ItemProcessListener<T, S> {