commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ggreg...@apache.org
Subject svn commit: r1508585 - in /commons/proper/csv/trunk/src: main/java/org/apache/commons/csv/CSVFormat.java main/java/org/apache/commons/csv/CSVParser.java test/java/org/apache/commons/csv/CSVFormatTest.java
Date Tue, 30 Jul 2013 19:32:40 GMT
Author: ggregory
Date: Tue Jul 30 19:32:40 2013
New Revision: 1508585

URL: http://svn.apache.org/r1508585
Log:
Make org.apache.commons.csv.CSVFormat.getHeader() public and make it return a clone.

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/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=1508585&r1=1508584&r2=1508585&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
19:32:40 2013
@@ -334,8 +334,13 @@ public class CSVFormat implements Serial
         return escape;
     }
 
-    String[] getHeader() {
-        return header;
+    /**
+     * Returns a copy of the header array. 
+     * 
+     * @return a copy of the header array
+     */
+    public String[] getHeader() {
+        return header != null ? header.clone() : null;
     }
 
     /**

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=1508585&r1=1508584&r2=1508585&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
19:32:40 2013
@@ -321,18 +321,19 @@ public class CSVParser implements Iterab
      */
     private Map<String, Integer> initializeHeader() throws IOException {
         Map<String, Integer> hdrMap = null;
-        if (this.format.getHeader() != null) {
+        String[] formatHeader = this.format.getHeader();
+        if (formatHeader != null) {
             hdrMap = new LinkedHashMap<String, Integer>();
 
             String[] header = null;
-            if (this.format.getHeader().length == 0) {
+            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();
                 }
             } else {
-                header = this.format.getHeader();
+                header = formatHeader;
             }
 
             // build the name to index mappings

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=1508585&r1=1508584&r2=1508585&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 Tue Jul
30 19:32:40 2013
@@ -31,6 +31,7 @@ import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
 import java.io.ObjectInputStream;
 import java.io.ObjectOutputStream;
+import java.util.Arrays;
 
 import org.junit.Test;
 
@@ -224,9 +225,27 @@ public class CSVFormatTest {
     @Test
     public void testWithHeader() throws Exception {
         String[] header = new String[]{"one", "two", "three"};
+        // withHeader() makes a copy of the header array.
         CSVFormat formatWithHeader = CSVFormat.DEFAULT.withHeader(header);
         assertArrayEquals(header, formatWithHeader.getHeader());
         assertNotSame(header, formatWithHeader.getHeader());
+        header[0] = "A";
+        header[1] = "B";
+        header[2] = "C";
+        assertFalse(Arrays.equals(formatWithHeader.getHeader(), header));
+    }
+
+    @Test
+    public void testGetHeader() throws Exception {
+        String[] header = new String[]{"one", "two", "three"};
+        CSVFormat formatWithHeader = CSVFormat.DEFAULT.withHeader(header);
+        // getHeader() makes a copy of the header array.
+        String[] headerCopy = formatWithHeader.getHeader();
+        headerCopy[0] = "A";
+        headerCopy[1] = "B";
+        headerCopy[2] = "C";
+        assertFalse(Arrays.equals(formatWithHeader.getHeader(), headerCopy));
+        assertNotSame(formatWithHeader.getHeader(), headerCopy);
     }
 
     @Test



Mime
View raw message