Support empty comments in FlatFileItemReaderBuilder

Previously, if supplying empty comments or no comments to comments() of the FlatFileItemReaderBuilder, the comments would be ignored and instead use the default comments "#" provided by the FlatFileItemReader.

Resolves BATCH-2837
This commit is contained in:
Drummond Dawson
2019-10-03 20:20:25 -04:00
committed by Mahmoud Ben Hassine
parent 6746cbd311
commit 7b2c684274
2 changed files with 46 additions and 4 deletions

View File

@@ -492,10 +492,7 @@ public class FlatFileItemReaderBuilder<T> {
}
reader.setLinesToSkip(this.linesToSkip);
if(!this.comments.isEmpty()) {
reader.setComments(this.comments.toArray(new String[this.comments.size()]));
}
reader.setComments(this.comments.toArray(new String[this.comments.size()]));
reader.setSkippedLinesCallback(this.skippedLinesCallback);
reader.setRecordSeparatorPolicy(this.recordSeparatorPolicy);

View File

@@ -268,6 +268,51 @@ public class FlatFileItemReaderBuilderTests {
assertNull(reader.read());
}
@Test
public void testEmptyComments() throws Exception {
FlatFileItemReader<Foo> reader = new FlatFileItemReaderBuilder<Foo>()
.name("fooReader")
.resource(getResource("1,2,3\n4,5,6"))
.comments(new String[]{})
.delimited()
.names(new String[] {"first", "second", "third"})
.targetType(Foo.class)
.build();
reader.open(new ExecutionContext());
Foo item = reader.read();
assertEquals(1, item.getFirst());
assertEquals(2, item.getSecond());
assertEquals("3", item.getThird());
item = reader.read();
assertEquals(4, item.getFirst());
assertEquals(5, item.getSecond());
assertEquals("6", item.getThird());
assertNull(reader.read());
}
@Test
public void testDefaultComments() throws Exception {
FlatFileItemReader<Foo> reader = new FlatFileItemReaderBuilder<Foo>()
.name("fooReader")
.resource(getResource("1,2,3\n4,5,6"))
.delimited()
.names(new String[] {"first", "second", "third"})
.targetType(Foo.class)
.build();
reader.open(new ExecutionContext());
Foo item = reader.read();
assertEquals(1, item.getFirst());
assertEquals(2, item.getSecond());
assertEquals("3", item.getThird());
item = reader.read();
assertEquals(4, item.getFirst());
assertEquals(5, item.getSecond());
assertEquals("6", item.getThird());
assertNull(reader.read());
}
@Test
public void testPrototypeBean() throws Exception {
BeanFactory factory = new AnnotationConfigApplicationContext(Beans.class);