View Javadoc

1   /*
2    * Copyright 2006-2007 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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   * Base class for simple tests with small trade data set.
34   *
35   * @author Dave Syer
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  		// This has to be synchronized because we are going to test the state
78  		// (count) at the end of a concurrent batch run.
79  		public synchronized void write(List<? extends Trade> data) {
80  			count++;
81  			System.out.println("Executing trade '" + data + "'");
82  		}
83  	}
84  
85  }