Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FlatFileItemWriter now uses charset to determine default encoding #3910

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -1,5 +1,5 @@
/*
* Copyright 2006-2018 the original author or authors.
* Copyright 2006-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.
Expand All @@ -23,6 +23,7 @@
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;

Expand Down Expand Up @@ -59,6 +60,7 @@
* @author Dave Syer
* @author Michael Minella
* @author Mahmoud Ben Hassine
* @author Glenn Renfro
*
* @since 4.1
*/
Expand All @@ -71,8 +73,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";

Expand Down
@@ -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.
Expand All @@ -19,11 +19,13 @@
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;
Expand All @@ -38,6 +40,7 @@
* @author Michael Minella
* @author Mahmoud Ben Hassine
* @author Drummond Dawson
* @author Glenn Renfro
*/
public class FlatFileItemWriterBuilderTests {

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

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

String encoding = Charset.defaultCharset().name();

FlatFileItemWriter<Foo> writer = new FlatFileItemWriterBuilder<Foo>()
.name("foo")
.resource(output)
Expand All @@ -276,14 +281,40 @@ public void testFlags() throws Exception {
.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) {
Expand Down
@@ -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.
Expand All @@ -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;
Expand All @@ -36,6 +37,7 @@

/**
* @author Mahmoud Ben Hassine
* @author Glenn Renfro
*/
public class JsonFileItemWriterBuilderTest {

Expand Down Expand Up @@ -100,7 +102,45 @@ public void testJsonFileItemWriterCreation() {
.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"));
Expand Down