From 0c39462989843aec0bb3c40d9d3389149936b405 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E1=84=87=E1=85=A1=E1=86=A8=E1=84=8C=E1=85=AE=E1=86=AB?= =?UTF-8?q?=E1=84=80=E1=85=B2?= Date: Thu, 30 Jul 2026 02:09:37 +0900 Subject: [PATCH] =?UTF-8?q?fix(batch):=20EgovPartitionFlatFileItemWriter?= =?UTF-8?q?=EC=9D=98=20Spring=20Batch=205=20=EA=B3=84=EC=95=BD=20=EB=B3=B5?= =?UTF-8?q?=EA=B5=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../file/EgovPartitionFlatFileItemWriter.java | 8 +- .../EgovPartitionFlatFileItemWriterTest.java | 73 +++++++++++++++++++ 2 files changed, 80 insertions(+), 1 deletion(-) create mode 100644 Batch/org.egovframe.rte.bat.core/src/test/java/org/egovframe/rte/bat/core/item/file/EgovPartitionFlatFileItemWriterTest.java diff --git a/Batch/org.egovframe.rte.bat.core/src/main/java/org/egovframe/rte/bat/core/item/file/EgovPartitionFlatFileItemWriter.java b/Batch/org.egovframe.rte.bat.core/src/main/java/org/egovframe/rte/bat/core/item/file/EgovPartitionFlatFileItemWriter.java index a217781f..2f91dd72 100755 --- a/Batch/org.egovframe.rte.bat.core/src/main/java/org/egovframe/rte/bat/core/item/file/EgovPartitionFlatFileItemWriter.java +++ b/Batch/org.egovframe.rte.bat.core/src/main/java/org/egovframe/rte/bat/core/item/file/EgovPartitionFlatFileItemWriter.java @@ -214,6 +214,7 @@ public void setTransactional(boolean transactional) { */ @Override public void setResource(WritableResource resource) { + setResource((Resource) resource); } /** @@ -224,7 +225,8 @@ public void setResource(WritableResource resource) { * @param chunk the chunk of items to be written; must not be null */ @Override - public void write(@NonNull Chunk chunk) { + public synchronized void write(@NonNull Chunk chunk) { + doWrite(chunk.getItems()); } /** @@ -235,6 +237,10 @@ public void write(@NonNull Chunk chunk) { * @param items output Stream 에 쓰여실 itmes 리스트 */ public synchronized void write(List items) throws Exception { + doWrite(items); + } + + private void doWrite(List items) { if (!getOutputState().isInitialized()) { throw new WriterNotOpenException("Writer must be open before it can be written to"); } diff --git a/Batch/org.egovframe.rte.bat.core/src/test/java/org/egovframe/rte/bat/core/item/file/EgovPartitionFlatFileItemWriterTest.java b/Batch/org.egovframe.rte.bat.core/src/test/java/org/egovframe/rte/bat/core/item/file/EgovPartitionFlatFileItemWriterTest.java new file mode 100644 index 00000000..91775787 --- /dev/null +++ b/Batch/org.egovframe.rte.bat.core/src/test/java/org/egovframe/rte/bat/core/item/file/EgovPartitionFlatFileItemWriterTest.java @@ -0,0 +1,73 @@ +/* + * Copyright 2008-2024 MOIS(Ministry of the Interior and Safety). + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.egovframe.rte.bat.core.item.file; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; +import org.springframework.batch.item.Chunk; +import org.springframework.batch.item.ExecutionContext; +import org.springframework.core.io.FileSystemResource; +import org.springframework.core.io.Resource; +import org.springframework.core.io.WritableResource; + +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.List; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + +class EgovPartitionFlatFileItemWriterTest { + + @TempDir + Path tempDir; + + @Test + void setsWritableResource() throws Exception { + Path output = tempDir.resolve("output.txt"); + WritableResource resource = new FileSystemResource(output); + EgovPartitionFlatFileItemWriter writer = createWriter(); + + writer.setResource(resource); + writer.open(new ExecutionContext()); + writer.close(); + + assertTrue(Files.exists(output)); + } + + @Test + void writesChunk() throws Exception { + Path output = tempDir.resolve("output.txt"); + Resource resource = new FileSystemResource(output); + EgovPartitionFlatFileItemWriter writer = createWriter(); + + writer.setResource(resource); + writer.open(new ExecutionContext()); + writer.write(new Chunk<>(List.of("first", "second"))); + writer.close(); + + assertEquals("first\nsecond\n", Files.readString(output)); + } + + private EgovPartitionFlatFileItemWriter createWriter() throws Exception { + EgovPartitionFlatFileItemWriter writer = new EgovPartitionFlatFileItemWriter<>(); + writer.setLineAggregator(item -> item); + writer.setLineSeparator("\n"); + writer.setTransactional(false); + writer.afterPropertiesSet(); + return writer; + } +}