Skip to content

Commit 6c619b9

Browse files
cppwfsfmbenhassine
authored andcommitted
FlatFileItemWriter now uses charset to determine default encoding
It now matches the encoding scheme of FlatFileItemReader Resolves #1154
1 parent c9d827c commit 6c619b9

File tree

3 files changed

+78
-5
lines changed

3 files changed

+78
-5
lines changed

spring-batch-infrastructure/src/main/java/org/springframework/batch/item/support/AbstractFileItemWriter.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import java.io.Writer;
2424
import java.nio.channels.Channels;
2525
import java.nio.channels.FileChannel;
26+
import java.nio.charset.Charset;
2627
import java.nio.charset.UnsupportedCharsetException;
2728
import java.util.List;
2829

@@ -59,6 +60,7 @@
5960
* @author Dave Syer
6061
* @author Michael Minella
6162
* @author Mahmoud Ben Hassine
63+
* @author Glenn Renfro
6264
* @author Remi Kaeffer
6365
*
6466
* @since 4.1
@@ -72,8 +74,8 @@ public abstract class AbstractFileItemWriter<T> extends AbstractItemStreamItemWr
7274

7375
public static final String DEFAULT_LINE_SEPARATOR = System.getProperty("line.separator");
7476

75-
// default encoding for writing to output files - set to UTF-8.
76-
public static final String DEFAULT_CHARSET = "UTF-8";
77+
// default encoding for writing to flat files - set to charset of this Java virtual machine.
78+
public static final String DEFAULT_CHARSET = Charset.defaultCharset().name();
7779

7880
private static final String WRITTEN_STATISTICS_NAME = "written";
7981

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

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2019 the original author or authors.
2+
* Copyright 2016-2021 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.
@@ -19,11 +19,13 @@
1919
import java.io.File;
2020
import java.io.IOException;
2121
import java.io.InputStreamReader;
22+
import java.nio.charset.Charset;
2223
import java.util.Arrays;
2324

2425
import org.junit.Test;
2526

2627
import org.springframework.batch.item.ExecutionContext;
28+
2729
import org.springframework.batch.item.file.FlatFileItemWriter;
2830
import org.springframework.batch.item.file.transform.PassThroughLineAggregator;
2931
import org.springframework.core.io.FileSystemResource;
@@ -38,6 +40,7 @@
3840
* @author Michael Minella
3941
* @author Mahmoud Ben Hassine
4042
* @author Drummond Dawson
43+
* @author Glenn Renfro
4144
*/
4245
public class FlatFileItemWriterBuilderTests {
4346

@@ -264,6 +267,8 @@ public void testFlags() throws Exception {
264267

265268
Resource output = new FileSystemResource(File.createTempFile("foo", "txt"));
266269

270+
String encoding = Charset.defaultCharset().name();
271+
267272
FlatFileItemWriter<Foo> writer = new FlatFileItemWriterBuilder<Foo>()
268273
.name("foo")
269274
.resource(output)
@@ -276,14 +281,40 @@ public void testFlags() throws Exception {
276281
.lineAggregator(new PassThroughLineAggregator<>())
277282
.build();
278283

284+
validateBuilderFlags(writer, encoding);
285+
}
286+
287+
@Test
288+
public void testFlagsWithEncoding() throws Exception {
289+
290+
Resource output = new FileSystemResource(File.createTempFile("foo", "txt"));
291+
String encoding = "UTF-8";
292+
FlatFileItemWriter<Foo> writer = new FlatFileItemWriterBuilder<Foo>()
293+
.name("foo")
294+
.encoding(encoding)
295+
.resource(output)
296+
.shouldDeleteIfEmpty(true)
297+
.shouldDeleteIfExists(false)
298+
.saveState(false)
299+
.forceSync(true)
300+
.append(true)
301+
.transactional(false)
302+
.lineAggregator(new PassThroughLineAggregator<>())
303+
.build();
304+
validateBuilderFlags(writer, encoding);
305+
}
306+
307+
private void validateBuilderFlags(FlatFileItemWriter<Foo> writer, String encoding) {
279308
assertFalse((Boolean) ReflectionTestUtils.getField(writer, "saveState"));
280309
assertTrue((Boolean) ReflectionTestUtils.getField(writer, "append"));
281310
assertFalse((Boolean) ReflectionTestUtils.getField(writer, "transactional"));
282311
assertTrue((Boolean) ReflectionTestUtils.getField(writer, "shouldDeleteIfEmpty"));
283312
assertFalse((Boolean) ReflectionTestUtils.getField(writer, "shouldDeleteIfExists"));
284313
assertTrue((Boolean) ReflectionTestUtils.getField(writer, "forceSync"));
314+
assertEquals( encoding, ReflectionTestUtils.getField(writer, "encoding"));
285315
}
286316

317+
287318
private String readLine(String encoding, Resource outputFile ) throws IOException {
288319

289320
if (reader == null) {

spring-batch-infrastructure/src/test/java/org/springframework/batch/item/json/builder/JsonFileItemWriterBuilderTest.java

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2018 the original author or authors.
2+
* Copyright 2018-2021 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.
@@ -17,6 +17,7 @@
1717
package org.springframework.batch.item.json.builder;
1818

1919
import java.io.File;
20+
import java.nio.charset.Charset;
2021
import java.nio.file.Files;
2122

2223
import org.junit.Before;
@@ -36,6 +37,7 @@
3637

3738
/**
3839
* @author Mahmoud Ben Hassine
40+
* @author Glenn Renfro
3941
*/
4042
public class JsonFileItemWriterBuilderTest {
4143

@@ -100,7 +102,45 @@ public void testJsonFileItemWriterCreation() {
100102
.transactional(transactional)
101103
.build();
102104

103-
// then
105+
//then
106+
validateBuilderFlags(writer, encoding, lineSeparator, headerCallback, footerCallback);
107+
}
108+
109+
@Test
110+
public void testJsonFileItemWriterCreationDefaultEncoding() {
111+
// given
112+
boolean append = true;
113+
boolean forceSync = true;
114+
boolean transactional = true;
115+
boolean shouldDeleteIfEmpty = true;
116+
boolean shouldDeleteIfExists = true;
117+
String encoding = Charset.defaultCharset().name();
118+
String lineSeparator = "#";
119+
FlatFileHeaderCallback headerCallback = Mockito.mock(FlatFileHeaderCallback.class);
120+
FlatFileFooterCallback footerCallback = Mockito.mock(FlatFileFooterCallback.class);
121+
122+
// when
123+
JsonFileItemWriter<String> writer = new JsonFileItemWriterBuilder<String>()
124+
.name("jsonFileItemWriter")
125+
.resource(this.resource)
126+
.jsonObjectMarshaller(this.jsonObjectMarshaller)
127+
.append(append)
128+
.forceSync(forceSync)
129+
.headerCallback(headerCallback)
130+
.footerCallback(footerCallback)
131+
.lineSeparator(lineSeparator)
132+
.shouldDeleteIfEmpty(shouldDeleteIfEmpty)
133+
.shouldDeleteIfExists(shouldDeleteIfExists)
134+
.transactional(transactional)
135+
.build();
136+
137+
//then
138+
validateBuilderFlags(writer, encoding, lineSeparator, headerCallback, footerCallback);
139+
}
140+
141+
private void validateBuilderFlags(JsonFileItemWriter<String> writer, String encoding,
142+
String lineSeparator, FlatFileHeaderCallback headerCallback,
143+
FlatFileFooterCallback footerCallback) {
104144
assertTrue((Boolean) ReflectionTestUtils.getField(writer, "saveState"));
105145
assertTrue((Boolean) ReflectionTestUtils.getField(writer, "append"));
106146
assertTrue((Boolean) ReflectionTestUtils.getField(writer, "transactional"));

0 commit comments

Comments
 (0)