Skip to content

Commit c7c2f30

Browse files
mminellafmbenhassine
authored andcommitted
Align XML and Java based configuration
This commit aligns the XML and Java based validations. When using XML to configure a chunk oriented step both an ItemReader and ItemWriter are requied. However when using Java based configuration the ItemWriter is optional if an ItemProcessor is present. Having no ItemWriter and only an ItemProcessor lead to strange results in one of our batch jobs, which was accidentily configured without an ItemProcessor. Related: BATCH-1520 Fixes: BATCH-2624
1 parent c1393a0 commit c7c2f30

File tree

4 files changed

+7
-56
lines changed

4 files changed

+7
-56
lines changed

spring-batch-core/src/main/java/org/springframework/batch/core/step/builder/FaultTolerantStepBuilder.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ protected void registerStepListenerAsSkipListener() {
178178
@Override
179179
protected Tasklet createTasklet() {
180180
Assert.state(getReader() != null, "ItemReader must be provided");
181-
Assert.state(getProcessor() != null || getWriter() != null, "ItemWriter or ItemProcessor must be provided");
181+
Assert.state(getWriter() != null, "ItemWriter must be provided");
182182
addSpecialExceptions();
183183
registerSkipListeners();
184184
ChunkProvider<I> chunkProvider = createChunkProvider();
@@ -773,6 +773,6 @@ public boolean equals(Object obj) {
773773
}
774774
return chunkListener.equals(obj);
775775
}
776-
776+
777777
}
778778
}

spring-batch-core/src/main/java/org/springframework/batch/core/step/builder/SimpleStepBuilder.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2006-2014 the original author or authors.
2+
* Copyright 2006-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -153,7 +153,7 @@ private void checkAndAddItemListener(StepListener stepListener) {
153153
@Override
154154
protected Tasklet createTasklet() {
155155
Assert.state(reader != null, "ItemReader must be provided");
156-
Assert.state(processor != null || writer != null, "ItemWriter or ItemProcessor must be provided");
156+
Assert.state(writer != null, "ItemWriter must be provided");
157157
RepeatOperations repeatOperations = createChunkOperations();
158158
SimpleChunkProvider<I> chunkProvider = new SimpleChunkProvider<I>(reader, repeatOperations);
159159
SimpleChunkProcessor<I, O> chunkProcessor = new SimpleChunkProcessor<I, O>(processor, writer);

spring-batch-core/src/test/java/org/springframework/batch/core/step/item/FaultTolerantStepFactoryBeanTests.java

+1-25
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2008-2014 the original author or authors.
2+
* Copyright 2008-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -371,30 +371,6 @@ public void testProcessFilter() throws Exception {
371371
.getName()));
372372
}
373373

374-
@Test
375-
public void testNullWriter() throws Exception {
376-
377-
factory.setItemWriter(null);
378-
Step step = factory.getObject();
379-
380-
step.execute(stepExecution);
381-
382-
assertEquals(0, stepExecution.getSkipCount());
383-
assertEquals(0, stepExecution.getReadSkipCount());
384-
assertEquals(5, stepExecution.getReadCount());
385-
// Write count is incremented even if nothing happens
386-
assertEquals(5, stepExecution.getWriteCount());
387-
assertEquals(0, stepExecution.getFilterCount());
388-
assertEquals(0, stepExecution.getRollbackCount());
389-
390-
// writer skips "4"
391-
assertTrue(reader.getRead().contains("4"));
392-
393-
assertEquals(BatchStatus.COMPLETED, stepExecution.getStatus());
394-
assertStepExecutionsAreEqual(stepExecution, repository.getLastStepExecution(jobExecution.getJobInstance(), step
395-
.getName()));
396-
}
397-
398374
/**
399375
* Check items causing errors are skipped as expected.
400376
*/

spring-batch-core/src/test/java/org/springframework/batch/core/step/item/SimpleStepFactoryBeanTests.java

+2-27
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2006-2013 the original author or authors.
2+
* Copyright 2006-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -27,6 +27,7 @@
2727
import java.util.List;
2828

2929
import org.junit.Before;
30+
import org.junit.Ignore;
3031
import org.junit.Test;
3132
import org.springframework.batch.core.BatchStatus;
3233
import org.springframework.batch.core.ChunkListener;
@@ -475,32 +476,6 @@ public void onWriteError(Exception exception, List<? extends String> items) {
475476

476477
}
477478

478-
@Test
479-
public void testNullWriter() throws Exception {
480-
481-
SimpleStepFactoryBean<String, String> factory = getStepFactory(new String[] { "foo", "bar", "spam" });
482-
factory.setItemWriter(null);
483-
factory.setItemProcessor(new ItemProcessor<String, String>() {
484-
@Override
485-
public String process(String item) throws Exception {
486-
written.add(item);
487-
return null;
488-
}
489-
});
490-
491-
Step step = factory.getObject();
492-
493-
job.setSteps(Collections.singletonList(step));
494-
495-
JobExecution jobExecution = repository.createJobExecution(job.getName(), new JobParameters());
496-
497-
job.execute(jobExecution);
498-
499-
assertEquals(BatchStatus.COMPLETED, jobExecution.getStatus());
500-
assertEquals("[foo, bar, spam]", written.toString());
501-
502-
}
503-
504479
private SimpleStepFactoryBean<String, String> getStepFactory(String... args) throws Exception {
505480
SimpleStepFactoryBean<String, String> factory = new SimpleStepFactoryBean<String, String>();
506481

0 commit comments

Comments
 (0)