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:
committed by
Mahmoud Ben Hassine
parent
dc3fee597d
commit
bfbaab5872
@@ -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}
|
||||
@@ -464,9 +463,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<>();
|
||||
|
||||
@@ -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.
|
||||
@@ -119,6 +119,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 {
|
||||
|
||||
|
||||
Reference in New Issue
Block a user