Skip to content

Commit 6a34b82

Browse files
committed
[CSV-68] Use the Builder pattern for CSVFormat.
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/csv/trunk@1409449 13f79535-47bb-0310-9956-ffa450edef68
1 parent d266471 commit 6a34b82

10 files changed

Lines changed: 522 additions & 446 deletions

File tree

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

Lines changed: 336 additions & 263 deletions
Large diffs are not rendered by default.

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,6 @@ public CSVParser(final Reader input) throws IOException {
103103
* thrown if the parameters of the format are inconsistent
104104
*/
105105
public CSVParser(final Reader input, final CSVFormat format) throws IOException {
106-
format.validate();
107106
this.lexer = new CSVLexer(format, new ExtendedBufferedReader(input));
108107
this.headerMap = initializeHeader(format);
109108
}

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ public class CSVPrinter implements Flushable, Closeable {
5959
public CSVPrinter(final Appendable out, final CSVFormat format) {
6060
this.out = out;
6161
this.format = format == null ? CSVFormat.DEFAULT : format;
62-
this.format.validate();
6362
}
6463

6564
// ======================================================

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import java.util.Collection;
3333
import java.util.List;
3434

35+
import org.apache.commons.csv.CSVFormat.CSVFormatBuilder;
3536
import org.junit.Test;
3637
import org.junit.runner.RunWith;
3738
import org.junit.runners.Parameterized;
@@ -89,17 +90,18 @@ public void testCSVFile() throws Exception {
8990
assertTrue(testName+" require 1 param", split.length >= 1);
9091
// first line starts with csv data file name
9192
final BufferedReader csvFile = new BufferedReader(new FileReader(new File(BASE, split[0])));
92-
CSVFormat fmt = new CSVFormat(',').withQuoteChar('"');
93+
CSVFormatBuilder builder = CSVFormat.newFormat(',').withQuoteChar('"');
94+
CSVFormat fmt = builder.build();
9395
boolean checkComments = false;
9496
for(int i=1; i < split.length; i++) {
9597
final String option = split[i];
9698
final String[] option_parts = option.split("=",2);
9799
if ("IgnoreEmpty".equalsIgnoreCase(option_parts[0])){
98-
fmt = fmt.withIgnoreEmptyLines(Boolean.parseBoolean(option_parts[1]));
100+
fmt = builder.withIgnoreEmptyLines(Boolean.parseBoolean(option_parts[1])).build();
99101
} else if ("IgnoreSpaces".equalsIgnoreCase(option_parts[0])) {
100-
fmt = fmt.withIgnoreSurroundingSpaces(Boolean.parseBoolean(option_parts[1]));
102+
fmt = builder.withIgnoreSurroundingSpaces(Boolean.parseBoolean(option_parts[1])).build();
101103
} else if ("CommentStart".equalsIgnoreCase(option_parts[0])) {
102-
fmt = fmt.withCommentStart(option_parts[1].charAt(0));
104+
fmt = builder.withCommentStart(option_parts[1].charAt(0)).build();
103105
} else if ("CheckComments".equalsIgnoreCase(option_parts[0])) {
104106
checkComments = true;
105107
} else {
Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.commons.csv;
19+
20+
import static org.apache.commons.csv.Constants.*;
21+
import static org.junit.Assert.*;
22+
23+
import org.apache.commons.csv.CSVFormat.CSVFormatBuilder;
24+
import org.junit.Before;
25+
import org.junit.Test;
26+
27+
/**
28+
*
29+
*
30+
* @version $Id$
31+
*/
32+
public class CSVFormatBuilderTest {
33+
34+
private CSVFormatBuilder builder;
35+
36+
@Before
37+
public void setUp() throws Exception {
38+
builder = new CSVFormatBuilder('+', '!', null, '#', '!', true, true, CRLF, null);
39+
}
40+
41+
@Test
42+
public void testDelimiter() {
43+
assertEquals('?', builder.withDelimiter('?').build().getDelimiter());
44+
}
45+
46+
@Test(expected = IllegalArgumentException.class)
47+
public void testNewFormatLFThrowsException() {
48+
CSVFormat.newFormat(LF);
49+
}
50+
51+
@Test(expected = IllegalArgumentException.class)
52+
public void testNewFormatCRThrowsException() {
53+
CSVFormat.newFormat(CR);
54+
}
55+
56+
@Test(expected = IllegalArgumentException.class)
57+
public void testWithDelimiterLFThrowsException() {
58+
builder.withDelimiter(LF).build();
59+
}
60+
61+
@Test(expected = IllegalStateException.class)
62+
public void testDelimiterSameAsEscapeThrowsException() {
63+
builder.withDelimiter('!').withEscape('!').build();
64+
}
65+
66+
@Test(expected = IllegalStateException.class)
67+
public void testDelimiterSameAsCommentStartThrowsException() {
68+
builder.withDelimiter('!').withCommentStart('!').build();
69+
}
70+
71+
@Test
72+
public void testQuoteChar() {
73+
assertEquals('?', builder.withQuoteChar('?').build().getQuoteChar().charValue());
74+
}
75+
76+
@Test(expected = IllegalStateException.class)
77+
public void testQuoteCharSameAsCommentStartThrowsException() {
78+
builder.withQuoteChar('!').withCommentStart('!').build();
79+
}
80+
81+
@Test(expected = IllegalStateException.class)
82+
public void testQuoteCharSameAsCommentStartThrowsExceptionForWrapperType() {
83+
// Cannot assume that callers won't use different Character objects
84+
builder.withQuoteChar(new Character('!')).withCommentStart('!').build();
85+
}
86+
87+
@Test(expected = IllegalStateException.class)
88+
public void testQuoteCharSameAsDelimiterThrowsException() {
89+
builder.withQuoteChar('!').withDelimiter('!').build();
90+
}
91+
92+
@Test(expected = IllegalArgumentException.class)
93+
public void testWithQuoteLFThrowsException() {
94+
builder.withQuoteChar(LF).build();
95+
}
96+
97+
@Test
98+
public void testQuotePolicy() {
99+
assertEquals(Quote.ALL, builder.withQuotePolicy(Quote.ALL).build().getQuotePolicy());
100+
}
101+
102+
@Test(expected = IllegalStateException.class)
103+
public void testQuotePolicyNoneWithoutEscapeThrowsException() {
104+
CSVFormat.newFormat('!').withQuotePolicy(Quote.NONE).build();
105+
}
106+
107+
@Test
108+
public void testCommentStart() {
109+
assertEquals('?', builder.withCommentStart('?').build().getCommentStart().charValue());
110+
}
111+
112+
@Test(expected = IllegalArgumentException.class)
113+
public void testWithCommentStartCRThrowsException() {
114+
builder.withCommentStart(CR).build();
115+
}
116+
117+
@Test
118+
public void testRecoardSeparator() {
119+
assertEquals("?", builder.withRecordSeparator("?").build().getRecordSeparator());
120+
}
121+
122+
@Test
123+
public void testEscape() {
124+
assertEquals('?', builder.withEscape('?').build().getEscape().charValue());
125+
}
126+
127+
@Test(expected = IllegalArgumentException.class)
128+
public void testWithEscapeCRThrowsExceptions() {
129+
builder.withEscape(CR).build();
130+
}
131+
132+
@Test(expected = IllegalStateException.class)
133+
public void testEscapeSameAsCommentStartThrowsException() {
134+
builder.withEscape('!').withCommentStart('!').build();
135+
}
136+
137+
@Test(expected = IllegalStateException.class)
138+
public void testEscapeSameAsCommentStartThrowsExceptionForWrapperType() {
139+
// Cannot assume that callers won't use different Character objects
140+
builder.withEscape(new Character('!')).withCommentStart(new Character('!')).build();
141+
}
142+
143+
@Test
144+
public void testIgnoreSurroundingSpaces() {
145+
assertFalse(builder.withIgnoreSurroundingSpaces(false).build().getIgnoreSurroundingSpaces());
146+
}
147+
148+
@Test
149+
public void testIgnoreEmptyLines() {
150+
assertFalse(builder.withIgnoreEmptyLines(false).build().getIgnoreEmptyLines());
151+
}
152+
}

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

Lines changed: 1 addition & 150 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import java.io.ObjectInputStream;
3030
import java.io.ObjectOutputStream;
3131

32+
import org.apache.commons.csv.CSVFormat.CSVFormatBuilder;
3233
import org.junit.Test;
3334

3435
/**
@@ -38,46 +39,6 @@
3839
*/
3940
public class CSVFormatTest {
4041

41-
@Test
42-
public void testImmutalibity() {
43-
final CSVFormat format = new CSVFormat('!', '!', Quote.MINIMAL, '!', '!', true, true, CRLF, null);
44-
45-
format.withDelimiter('?');
46-
format.withQuoteChar('?');
47-
format.withQuotePolicy(Quote.ALL);
48-
format.withCommentStart('?');
49-
format.withRecordSeparator("?");
50-
format.withEscape('?');
51-
format.withIgnoreSurroundingSpaces(false);
52-
format.withIgnoreEmptyLines(false);
53-
54-
assertEquals('!', format.getDelimiter());
55-
assertEquals('!', format.getQuoteChar().charValue());
56-
assertEquals('!', format.getCommentStart().charValue());
57-
assertEquals('!', format.getEscape().charValue());
58-
assertEquals(CRLF, format.getRecordSeparator());
59-
60-
assertTrue(format.getIgnoreSurroundingSpaces());
61-
assertTrue(format.getIgnoreEmptyLines());
62-
63-
assertEquals(Quote.MINIMAL, format.getQuotePolicy());
64-
}
65-
66-
@Test
67-
public void testMutators() {
68-
final CSVFormat format = new CSVFormat('!', '!', null, '!', '!', true, true, CRLF, null);
69-
70-
assertEquals('?', format.withDelimiter('?').getDelimiter());
71-
assertEquals('?', format.withQuoteChar('?').getQuoteChar().charValue());
72-
assertEquals(Quote.ALL, format.withQuotePolicy(Quote.ALL).getQuotePolicy());
73-
assertEquals('?', format.withCommentStart('?').getCommentStart().charValue());
74-
assertEquals("?", format.withRecordSeparator("?").getRecordSeparator());
75-
assertEquals('?', format.withEscape('?').getEscape().charValue());
76-
77-
assertFalse(format.withIgnoreSurroundingSpaces(false).getIgnoreSurroundingSpaces());
78-
assertFalse(format.withIgnoreEmptyLines(false).getIgnoreEmptyLines());
79-
}
80-
8142
@Test
8243
public void testFormat() {
8344
final CSVFormat format = CSVFormat.DEFAULT;
@@ -87,116 +48,6 @@ public void testFormat() {
8748
assertEquals("\"x,y\",z", format.format("x,y", "z"));
8849
}
8950

90-
@Test
91-
public void testValidation() {
92-
try {
93-
new CSVFormat('\n');
94-
fail();
95-
} catch (final IllegalArgumentException e) {
96-
// expected
97-
}
98-
99-
try {
100-
new CSVFormat('\r');
101-
fail();
102-
} catch (final IllegalArgumentException e) {
103-
// expected
104-
}
105-
106-
final CSVFormat format = CSVFormat.DEFAULT;
107-
108-
try {
109-
format.withDelimiter('\n');
110-
fail();
111-
} catch (final IllegalArgumentException e) {
112-
// expected
113-
}
114-
115-
try {
116-
format.withEscape('\r');
117-
fail();
118-
} catch (final IllegalArgumentException e) {
119-
// expected
120-
}
121-
122-
try {
123-
format.withQuoteChar('\n');
124-
fail();
125-
} catch (final IllegalArgumentException e) {
126-
// expected
127-
}
128-
129-
try {
130-
format.withCommentStart('\r');
131-
fail();
132-
} catch (final IllegalArgumentException e) {
133-
// expected
134-
}
135-
136-
try {
137-
format.withDelimiter('!').withEscape('!').validate();
138-
fail();
139-
} catch (final IllegalStateException e) {
140-
// expected
141-
}
142-
143-
try {
144-
format.withDelimiter('!').withCommentStart('!').validate();
145-
fail();
146-
} catch (final IllegalStateException e) {
147-
// expected
148-
}
149-
150-
try {
151-
format.withQuoteChar('!').withCommentStart('!').validate();
152-
fail();
153-
} catch (final IllegalStateException e) {
154-
// expected
155-
}
156-
157-
// Cannot assume that callers won't use different Character objects
158-
try {
159-
format.withQuoteChar(new Character('!')).withCommentStart('!').validate();
160-
fail();
161-
} catch (final IllegalStateException e) {
162-
// expected
163-
}
164-
165-
format.withQuoteChar(null).withCommentStart(null).validate();
166-
167-
try {
168-
format.withEscape('!').withCommentStart('!').validate();
169-
fail();
170-
} catch (final IllegalStateException e) {
171-
// expected
172-
}
173-
174-
// Cannot assume that callers won't use different Character objects
175-
try {
176-
format.withEscape(new Character('!')).withCommentStart(new Character('!')).validate();
177-
fail();
178-
} catch (final IllegalStateException e) {
179-
// expected
180-
}
181-
182-
format.withEscape(null).withCommentStart(null).validate();
183-
184-
185-
try {
186-
format.withQuoteChar('!').withDelimiter('!').validate();
187-
fail();
188-
} catch (final IllegalStateException e) {
189-
// expected
190-
}
191-
192-
try {
193-
format.withQuoteChar('!').withQuotePolicy(Quote.NONE).validate();
194-
fail();
195-
} catch (final IllegalStateException e) {
196-
// expected
197-
}
198-
}
199-
20051
@SuppressWarnings("boxing") // no need to worry about boxing here
20152
@Test
20253
public void testSerialization() throws Exception {

0 commit comments

Comments
 (0)