Skip to content

Commit 6880685

Browse files
cppwfsfmbenhassine
authored andcommitted
Add setter for strict field in DelimitedBuilder
Resolves #809
1 parent a73fbc3 commit 6880685

File tree

2 files changed

+53
-1
lines changed

2 files changed

+53
-1
lines changed

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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -533,7 +533,7 @@ public static class DelimitedBuilder<T> {
533533

534534
private FieldSetFactory fieldSetFactory = new DefaultFieldSetFactory();
535535

536-
private final boolean strict = true;
536+
private boolean strict = true;
537537

538538
protected DelimitedBuilder(FlatFileItemReaderBuilder<T> parent) {
539539
this.parent = parent;
@@ -609,6 +609,20 @@ public FlatFileItemReaderBuilder<T> names(String... names) {
609609
return this.parent;
610610
}
611611

612+
/**
613+
* If true (the default) then the number of tokens in line must match the number
614+
* of tokens defined (by {@link Range}, columns, etc.) in {@link LineTokenizer}.
615+
* If false then lines with less tokens will be tolerated and padded with empty
616+
* columns, and lines with more tokens will simply be truncated.
617+
*
618+
* @since 5.1
619+
* @param strict the strict flag to set
620+
*/
621+
public DelimitedBuilder<T> strict(boolean strict) {
622+
this.strict = strict;
623+
return this;
624+
}
625+
612626
/**
613627
* Returns a {@link DelimitedLineTokenizer}
614628
* @return {@link DelimitedLineTokenizer}

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

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
* @author Michael Minella
5555
* @author Mahmoud Ben Hassine
5656
* @author Drummond Dawson
57+
* @author Glenn Renfro
5758
*/
5859
class FlatFileItemReaderBuilderTests {
5960

@@ -215,6 +216,43 @@ void testStrict() throws Exception {
215216
assertNull(reader.read());
216217
}
217218

219+
@Test
220+
public void testDelimitedRelaxed() throws Exception {
221+
FlatFileItemReader<Foo> reader = new FlatFileItemReaderBuilder<Foo>().name("fooReader")
222+
.resource(getResource("1 2 3"))
223+
.delimited()
224+
.delimiter(" ")
225+
.strict(false)
226+
.names("first", "second")
227+
.targetType(Foo.class)
228+
.build();
229+
230+
reader.open(new ExecutionContext());
231+
Foo item = reader.read();
232+
assertEquals(1, item.getFirst());
233+
assertEquals(2, item.getSecond());
234+
assertNull(item.getThird());
235+
}
236+
237+
@Test
238+
public void testDelimitedStrict() {
239+
FlatFileItemReader<Foo> reader = new FlatFileItemReaderBuilder<Foo>().name("fooReader")
240+
.resource(getResource("1 2 3"))
241+
.delimited()
242+
.delimiter(" ")
243+
.strict(true)
244+
.names("first", "second")
245+
.targetType(Foo.class)
246+
.build();
247+
248+
reader.open(new ExecutionContext());
249+
250+
Exception exception = assertThrows(RuntimeException.class, reader::read);
251+
String expectedMessage = "Parsing error at line: 1 in resource=[Byte array resource [resource loaded from byte array]], input=[1 2 3]";
252+
String actualMessage = exception.getMessage();
253+
assertTrue(actualMessage.contains(expectedMessage));
254+
}
255+
218256
@Test
219257
void testCustomLineTokenizerFieldSetMapper() throws Exception {
220258
FlatFileItemReader<Foo> reader = new FlatFileItemReaderBuilder<Foo>().name("fooReader")

0 commit comments

Comments
 (0)