From 0898276346e84de9fa3335742847bb4fc4feafc4 Mon Sep 17 00:00:00 2001 From: Mahmoud Ben Hassine Date: Tue, 21 May 2024 16:14:16 +0200 Subject: [PATCH] Make line separator configurable in RecursiveCollectionLineAggregator Resolves #4594 --- .../RecursiveCollectionLineAggregator.java | 23 +++++++++++++++---- ...cursiveCollectionLineAggregatorTests.java} | 20 +++++++++++----- 2 files changed, 32 insertions(+), 11 deletions(-) rename spring-batch-infrastructure/src/test/java/org/springframework/batch/item/file/transform/{RecursiveCollectionItemTransformerTests.java => RecursiveCollectionLineAggregatorTests.java} (72%) diff --git a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/file/transform/RecursiveCollectionLineAggregator.java b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/file/transform/RecursiveCollectionLineAggregator.java index b5c7fa8ef..ddb447047 100644 --- a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/file/transform/RecursiveCollectionLineAggregator.java +++ b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/file/transform/RecursiveCollectionLineAggregator.java @@ -1,5 +1,5 @@ /* - * Copyright 2006-2023 the original author or authors. + * Copyright 2006-2024 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. @@ -18,16 +18,19 @@ package org.springframework.batch.item.file.transform; import java.util.Collection; +import org.springframework.util.Assert; + /** * An implementation of {@link LineAggregator} that concatenates a collection of items of - * a common type with the system line separator. + * a common type with a line separator. * * @author Dave Syer + * @author Mahmoud Ben Hassine * */ public class RecursiveCollectionLineAggregator implements LineAggregator> { - private static final String LINE_SEPARATOR = System.getProperty("line.separator"); + private String lineSeparator = System.lineSeparator(); private LineAggregator delegate = new PassThroughLineAggregator<>(); @@ -41,13 +44,23 @@ public class RecursiveCollectionLineAggregator implements LineAggregator items) { StringBuilder builder = new StringBuilder(); for (T value : items) { - builder.append(delegate.aggregate(value)).append(LINE_SEPARATOR); + builder.append(delegate.aggregate(value)).append(lineSeparator); } - return builder.delete(builder.length() - LINE_SEPARATOR.length(), builder.length()).toString(); + return builder.delete(builder.length() - lineSeparator.length(), builder.length()).toString(); } } diff --git a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/file/transform/RecursiveCollectionItemTransformerTests.java b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/file/transform/RecursiveCollectionLineAggregatorTests.java similarity index 72% rename from spring-batch-infrastructure/src/test/java/org/springframework/batch/item/file/transform/RecursiveCollectionItemTransformerTests.java rename to spring-batch-infrastructure/src/test/java/org/springframework/batch/item/file/transform/RecursiveCollectionLineAggregatorTests.java index 4e0048ce3..eef13e3db 100644 --- a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/file/transform/RecursiveCollectionItemTransformerTests.java +++ b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/file/transform/RecursiveCollectionLineAggregatorTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2006-2023 the original author or authors. + * Copyright 2006-2024 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,6 +19,7 @@ import java.util.Arrays; import java.util.Collections; import org.junit.jupiter.api.Test; + import org.springframework.util.StringUtils; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -28,9 +29,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals; * @author Mahmoud Ben Hassine * */ -class RecursiveCollectionItemTransformerTests { - - private static final String LINE_SEPARATOR = System.getProperty("line.separator"); +class RecursiveCollectionLineAggregatorTests { private final RecursiveCollectionLineAggregator aggregator = new RecursiveCollectionLineAggregator<>(); @@ -41,9 +40,18 @@ class RecursiveCollectionItemTransformerTests { } @Test - void testTransformList() { + void testAggregateListWithDefaultLineSeparator() { String result = aggregator.aggregate(Arrays.asList(StringUtils.commaDelimitedListToStringArray("foo,bar"))); - String[] array = StringUtils.delimitedListToStringArray(result, LINE_SEPARATOR); + String[] array = StringUtils.delimitedListToStringArray(result, System.lineSeparator()); + assertEquals("foo", array[0]); + assertEquals("bar", array[1]); + } + + @Test + void testAggregateListWithCustomLineSeparator() { + aggregator.setLineSeparator("#"); + String result = aggregator.aggregate(Arrays.asList(StringUtils.commaDelimitedListToStringArray("foo,bar"))); + String[] array = StringUtils.delimitedListToStringArray(result, "#"); assertEquals("foo", array[0]); assertEquals("bar", array[1]); }