Skip to content

Commit

Permalink
Add setter for strict field in DelimitedBuilder
Browse files Browse the repository at this point in the history
Resolves #809
  • Loading branch information
cppwfs authored and fmbenhassine committed Sep 14, 2023
1 parent a73fbc3 commit 6880685
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -533,7 +533,7 @@ public static class DelimitedBuilder<T> {

private FieldSetFactory fieldSetFactory = new DefaultFieldSetFactory();

private final boolean strict = true;
private boolean strict = true;

protected DelimitedBuilder(FlatFileItemReaderBuilder<T> parent) {
this.parent = parent;
Expand Down Expand Up @@ -609,6 +609,20 @@ public FlatFileItemReaderBuilder<T> names(String... names) {
return this.parent;
}

/**
* If true (the default) then the number of tokens in line must match the number
* of tokens defined (by {@link Range}, columns, etc.) in {@link LineTokenizer}.
* If false then lines with less tokens will be tolerated and padded with empty
* columns, and lines with more tokens will simply be truncated.
*
* @since 5.1
* @param strict the strict flag to set
*/
public DelimitedBuilder<T> strict(boolean strict) {
this.strict = strict;
return this;
}

/**
* Returns a {@link DelimitedLineTokenizer}
* @return {@link DelimitedLineTokenizer}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
* @author Michael Minella
* @author Mahmoud Ben Hassine
* @author Drummond Dawson
* @author Glenn Renfro
*/
class FlatFileItemReaderBuilderTests {

Expand Down Expand Up @@ -215,6 +216,43 @@ void testStrict() throws Exception {
assertNull(reader.read());
}

@Test
public void testDelimitedRelaxed() throws Exception {
FlatFileItemReader<Foo> reader = new FlatFileItemReaderBuilder<Foo>().name("fooReader")
.resource(getResource("1 2 3"))
.delimited()
.delimiter(" ")
.strict(false)
.names("first", "second")
.targetType(Foo.class)
.build();

reader.open(new ExecutionContext());
Foo item = reader.read();
assertEquals(1, item.getFirst());
assertEquals(2, item.getSecond());
assertNull(item.getThird());
}

@Test
public void testDelimitedStrict() {
FlatFileItemReader<Foo> reader = new FlatFileItemReaderBuilder<Foo>().name("fooReader")
.resource(getResource("1 2 3"))
.delimited()
.delimiter(" ")
.strict(true)
.names("first", "second")
.targetType(Foo.class)
.build();

reader.open(new ExecutionContext());

Exception exception = assertThrows(RuntimeException.class, reader::read);
String expectedMessage = "Parsing error at line: 1 in resource=[Byte array resource [resource loaded from byte array]], input=[1 2 3]";
String actualMessage = exception.getMessage();
assertTrue(actualMessage.contains(expectedMessage));
}

@Test
void testCustomLineTokenizerFieldSetMapper() throws Exception {
FlatFileItemReader<Foo> reader = new FlatFileItemReaderBuilder<Foo>().name("fooReader")
Expand Down

0 comments on commit 6880685

Please sign in to comment.