Skip to content

Commit 2598862

Browse files
committed
Fixed the immutability of the delimiter in CSVFormat
git-svn-id: https://svn.apache.org/repos/asf/commons/sandbox/csv/trunk@1200283 13f79535-47bb-0310-9956-ffa450edef68
1 parent 9acd5cd commit 2598862

2 files changed

Lines changed: 36 additions & 21 deletions

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ public char getDelimiter() {
102102

103103
public CSVFormat withDelimiter(char delimiter) {
104104
CSVFormat format = (CSVFormat) clone();
105-
this.delimiter = delimiter;
105+
format.delimiter = delimiter;
106106
return format;
107107
}
108108

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

Lines changed: 35 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -22,27 +22,42 @@
2222
public class CSVFormatTest extends TestCase {
2323

2424
public void testImmutalibity() {
25-
CSVFormat format1 = new CSVFormat('!', '!', '!', '!', true, true, true, true);
26-
CSVFormat format2 = format1.withDelimiter('?')
27-
.withEncapsulator('?')
28-
.withCommentStart('?')
29-
.withLineSeparator("?")
30-
.withEscape('?')
31-
.withLeadingSpacesIgnored(false)
32-
.withTrailingSpacesIgnored(false)
33-
.withEmptyLinesIgnored(false)
34-
.withUnicodeEscapesInterpreted(false);
35-
36-
assertNotSame(format1.getDelimiter(), format2.getDelimiter());
37-
assertNotSame(format1.getEncapsulator(), format2.getEncapsulator());
38-
assertNotSame(format1.getCommentStart(), format2.getCommentStart());
39-
assertNotSame(format1.getEscape(), format2.getEscape());
40-
assertNotSame(format1.getLineSeparator(), format2.getLineSeparator());
25+
CSVFormat format = new CSVFormat('!', '!', '!', '!', true, true, true, true);
26+
27+
format.withDelimiter('?');
28+
format.withEncapsulator('?');
29+
format.withCommentStart('?');
30+
format.withLineSeparator("?");
31+
format.withEscape('?');
32+
format.withLeadingSpacesIgnored(false);
33+
format.withTrailingSpacesIgnored(false);
34+
format.withEmptyLinesIgnored(false);
35+
format.withUnicodeEscapesInterpreted(false);
36+
37+
assertEquals('!', format.getDelimiter());
38+
assertEquals('!', format.getEncapsulator());
39+
assertEquals('!', format.getCommentStart());
40+
assertEquals("\n", format.getLineSeparator());
41+
assertEquals('!', format.getEscape());
4142

42-
assertNotSame(format1.isTrailingSpacesIgnored(), format2.isTrailingSpacesIgnored());
43-
assertNotSame(format1.isLeadingSpacesIgnored(), format2.isLeadingSpacesIgnored());
44-
assertNotSame(format1.isEmptyLinesIgnored(), format2.isEmptyLinesIgnored());
45-
assertNotSame(format1.isUnicodeEscapesInterpreted(), format2.isUnicodeEscapesInterpreted());
43+
assertEquals(true, format.isLeadingSpacesIgnored());
44+
assertEquals(true, format.isTrailingSpacesIgnored());
45+
assertEquals(true, format.isEmptyLinesIgnored());
46+
assertEquals(true, format.isUnicodeEscapesInterpreted());
4647
}
4748

49+
public void testMutators() {
50+
CSVFormat format = new CSVFormat('!', '!', '!', '!', true, true, true, true);
51+
52+
assertEquals('?', format.withDelimiter('?').getDelimiter());
53+
assertEquals('?', format.withEncapsulator('?').getEncapsulator());
54+
assertEquals('?', format.withCommentStart('?').getCommentStart());
55+
assertEquals("?", format.withLineSeparator("?").getLineSeparator());
56+
assertEquals('?', format.withEscape('?').getEscape());
57+
58+
assertEquals(false, format.withLeadingSpacesIgnored(false).isLeadingSpacesIgnored());
59+
assertEquals(false, format.withTrailingSpacesIgnored(false).isTrailingSpacesIgnored());
60+
assertEquals(false, format.withEmptyLinesIgnored(false).isEmptyLinesIgnored());
61+
assertEquals(false, format.withUnicodeEscapesInterpreted(false).isUnicodeEscapesInterpreted());
62+
}
4863
}

0 commit comments

Comments
 (0)