Skip to content

Commit c329b9f

Browse files
patbaumgartnermartinfrancois
authored andcommitted
Throwing error when TargetType and FieldSetMapper is provided
Co-authored-by: martinfrancois <[email protected]> Signed-off-by: Patrick Baumgartner <[email protected]> Signed-off-by: martinfrancois <[email protected]>
1 parent 673cfe3 commit c329b9f

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

spring-batch-infrastructure/src/main/java/org/springframework/batch/item/file/builder/FlatFileItemReaderBuilder.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@
5858
* @author Glenn Renfro
5959
* @author Mahmoud Ben Hassine
6060
* @author Drummond Dawson
61+
* @author Patrick Baumgartner
62+
* @author François Martin
6163
* @since 4.0
6264
* @see FlatFileItemReader
6365
*/
@@ -459,6 +461,9 @@ else if (this.delimitedBuilder != null) {
459461
throw new IllegalStateException("No LineTokenizer implementation was provided.");
460462
}
461463

464+
Assert.state(this.targetType == null || this.fieldSetMapper == null,
465+
"Either a TargetType or FieldSetMapper can be set, can't be both.");
466+
462467
if (this.targetType != null || StringUtils.hasText(this.prototypeBeanName)) {
463468
if (this.targetType != null && this.targetType.isRecord()) {
464469
RecordFieldSetMapper<T> mapper = new RecordFieldSetMapper<>(this.targetType);

spring-batch-infrastructure/src/test/java/org/springframework/batch/item/file/builder/FlatFileItemReaderBuilderTests.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@
5656
* @author Mahmoud Ben Hassine
5757
* @author Drummond Dawson
5858
* @author Glenn Renfro
59+
* @author Patrick Baumgartner
60+
* @author François Martin
5961
*/
6062
class FlatFileItemReaderBuilderTests {
6163

@@ -563,6 +565,17 @@ void testErrorMessageWhenNoLineTokenizerWasProvided() {
563565
}
564566
}
565567

568+
@Test
569+
void testErrorWhenTargetTypeAndFieldSetMapperIsProvided() {
570+
var builder = new FlatFileItemReaderBuilder<Foo>().name("fooReader")
571+
.resource(getResource("1;2;3"))
572+
.lineTokenizer(line -> new DefaultFieldSet(line.split(";")))
573+
.targetType(Foo.class)
574+
.fieldSetMapper(fieldSet -> new Foo());
575+
var exception = assertThrows(IllegalStateException.class, builder::build);
576+
assertEquals("Either a TargetType or FieldSetMapper can be set, can't be both.", exception.getMessage());
577+
}
578+
566579
@Test
567580
void testSetupWithRecordTargetType() {
568581
// given

0 commit comments

Comments
 (0)