Skip to content

Commit 1a6ecfd

Browse files
committed
Fix line tokenizer validation in FlatFileItemReaderBuilder
Resolves #766
1 parent 5874257 commit 1a6ecfd

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2019 the original author or authors.
2+
* Copyright 2016-2020 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.
@@ -474,7 +474,7 @@ public FlatFileItemReader<T> build() {
474474

475475
DefaultLineMapper<T> lineMapper = new DefaultLineMapper<>();
476476

477-
if(this.lineTokenizer != null && this.fieldSetMapper != null) {
477+
if(this.lineTokenizer != null) {
478478
lineMapper.setLineTokenizer(this.lineTokenizer);
479479
}
480480
else if(this.fixedLengthBuilder != null) {

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

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2019 the original author or authors.
2+
* Copyright 2016-2020 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.
@@ -524,6 +524,24 @@ public void testCustomEncoding() {
524524
assertEquals(encoding, ReflectionTestUtils.getField(reader, "encoding"));
525525
}
526526

527+
@Test
528+
public void testErrorMessageWhenNoFieldSetMapperIsProvided() {
529+
try {
530+
new FlatFileItemReaderBuilder<Foo>()
531+
.name("fooReader")
532+
.resource(getResource("1;2;3"))
533+
.lineTokenizer(line -> new DefaultFieldSet(line.split(";")))
534+
.build();
535+
} catch (IllegalStateException exception) {
536+
String exceptionMessage = exception.getMessage();
537+
if (exceptionMessage.equals("No LineTokenizer implementation was provided.")) {
538+
fail("Error message should not be 'No LineTokenizer implementation was provided.'" +
539+
" when a LineTokenizer is provided");
540+
}
541+
assertEquals("No FieldSetMapper implementation was provided.", exceptionMessage);
542+
}
543+
}
544+
527545
private Resource getResource(String contents) {
528546
return new ByteArrayResource(contents.getBytes());
529547
}

0 commit comments

Comments
 (0)