Skip to content

Commit f3ae970

Browse files
committed
Updated based on code review.
1 parent 4c67da0 commit f3ae970

File tree

4 files changed

+79
-13
lines changed

4 files changed

+79
-13
lines changed

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

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2006-2021 the original author or authors.
2+
* Copyright 2006-2018 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.
@@ -16,7 +16,6 @@
1616

1717
package org.springframework.batch.item.file;
1818

19-
import java.nio.charset.Charset;
2019
import java.util.List;
2120

2221
import org.springframework.batch.item.file.transform.LineAggregator;
@@ -40,15 +39,11 @@
4039
* @author Dave Syer
4140
* @author Michael Minella
4241
* @author Mahmoud Ben Hassine
43-
* @author Glenn Renfro
4442
*/
4543
public class FlatFileItemWriter<T> extends AbstractFileItemWriter<T> {
4644

4745
protected LineAggregator<T> lineAggregator;
4846

49-
// default encoding for writing to flat files - set to charset of this Java virtual machine.
50-
public static final String DEFAULT_CHARSET = Charset.defaultCharset().name();
51-
5247
public FlatFileItemWriter() {
5348
this.setExecutionContextName(ClassUtils.getShortName(FlatFileItemWriter.class));
5449
}

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2006-2018 the original author or authors.
2+
* Copyright 2006-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.
@@ -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
*
6365
* @since 4.1
6466
*/
@@ -71,8 +73,8 @@ public abstract class AbstractFileItemWriter<T> extends AbstractItemStreamItemWr
7173

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

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

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

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

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import org.junit.Test;
2626

2727
import org.springframework.batch.item.ExecutionContext;
28+
2829
import org.springframework.batch.item.file.FlatFileItemWriter;
2930
import org.springframework.batch.item.file.transform.PassThroughLineAggregator;
3031
import org.springframework.core.io.FileSystemResource;
@@ -266,8 +267,31 @@ public void testFlags() throws Exception {
266267

267268
Resource output = new FileSystemResource(File.createTempFile("foo", "txt"));
268269

270+
String encoding = Charset.defaultCharset().name();
271+
272+
FlatFileItemWriter<Foo> writer = new FlatFileItemWriterBuilder<Foo>()
273+
.name("foo")
274+
.resource(output)
275+
.shouldDeleteIfEmpty(true)
276+
.shouldDeleteIfExists(false)
277+
.saveState(false)
278+
.forceSync(true)
279+
.append(true)
280+
.transactional(false)
281+
.lineAggregator(new PassThroughLineAggregator<>())
282+
.build();
283+
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";
269292
FlatFileItemWriter<Foo> writer = new FlatFileItemWriterBuilder<Foo>()
270293
.name("foo")
294+
.encoding("UTF-8")
271295
.resource(output)
272296
.shouldDeleteIfEmpty(true)
273297
.shouldDeleteIfExists(false)
@@ -277,16 +301,21 @@ public void testFlags() throws Exception {
277301
.transactional(false)
278302
.lineAggregator(new PassThroughLineAggregator<>())
279303
.build();
304+
validateBuilderFlags(writer, encoding);
305+
}
306+
307+
private void validateBuilderFlags(FlatFileItemWriter<Foo> writer, String encoding) {
280308
assertFalse((Boolean) ReflectionTestUtils.getField(writer, "saveState"));
281309
assertTrue((Boolean) ReflectionTestUtils.getField(writer, "append"));
282310
assertFalse((Boolean) ReflectionTestUtils.getField(writer, "transactional"));
283311
assertTrue((Boolean) ReflectionTestUtils.getField(writer, "shouldDeleteIfEmpty"));
284312
assertFalse((Boolean) ReflectionTestUtils.getField(writer, "shouldDeleteIfExists"));
285313
assertTrue((Boolean) ReflectionTestUtils.getField(writer, "forceSync"));
286-
assertEquals( Charset.defaultCharset().name(), ReflectionTestUtils.getField(writer, "encoding"));
314+
assertEquals( encoding, ReflectionTestUtils.getField(writer, "encoding"));
287315
}
288316

289-
private String readLine(String encoding, Resource outputFile ) throws IOException {
317+
318+
private String readLine(String encoding, Resource outputFile ) throws IOException {
290319

291320
if (reader == null) {
292321
reader = new BufferedReader(new InputStreamReader(outputFile.getInputStream(), encoding));

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)