1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.springframework.batch.repeat.support;
18
19 import java.util.List;
20
21 import org.junit.Before;
22 import org.springframework.batch.item.ExecutionContext;
23 import org.springframework.batch.item.ItemWriter;
24 import org.springframework.batch.item.file.FlatFileItemReader;
25 import org.springframework.batch.item.file.mapping.DefaultLineMapper;
26 import org.springframework.batch.item.file.mapping.FieldSetMapper;
27 import org.springframework.batch.item.file.transform.DelimitedLineTokenizer;
28 import org.springframework.batch.item.file.transform.FieldSet;
29 import org.springframework.core.io.ClassPathResource;
30 import org.springframework.core.io.Resource;
31
32
33
34
35
36
37
38 public abstract class AbstractTradeBatchTests {
39
40 public static final int NUMBER_OF_ITEMS = 5;
41
42 Resource resource = new ClassPathResource("trades.csv", getClass());
43
44 protected TradeWriter processor = new TradeWriter();
45
46 protected TradeItemReader provider;
47
48 @Before
49 public void setUp() throws Exception {
50 provider = new TradeItemReader(resource);
51 provider.open(new ExecutionContext());
52 }
53
54 protected static class TradeItemReader extends FlatFileItemReader<Trade> {
55
56 protected TradeItemReader(Resource resource) throws Exception {
57 super();
58 setResource(resource);
59 DefaultLineMapper<Trade> mapper = new DefaultLineMapper<Trade>();
60 mapper.setLineTokenizer(new DelimitedLineTokenizer());
61 mapper.setFieldSetMapper(new TradeMapper());
62 setLineMapper(mapper);
63 afterPropertiesSet();
64 }
65
66 }
67
68 protected static class TradeMapper implements FieldSetMapper<Trade> {
69 public Trade mapFieldSet(FieldSet fs) {
70 return new Trade(fs);
71 }
72 }
73
74 protected static class TradeWriter implements ItemWriter<Trade> {
75 int count = 0;
76
77
78
79 public synchronized void write(List<? extends Trade> data) {
80 count++;
81 System.out.println("Executing trade '" + data + "'");
82 }
83 }
84
85 }