commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From brit...@apache.org
Subject svn commit: r1609548 - in /commons/proper/csv/trunk/src: main/java/org/apache/commons/csv/CSVFormat.java test/java/org/apache/commons/csv/CSVFormatTest.java
Date Thu, 10 Jul 2014 18:30:30 GMT
Author: britter
Date: Thu Jul 10 18:30:29 2014
New Revision: 1609548

URL: http://svn.apache.org/r1609548
Log:
Make sure only record separators we can handle are used

Modified:
    commons/proper/csv/trunk/src/main/java/org/apache/commons/csv/CSVFormat.java
    commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/CSVFormatTest.java

Modified: commons/proper/csv/trunk/src/main/java/org/apache/commons/csv/CSVFormat.java
URL: http://svn.apache.org/viewvc/commons/proper/csv/trunk/src/main/java/org/apache/commons/csv/CSVFormat.java?rev=1609548&r1=1609547&r2=1609548&view=diff
==============================================================================
--- commons/proper/csv/trunk/src/main/java/org/apache/commons/csv/CSVFormat.java (original)
+++ commons/proper/csv/trunk/src/main/java/org/apache/commons/csv/CSVFormat.java Thu Jul 10
18:30:29 2014
@@ -701,6 +701,12 @@ public final class CSVFormat implements 
             throw new IllegalArgumentException("No quotes mode set but no escape character
is set");
         }
 
+        if(recordSeparator != null
+                && !(CRLF.equals(recordSeparator)
+                    || String.valueOf(CR).equals(recordSeparator)
+                    || String.valueOf(LF).equals(recordSeparator))) {
+            throw new IllegalArgumentException("Record separator can only by CR, LF or CRLF");
+        }
     }
 
     /**
@@ -927,6 +933,8 @@ public final class CSVFormat implements 
      *            the record separator to use for output.
      *
      * @return A new CSVFormat that is equal to this but with the the specified output record
separator
+     * @throws IllegalArgumentException
+     *              if recordSeparator is neither CR nor LF
      */
     public CSVFormat withRecordSeparator(final char recordSeparator) {
         return withRecordSeparator(String.valueOf(recordSeparator));
@@ -941,6 +949,8 @@ public final class CSVFormat implements 
      *            the record separator to use for output.
      *
      * @return A new CSVFormat that is equal to this but with the the specified output record
separator
+     * @throws IllegalArgumentException
+     *              if recordSeparator is none of CR, LF or CRLF
      */
     public CSVFormat withRecordSeparator(final String recordSeparator) {
         return new CSVFormat(delimiter, quoteChar, quotePolicy, commentStart, escape,

Modified: commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/CSVFormatTest.java
URL: http://svn.apache.org/viewvc/commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/CSVFormatTest.java?rev=1609548&r1=1609547&r2=1609548&view=diff
==============================================================================
--- commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/CSVFormatTest.java (original)
+++ commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/CSVFormatTest.java Thu Jul
10 18:30:29 2014
@@ -19,6 +19,7 @@ package org.apache.commons.csv;
 
 import static org.apache.commons.csv.CSVFormat.RFC4180;
 import static org.apache.commons.csv.Constants.CR;
+import static org.apache.commons.csv.Constants.CRLF;
 import static org.apache.commons.csv.Constants.LF;
 import static org.junit.Assert.assertArrayEquals;
 import static org.junit.Assert.assertEquals;
@@ -118,7 +119,7 @@ public class CSVFormatTest {
     @Test
     public void testEqualsHeader() {
         final CSVFormat right = CSVFormat.newFormat('\'')
-                .withRecordSeparator('*')
+                .withRecordSeparator(CR)
                 .withCommentStart('#')
                 .withEscape('+')
                 .withHeader("One", "Two", "Three")
@@ -183,7 +184,7 @@ public class CSVFormatTest {
     @Test
     public void testEqualsRecordSeparator() {
         final CSVFormat right = CSVFormat.newFormat('\'')
-                .withRecordSeparator('*')
+                .withRecordSeparator(CR)
                 .withCommentStart('#')
                 .withEscape('+')
                 .withIgnoreEmptyLines(true)
@@ -191,7 +192,7 @@ public class CSVFormatTest {
                 .withQuoteChar('"')
                 .withQuotePolicy(Quote.ALL);
         final CSVFormat left = right
-                .withRecordSeparator('!');
+                .withRecordSeparator(LF);
 
         assertNotEquals(right, left);
     }
@@ -199,7 +200,7 @@ public class CSVFormatTest {
     @Test
     public void testEqualsNullString() {
         final CSVFormat right = CSVFormat.newFormat('\'')
-                .withRecordSeparator('*')
+                .withRecordSeparator(CR)
                 .withCommentStart('#')
                 .withEscape('+')
                 .withIgnoreEmptyLines(true)
@@ -216,7 +217,7 @@ public class CSVFormatTest {
     @Test
     public void testEqualsSkipHeaderRecord() {
         final CSVFormat right = CSVFormat.newFormat('\'')
-                .withRecordSeparator('*')
+                .withRecordSeparator(CR)
                 .withCommentStart('#')
                 .withEscape('+')
                 .withIgnoreEmptyLines(true)
@@ -409,8 +410,25 @@ public class CSVFormatTest {
     }
 
     @Test
-    public void testWithRecordSeparator() throws Exception {
-        final CSVFormat formatWithRecordSeparator = CSVFormat.DEFAULT.withRecordSeparator('!');
-        assertEquals("!", formatWithRecordSeparator.getRecordSeparator());
+    public void testWithRecordSeparatorCR() throws Exception {
+        final CSVFormat formatWithRecordSeparator = CSVFormat.DEFAULT.withRecordSeparator(CR);
+        assertEquals(String.valueOf(CR), formatWithRecordSeparator.getRecordSeparator());
+    }
+
+    @Test
+    public void testWithRecordSeparatorLF() throws Exception {
+        final CSVFormat formatWithRecordSeparator = CSVFormat.DEFAULT.withRecordSeparator(LF);
+        assertEquals(String.valueOf(LF), formatWithRecordSeparator.getRecordSeparator());
+    }
+
+    @Test
+    public void testWithRecordSeparatorCRLF() throws Exception {
+        final CSVFormat formatWithRecordSeparator = CSVFormat.DEFAULT.withRecordSeparator(CRLF);
+        assertEquals(CRLF, formatWithRecordSeparator.getRecordSeparator());
+    }
+
+    @Test(expected = IllegalArgumentException.class)
+    public void testWithRecordSeparatorIllegal() throws Exception {
+        CSVFormat.DEFAULT.withRecordSeparator('!');
     }
 }



Mime
View raw message