Skip to content

Commit 5591444

Browse files
authored
Fix the old test case record.getComment() will never be null (apache#62)
* The old test case record.getComment() will never be null and if record.getComment() be null the test code misplace the null test. Add a new test file that record.getComment() will be null and test record.getComment() no null before using * keep the caching of "record.getComment()"
1 parent 72edc56 commit 5591444

4 files changed

Lines changed: 28 additions & 10 deletions

File tree

pom.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,8 @@
257257
<exclude>src/test/resources/ferc.gov/transaction.txt</exclude>
258258
<exclude>src/test/resources/**/*.bin</exclude>
259259
<exclude>src/test/resources/CSV-259/sample.txt</exclude>
260+
<exclude>src/test/resources/CSVFileParser/testCSV246.csv</exclude>
261+
<exclude>src/test/resources/CSVFileParser/testCSV246_checkWithNoComment.txt</exclude>
260262
</excludes>
261263
</configuration>
262264
</plugin>
@@ -375,6 +377,8 @@
375377
<exclude>src/test/resources/ferc.gov/transaction.txt</exclude>
376378
<exclude>src/test/resources/**/*.bin</exclude>
377379
<exclude>src/test/resources/CSV-259/sample.txt</exclude>
380+
<exclude>src/test/resources/CSVFileParser/testCSV246.csv</exclude>
381+
<exclude>src/test/resources/CSVFileParser/testCSV246_checkWithNoComment.txt</exclude>
378382
</excludes>
379383
</configuration>
380384
</plugin>

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

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -93,11 +93,9 @@ public void testCSVFile(final File testFile) throws Exception {
9393
try (final CSVParser parser = CSVParser.parse(new File(BASE, split[0]), Charset.defaultCharset(), format)) {
9494
for (final CSVRecord record : parser) {
9595
String parsed = Arrays.toString(record.values());
96-
if (checkComments) {
97-
final String comment = record.getComment().replace("\n", "\\n");
98-
if (comment != null) {
99-
parsed += "#" + comment;
100-
}
96+
String comment = record.getComment();
97+
if (checkComments && comment != null) {
98+
parsed += "#" + comment.replace("\n", "\\n");
10199
}
102100
final int count = record.size();
103101
assertEquals(readTestData(testData), count + ":" + parsed, testFile.getName());
@@ -140,11 +138,9 @@ public void testCSVUrl(final File testFile) throws Exception {
140138
try (final CSVParser parser = CSVParser.parse(resource, Charset.forName("UTF-8"), format)) {
141139
for (final CSVRecord record : parser) {
142140
String parsed = Arrays.toString(record.values());
143-
if (checkComments) {
144-
final String comment = record.getComment().replace("\n", "\\n");
145-
if (comment != null) {
146-
parsed += "#" + comment;
147-
}
141+
String comment = record.getComment();
142+
if (checkComments && comment != null) {
143+
parsed += "#" + comment.replace("\n", "\\n");
148144
}
149145
final int count = record.size();
150146
assertEquals(readTestData(testData), count + ":" + parsed, testFile.getName());
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
a,b,c,e,f
2+
# Very Long
3+
# Comment 2
4+
g,h,i,j,k
5+
# Very Long
6+
7+
# Comment 3
8+
l,m,n,o,p
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
testCSV246.csv CommentStart=# CheckComments
2+
Delimiter=<,> QuoteChar=<"> CommentStart=<#> SkipHeaderRecord:false
3+
5:[a, b, c, e, f]
4+
# Very Long
5+
# Comment 2
6+
5:[g, h, i, j, k]#Very Long\nComment 2
7+
# Very Long
8+
1:[]#Very Long
9+
# Comment 3
10+
5:[l, m, n, o, p]#Comment 3

0 commit comments

Comments
 (0)