From 2598862d94b7f6f3dfae057193b4f0cb5be8dee8 Mon Sep 17 00:00:00 2001 From: Emmanuel Bourg Date: Thu, 10 Nov 2011 11:58:02 +0000 Subject: [PATCH] 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 --- .../org/apache/commons/csv/CSVFormat.java | 2 +- .../org/apache/commons/csv/CSVFormatTest.java | 55 ++++++++++++------- 2 files changed, 36 insertions(+), 21 deletions(-) diff --git a/src/main/java/org/apache/commons/csv/CSVFormat.java b/src/main/java/org/apache/commons/csv/CSVFormat.java index c54796b8..90eb710a 100644 --- a/src/main/java/org/apache/commons/csv/CSVFormat.java +++ b/src/main/java/org/apache/commons/csv/CSVFormat.java @@ -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; } diff --git a/src/test/java/org/apache/commons/csv/CSVFormatTest.java b/src/test/java/org/apache/commons/csv/CSVFormatTest.java index 14d1ad0d..bfedb54d 100644 --- a/src/test/java/org/apache/commons/csv/CSVFormatTest.java +++ b/src/test/java/org/apache/commons/csv/CSVFormatTest.java @@ -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()); + } }