1 package org.springframework.batch.integration.chunk;
2
3 import java.util.ArrayList;
4 import java.util.List;
5
6 import org.apache.commons.logging.Log;
7 import org.apache.commons.logging.LogFactory;
8 import org.springframework.batch.item.ItemReader;
9 import org.springframework.batch.item.ParseException;
10 import org.springframework.batch.item.UnexpectedInputException;
11 import org.springframework.stereotype.Component;
12
13 @Component
14 public class TestItemReader<T> implements ItemReader<T> {
15
16 private static final Log logger = LogFactory.getLog(TestItemReader.class);
17
18
19
20
21 public volatile int count = 0;
22
23
24
25
26 public final static String FAIL_ON = "bad";
27
28
29
30
31 public static final String WAIT_ON = "wait";
32
33 private List<T> items = new ArrayList<T>();
34
35
36
37
38 public void setItems(List<T> items) {
39 this.items = items;
40 }
41
42 public T read() throws Exception, UnexpectedInputException, ParseException {
43
44 if (count>=items.size()) {
45 return null;
46 }
47
48 T item = items.get(count++);
49
50 logger.debug("Reading "+item);
51
52 if (item.equals(WAIT_ON)) {
53 try {
54 Thread.sleep(200);
55 }
56 catch (InterruptedException e) {
57 Thread.currentThread().interrupt();
58 throw new RuntimeException("Unexpected interruption.", e);
59 }
60 }
61
62 if (item.equals(FAIL_ON)) {
63 throw new IllegalStateException("Planned failure on: " + FAIL_ON);
64 }
65
66 return item;
67
68 }
69
70 }