Skip to content

Commit 43560d9

Browse files
committed
Make sure only record separators we can handle are used
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/csv/trunk@1609548 13f79535-47bb-0310-9956-ffa450edef68
1 parent b7cd1ae commit 43560d9

2 files changed

Lines changed: 36 additions & 8 deletions

File tree

src/main/java/org/apache/commons/csv/CSVFormat.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -701,6 +701,12 @@ private void validate() throws IllegalArgumentException {
701701
throw new IllegalArgumentException("No quotes mode set but no escape character is set");
702702
}
703703

704+
if(recordSeparator != null
705+
&& !(CRLF.equals(recordSeparator)
706+
|| String.valueOf(CR).equals(recordSeparator)
707+
|| String.valueOf(LF).equals(recordSeparator))) {
708+
throw new IllegalArgumentException("Record separator can only by CR, LF or CRLF");
709+
}
704710
}
705711

706712
/**
@@ -927,6 +933,8 @@ public CSVFormat withQuotePolicy(final Quote quotePolicy) {
927933
* the record separator to use for output.
928934
*
929935
* @return A new CSVFormat that is equal to this but with the the specified output record separator
936+
* @throws IllegalArgumentException
937+
* if recordSeparator is neither CR nor LF
930938
*/
931939
public CSVFormat withRecordSeparator(final char recordSeparator) {
932940
return withRecordSeparator(String.valueOf(recordSeparator));
@@ -941,6 +949,8 @@ public CSVFormat withRecordSeparator(final char recordSeparator) {
941949
* the record separator to use for output.
942950
*
943951
* @return A new CSVFormat that is equal to this but with the the specified output record separator
952+
* @throws IllegalArgumentException
953+
* if recordSeparator is none of CR, LF or CRLF
944954
*/
945955
public CSVFormat withRecordSeparator(final String recordSeparator) {
946956
return new CSVFormat(delimiter, quoteChar, quotePolicy, commentStart, escape,

src/test/java/org/apache/commons/csv/CSVFormatTest.java

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
import static org.apache.commons.csv.CSVFormat.RFC4180;
2121
import static org.apache.commons.csv.Constants.CR;
22+
import static org.apache.commons.csv.Constants.CRLF;
2223
import static org.apache.commons.csv.Constants.LF;
2324
import static org.junit.Assert.assertArrayEquals;
2425
import static org.junit.Assert.assertEquals;
@@ -118,7 +119,7 @@ public void testEqualsEscape() {
118119
@Test
119120
public void testEqualsHeader() {
120121
final CSVFormat right = CSVFormat.newFormat('\'')
121-
.withRecordSeparator('*')
122+
.withRecordSeparator(CR)
122123
.withCommentStart('#')
123124
.withEscape('+')
124125
.withHeader("One", "Two", "Three")
@@ -183,23 +184,23 @@ public void testEqualsQuotePolicy() {
183184
@Test
184185
public void testEqualsRecordSeparator() {
185186
final CSVFormat right = CSVFormat.newFormat('\'')
186-
.withRecordSeparator('*')
187+
.withRecordSeparator(CR)
187188
.withCommentStart('#')
188189
.withEscape('+')
189190
.withIgnoreEmptyLines(true)
190191
.withIgnoreSurroundingSpaces(true)
191192
.withQuoteChar('"')
192193
.withQuotePolicy(Quote.ALL);
193194
final CSVFormat left = right
194-
.withRecordSeparator('!');
195+
.withRecordSeparator(LF);
195196

196197
assertNotEquals(right, left);
197198
}
198199

199200
@Test
200201
public void testEqualsNullString() {
201202
final CSVFormat right = CSVFormat.newFormat('\'')
202-
.withRecordSeparator('*')
203+
.withRecordSeparator(CR)
203204
.withCommentStart('#')
204205
.withEscape('+')
205206
.withIgnoreEmptyLines(true)
@@ -216,7 +217,7 @@ public void testEqualsNullString() {
216217
@Test
217218
public void testEqualsSkipHeaderRecord() {
218219
final CSVFormat right = CSVFormat.newFormat('\'')
219-
.withRecordSeparator('*')
220+
.withRecordSeparator(CR)
220221
.withCommentStart('#')
221222
.withEscape('+')
222223
.withIgnoreEmptyLines(true)
@@ -409,8 +410,25 @@ public void testWithQuotePolicy() throws Exception {
409410
}
410411

411412
@Test
412-
public void testWithRecordSeparator() throws Exception {
413-
final CSVFormat formatWithRecordSeparator = CSVFormat.DEFAULT.withRecordSeparator('!');
414-
assertEquals("!", formatWithRecordSeparator.getRecordSeparator());
413+
public void testWithRecordSeparatorCR() throws Exception {
414+
final CSVFormat formatWithRecordSeparator = CSVFormat.DEFAULT.withRecordSeparator(CR);
415+
assertEquals(String.valueOf(CR), formatWithRecordSeparator.getRecordSeparator());
416+
}
417+
418+
@Test
419+
public void testWithRecordSeparatorLF() throws Exception {
420+
final CSVFormat formatWithRecordSeparator = CSVFormat.DEFAULT.withRecordSeparator(LF);
421+
assertEquals(String.valueOf(LF), formatWithRecordSeparator.getRecordSeparator());
422+
}
423+
424+
@Test
425+
public void testWithRecordSeparatorCRLF() throws Exception {
426+
final CSVFormat formatWithRecordSeparator = CSVFormat.DEFAULT.withRecordSeparator(CRLF);
427+
assertEquals(CRLF, formatWithRecordSeparator.getRecordSeparator());
428+
}
429+
430+
@Test(expected = IllegalArgumentException.class)
431+
public void testWithRecordSeparatorIllegal() throws Exception {
432+
CSVFormat.DEFAULT.withRecordSeparator('!');
415433
}
416434
}

0 commit comments

Comments
 (0)