BATCH-1761: test cases from Morten

This commit is contained in:
Dave Syer
2012-06-27 16:14:00 +01:00
parent 2d4363e893
commit d043fa9e30
2 changed files with 170 additions and 0 deletions

View File

@@ -31,6 +31,7 @@ import org.apache.commons.logging.LogFactory;
import org.junit.Before;
import org.junit.Test;
import org.springframework.batch.core.BatchStatus;
import org.springframework.batch.core.ExitStatus;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.JobParametersBuilder;
@@ -45,6 +46,7 @@ import org.springframework.batch.core.repository.dao.MapStepExecutionDao;
import org.springframework.batch.core.repository.support.SimpleJobRepository;
import org.springframework.batch.core.step.AbstractStep;
import org.springframework.batch.item.ExecutionContext;
import org.springframework.batch.item.ItemProcessor;
import org.springframework.batch.item.ItemReader;
import org.springframework.batch.item.ItemStream;
import org.springframework.batch.item.ItemWriter;
@@ -123,6 +125,135 @@ public class FaultTolerantStepFactoryBeanRetryTests {
assertTrue(factory.getObject() instanceof Step);
}
@Test
public void testProcessAllItemsWhenErrorInWriterTransformation() throws Exception{
FaultTolerantStepFactoryBean<String, Integer> factory = new FaultTolerantStepFactoryBean<String, Integer>();
factory.setBeanName("step");
factory.setItemReader(new ListItemReader<String>(new ArrayList<String>()));
factory.setJobRepository(repository);
factory.setTransactionManager(new ResourcelessTransactionManager());
@SuppressWarnings("unchecked")
Map<Class<? extends Throwable>, Boolean> exceptionMap = getExceptionMap(Exception.class);
factory.setRetryableExceptionClasses(exceptionMap);
ItemWriter<Integer> failingWriter = new ItemWriter<Integer>() {
public void write(List<? extends Integer> data) throws Exception {
int count = 0;
for (Integer item : data) {
if (count++ == 2) {
throw new Exception("Planned failure in writer");
}
written.add(item);
}
}};
ItemProcessor<String, Integer> processor = new ItemProcessor<String, Integer>() {
public Integer process(String item) throws Exception {
processed.add(item);
return Integer.parseInt(item);
}
};
ItemReader<String> reader = new ListItemReader<String>(Arrays.asList("1", "2", "3"));
factory.setCommitInterval(3);
factory.setRetryLimit(3);
factory.setSkippableExceptionClasses(new HashMap<Class<? extends Throwable>, Boolean>());
factory.setItemReader(reader);
factory.setItemProcessor(processor);
factory.setItemWriter(failingWriter);
Step step = (Step) factory.getObject();
StepExecution stepExecution = new StepExecution(step.getName(), jobExecution);
repository.add(stepExecution);
step.execute(stepExecution);
// System.out.println(stepExecution.getWriteCount());
// System.out.println(processed.size());
// System.out.println(processed);
// System.out.println(written);
assertEquals((1+3)*3, processed.size()); //(Initial try + retry limit)*item count
}
@Test
public void testProcessAllItemsWhenErrorInWriter() throws Exception {
final int RETRY_LIMIT = 3;
final List<String> ITEM_LIST = Arrays.asList("a", " b", "c");
ItemWriter<String> failingWriter = new ItemWriter<String>() {
public void write(List<? extends String> data) throws Exception {
int count = 0;
for (String item : data) {
if (count++ == 2) {
throw new Exception("Planned failure in writer");
}
written.add(item);
}
}
};
ItemProcessor<String, String> processor = new ItemProcessor<String, String>() {
public String process(String item) throws Exception {
processed.add(item);
return item;
}
};
ItemReader<String> reader = new ListItemReader<String>(ITEM_LIST);
factory.setCommitInterval(3);
factory.setRetryLimit(RETRY_LIMIT);
factory.setSkipLimit(1);
@SuppressWarnings("unchecked")
Map<Class<? extends Throwable>, Boolean> exceptionMap = getExceptionMap(Exception.class);
factory.setSkippableExceptionClasses(exceptionMap);
factory.setItemReader(reader);
factory.setItemProcessor(processor);
factory.setItemWriter(failingWriter);
Step step = (Step) factory.getObject();
StepExecution stepExecution = new StepExecution(step.getName(), jobExecution);
repository.add(stepExecution);
step.execute(stepExecution);
// System.out.println(processed);
assertEquals(ExitStatus.COMPLETED.getExitCode(), stepExecution.getExitStatus().getExitCode());
assertEquals((1 + RETRY_LIMIT) * ITEM_LIST.size(), processed.size());
}
@Test
public void testNoItemsReprocessedWhenErrorInWriterAndProcessorNotTransactional() throws Exception{
ItemWriter<String> failingWriter = new ItemWriter<String>() {
public void write(List<? extends String> data) throws Exception {
int count = 0;
for (String item : data) {
if (count++ == 2) {
throw new Exception("Planned failure in writer");
}
written.add(item);
}
}};
ItemProcessor<String, String> processor = new ItemProcessor<String, String>() {
public String process(String item) throws Exception {
processed.add(item);
return item;
}
};
ItemReader<String> reader = new ListItemReader<String>(Arrays.asList("a", "b", "c"));
factory.setProcessorTransactional(false);
factory.setCommitInterval(3);
factory.setRetryLimit(3);
factory.setSkippableExceptionClasses(new HashMap<Class<? extends Throwable>, Boolean>());
factory.setItemReader(reader);
factory.setItemProcessor(processor);
factory.setItemWriter(failingWriter);
Step step = (Step) factory.getObject();
StepExecution stepExecution = new StepExecution(step.getName(), jobExecution);
repository.add(stepExecution);
step.execute(stepExecution);
System.out.println(stepExecution.getWriteCount());
System.out.println(processed.size());
System.out.println(processed);
System.out.println(written);
assertEquals(3, processed.size()); //(Initial try + retry limit)*item count
}
/**
* N.B. this doesn't really test retry, since the retry is only on write
* failures, but it does test that read errors are re-presented for another