Use Builder.

Don't declared unused exceptions in throw
This commit is contained in:
Gary Gregory 2021-07-09 11:18:28 -04:00
parent 5f605e613a
commit f6f08321f6
12 changed files with 166 additions and 106 deletions

View File

@ -25,30 +25,41 @@ import org.junit.jupiter.api.Test;
public class JiraCsv148Test {
/**
* The difference between withTrim()and withIgnoreSurroundingSpace()
* difference: withTrim() can remove the leading and trailing spaces and newlines in quotation marks,
* while withIgnoreSurroundingSpace() cannot
* The same point: you can remove the leading and trailing spaces,tabs and other symbols.
* The difference between withTrim()and withIgnoreSurroundingSpace() difference: withTrim() can remove the leading
* and trailing spaces and newlines in quotation marks, while withIgnoreSurroundingSpace() cannot The same point:
* you can remove the leading and trailing spaces,tabs and other symbols.
*/
@Test
public void testWithTrimEmpty() throws Exception {
final CSVFormat format = CSVFormat.DEFAULT.withQuoteMode(QuoteMode.ALL).withTrim();
assertEquals("\"\",\"\",\"Single space on the left\",\"Single space on the right\"," +
"\"Single spaces on both sides\",\"Multiple spaces on the left\"," +
"\"Multiple spaces on the right\",\"Multiple spaces on both sides\"",
format.format("", " ", " Single space on the left", "Single space on the right ",
" Single spaces on both sides ", " Multiple spaces on the left",
"Multiple spaces on the right ", " Multiple spaces on both sides "));
public void testWithTrimEmpty() {
// @formatter:off
final CSVFormat format = CSVFormat.DEFAULT.builder()
.setQuoteMode(QuoteMode.ALL)
.setTrim(true)
.build();
// @formatter:on
assertEquals(
"\"\",\"\",\"Single space on the left\",\"Single space on the right\","
+ "\"Single spaces on both sides\",\"Multiple spaces on the left\","
+ "\"Multiple spaces on the right\",\"Multiple spaces on both sides\"",
format.format("", " ", " Single space on the left", "Single space on the right ",
" Single spaces on both sides ", " Multiple spaces on the left", "Multiple spaces on the right ",
" Multiple spaces on both sides "));
}
@Test
public void testWithIgnoreSurroundingSpacesEmpty() throws Exception {
final CSVFormat format = CSVFormat.DEFAULT.withQuoteMode(QuoteMode.ALL).withIgnoreSurroundingSpaces();
assertEquals("\"\",\" \",\" Single space on the left\",\"Single space on the right \"," +
"\" Single spaces on both sides \",\" Multiple spaces on the left\"," +
"\"Multiple spaces on the right \",\" Multiple spaces on both sides \"",
format.format("", " ", " Single space on the left", "Single space on the right ",
" Single spaces on both sides ", " Multiple spaces on the left",
"Multiple spaces on the right ", " Multiple spaces on both sides "));
public void testWithIgnoreSurroundingSpacesEmpty() {
// @formatter:off
final CSVFormat format = CSVFormat.DEFAULT.builder()
.setQuoteMode(QuoteMode.ALL)
.setIgnoreSurroundingSpaces(true)
.build();
// @formatter:on
assertEquals(
"\"\",\" \",\" Single space on the left\",\"Single space on the right \","
+ "\" Single spaces on both sides \",\" Multiple spaces on the left\","
+ "\"Multiple spaces on the right \",\" Multiple spaces on both sides \"",
format.format("", " ", " Single space on the left", "Single space on the right ",
" Single spaces on both sides ", " Multiple spaces on the left", "Multiple spaces on the right ",
" Multiple spaces on both sides "));
}
}

View File

@ -41,7 +41,13 @@ public class JiraCsv149Test {
source += CR_LF;
}
final StringReader records = new StringReader(source);
final CSVFormat format = CSVFormat.RFC4180.withFirstRecordAsHeader().withQuote('"');
// @formatter:off
final CSVFormat format = CSVFormat.RFC4180.builder()
.setHeader()
.setSkipHeaderRecord(true)
.setQuote('"')
.build();
// @formatter:on
int lineCounter = 2;
try (final CSVParser parser = new CSVParser(records, format)) {
for (final CSVRecord record : parser) {

View File

@ -29,8 +29,13 @@ public class JiraCsv154Test {
@Test
public void testJiraCsv154_withCommentMarker() throws IOException {
final String comment = "This is a header comment";
final CSVFormat format = CSVFormat.EXCEL.withHeader("H1", "H2").withCommentMarker('#')
.withHeaderComments(comment);
// @formatter:off
final CSVFormat format = CSVFormat.EXCEL.builder()
.setHeader("H1", "H2")
.setCommentMarker('#')
.setHeaderComments(comment)
.build();
// @formatter:on
final StringBuilder out = new StringBuilder();
try (final CSVPrinter printer = format.print(out)) {
printer.print("A");
@ -43,8 +48,13 @@ public class JiraCsv154Test {
@Test
public void testJiraCsv154_withHeaderComments() throws IOException {
final String comment = "This is a header comment";
final CSVFormat format = CSVFormat.EXCEL.withHeader("H1", "H2").withHeaderComments(comment)
.withCommentMarker('#');
// @formatter:off
final CSVFormat format = CSVFormat.EXCEL.builder()
.setHeader("H1", "H2")
.setHeaderComments(comment)
.setCommentMarker('#')
.build();
// @formatter:on
final StringBuilder out = new StringBuilder();
try (final CSVPrinter printer = format.print(out)) {
printer.print("A");

View File

@ -50,22 +50,23 @@ public class JiraCsv167Test {
}
}
}
CSVFormat format = CSVFormat.DEFAULT;
//
format = format.withAllowMissingColumnNames(false);
format = format.withCommentMarker('#');
format = format.withDelimiter(',');
format = format.withEscape('\\');
format = format.withHeader("author", "title", "publishDate");
format = format.withHeaderComments("headerComment");
format = format.withNullString("NULL");
format = format.withIgnoreEmptyLines(true);
format = format.withIgnoreSurroundingSpaces(true);
format = format.withQuote('"');
format = format.withQuoteMode(QuoteMode.ALL);
format = format.withRecordSeparator('\n');
format = format.withSkipHeaderRecord(false);
//
CSVFormat format = CSVFormat.DEFAULT.builder()
// @formatter:off
.setAllowMissingColumnNames(false)
.setCommentMarker('#')
.setDelimiter(',')
.setEscape('\\')
.setHeader("author", "title", "publishDate")
.setHeaderComments("headerComment")
.setNullString("NULL")
.setIgnoreEmptyLines(true)
.setIgnoreSurroundingSpaces(true)
.setQuote('"')
.setQuoteMode(QuoteMode.ALL)
.setRecordSeparator('\n')
.setSkipHeaderRecord(false)
.build();
// @formatter:on
int comments = 0;
int records = 0;
try (final CSVParser parser = format.parse(getTestInput())) {
@ -82,6 +83,7 @@ public class JiraCsv167Test {
}
private Reader getTestInput() {
return new InputStreamReader(ClassLoader.getSystemClassLoader().getResourceAsStream("org/apache/commons/csv/csv-167/sample1.csv"));
return new InputStreamReader(
ClassLoader.getSystemClassLoader().getResourceAsStream("org/apache/commons/csv/csv-167/sample1.csv"));
}
}

View File

@ -31,11 +31,18 @@ import org.junit.jupiter.api.Test;
public class JiraCsv198Test {
private static final CSVFormat CSV_FORMAT = CSVFormat.EXCEL.withDelimiter('^').withFirstRecordAsHeader();
// @formatter:off
private static final CSVFormat CSV_FORMAT = CSVFormat.EXCEL.builder()
.setDelimiter('^')
.setHeader()
.setSkipHeaderRecord(true)
.build();
// @formatter:on
@Test
public void test() throws UnsupportedEncodingException, IOException {
final InputStream pointsOfReference = getClass().getResourceAsStream("/org/apache/commons/csv/CSV-198/optd_por_public.csv");
final InputStream pointsOfReference = getClass()
.getResourceAsStream("/org/apache/commons/csv/CSV-198/optd_por_public.csv");
assertNotNull(pointsOfReference);
try (@SuppressWarnings("resource")
CSVParser parser = CSV_FORMAT.parse(new InputStreamReader(pointsOfReference, StandardCharsets.UTF_8))) {

View File

@ -24,17 +24,20 @@ import org.apache.commons.csv.QuoteMode;
import org.junit.jupiter.api.Test;
/**
* JIRA: <a href="https://issues.apache.org/jira/browse/CSV-203">withNullString value is printed without quotes when QuoteMode.ALL is specified</a>
* JIRA: <a href="https://issues.apache.org/jira/browse/CSV-203">withNullString value is printed without quotes when
* QuoteMode.ALL is specified</a>
*/
public class JiraCsv203Test {
@Test
public void testQuoteModeAll() throws Exception {
final CSVFormat format = CSVFormat.EXCEL
.withNullString("N/A")
.withIgnoreSurroundingSpaces(true)
.withQuoteMode(QuoteMode.ALL);
// @formatter:off
final CSVFormat format = CSVFormat.EXCEL.builder()
.setNullString("N/A")
.setIgnoreSurroundingSpaces(true)
.setQuoteMode(QuoteMode.ALL)
.build();
// @formatter:on
final StringBuffer buffer = new StringBuffer();
try (final CSVPrinter printer = new CSVPrinter(buffer, format)) {
printer.printRecord(null, "Hello", null, "World");
@ -44,11 +47,13 @@ public class JiraCsv203Test {
@Test
public void testQuoteModeAllNonNull() throws Exception {
final CSVFormat format = CSVFormat.EXCEL
.withNullString("N/A")
.withIgnoreSurroundingSpaces(true)
.withQuoteMode(QuoteMode.ALL_NON_NULL);
// @formatter:off
final CSVFormat format = CSVFormat.EXCEL.builder()
.setNullString("N/A")
.setIgnoreSurroundingSpaces(true)
.setQuoteMode(QuoteMode.ALL_NON_NULL)
.build();
// @formatter:on
final StringBuffer buffer = new StringBuffer();
try (final CSVPrinter printer = new CSVPrinter(buffer, format)) {
printer.printRecord(null, "Hello", null, "World");
@ -58,10 +63,12 @@ public class JiraCsv203Test {
@Test
public void testWithoutQuoteMode() throws Exception {
final CSVFormat format = CSVFormat.EXCEL
.withNullString("N/A")
.withIgnoreSurroundingSpaces(true);
// @formatter:off
final CSVFormat format = CSVFormat.EXCEL.builder()
.setNullString("N/A")
.setIgnoreSurroundingSpaces(true)
.build();
// @formatter:on
final StringBuffer buffer = new StringBuffer();
try (final CSVPrinter printer = new CSVPrinter(buffer, format)) {
printer.printRecord(null, "Hello", null, "World");
@ -71,11 +78,13 @@ public class JiraCsv203Test {
@Test
public void testQuoteModeMinimal() throws Exception {
final CSVFormat format = CSVFormat.EXCEL
.withNullString("N/A")
.withIgnoreSurroundingSpaces(true)
.withQuoteMode(QuoteMode.MINIMAL);
// @formatter:off
final CSVFormat format = CSVFormat.EXCEL.builder()
.setNullString("N/A")
.setIgnoreSurroundingSpaces(true)
.setQuoteMode(QuoteMode.MINIMAL)
.build();
// @formatter:on
final StringBuffer buffer = new StringBuffer();
try (final CSVPrinter printer = new CSVPrinter(buffer, format)) {
printer.printRecord(null, "Hello", null, "World");
@ -85,11 +94,13 @@ public class JiraCsv203Test {
@Test
public void testQuoteModeNonNumeric() throws Exception {
final CSVFormat format = CSVFormat.EXCEL
.withNullString("N/A")
.withIgnoreSurroundingSpaces(true)
.withQuoteMode(QuoteMode.NON_NUMERIC);
// @formatter:off
final CSVFormat format = CSVFormat.EXCEL.builder()
.setNullString("N/A")
.setIgnoreSurroundingSpaces(true)
.setQuoteMode(QuoteMode.NON_NUMERIC)
.build();
// @formatter:on
final StringBuffer buffer = new StringBuffer();
try (final CSVPrinter printer = new CSVPrinter(buffer, format)) {
printer.printRecord(null, "Hello", null, "World");
@ -99,11 +110,13 @@ public class JiraCsv203Test {
@Test
public void testWithoutNullString() throws Exception {
final CSVFormat format = CSVFormat.EXCEL
//.withNullString("N/A")
.withIgnoreSurroundingSpaces(true)
.withQuoteMode(QuoteMode.ALL);
// @formatter:off
final CSVFormat format = CSVFormat.EXCEL.builder()
//.setNullString("N/A")
.setIgnoreSurroundingSpaces(true)
.setQuoteMode(QuoteMode.ALL)
.build();
// @formatter:on
final StringBuffer buffer = new StringBuffer();
try (final CSVPrinter printer = new CSVPrinter(buffer, format)) {
printer.printRecord(null, "Hello", null, "World");
@ -113,15 +126,17 @@ public class JiraCsv203Test {
@Test
public void testWithEmptyValues() throws Exception {
final CSVFormat format = CSVFormat.EXCEL
.withNullString("N/A")
.withIgnoreSurroundingSpaces(true)
.withQuoteMode(QuoteMode.ALL);
// @formatter:off
final CSVFormat format = CSVFormat.EXCEL.builder()
.setNullString("N/A")
.setIgnoreSurroundingSpaces(true)
.setQuoteMode(QuoteMode.ALL)
.build();
// @formatter:on
final StringBuffer buffer = new StringBuffer();
try (final CSVPrinter printer = new CSVPrinter(buffer, format)) {
printer.printRecord("", "Hello", "", "World");
//printer.printRecord(new Object[] { null, "Hello", null, "World" });
// printer.printRecord(new Object[] { null, "Hello", null, "World" });
}
assertEquals("\"\",\"Hello\",\"\",\"World\"\r\n", buffer.toString());
}

View File

@ -49,14 +49,15 @@ public class JiraCsv206Test {
assertEquals("123 Main St.", record.get(2));
}
// Write with multiple character delimiter
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.";
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.";
final String comment = "Change delimiter to [I]";
// @formatter:off
final CSVFormat format = CSVFormat.EXCEL.builder()
.setDelimiter("[I]").setHeader("first name", "last name", "address")
.setCommentMarker('#')
.setHeaderComments(comment).build();
// @formatter:off
// @formatter:on
final StringBuilder out = new StringBuilder();
try (final CSVPrinter printer = format.print(out)) {
printer.print(record.get(0));

View File

@ -30,13 +30,19 @@ public class JiraCsv211Test {
@Test
public void testJiraCsv211Format() throws IOException {
final String[] values = { "1", "Jane Doe", "USA", "" };
final String[] values = {"1", "Jane Doe", "USA", ""};
final CSVFormat printFormat = CSVFormat.DEFAULT.withDelimiter('\t').withHeader("ID", "Name", "Country", "Age");
// @formatter:off
final CSVFormat printFormat = CSVFormat.DEFAULT.builder()
.setDelimiter('\t')
.setHeader("ID", "Name", "Country", "Age")
.build();
// @formatter:on
final String formatted = printFormat.format(values);
assertEquals("ID\tName\tCountry\tAge\r\n1\tJane Doe\tUSA\t", formatted);
final CSVFormat parseFormat = CSVFormat.DEFAULT.withDelimiter('\t').withFirstRecordAsHeader();
final CSVFormat parseFormat = CSVFormat.DEFAULT.builder().setDelimiter('\t').setHeader()
.setSkipHeaderRecord(true).build();
try (final CSVParser parser = parseFormat.parse(new StringReader(formatted))) {
for (final CSVRecord record : parser) {
assertEquals("1", record.get(0));
@ -44,5 +50,6 @@ public class JiraCsv211Test {
assertEquals("USA", record.get(2));
assertEquals("", record.get(3));
}
}}
}
}
}

View File

@ -41,15 +41,16 @@ public class JiraCsv213Test {
private void createEndChannel(final File csvFile) {
// @formatter:off
final CSVFormat csvFormat =
CSVFormat.DEFAULT
.withDelimiter(';')
.withFirstRecordAsHeader()
.withRecordSeparator('\n')
.withQuoteMode(QuoteMode.ALL);
final CSVFormat csvFormat = CSVFormat.DEFAULT.builder()
.setDelimiter(';')
.setHeader()
.setSkipHeaderRecord(true)
.setRecordSeparator('\n')
.setQuoteMode(QuoteMode.ALL)
.build();
// @formatter:on
try (CSVParser parser = csvFormat
.parse(new InputStreamReader(new FileInputStream(csvFile), StandardCharsets.UTF_8))) {
.parse(new InputStreamReader(new FileInputStream(csvFile), StandardCharsets.UTF_8))) {
if (parser.iterator().hasNext()) {
// System.out.println(parser.getCurrentLineNumber());
// System.out.println(parser.getRecordNumber());

View File

@ -36,10 +36,9 @@ public class JiraCsv247Test {
@Test
public void testHeadersMissingOneColumnWhenAllowingMissingColumnNames() throws Exception {
final CSVFormat format = CSVFormat.DEFAULT.withHeader().withAllowMissingColumnNames(true);
final CSVFormat format = CSVFormat.DEFAULT.builder().setHeader().setAllowMissingColumnNames(true).build();
assertTrue(format.getAllowMissingColumnNames(),
"We should allow missing column names");
assertTrue(format.getAllowMissingColumnNames(), "We should allow missing column names");
final Reader in = new StringReader("a,,c,d,e\n1,2,3,4,5\nv,w,x,y,z");
try (final CSVParser parser = format.parse(in)) {
@ -62,8 +61,8 @@ public class JiraCsv247Test {
}
@Test
public void testHeadersMissingThrowsWhenNotAllowingMissingColumnNames() throws Exception {
final CSVFormat format = CSVFormat.DEFAULT.withHeader();
public void testHeadersMissingThrowsWhenNotAllowingMissingColumnNames() {
final CSVFormat format = CSVFormat.DEFAULT.builder().setHeader().build();
assertFalse(format.getAllowMissingColumnNames(), "By default we should not allow missing column names");

View File

@ -33,8 +33,9 @@ public class JiraCsv248Test {
/**
* Test deserialisation of a CSVRecord created using version 1.6.
*
* <p>This test asserts that serialization from 1.8 onwards is consistent with
* previous versions. Serialization was broken in version 1.7.
* <p>
* This test asserts that serialization from 1.8 onwards is consistent with previous versions. Serialization was
* broken in version 1.7.
*
* @throws IOException Signals that an I/O exception has occurred.
* @throws ClassNotFoundException If the CSVRecord cannot be deserialized
@ -42,11 +43,11 @@ public class JiraCsv248Test {
@Test
public void testJiraCsv248() throws IOException, ClassNotFoundException {
// Record was originally created using CSV version 1.6 with the following code:
//try (final CSVParser parser = CSVParser.parse("A,B\n#my comment\nOne,Two", CSVFormat.DEFAULT.withHeader().withCommentMarker('#'))) {
// CSVRecord rec = parser.iterator().next();
//}
try (InputStream in = getTestInput();
final ObjectInputStream ois = new ObjectInputStream(in)) {
// try (final CSVParser parser = CSVParser.parse("A,B\n#my comment\nOne,Two",
// CSVFormat.DEFAULT.builder().setHeader().setCommentMarker('#'))) {
// CSVRecord rec = parser.iterator().next();
// }
try (InputStream in = getTestInput(); final ObjectInputStream ois = new ObjectInputStream(in)) {
final Object object = ois.readObject();
assertTrue(object instanceof CSVRecord);
final CSVRecord rec = (CSVRecord) object;

View File

@ -34,7 +34,7 @@ public class JiraCsv249Test {
@Test
public void testJiraCsv249() throws IOException {
final CSVFormat csvFormat = CSVFormat.DEFAULT.withEscape('\\');
final CSVFormat csvFormat = CSVFormat.DEFAULT.builder().setEscape('\\').build();
final StringWriter stringWriter = new StringWriter();
try (CSVPrinter printer = new CSVPrinter(stringWriter, csvFormat)) {
printer.printRecord("foo \\", "bar");