Support empty delimiter in DelimitedBuilder of FlatFileItemWriterBuilder

Previously, if supplying an empty delimiter, to the delimited().delimiter() of DelimitedBuilder from the parent FlatFileItemWriterBuilder, it would be ignored and instead use the default delimiter ",".

Resolves BATCH-2844
This commit is contained in:
Drummond Dawson
2019-10-02 22:23:34 -04:00
committed by Mahmoud Ben Hassine
parent ba4aa8cae9
commit 3f17b90438
2 changed files with 30 additions and 5 deletions

View File

@@ -33,7 +33,6 @@ import org.springframework.batch.item.file.transform.FormatterLineAggregator;
import org.springframework.batch.item.file.transform.LineAggregator;
import org.springframework.core.io.Resource;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
/**
* A builder implementation for the {@link FlatFileItemWriter}
@@ -463,9 +462,7 @@ public class FlatFileItemWriterBuilder<T> {
"A list of field names or a field extractor is required");
DelimitedLineAggregator<T> delimitedLineAggregator = new DelimitedLineAggregator<>();
if (StringUtils.hasLength(this.delimiter)) {
delimitedLineAggregator.setDelimiter(this.delimiter);
}
delimitedLineAggregator.setDelimiter(this.delimiter);
if (this.fieldExtractor == null) {
BeanWrapperFieldExtractor<T> beanWrapperFieldExtractor = new BeanWrapperFieldExtractor<>();

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2016-2018 the original author or authors.
* Copyright 2016-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -118,6 +118,34 @@ public class FlatFileItemWriterBuilderTests {
assertEquals("HEADER$1,2,3$4,5,6$FOOTER", readLine("UTF-16LE", output));
}
@Test
public void testDelimitedOutputWithEmptyDelimiter() throws Exception {
Resource output = new FileSystemResource(File.createTempFile("foo", "txt"));
FlatFileItemWriter<Foo> writer = new FlatFileItemWriterBuilder<Foo>()
.name("foo")
.resource(output)
.lineSeparator("$")
.delimited()
.delimiter("")
.names("first", "second", "third")
.encoding("UTF-16LE")
.headerCallback(writer1 -> writer1.append("HEADER"))
.footerCallback(writer12 -> writer12.append("FOOTER"))
.build();
ExecutionContext executionContext = new ExecutionContext();
writer.open(executionContext);
writer.write(Arrays.asList(new Foo(1, 2, "3"), new Foo(4, 5, "6")));
writer.close();
assertEquals("HEADER$123$456$FOOTER", readLine("UTF-16LE", output));
}
@Test
public void testDelimitedOutputWithDefaultFieldExtractor() throws Exception {