Skip to content

Commit 3f17b90

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 ba4aa8c commit 3f17b90

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}
@@ -463,9 +462,7 @@ public DelimitedLineAggregator<T> build() {
463462
"A list of field names or a field extractor is required");
464463

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

470467
if (this.fieldExtractor == null) {
471468
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.
@@ -118,6 +118,34 @@ public void testDelimitedOutputWithDefaultDelimiter() throws Exception {
118118
assertEquals("HEADER$1,2,3$4,5,6$FOOTER", readLine("UTF-16LE", output));
119119
}
120120

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

0 commit comments

Comments
 (0)