From 1da75703a46305dbdf45b29e6d886533769c2a3c Mon Sep 17 00:00:00 2001 From: Mykola Faryma Date: Tue, 27 Sep 2022 13:10:43 +0300 Subject: [PATCH] Check setDelimiter() for empty string Signed-off-by: Mykola Faryma --- src/main/java/org/apache/commons/csv/CSVFormat.java | 3 +++ src/test/java/org/apache/commons/csv/CSVFormatTest.java | 5 +++++ 2 files changed, 8 insertions(+) diff --git a/src/main/java/org/apache/commons/csv/CSVFormat.java b/src/main/java/org/apache/commons/csv/CSVFormat.java index ba39b742..5a47cb8e 100644 --- a/src/main/java/org/apache/commons/csv/CSVFormat.java +++ b/src/main/java/org/apache/commons/csv/CSVFormat.java @@ -346,6 +346,9 @@ public final class CSVFormat implements Serializable { if (containsLineBreak(delimiter)) { throw new IllegalArgumentException("The delimiter cannot be a line break"); } + if (delimiter.isEmpty()) { + throw new IllegalArgumentException("The delimiter cannot be empty"); + } this.delimiter = delimiter; return this; } diff --git a/src/test/java/org/apache/commons/csv/CSVFormatTest.java b/src/test/java/org/apache/commons/csv/CSVFormatTest.java index 0be97745..d25de480 100644 --- a/src/test/java/org/apache/commons/csv/CSVFormatTest.java +++ b/src/test/java/org/apache/commons/csv/CSVFormatTest.java @@ -1569,4 +1569,9 @@ public class CSVFormatTest { final CSVFormat formatWithRecordSeparator = CSVFormat.DEFAULT.withSystemRecordSeparator(); assertEquals(System.lineSeparator(), formatWithRecordSeparator.getRecordSeparator()); } + + @Test + public void testDelimiterEmptyStringThrowsException1() { + assertThrows(IllegalArgumentException.class, () -> CSVFormat.DEFAULT.builder().setDelimiter("").build()); + } }