Skip to content

Commit 62a2e65

Browse files
drumoniifmbenhassine
authored andcommitted
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
1 parent 44c0b8d commit 62a2e65

File tree

2 files changed

+30
-5
lines changed

2 files changed

+30
-5
lines changed

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
import org.springframework.batch.item.file.transform.LineAggregator;
3434
import org.springframework.core.io.Resource;
3535
import org.springframework.util.Assert;
36-
import org.springframework.util.StringUtils;
3736

3837
/**
3938
* A builder implementation for the {@link FlatFileItemWriter}
@@ -464,9 +463,7 @@ public DelimitedLineAggregator<T> build() {
464463
"A list of field names or a field extractor is required");
465464

466465
DelimitedLineAggregator<T> delimitedLineAggregator = new DelimitedLineAggregator<>();
467-
if (StringUtils.hasLength(this.delimiter)) {
468-
delimitedLineAggregator.setDelimiter(this.delimiter);
469-
}
466+
delimitedLineAggregator.setDelimiter(this.delimiter);
470467

471468
if (this.fieldExtractor == null) {
472469
BeanWrapperFieldExtractor<T> beanWrapperFieldExtractor = new BeanWrapperFieldExtractor<>();

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

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2018 the original author or authors.
2+
* Copyright 2016-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -119,6 +119,34 @@ public void testDelimitedOutputWithDefaultDelimiter() throws Exception {
119119
assertEquals("HEADER$1,2,3$4,5,6$FOOTER", readLine("UTF-16LE", output));
120120
}
121121

122+
@Test
123+
public void testDelimitedOutputWithEmptyDelimiter() throws Exception {
124+
125+
Resource output = new FileSystemResource(File.createTempFile("foo", "txt"));
126+
127+
FlatFileItemWriter<Foo> writer = new FlatFileItemWriterBuilder<Foo>()
128+
.name("foo")
129+
.resource(output)
130+
.lineSeparator("$")
131+
.delimited()
132+
.delimiter("")
133+
.names("first", "second", "third")
134+
.encoding("UTF-16LE")
135+
.headerCallback(writer1 -> writer1.append("HEADER"))
136+
.footerCallback(writer12 -> writer12.append("FOOTER"))
137+
.build();
138+
139+
ExecutionContext executionContext = new ExecutionContext();
140+
141+
writer.open(executionContext);
142+
143+
writer.write(Arrays.asList(new Foo(1, 2, "3"), new Foo(4, 5, "6")));
144+
145+
writer.close();
146+
147+
assertEquals("HEADER$123$456$FOOTER", readLine("UTF-16LE", output));
148+
}
149+
122150
@Test
123151
public void testDelimitedOutputWithDefaultFieldExtractor() throws Exception {
124152

0 commit comments

Comments
 (0)