Author: ggregory
Date: Tue Jul 30 20:36:12 2013
New Revision: 1508612

URL: http://svn.apache.org/r1508612
Log:
When withHeader is set to any non-null value, the first record is the first 
<em>data</em> record, not the header record.

Modified:
    commons/proper/csv/trunk/src/main/java/org/apache/commons/csv/CSVFormat.java
    commons/proper/csv/trunk/src/main/java/org/apache/commons/csv/CSVParser.java
    
commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/CSVParserTest.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=1508612&r1=1508611&r2=1508612&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 
Tue Jul 30 20:36:12 2013
@@ -614,22 +614,24 @@ public class CSVFormat implements Serial
     }
 
     /**
-     * Sets the header of the format. The header can either be parsed 
automatically from the
-     * input file with:
-     *
+     * Sets the header of the format. The header can either be parsed 
automatically from the input file with:
+     * 
      * <pre>
      * CSVFormat format = aformat.withHeader();
      * </pre>
-     *
+     * 
      * or specified manually with:
-     *
+     * 
      * <pre>
      * CSVFormat format = aformat.withHeader(&quot;name&quot;, 
&quot;email&quot;, &quot;phone&quot;);
      * </pre>
-     *
+     * 
+     * When this option is is set to any non-null value, the first record is 
the first <em>data</em> record, not the
+     * header record.
+     * 
      * @param header
      *            the header, <tt>null</tt> if disabled, empty if parsed 
automatically, user specified otherwise.
-     *
+     * 
      * @return A new CSVFormat that is equal to this but with the specified 
header
      */
     public CSVFormat withHeader(final String... header) {

Modified: 
commons/proper/csv/trunk/src/main/java/org/apache/commons/csv/CSVParser.java
URL: 
http://svn.apache.org/viewvc/commons/proper/csv/trunk/src/main/java/org/apache/commons/csv/CSVParser.java?rev=1508612&r1=1508611&r2=1508612&view=diff
==============================================================================
--- 
commons/proper/csv/trunk/src/main/java/org/apache/commons/csv/CSVParser.java 
(original)
+++ 
commons/proper/csv/trunk/src/main/java/org/apache/commons/csv/CSVParser.java 
Tue Jul 30 20:36:12 2013
@@ -323,12 +323,12 @@ public class CSVParser implements Iterab
         Map<String, Integer> hdrMap = null;
         String[] formatHeader = this.format.getHeader();
         if (formatHeader != null) {
+            final CSVRecord record = this.nextRecord();
             hdrMap = new LinkedHashMap<String, Integer>();
 
             String[] header = null;
             if (formatHeader.length == 0) {
                 // read the header from the first line of the file
-                final CSVRecord record = this.nextRecord();
                 if (record != null) {
                     header = record.values();
                 }

Modified: 
commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/CSVParserTest.java
URL: 
http://svn.apache.org/viewvc/commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/CSVParserTest.java?rev=1508612&r1=1508611&r2=1508612&view=diff
==============================================================================
--- 
commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/CSVParserTest.java
 (original)
+++ 
commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/CSVParserTest.java
 Tue Jul 30 20:36:12 2013
@@ -507,6 +507,26 @@ public class CSVParserTest {
     }
 
     @Test
+    public void testSkipSetHeader() throws Exception {
+        final Reader in = new StringReader("a,b,c\n1,2,3\nx,y,z");
+        final Iterator<CSVRecord> records = CSVFormat.DEFAULT.withHeader("a", 
"b", "c").parse(in).iterator();
+        final CSVRecord record = records.next();
+        assertEquals("1", record.get("a"));
+        assertEquals("2", record.get("b"));
+        assertEquals("3", record.get("c"));
+    }
+
+    @Test
+    public void testSkipAutoHeader() throws Exception {
+        final Reader in = new StringReader("a,b,c\n1,2,3\nx,y,z");
+        final Iterator<CSVRecord> records = 
CSVFormat.DEFAULT.withHeader().parse(in).iterator();
+        final CSVRecord record = records.next();
+        assertEquals("1", record.get("a"));
+        assertEquals("2", record.get("b"));
+        assertEquals("3", record.get("c"));
+    }
+
+    @Test
     public void testHeaderComment() throws Exception {
         final Reader in = new StringReader("# comment\na,b,c\n1,2,3\nx,y,z");
 
@@ -529,7 +549,7 @@ public class CSVParserTest {
 
         final Iterator<CSVRecord> records = CSVFormat.DEFAULT.withHeader("A", 
"B", "C").parse(in).iterator();
 
-        for (int i = 0; i < 3; i++) {
+        for (int i = 0; i < 2; i++) {
             assertTrue(records.hasNext());
             final CSVRecord record = records.next();
             assertTrue(record.isMapped("A"));
@@ -545,24 +565,31 @@ public class CSVParserTest {
     }
 
     @Test
+    public void testProvidedHeaderAuto() throws Exception {
+        final Reader in = new StringReader("a,b,c\n1,2,3\nx,y,z");
+
+        final Iterator<CSVRecord> records = 
CSVFormat.DEFAULT.withHeader().parse(in).iterator();
+
+        for (int i = 0; i < 2; i++) {
+            assertTrue(records.hasNext());
+            final CSVRecord record = records.next();
+            assertTrue(record.isMapped("a"));
+            assertTrue(record.isMapped("b"));
+            assertTrue(record.isMapped("c"));
+            assertFalse(record.isMapped("NOT MAPPED"));
+            assertEquals(record.get(0), record.get("a"));
+            assertEquals(record.get(1), record.get("b"));
+            assertEquals(record.get(2), record.get("c"));
+        }
+
+        assertFalse(records.hasNext());
+    }
+
+    @Test
     public void testMappedButNotSetAsOutlook2007ContactExport() throws 
Exception {
         final Reader in = new StringReader("a,b,c\n1,2\nx,y,z");
-
         final Iterator<CSVRecord> records = CSVFormat.DEFAULT.withHeader("A", 
"B", "C").parse(in).iterator();
-
-        // header record
-        assertTrue(records.hasNext());
-        CSVRecord record = records.next();
-        assertTrue(record.isMapped("A"));
-        assertTrue(record.isMapped("B"));
-        assertTrue(record.isMapped("C"));
-        assertTrue(record.isSet("A"));
-        assertTrue(record.isSet("B"));
-        assertTrue(record.isSet("C"));
-        assertEquals("a", record.get("A"));
-        assertEquals("b", record.get("B"));
-        assertEquals("c", record.get("C"));
-        assertTrue(record.isConsistent());
+        CSVRecord record;
 
         // 1st record
         record = records.next();
@@ -604,7 +631,7 @@ public class CSVParserTest {
         final Iterator<CSVRecord> records = parser.iterator();
 
         // Parse to make sure getHeaderMap did not have a side-effect.
-        for (int i = 0; i < 3; i++) {
+        for (int i = 0; i < 2; i++) {
             assertTrue(records.hasNext());
             final CSVRecord record = records.next();
             assertEquals(record.get(0), record.get("A"));


Reply via email to