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
This commit is contained in:
Emmanuel Bourg 2011-11-10 11:58:02 +00:00
parent 9acd5cd4d3
commit 2598862d94
2 changed files with 36 additions and 21 deletions

View File

@ -102,7 +102,7 @@ public class CSVFormat implements Cloneable, Serializable {
public CSVFormat withDelimiter(char delimiter) {
CSVFormat format = (CSVFormat) clone();
this.delimiter = delimiter;
format.delimiter = delimiter;
return format;
}

View File

@ -22,27 +22,42 @@ import junit.framework.TestCase;
public class CSVFormatTest extends TestCase {
public void testImmutalibity() {
CSVFormat format1 = new CSVFormat('!', '!', '!', '!', true, true, true, true);
CSVFormat format2 = format1.withDelimiter('?')
.withEncapsulator('?')
.withCommentStart('?')
.withLineSeparator("?")
.withEscape('?')
.withLeadingSpacesIgnored(false)
.withTrailingSpacesIgnored(false)
.withEmptyLinesIgnored(false)
.withUnicodeEscapesInterpreted(false);
assertNotSame(format1.getDelimiter(), format2.getDelimiter());
assertNotSame(format1.getEncapsulator(), format2.getEncapsulator());
assertNotSame(format1.getCommentStart(), format2.getCommentStart());
assertNotSame(format1.getEscape(), format2.getEscape());
assertNotSame(format1.getLineSeparator(), format2.getLineSeparator());
CSVFormat format = new CSVFormat('!', '!', '!', '!', true, true, true, true);
assertNotSame(format1.isTrailingSpacesIgnored(), format2.isTrailingSpacesIgnored());
assertNotSame(format1.isLeadingSpacesIgnored(), format2.isLeadingSpacesIgnored());
assertNotSame(format1.isEmptyLinesIgnored(), format2.isEmptyLinesIgnored());
assertNotSame(format1.isUnicodeEscapesInterpreted(), format2.isUnicodeEscapesInterpreted());
format.withDelimiter('?');
format.withEncapsulator('?');
format.withCommentStart('?');
format.withLineSeparator("?");
format.withEscape('?');
format.withLeadingSpacesIgnored(false);
format.withTrailingSpacesIgnored(false);
format.withEmptyLinesIgnored(false);
format.withUnicodeEscapesInterpreted(false);
assertEquals('!', format.getDelimiter());
assertEquals('!', format.getEncapsulator());
assertEquals('!', format.getCommentStart());
assertEquals("\n", format.getLineSeparator());
assertEquals('!', format.getEscape());
assertEquals(true, format.isLeadingSpacesIgnored());
assertEquals(true, format.isTrailingSpacesIgnored());
assertEquals(true, format.isEmptyLinesIgnored());
assertEquals(true, format.isUnicodeEscapesInterpreted());
}
public void testMutators() {
CSVFormat format = new CSVFormat('!', '!', '!', '!', true, true, true, true);
assertEquals('?', format.withDelimiter('?').getDelimiter());
assertEquals('?', format.withEncapsulator('?').getEncapsulator());
assertEquals('?', format.withCommentStart('?').getCommentStart());
assertEquals("?", format.withLineSeparator("?").getLineSeparator());
assertEquals('?', format.withEscape('?').getEscape());
assertEquals(false, format.withLeadingSpacesIgnored(false).isLeadingSpacesIgnored());
assertEquals(false, format.withTrailingSpacesIgnored(false).isTrailingSpacesIgnored());
assertEquals(false, format.withEmptyLinesIgnored(false).isEmptyLinesIgnored());
assertEquals(false, format.withUnicodeEscapesInterpreted(false).isUnicodeEscapesInterpreted());
}
}