Skip to content

Commit 69cdb8a

Browse files
author
Brent Worden
committed
CSV-124 apply provided patch and update unit tests
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/csv/trunk@1624048 13f79535-47bb-0310-9956-ffa450edef68
1 parent 5812793 commit 69cdb8a

2 files changed

Lines changed: 29 additions & 22 deletions

File tree

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -227,15 +227,18 @@ public Map<String, String> toMap() {
227227
return putIn(new HashMap<String, String>(values.length));
228228
}
229229

230+
230231
/**
231-
* Returns a string representation of the contents of this record. The result is constructed by passing the internal
232-
* values array to {@link Arrays#toString(Object[])}.
232+
* Returns a string representation of the contents of this record. The result is constructed by comment, mapping,
233+
* recordNumber and by passing the internal values array to {@link Arrays#toString(Object[])}.
233234
*
234235
* @return a String representation of this record.
235236
*/
236237
@Override
237238
public String toString() {
238-
return Arrays.toString(values);
239+
return "CSVRecord [comment=" + comment + ", mapping=" + mapping +
240+
", recordNumber=" + recordNumber + ", values=" +
241+
Arrays.toString(values) + "]";
239242
}
240243

241244
String[] values() {

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

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import java.net.URL;
3232
import java.nio.charset.Charset;
3333
import java.util.ArrayList;
34+
import java.util.Arrays;
3435
import java.util.Collection;
3536
import java.util.List;
3637

@@ -50,12 +51,12 @@ public class CSVFileParserTest {
5051
private static final File BASE = new File("src/test/resources/CSVFileParser");
5152

5253
private final BufferedReader testData;
54+
5355
private final String testName;
5456

55-
public CSVFileParserTest(final File file) throws FileNotFoundException
56-
{
57-
this.testName = file.getName();
58-
this.testData = new BufferedReader(new FileReader(file));
57+
public CSVFileParserTest(final File file) throws FileNotFoundException {
58+
this.testName = file.getName();
59+
this.testData = new BufferedReader(new FileReader(file));
5960
}
6061

6162
private String readTestData() throws IOException {
@@ -67,18 +68,21 @@ private String readTestData() throws IOException {
6768
}
6869

6970
@Parameters
70-
public static Collection<Object[]> generateData()
71-
{
71+
public static Collection<Object[]> generateData() {
7272
final List<Object[]> list = new ArrayList<Object[]>();
7373

7474
final FilenameFilter filenameFilter = new FilenameFilter() {
75+
76+
@Override
7577
public boolean accept(final File dir, final String name) {
7678
return name.startsWith("test") && name.endsWith(".txt");
7779
}
7880
};
7981
final File[] files = BASE.listFiles(filenameFilter);
80-
for(final File f : files){
81-
list.add(new Object[]{f});
82+
for (final File f : files) {
83+
list.add(new Object[] {
84+
f
85+
});
8286
}
8387
return list;
8488
}
@@ -88,14 +92,14 @@ public void testCSVFile() throws Exception {
8892
String line = readTestData();
8993
assertNotNull("file must contain config line", line);
9094
final String[] split = line.split(" ");
91-
assertTrue(testName+" require 1 param", split.length >= 1);
92-
// first line starts with csv data file name
95+
assertTrue(testName + " require 1 param", split.length >= 1);
96+
// first line starts with csv data file name
9397
CSVFormat format = CSVFormat.newFormat(',').withQuote('"');
9498
boolean checkComments = false;
95-
for(int i=1; i < split.length; i++) {
99+
for (int i = 1; i < split.length; i++) {
96100
final String option = split[i];
97-
final String[] option_parts = option.split("=",2);
98-
if ("IgnoreEmpty".equalsIgnoreCase(option_parts[0])){
101+
final String[] option_parts = option.split("=", 2);
102+
if ("IgnoreEmpty".equalsIgnoreCase(option_parts[0])) {
99103
format = format.withIgnoreEmptyLines(Boolean.parseBoolean(option_parts[1]));
100104
} else if ("IgnoreSpaces".equalsIgnoreCase(option_parts[0])) {
101105
format = format.withIgnoreSurroundingSpaces(Boolean.parseBoolean(option_parts[1]));
@@ -104,25 +108,25 @@ public void testCSVFile() throws Exception {
104108
} else if ("CheckComments".equalsIgnoreCase(option_parts[0])) {
105109
checkComments = true;
106110
} else {
107-
fail(testName+" unexpected option: "+option);
111+
fail(testName + " unexpected option: " + option);
108112
}
109113
}
110114
line = readTestData(); // get string version of format
111-
assertEquals(testName+" Expected format ", line, format.toString());
115+
assertEquals(testName + " Expected format ", line, format.toString());
112116

113117
// Now parse the file and compare against the expected results
114118
// We use a buffered reader internally so no need to create one here.
115119
final CSVParser parser = CSVParser.parse(new File(BASE, split[0]), Charset.defaultCharset(), format);
116-
for(final CSVRecord record : parser) {
117-
String parsed = record.toString();
120+
for (final CSVRecord record : parser) {
121+
String parsed = Arrays.toString(record.values());
118122
if (checkComments) {
119123
final String comment = record.getComment().replace("\n", "\\n");
120124
if (comment != null) {
121125
parsed += "#" + comment;
122126
}
123127
}
124128
final int count = record.size();
125-
assertEquals(testName, readTestData(), count+":"+parsed);
129+
assertEquals(testName, readTestData(), count + ":" + parsed);
126130
}
127131
parser.close();
128132
}
@@ -158,7 +162,7 @@ public void testCSVUrl() throws Exception {
158162
final URL resource = ClassLoader.getSystemResource("CSVFileParser/" + split[0]);
159163
final CSVParser parser = CSVParser.parse(resource, Charset.forName("UTF-8"), format);
160164
for (final CSVRecord record : parser) {
161-
String parsed = record.toString();
165+
String parsed = Arrays.toString(record.values());
162166
if (checkComments) {
163167
final String comment = record.getComment().replace("\n", "\\n");
164168
if (comment != null) {

0 commit comments

Comments
 (0)