FlatFileItemWriter now uses charset to determine default encoding

It now matches the encoding scheme of FlatFileItemReader

Resolves #1154
This commit is contained in:
Glenn Renfro
2021-05-17 16:50:56 -04:00
committed by Mahmoud Ben Hassine
parent c9d827ca11
commit 6c619b9f94
3 changed files with 78 additions and 5 deletions

View File

@@ -23,6 +23,7 @@ import java.io.IOException;
import java.io.Writer;
import java.nio.channels.Channels;
import java.nio.channels.FileChannel;
import java.nio.charset.Charset;
import java.nio.charset.UnsupportedCharsetException;
import java.util.List;
@@ -59,6 +60,7 @@ import org.springframework.util.Assert;
* @author Dave Syer
* @author Michael Minella
* @author Mahmoud Ben Hassine
* @author Glenn Renfro
* @author Remi Kaeffer
*
* @since 4.1
@@ -72,8 +74,8 @@ public abstract class AbstractFileItemWriter<T> extends AbstractItemStreamItemWr
public static final String DEFAULT_LINE_SEPARATOR = System.getProperty("line.separator");
// default encoding for writing to output files - set to UTF-8.
public static final String DEFAULT_CHARSET = "UTF-8";
// default encoding for writing to flat files - set to charset of this Java virtual machine.
public static final String DEFAULT_CHARSET = Charset.defaultCharset().name();
private static final String WRITTEN_STATISTICS_NAME = "written";

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2016-2019 the original author or authors.
* Copyright 2016-2021 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.
@@ -19,11 +19,13 @@ import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.util.Arrays;
import org.junit.Test;
import org.springframework.batch.item.ExecutionContext;
import org.springframework.batch.item.file.FlatFileItemWriter;
import org.springframework.batch.item.file.transform.PassThroughLineAggregator;
import org.springframework.core.io.FileSystemResource;
@@ -38,6 +40,7 @@ import static org.junit.Assert.assertTrue;
* @author Michael Minella
* @author Mahmoud Ben Hassine
* @author Drummond Dawson
* @author Glenn Renfro
*/
public class FlatFileItemWriterBuilderTests {
@@ -264,6 +267,8 @@ public class FlatFileItemWriterBuilderTests {
Resource output = new FileSystemResource(File.createTempFile("foo", "txt"));
String encoding = Charset.defaultCharset().name();
FlatFileItemWriter<Foo> writer = new FlatFileItemWriterBuilder<Foo>()
.name("foo")
.resource(output)
@@ -276,14 +281,40 @@ public class FlatFileItemWriterBuilderTests {
.lineAggregator(new PassThroughLineAggregator<>())
.build();
validateBuilderFlags(writer, encoding);
}
@Test
public void testFlagsWithEncoding() throws Exception {
Resource output = new FileSystemResource(File.createTempFile("foo", "txt"));
String encoding = "UTF-8";
FlatFileItemWriter<Foo> writer = new FlatFileItemWriterBuilder<Foo>()
.name("foo")
.encoding(encoding)
.resource(output)
.shouldDeleteIfEmpty(true)
.shouldDeleteIfExists(false)
.saveState(false)
.forceSync(true)
.append(true)
.transactional(false)
.lineAggregator(new PassThroughLineAggregator<>())
.build();
validateBuilderFlags(writer, encoding);
}
private void validateBuilderFlags(FlatFileItemWriter<Foo> writer, String encoding) {
assertFalse((Boolean) ReflectionTestUtils.getField(writer, "saveState"));
assertTrue((Boolean) ReflectionTestUtils.getField(writer, "append"));
assertFalse((Boolean) ReflectionTestUtils.getField(writer, "transactional"));
assertTrue((Boolean) ReflectionTestUtils.getField(writer, "shouldDeleteIfEmpty"));
assertFalse((Boolean) ReflectionTestUtils.getField(writer, "shouldDeleteIfExists"));
assertTrue((Boolean) ReflectionTestUtils.getField(writer, "forceSync"));
assertEquals( encoding, ReflectionTestUtils.getField(writer, "encoding"));
}
private String readLine(String encoding, Resource outputFile ) throws IOException {
if (reader == null) {

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2018 the original author or authors.
* Copyright 2018-2021 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.
@@ -17,6 +17,7 @@
package org.springframework.batch.item.json.builder;
import java.io.File;
import java.nio.charset.Charset;
import java.nio.file.Files;
import org.junit.Before;
@@ -36,6 +37,7 @@ import static org.junit.Assert.assertTrue;
/**
* @author Mahmoud Ben Hassine
* @author Glenn Renfro
*/
public class JsonFileItemWriterBuilderTest {
@@ -100,7 +102,45 @@ public class JsonFileItemWriterBuilderTest {
.transactional(transactional)
.build();
// then
//then
validateBuilderFlags(writer, encoding, lineSeparator, headerCallback, footerCallback);
}
@Test
public void testJsonFileItemWriterCreationDefaultEncoding() {
// given
boolean append = true;
boolean forceSync = true;
boolean transactional = true;
boolean shouldDeleteIfEmpty = true;
boolean shouldDeleteIfExists = true;
String encoding = Charset.defaultCharset().name();
String lineSeparator = "#";
FlatFileHeaderCallback headerCallback = Mockito.mock(FlatFileHeaderCallback.class);
FlatFileFooterCallback footerCallback = Mockito.mock(FlatFileFooterCallback.class);
// when
JsonFileItemWriter<String> writer = new JsonFileItemWriterBuilder<String>()
.name("jsonFileItemWriter")
.resource(this.resource)
.jsonObjectMarshaller(this.jsonObjectMarshaller)
.append(append)
.forceSync(forceSync)
.headerCallback(headerCallback)
.footerCallback(footerCallback)
.lineSeparator(lineSeparator)
.shouldDeleteIfEmpty(shouldDeleteIfEmpty)
.shouldDeleteIfExists(shouldDeleteIfExists)
.transactional(transactional)
.build();
//then
validateBuilderFlags(writer, encoding, lineSeparator, headerCallback, footerCallback);
}
private void validateBuilderFlags(JsonFileItemWriter<String> writer, String encoding,
String lineSeparator, FlatFileHeaderCallback headerCallback,
FlatFileFooterCallback footerCallback) {
assertTrue((Boolean) ReflectionTestUtils.getField(writer, "saveState"));
assertTrue((Boolean) ReflectionTestUtils.getField(writer, "append"));
assertTrue((Boolean) ReflectionTestUtils.getField(writer, "transactional"));