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.sample.support;
18
19 import org.springframework.batch.core.UnexpectedJobExecutionException;
20 import org.springframework.batch.item.ItemReader;
21
22 /**
23 * Hacked {@link ItemReader} that throws exception on a given record number
24 * (useful for testing restart).
25 *
26 * @author Robert Kasanicky
27 * @author Lucas Ward
28 *
29 */
30 public class ExceptionThrowingItemReaderProxy<T> implements ItemReader<T> {
31
32 private int counter = 0;
33
34 private int throwExceptionOnRecordNumber = 4;
35
36 private ItemReader<T> delegate;
37
38 /**
39 * @param throwExceptionOnRecordNumber The number of record on which
40 * exception should be thrown
41 */
42 public void setThrowExceptionOnRecordNumber(int throwExceptionOnRecordNumber) {
43 this.throwExceptionOnRecordNumber = throwExceptionOnRecordNumber;
44 }
45
46 public T read() throws Exception {
47
48 counter++;
49 if (counter == throwExceptionOnRecordNumber) {
50 throw new UnexpectedJobExecutionException("Planned failure on count=" + counter);
51 }
52
53 return delegate.read();
54 }
55
56 public void setDelegate(ItemReader<T> delegate) {
57 this.delegate = delegate;
58 }
59
60 }