diff --git a/spring-batch-infrastructure/src/main/java/org/springframework/batch/infrastructure/item/file/transform/RecursiveCollectionLineAggregator.java b/spring-batch-infrastructure/src/main/java/org/springframework/batch/infrastructure/item/file/transform/RecursiveCollectionLineAggregator.java index 4451364789..7184bfda2c 100644 --- a/spring-batch-infrastructure/src/main/java/org/springframework/batch/infrastructure/item/file/transform/RecursiveCollectionLineAggregator.java +++ b/spring-batch-infrastructure/src/main/java/org/springframework/batch/infrastructure/item/file/transform/RecursiveCollectionLineAggregator.java @@ -56,6 +56,9 @@ public void setLineSeparator(String lineSeparator) { @Override public String aggregate(Collection items) { + if (items.isEmpty()) { + return ""; + } StringBuilder builder = new StringBuilder(); for (T value : items) { builder.append(delegate.aggregate(value)).append(lineSeparator); diff --git a/spring-batch-infrastructure/src/test/java/org/springframework/batch/infrastructure/item/file/transform/RecursiveCollectionLineAggregatorTests.java b/spring-batch-infrastructure/src/test/java/org/springframework/batch/infrastructure/item/file/transform/RecursiveCollectionLineAggregatorTests.java index a18e18fdf7..d656bcc1bb 100644 --- a/spring-batch-infrastructure/src/test/java/org/springframework/batch/infrastructure/item/file/transform/RecursiveCollectionLineAggregatorTests.java +++ b/spring-batch-infrastructure/src/test/java/org/springframework/batch/infrastructure/item/file/transform/RecursiveCollectionLineAggregatorTests.java @@ -20,7 +20,6 @@ import org.junit.jupiter.api.Test; -import org.springframework.batch.infrastructure.item.file.transform.RecursiveCollectionLineAggregator; import org.springframework.util.StringUtils; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -34,6 +33,12 @@ class RecursiveCollectionLineAggregatorTests { private final RecursiveCollectionLineAggregator aggregator = new RecursiveCollectionLineAggregator<>(); + @Test + void testSetDelegateAndPassEmptyCollection() { + aggregator.setDelegate(item -> "bar"); + assertEquals("", aggregator.aggregate(Collections.emptyList())); + } + @Test void testSetDelegateAndPassInString() { aggregator.setDelegate(item -> "bar");