Skip to content

Commit f6f0832

Browse files
committed
Use Builder.
Don't declared unused exceptions in throw
1 parent 5f605e6 commit f6f0832

12 files changed

Lines changed: 166 additions & 106 deletions

src/test/java/org/apache/commons/csv/issues/JiraCsv148Test.java

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -25,30 +25,41 @@
2525
public class JiraCsv148Test {
2626

2727
/**
28-
* The difference between withTrim()and withIgnoreSurroundingSpace():
29-
* difference: withTrim() can remove the leading and trailing spaces and newlines in quotation marks,
30-
* while withIgnoreSurroundingSpace() cannot
31-
* The same point: you can remove the leading and trailing spaces,tabs and other symbols.
28+
* The difference between withTrim()and withIgnoreSurroundingSpace(): difference: withTrim() can remove the leading
29+
* and trailing spaces and newlines in quotation marks, while withIgnoreSurroundingSpace() cannot The same point:
30+
* you can remove the leading and trailing spaces,tabs and other symbols.
3231
*/
3332
@Test
34-
public void testWithTrimEmpty() throws Exception {
35-
final CSVFormat format = CSVFormat.DEFAULT.withQuoteMode(QuoteMode.ALL).withTrim();
36-
assertEquals("\"\",\"\",\"Single space on the left\",\"Single space on the right\"," +
37-
"\"Single spaces on both sides\",\"Multiple spaces on the left\"," +
38-
"\"Multiple spaces on the right\",\"Multiple spaces on both sides\"",
39-
format.format("", " ", " Single space on the left", "Single space on the right ",
40-
" Single spaces on both sides ", " Multiple spaces on the left",
41-
"Multiple spaces on the right ", " Multiple spaces on both sides "));
33+
public void testWithTrimEmpty() {
34+
// @formatter:off
35+
final CSVFormat format = CSVFormat.DEFAULT.builder()
36+
.setQuoteMode(QuoteMode.ALL)
37+
.setTrim(true)
38+
.build();
39+
// @formatter:on
40+
assertEquals(
41+
"\"\",\"\",\"Single space on the left\",\"Single space on the right\","
42+
+ "\"Single spaces on both sides\",\"Multiple spaces on the left\","
43+
+ "\"Multiple spaces on the right\",\"Multiple spaces on both sides\"",
44+
format.format("", " ", " Single space on the left", "Single space on the right ",
45+
" Single spaces on both sides ", " Multiple spaces on the left", "Multiple spaces on the right ",
46+
" Multiple spaces on both sides "));
4247
}
4348

4449
@Test
45-
public void testWithIgnoreSurroundingSpacesEmpty() throws Exception {
46-
final CSVFormat format = CSVFormat.DEFAULT.withQuoteMode(QuoteMode.ALL).withIgnoreSurroundingSpaces();
47-
assertEquals("\"\",\" \",\" Single space on the left\",\"Single space on the right \"," +
48-
"\" Single spaces on both sides \",\" Multiple spaces on the left\"," +
49-
"\"Multiple spaces on the right \",\" Multiple spaces on both sides \"",
50-
format.format("", " ", " Single space on the left", "Single space on the right ",
51-
" Single spaces on both sides ", " Multiple spaces on the left",
52-
"Multiple spaces on the right ", " Multiple spaces on both sides "));
50+
public void testWithIgnoreSurroundingSpacesEmpty() {
51+
// @formatter:off
52+
final CSVFormat format = CSVFormat.DEFAULT.builder()
53+
.setQuoteMode(QuoteMode.ALL)
54+
.setIgnoreSurroundingSpaces(true)
55+
.build();
56+
// @formatter:on
57+
assertEquals(
58+
"\"\",\" \",\" Single space on the left\",\"Single space on the right \","
59+
+ "\" Single spaces on both sides \",\" Multiple spaces on the left\","
60+
+ "\"Multiple spaces on the right \",\" Multiple spaces on both sides \"",
61+
format.format("", " ", " Single space on the left", "Single space on the right ",
62+
" Single spaces on both sides ", " Multiple spaces on the left", "Multiple spaces on the right ",
63+
" Multiple spaces on both sides "));
5364
}
5465
}

src/test/java/org/apache/commons/csv/issues/JiraCsv149Test.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,13 @@ private void testJiraCsv149EndWithEolAtEof(final boolean eolAtEof) throws IOExce
4141
source += CR_LF;
4242
}
4343
final StringReader records = new StringReader(source);
44-
final CSVFormat format = CSVFormat.RFC4180.withFirstRecordAsHeader().withQuote('"');
44+
// @formatter:off
45+
final CSVFormat format = CSVFormat.RFC4180.builder()
46+
.setHeader()
47+
.setSkipHeaderRecord(true)
48+
.setQuote('"')
49+
.build();
50+
// @formatter:on
4551
int lineCounter = 2;
4652
try (final CSVParser parser = new CSVParser(records, format)) {
4753
for (final CSVRecord record : parser) {

src/test/java/org/apache/commons/csv/issues/JiraCsv154Test.java

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,13 @@ public class JiraCsv154Test {
2929
@Test
3030
public void testJiraCsv154_withCommentMarker() throws IOException {
3131
final String comment = "This is a header comment";
32-
final CSVFormat format = CSVFormat.EXCEL.withHeader("H1", "H2").withCommentMarker('#')
33-
.withHeaderComments(comment);
32+
// @formatter:off
33+
final CSVFormat format = CSVFormat.EXCEL.builder()
34+
.setHeader("H1", "H2")
35+
.setCommentMarker('#')
36+
.setHeaderComments(comment)
37+
.build();
38+
// @formatter:on
3439
final StringBuilder out = new StringBuilder();
3540
try (final CSVPrinter printer = format.print(out)) {
3641
printer.print("A");
@@ -43,8 +48,13 @@ public void testJiraCsv154_withCommentMarker() throws IOException {
4348
@Test
4449
public void testJiraCsv154_withHeaderComments() throws IOException {
4550
final String comment = "This is a header comment";
46-
final CSVFormat format = CSVFormat.EXCEL.withHeader("H1", "H2").withHeaderComments(comment)
47-
.withCommentMarker('#');
51+
// @formatter:off
52+
final CSVFormat format = CSVFormat.EXCEL.builder()
53+
.setHeader("H1", "H2")
54+
.setHeaderComments(comment)
55+
.setCommentMarker('#')
56+
.build();
57+
// @formatter:on
4858
final StringBuilder out = new StringBuilder();
4959
try (final CSVPrinter printer = format.print(out)) {
5060
printer.print("A");

src/test/java/org/apache/commons/csv/issues/JiraCsv167Test.java

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -50,22 +50,23 @@ public void parse() throws IOException {
5050
}
5151
}
5252
}
53-
CSVFormat format = CSVFormat.DEFAULT;
54-
//
55-
format = format.withAllowMissingColumnNames(false);
56-
format = format.withCommentMarker('#');
57-
format = format.withDelimiter(',');
58-
format = format.withEscape('\\');
59-
format = format.withHeader("author", "title", "publishDate");
60-
format = format.withHeaderComments("headerComment");
61-
format = format.withNullString("NULL");
62-
format = format.withIgnoreEmptyLines(true);
63-
format = format.withIgnoreSurroundingSpaces(true);
64-
format = format.withQuote('"');
65-
format = format.withQuoteMode(QuoteMode.ALL);
66-
format = format.withRecordSeparator('\n');
67-
format = format.withSkipHeaderRecord(false);
68-
//
53+
CSVFormat format = CSVFormat.DEFAULT.builder()
54+
// @formatter:off
55+
.setAllowMissingColumnNames(false)
56+
.setCommentMarker('#')
57+
.setDelimiter(',')
58+
.setEscape('\\')
59+
.setHeader("author", "title", "publishDate")
60+
.setHeaderComments("headerComment")
61+
.setNullString("NULL")
62+
.setIgnoreEmptyLines(true)
63+
.setIgnoreSurroundingSpaces(true)
64+
.setQuote('"')
65+
.setQuoteMode(QuoteMode.ALL)
66+
.setRecordSeparator('\n')
67+
.setSkipHeaderRecord(false)
68+
.build();
69+
// @formatter:on
6970
int comments = 0;
7071
int records = 0;
7172
try (final CSVParser parser = format.parse(getTestInput())) {
@@ -82,6 +83,7 @@ public void parse() throws IOException {
8283
}
8384

8485
private Reader getTestInput() {
85-
return new InputStreamReader(ClassLoader.getSystemClassLoader().getResourceAsStream("org/apache/commons/csv/csv-167/sample1.csv"));
86+
return new InputStreamReader(
87+
ClassLoader.getSystemClassLoader().getResourceAsStream("org/apache/commons/csv/csv-167/sample1.csv"));
8688
}
8789
}

src/test/java/org/apache/commons/csv/issues/JiraCsv198Test.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,18 @@
3131

3232
public class JiraCsv198Test {
3333

34-
private static final CSVFormat CSV_FORMAT = CSVFormat.EXCEL.withDelimiter('^').withFirstRecordAsHeader();
34+
// @formatter:off
35+
private static final CSVFormat CSV_FORMAT = CSVFormat.EXCEL.builder()
36+
.setDelimiter('^')
37+
.setHeader()
38+
.setSkipHeaderRecord(true)
39+
.build();
40+
// @formatter:on
3541

3642
@Test
3743
public void test() throws UnsupportedEncodingException, IOException {
38-
final InputStream pointsOfReference = getClass().getResourceAsStream("/org/apache/commons/csv/CSV-198/optd_por_public.csv");
44+
final InputStream pointsOfReference = getClass()
45+
.getResourceAsStream("/org/apache/commons/csv/CSV-198/optd_por_public.csv");
3946
assertNotNull(pointsOfReference);
4047
try (@SuppressWarnings("resource")
4148
CSVParser parser = CSV_FORMAT.parse(new InputStreamReader(pointsOfReference, StandardCharsets.UTF_8))) {

src/test/java/org/apache/commons/csv/issues/JiraCsv203Test.java

Lines changed: 51 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,20 @@
2424
import org.junit.jupiter.api.Test;
2525

2626
/**
27-
* JIRA: <a href="https://issues.apache.org/jira/browse/CSV-203">withNullString value is printed without quotes when QuoteMode.ALL is specified</a>
27+
* JIRA: <a href="https://issues.apache.org/jira/browse/CSV-203">withNullString value is printed without quotes when
28+
* QuoteMode.ALL is specified</a>
2829
*/
2930
public class JiraCsv203Test {
3031

3132
@Test
3233
public void testQuoteModeAll() throws Exception {
33-
final CSVFormat format = CSVFormat.EXCEL
34-
.withNullString("N/A")
35-
.withIgnoreSurroundingSpaces(true)
36-
.withQuoteMode(QuoteMode.ALL);
37-
34+
// @formatter:off
35+
final CSVFormat format = CSVFormat.EXCEL.builder()
36+
.setNullString("N/A")
37+
.setIgnoreSurroundingSpaces(true)
38+
.setQuoteMode(QuoteMode.ALL)
39+
.build();
40+
// @formatter:on
3841
final StringBuffer buffer = new StringBuffer();
3942
try (final CSVPrinter printer = new CSVPrinter(buffer, format)) {
4043
printer.printRecord(null, "Hello", null, "World");
@@ -44,11 +47,13 @@ public void testQuoteModeAll() throws Exception {
4447

4548
@Test
4649
public void testQuoteModeAllNonNull() throws Exception {
47-
final CSVFormat format = CSVFormat.EXCEL
48-
.withNullString("N/A")
49-
.withIgnoreSurroundingSpaces(true)
50-
.withQuoteMode(QuoteMode.ALL_NON_NULL);
51-
50+
// @formatter:off
51+
final CSVFormat format = CSVFormat.EXCEL.builder()
52+
.setNullString("N/A")
53+
.setIgnoreSurroundingSpaces(true)
54+
.setQuoteMode(QuoteMode.ALL_NON_NULL)
55+
.build();
56+
// @formatter:on
5257
final StringBuffer buffer = new StringBuffer();
5358
try (final CSVPrinter printer = new CSVPrinter(buffer, format)) {
5459
printer.printRecord(null, "Hello", null, "World");
@@ -58,10 +63,12 @@ public void testQuoteModeAllNonNull() throws Exception {
5863

5964
@Test
6065
public void testWithoutQuoteMode() throws Exception {
61-
final CSVFormat format = CSVFormat.EXCEL
62-
.withNullString("N/A")
63-
.withIgnoreSurroundingSpaces(true);
64-
66+
// @formatter:off
67+
final CSVFormat format = CSVFormat.EXCEL.builder()
68+
.setNullString("N/A")
69+
.setIgnoreSurroundingSpaces(true)
70+
.build();
71+
// @formatter:on
6572
final StringBuffer buffer = new StringBuffer();
6673
try (final CSVPrinter printer = new CSVPrinter(buffer, format)) {
6774
printer.printRecord(null, "Hello", null, "World");
@@ -71,11 +78,13 @@ public void testWithoutQuoteMode() throws Exception {
7178

7279
@Test
7380
public void testQuoteModeMinimal() throws Exception {
74-
final CSVFormat format = CSVFormat.EXCEL
75-
.withNullString("N/A")
76-
.withIgnoreSurroundingSpaces(true)
77-
.withQuoteMode(QuoteMode.MINIMAL);
78-
81+
// @formatter:off
82+
final CSVFormat format = CSVFormat.EXCEL.builder()
83+
.setNullString("N/A")
84+
.setIgnoreSurroundingSpaces(true)
85+
.setQuoteMode(QuoteMode.MINIMAL)
86+
.build();
87+
// @formatter:on
7988
final StringBuffer buffer = new StringBuffer();
8089
try (final CSVPrinter printer = new CSVPrinter(buffer, format)) {
8190
printer.printRecord(null, "Hello", null, "World");
@@ -85,11 +94,13 @@ public void testQuoteModeMinimal() throws Exception {
8594

8695
@Test
8796
public void testQuoteModeNonNumeric() throws Exception {
88-
final CSVFormat format = CSVFormat.EXCEL
89-
.withNullString("N/A")
90-
.withIgnoreSurroundingSpaces(true)
91-
.withQuoteMode(QuoteMode.NON_NUMERIC);
92-
97+
// @formatter:off
98+
final CSVFormat format = CSVFormat.EXCEL.builder()
99+
.setNullString("N/A")
100+
.setIgnoreSurroundingSpaces(true)
101+
.setQuoteMode(QuoteMode.NON_NUMERIC)
102+
.build();
103+
// @formatter:on
93104
final StringBuffer buffer = new StringBuffer();
94105
try (final CSVPrinter printer = new CSVPrinter(buffer, format)) {
95106
printer.printRecord(null, "Hello", null, "World");
@@ -99,11 +110,13 @@ public void testQuoteModeNonNumeric() throws Exception {
99110

100111
@Test
101112
public void testWithoutNullString() throws Exception {
102-
final CSVFormat format = CSVFormat.EXCEL
103-
//.withNullString("N/A")
104-
.withIgnoreSurroundingSpaces(true)
105-
.withQuoteMode(QuoteMode.ALL);
106-
113+
// @formatter:off
114+
final CSVFormat format = CSVFormat.EXCEL.builder()
115+
//.setNullString("N/A")
116+
.setIgnoreSurroundingSpaces(true)
117+
.setQuoteMode(QuoteMode.ALL)
118+
.build();
119+
// @formatter:on
107120
final StringBuffer buffer = new StringBuffer();
108121
try (final CSVPrinter printer = new CSVPrinter(buffer, format)) {
109122
printer.printRecord(null, "Hello", null, "World");
@@ -113,15 +126,17 @@ public void testWithoutNullString() throws Exception {
113126

114127
@Test
115128
public void testWithEmptyValues() throws Exception {
116-
final CSVFormat format = CSVFormat.EXCEL
117-
.withNullString("N/A")
118-
.withIgnoreSurroundingSpaces(true)
119-
.withQuoteMode(QuoteMode.ALL);
120-
129+
// @formatter:off
130+
final CSVFormat format = CSVFormat.EXCEL.builder()
131+
.setNullString("N/A")
132+
.setIgnoreSurroundingSpaces(true)
133+
.setQuoteMode(QuoteMode.ALL)
134+
.build();
135+
// @formatter:on
121136
final StringBuffer buffer = new StringBuffer();
122137
try (final CSVPrinter printer = new CSVPrinter(buffer, format)) {
123138
printer.printRecord("", "Hello", "", "World");
124-
//printer.printRecord(new Object[] { null, "Hello", null, "World" });
139+
// printer.printRecord(new Object[] { null, "Hello", null, "World" });
125140
}
126141
assertEquals("\"\",\"Hello\",\"\",\"World\"\r\n", buffer.toString());
127142
}

src/test/java/org/apache/commons/csv/issues/JiraCsv206Test.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,15 @@ record = iterator.next();
4949
assertEquals("123 Main St.", record.get(2));
5050
}
5151
// Write with multiple character delimiter
52-
final String outString = "# Change delimiter to [I]\r\n" + "first name[I]last name[I]address\r\n" + "John[I]Smith[I]123 Main St.";
52+
final String outString = "# Change delimiter to [I]\r\n" + "first name[I]last name[I]address\r\n"
53+
+ "John[I]Smith[I]123 Main St.";
5354
final String comment = "Change delimiter to [I]";
5455
// @formatter:off
5556
final CSVFormat format = CSVFormat.EXCEL.builder()
5657
.setDelimiter("[I]").setHeader("first name", "last name", "address")
5758
.setCommentMarker('#')
5859
.setHeaderComments(comment).build();
59-
// @formatter:off
60+
// @formatter:on
6061
final StringBuilder out = new StringBuilder();
6162
try (final CSVPrinter printer = format.print(out)) {
6263
printer.print(record.get(0));

src/test/java/org/apache/commons/csv/issues/JiraCsv211Test.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,19 +30,26 @@ public class JiraCsv211Test {
3030

3131
@Test
3232
public void testJiraCsv211Format() throws IOException {
33-
final String[] values = { "1", "Jane Doe", "USA", "" };
33+
final String[] values = {"1", "Jane Doe", "USA", ""};
3434

35-
final CSVFormat printFormat = CSVFormat.DEFAULT.withDelimiter('\t').withHeader("ID", "Name", "Country", "Age");
35+
// @formatter:off
36+
final CSVFormat printFormat = CSVFormat.DEFAULT.builder()
37+
.setDelimiter('\t')
38+
.setHeader("ID", "Name", "Country", "Age")
39+
.build();
40+
// @formatter:on
3641
final String formatted = printFormat.format(values);
3742
assertEquals("ID\tName\tCountry\tAge\r\n1\tJane Doe\tUSA\t", formatted);
3843

39-
final CSVFormat parseFormat = CSVFormat.DEFAULT.withDelimiter('\t').withFirstRecordAsHeader();
44+
final CSVFormat parseFormat = CSVFormat.DEFAULT.builder().setDelimiter('\t').setHeader()
45+
.setSkipHeaderRecord(true).build();
4046
try (final CSVParser parser = parseFormat.parse(new StringReader(formatted))) {
4147
for (final CSVRecord record : parser) {
4248
assertEquals("1", record.get(0));
4349
assertEquals("Jane Doe", record.get(1));
4450
assertEquals("USA", record.get(2));
4551
assertEquals("", record.get(3));
4652
}
47-
}}
53+
}
54+
}
4855
}

src/test/java/org/apache/commons/csv/issues/JiraCsv213Test.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,16 @@ public class JiraCsv213Test {
4141

4242
private void createEndChannel(final File csvFile) {
4343
// @formatter:off
44-
final CSVFormat csvFormat =
45-
CSVFormat.DEFAULT
46-
.withDelimiter(';')
47-
.withFirstRecordAsHeader()
48-
.withRecordSeparator('\n')
49-
.withQuoteMode(QuoteMode.ALL);
44+
final CSVFormat csvFormat = CSVFormat.DEFAULT.builder()
45+
.setDelimiter(';')
46+
.setHeader()
47+
.setSkipHeaderRecord(true)
48+
.setRecordSeparator('\n')
49+
.setQuoteMode(QuoteMode.ALL)
50+
.build();
5051
// @formatter:on
5152
try (CSVParser parser = csvFormat
52-
.parse(new InputStreamReader(new FileInputStream(csvFile), StandardCharsets.UTF_8))) {
53+
.parse(new InputStreamReader(new FileInputStream(csvFile), StandardCharsets.UTF_8))) {
5354
if (parser.iterator().hasNext()) {
5455
// System.out.println(parser.getCurrentLineNumber());
5556
// System.out.println(parser.getRecordNumber());

0 commit comments

Comments
 (0)