Skip to content
Permalink
Browse files
Use Builder.
Don't declared unused exceptions in throw
  • Loading branch information
garydgregory committed Jul 9, 2021
1 parent 5f605e6 commit f6f08321f67e115045149365338a064528438f99
Show file tree
Hide file tree
Showing 12 changed files with 166 additions and 106 deletions.
@@ -25,30 +25,41 @@
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 "));
}
}
@@ -41,7 +41,13 @@ private void testJiraCsv149EndWithEolAtEof(final boolean eolAtEof) throws IOExce
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) {
@@ -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 void testJiraCsv154_withCommentMarker() throws IOException {
@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");
@@ -50,22 +50,23 @@ public void parse() throws IOException {
}
}
}
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 void parse() throws IOException {
}

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"));
}
}
@@ -31,11 +31,18 @@

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))) {
@@ -24,17 +24,20 @@
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 void testQuoteModeAll() throws Exception {

@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 void testQuoteModeAllNonNull() throws Exception {

@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 void testWithoutQuoteMode() throws Exception {

@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 void testQuoteModeMinimal() throws Exception {

@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 void testQuoteModeNonNumeric() throws Exception {

@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 void testWithoutNullString() throws Exception {

@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());
}
@@ -49,14 +49,15 @@ public void testJiraCsv206MultipleCharacterDelimiter() throws IOException {
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));
@@ -30,19 +30,26 @@ 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));
assertEquals("Jane Doe", record.get(1));
assertEquals("USA", record.get(2));
assertEquals("", record.get(3));
}
}}
}
}
}
@@ -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());

0 comments on commit f6f0832

Please sign in to comment.