diff --git a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/support/AbstractFileItemWriter.java b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/support/AbstractFileItemWriter.java index 3ca3ba9f9e..8aae128322 100644 --- a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/support/AbstractFileItemWriter.java +++ b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/support/AbstractFileItemWriter.java @@ -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. @@ -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; @@ -59,6 +60,7 @@ * @author Dave Syer * @author Michael Minella * @author Mahmoud Ben Hassine + * @author Glenn Renfro * * @since 4.1 */ @@ -71,8 +73,8 @@ public abstract class AbstractFileItemWriter 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"; diff --git a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/file/builder/FlatFileItemWriterBuilderTests.java b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/file/builder/FlatFileItemWriterBuilderTests.java index bda8417c50..b6cb2afa57 100644 --- a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/file/builder/FlatFileItemWriterBuilderTests.java +++ b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/file/builder/FlatFileItemWriterBuilderTests.java @@ -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.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 @@ * @author Michael Minella * @author Mahmoud Ben Hassine * @author Drummond Dawson + * @author Glenn Renfro */ public class FlatFileItemWriterBuilderTests { @@ -264,6 +267,8 @@ public void testFlags() throws Exception { Resource output = new FileSystemResource(File.createTempFile("foo", "txt")); + String encoding = Charset.defaultCharset().name(); + FlatFileItemWriter writer = new FlatFileItemWriterBuilder() .name("foo") .resource(output) @@ -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 writer = new FlatFileItemWriterBuilder() + .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 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) { diff --git a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/json/builder/JsonFileItemWriterBuilderTest.java b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/json/builder/JsonFileItemWriterBuilderTest.java index 7e57a25b65..5ddb61d4e6 100644 --- a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/json/builder/JsonFileItemWriterBuilderTest.java +++ b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/json/builder/JsonFileItemWriterBuilderTest.java @@ -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 @@ /** * @author Mahmoud Ben Hassine + * @author Glenn Renfro */ public class JsonFileItemWriterBuilderTest { @@ -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 writer = new JsonFileItemWriterBuilder() + .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 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"));