parent
5f605e613a
commit
f6f08321f6
|
@ -25,30 +25,41 @@ import org.junit.jupiter.api.Test;
|
||||||
public class JiraCsv148Test {
|
public class JiraCsv148Test {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The difference between withTrim()and withIgnoreSurroundingSpace():
|
* The difference between withTrim()and withIgnoreSurroundingSpace(): difference: withTrim() can remove the leading
|
||||||
* difference: withTrim() can remove the leading and trailing spaces and newlines in quotation marks,
|
* and trailing spaces and newlines in quotation marks, while withIgnoreSurroundingSpace() cannot The same point:
|
||||||
* while withIgnoreSurroundingSpace() cannot
|
* you can remove the leading and trailing spaces,tabs and other symbols.
|
||||||
* The same point: you can remove the leading and trailing spaces,tabs and other symbols.
|
|
||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
public void testWithTrimEmpty() throws Exception {
|
public void testWithTrimEmpty() {
|
||||||
final CSVFormat format = CSVFormat.DEFAULT.withQuoteMode(QuoteMode.ALL).withTrim();
|
// @formatter:off
|
||||||
assertEquals("\"\",\"\",\"Single space on the left\",\"Single space on the right\"," +
|
final CSVFormat format = CSVFormat.DEFAULT.builder()
|
||||||
"\"Single spaces on both sides\",\"Multiple spaces on the left\"," +
|
.setQuoteMode(QuoteMode.ALL)
|
||||||
"\"Multiple spaces on the right\",\"Multiple spaces on both sides\"",
|
.setTrim(true)
|
||||||
format.format("", " ", " Single space on the left", "Single space on the right ",
|
.build();
|
||||||
" Single spaces on both sides ", " Multiple spaces on the left",
|
// @formatter:on
|
||||||
"Multiple spaces on the right ", " Multiple spaces on both sides "));
|
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
|
@Test
|
||||||
public void testWithIgnoreSurroundingSpacesEmpty() throws Exception {
|
public void testWithIgnoreSurroundingSpacesEmpty() {
|
||||||
final CSVFormat format = CSVFormat.DEFAULT.withQuoteMode(QuoteMode.ALL).withIgnoreSurroundingSpaces();
|
// @formatter:off
|
||||||
assertEquals("\"\",\" \",\" Single space on the left\",\"Single space on the right \"," +
|
final CSVFormat format = CSVFormat.DEFAULT.builder()
|
||||||
"\" Single spaces on both sides \",\" Multiple spaces on the left\"," +
|
.setQuoteMode(QuoteMode.ALL)
|
||||||
"\"Multiple spaces on the right \",\" Multiple spaces on both sides \"",
|
.setIgnoreSurroundingSpaces(true)
|
||||||
format.format("", " ", " Single space on the left", "Single space on the right ",
|
.build();
|
||||||
" Single spaces on both sides ", " Multiple spaces on the left",
|
// @formatter:on
|
||||||
"Multiple spaces on the right ", " Multiple spaces on both sides "));
|
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 @@ public class JiraCsv149Test {
|
||||||
source += CR_LF;
|
source += CR_LF;
|
||||||
}
|
}
|
||||||
final StringReader records = new StringReader(source);
|
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;
|
int lineCounter = 2;
|
||||||
try (final CSVParser parser = new CSVParser(records, format)) {
|
try (final CSVParser parser = new CSVParser(records, format)) {
|
||||||
for (final CSVRecord record : parser) {
|
for (final CSVRecord record : parser) {
|
||||||
|
|
|
@ -29,8 +29,13 @@ public class JiraCsv154Test {
|
||||||
@Test
|
@Test
|
||||||
public void testJiraCsv154_withCommentMarker() throws IOException {
|
public void testJiraCsv154_withCommentMarker() throws IOException {
|
||||||
final String comment = "This is a header comment";
|
final String comment = "This is a header comment";
|
||||||
final CSVFormat format = CSVFormat.EXCEL.withHeader("H1", "H2").withCommentMarker('#')
|
// @formatter:off
|
||||||
.withHeaderComments(comment);
|
final CSVFormat format = CSVFormat.EXCEL.builder()
|
||||||
|
.setHeader("H1", "H2")
|
||||||
|
.setCommentMarker('#')
|
||||||
|
.setHeaderComments(comment)
|
||||||
|
.build();
|
||||||
|
// @formatter:on
|
||||||
final StringBuilder out = new StringBuilder();
|
final StringBuilder out = new StringBuilder();
|
||||||
try (final CSVPrinter printer = format.print(out)) {
|
try (final CSVPrinter printer = format.print(out)) {
|
||||||
printer.print("A");
|
printer.print("A");
|
||||||
|
@ -43,8 +48,13 @@ public class JiraCsv154Test {
|
||||||
@Test
|
@Test
|
||||||
public void testJiraCsv154_withHeaderComments() throws IOException {
|
public void testJiraCsv154_withHeaderComments() throws IOException {
|
||||||
final String comment = "This is a header comment";
|
final String comment = "This is a header comment";
|
||||||
final CSVFormat format = CSVFormat.EXCEL.withHeader("H1", "H2").withHeaderComments(comment)
|
// @formatter:off
|
||||||
.withCommentMarker('#');
|
final CSVFormat format = CSVFormat.EXCEL.builder()
|
||||||
|
.setHeader("H1", "H2")
|
||||||
|
.setHeaderComments(comment)
|
||||||
|
.setCommentMarker('#')
|
||||||
|
.build();
|
||||||
|
// @formatter:on
|
||||||
final StringBuilder out = new StringBuilder();
|
final StringBuilder out = new StringBuilder();
|
||||||
try (final CSVPrinter printer = format.print(out)) {
|
try (final CSVPrinter printer = format.print(out)) {
|
||||||
printer.print("A");
|
printer.print("A");
|
||||||
|
|
|
@ -50,22 +50,23 @@ public class JiraCsv167Test {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
CSVFormat format = CSVFormat.DEFAULT;
|
CSVFormat format = CSVFormat.DEFAULT.builder()
|
||||||
//
|
// @formatter:off
|
||||||
format = format.withAllowMissingColumnNames(false);
|
.setAllowMissingColumnNames(false)
|
||||||
format = format.withCommentMarker('#');
|
.setCommentMarker('#')
|
||||||
format = format.withDelimiter(',');
|
.setDelimiter(',')
|
||||||
format = format.withEscape('\\');
|
.setEscape('\\')
|
||||||
format = format.withHeader("author", "title", "publishDate");
|
.setHeader("author", "title", "publishDate")
|
||||||
format = format.withHeaderComments("headerComment");
|
.setHeaderComments("headerComment")
|
||||||
format = format.withNullString("NULL");
|
.setNullString("NULL")
|
||||||
format = format.withIgnoreEmptyLines(true);
|
.setIgnoreEmptyLines(true)
|
||||||
format = format.withIgnoreSurroundingSpaces(true);
|
.setIgnoreSurroundingSpaces(true)
|
||||||
format = format.withQuote('"');
|
.setQuote('"')
|
||||||
format = format.withQuoteMode(QuoteMode.ALL);
|
.setQuoteMode(QuoteMode.ALL)
|
||||||
format = format.withRecordSeparator('\n');
|
.setRecordSeparator('\n')
|
||||||
format = format.withSkipHeaderRecord(false);
|
.setSkipHeaderRecord(false)
|
||||||
//
|
.build();
|
||||||
|
// @formatter:on
|
||||||
int comments = 0;
|
int comments = 0;
|
||||||
int records = 0;
|
int records = 0;
|
||||||
try (final CSVParser parser = format.parse(getTestInput())) {
|
try (final CSVParser parser = format.parse(getTestInput())) {
|
||||||
|
@ -82,6 +83,7 @@ public class JiraCsv167Test {
|
||||||
}
|
}
|
||||||
|
|
||||||
private Reader getTestInput() {
|
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 @@ import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
public class JiraCsv198Test {
|
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
|
@Test
|
||||||
public void test() throws UnsupportedEncodingException, IOException {
|
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);
|
assertNotNull(pointsOfReference);
|
||||||
try (@SuppressWarnings("resource")
|
try (@SuppressWarnings("resource")
|
||||||
CSVParser parser = CSV_FORMAT.parse(new InputStreamReader(pointsOfReference, StandardCharsets.UTF_8))) {
|
CSVParser parser = CSV_FORMAT.parse(new InputStreamReader(pointsOfReference, StandardCharsets.UTF_8))) {
|
||||||
|
|
|
@ -24,17 +24,20 @@ import org.apache.commons.csv.QuoteMode;
|
||||||
import org.junit.jupiter.api.Test;
|
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 {
|
public class JiraCsv203Test {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testQuoteModeAll() throws Exception {
|
public void testQuoteModeAll() throws Exception {
|
||||||
final CSVFormat format = CSVFormat.EXCEL
|
// @formatter:off
|
||||||
.withNullString("N/A")
|
final CSVFormat format = CSVFormat.EXCEL.builder()
|
||||||
.withIgnoreSurroundingSpaces(true)
|
.setNullString("N/A")
|
||||||
.withQuoteMode(QuoteMode.ALL);
|
.setIgnoreSurroundingSpaces(true)
|
||||||
|
.setQuoteMode(QuoteMode.ALL)
|
||||||
|
.build();
|
||||||
|
// @formatter:on
|
||||||
final StringBuffer buffer = new StringBuffer();
|
final StringBuffer buffer = new StringBuffer();
|
||||||
try (final CSVPrinter printer = new CSVPrinter(buffer, format)) {
|
try (final CSVPrinter printer = new CSVPrinter(buffer, format)) {
|
||||||
printer.printRecord(null, "Hello", null, "World");
|
printer.printRecord(null, "Hello", null, "World");
|
||||||
|
@ -44,11 +47,13 @@ public class JiraCsv203Test {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testQuoteModeAllNonNull() throws Exception {
|
public void testQuoteModeAllNonNull() throws Exception {
|
||||||
final CSVFormat format = CSVFormat.EXCEL
|
// @formatter:off
|
||||||
.withNullString("N/A")
|
final CSVFormat format = CSVFormat.EXCEL.builder()
|
||||||
.withIgnoreSurroundingSpaces(true)
|
.setNullString("N/A")
|
||||||
.withQuoteMode(QuoteMode.ALL_NON_NULL);
|
.setIgnoreSurroundingSpaces(true)
|
||||||
|
.setQuoteMode(QuoteMode.ALL_NON_NULL)
|
||||||
|
.build();
|
||||||
|
// @formatter:on
|
||||||
final StringBuffer buffer = new StringBuffer();
|
final StringBuffer buffer = new StringBuffer();
|
||||||
try (final CSVPrinter printer = new CSVPrinter(buffer, format)) {
|
try (final CSVPrinter printer = new CSVPrinter(buffer, format)) {
|
||||||
printer.printRecord(null, "Hello", null, "World");
|
printer.printRecord(null, "Hello", null, "World");
|
||||||
|
@ -58,10 +63,12 @@ public class JiraCsv203Test {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testWithoutQuoteMode() throws Exception {
|
public void testWithoutQuoteMode() throws Exception {
|
||||||
final CSVFormat format = CSVFormat.EXCEL
|
// @formatter:off
|
||||||
.withNullString("N/A")
|
final CSVFormat format = CSVFormat.EXCEL.builder()
|
||||||
.withIgnoreSurroundingSpaces(true);
|
.setNullString("N/A")
|
||||||
|
.setIgnoreSurroundingSpaces(true)
|
||||||
|
.build();
|
||||||
|
// @formatter:on
|
||||||
final StringBuffer buffer = new StringBuffer();
|
final StringBuffer buffer = new StringBuffer();
|
||||||
try (final CSVPrinter printer = new CSVPrinter(buffer, format)) {
|
try (final CSVPrinter printer = new CSVPrinter(buffer, format)) {
|
||||||
printer.printRecord(null, "Hello", null, "World");
|
printer.printRecord(null, "Hello", null, "World");
|
||||||
|
@ -71,11 +78,13 @@ public class JiraCsv203Test {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testQuoteModeMinimal() throws Exception {
|
public void testQuoteModeMinimal() throws Exception {
|
||||||
final CSVFormat format = CSVFormat.EXCEL
|
// @formatter:off
|
||||||
.withNullString("N/A")
|
final CSVFormat format = CSVFormat.EXCEL.builder()
|
||||||
.withIgnoreSurroundingSpaces(true)
|
.setNullString("N/A")
|
||||||
.withQuoteMode(QuoteMode.MINIMAL);
|
.setIgnoreSurroundingSpaces(true)
|
||||||
|
.setQuoteMode(QuoteMode.MINIMAL)
|
||||||
|
.build();
|
||||||
|
// @formatter:on
|
||||||
final StringBuffer buffer = new StringBuffer();
|
final StringBuffer buffer = new StringBuffer();
|
||||||
try (final CSVPrinter printer = new CSVPrinter(buffer, format)) {
|
try (final CSVPrinter printer = new CSVPrinter(buffer, format)) {
|
||||||
printer.printRecord(null, "Hello", null, "World");
|
printer.printRecord(null, "Hello", null, "World");
|
||||||
|
@ -85,11 +94,13 @@ public class JiraCsv203Test {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testQuoteModeNonNumeric() throws Exception {
|
public void testQuoteModeNonNumeric() throws Exception {
|
||||||
final CSVFormat format = CSVFormat.EXCEL
|
// @formatter:off
|
||||||
.withNullString("N/A")
|
final CSVFormat format = CSVFormat.EXCEL.builder()
|
||||||
.withIgnoreSurroundingSpaces(true)
|
.setNullString("N/A")
|
||||||
.withQuoteMode(QuoteMode.NON_NUMERIC);
|
.setIgnoreSurroundingSpaces(true)
|
||||||
|
.setQuoteMode(QuoteMode.NON_NUMERIC)
|
||||||
|
.build();
|
||||||
|
// @formatter:on
|
||||||
final StringBuffer buffer = new StringBuffer();
|
final StringBuffer buffer = new StringBuffer();
|
||||||
try (final CSVPrinter printer = new CSVPrinter(buffer, format)) {
|
try (final CSVPrinter printer = new CSVPrinter(buffer, format)) {
|
||||||
printer.printRecord(null, "Hello", null, "World");
|
printer.printRecord(null, "Hello", null, "World");
|
||||||
|
@ -99,11 +110,13 @@ public class JiraCsv203Test {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testWithoutNullString() throws Exception {
|
public void testWithoutNullString() throws Exception {
|
||||||
final CSVFormat format = CSVFormat.EXCEL
|
// @formatter:off
|
||||||
//.withNullString("N/A")
|
final CSVFormat format = CSVFormat.EXCEL.builder()
|
||||||
.withIgnoreSurroundingSpaces(true)
|
//.setNullString("N/A")
|
||||||
.withQuoteMode(QuoteMode.ALL);
|
.setIgnoreSurroundingSpaces(true)
|
||||||
|
.setQuoteMode(QuoteMode.ALL)
|
||||||
|
.build();
|
||||||
|
// @formatter:on
|
||||||
final StringBuffer buffer = new StringBuffer();
|
final StringBuffer buffer = new StringBuffer();
|
||||||
try (final CSVPrinter printer = new CSVPrinter(buffer, format)) {
|
try (final CSVPrinter printer = new CSVPrinter(buffer, format)) {
|
||||||
printer.printRecord(null, "Hello", null, "World");
|
printer.printRecord(null, "Hello", null, "World");
|
||||||
|
@ -113,15 +126,17 @@ public class JiraCsv203Test {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testWithEmptyValues() throws Exception {
|
public void testWithEmptyValues() throws Exception {
|
||||||
final CSVFormat format = CSVFormat.EXCEL
|
// @formatter:off
|
||||||
.withNullString("N/A")
|
final CSVFormat format = CSVFormat.EXCEL.builder()
|
||||||
.withIgnoreSurroundingSpaces(true)
|
.setNullString("N/A")
|
||||||
.withQuoteMode(QuoteMode.ALL);
|
.setIgnoreSurroundingSpaces(true)
|
||||||
|
.setQuoteMode(QuoteMode.ALL)
|
||||||
|
.build();
|
||||||
|
// @formatter:on
|
||||||
final StringBuffer buffer = new StringBuffer();
|
final StringBuffer buffer = new StringBuffer();
|
||||||
try (final CSVPrinter printer = new CSVPrinter(buffer, format)) {
|
try (final CSVPrinter printer = new CSVPrinter(buffer, format)) {
|
||||||
printer.printRecord("", "Hello", "", "World");
|
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());
|
assertEquals("\"\",\"Hello\",\"\",\"World\"\r\n", buffer.toString());
|
||||||
}
|
}
|
||||||
|
|
|
@ -49,14 +49,15 @@ public class JiraCsv206Test {
|
||||||
assertEquals("123 Main St.", record.get(2));
|
assertEquals("123 Main St.", record.get(2));
|
||||||
}
|
}
|
||||||
// Write with multiple character delimiter
|
// 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]";
|
final String comment = "Change delimiter to [I]";
|
||||||
// @formatter:off
|
// @formatter:off
|
||||||
final CSVFormat format = CSVFormat.EXCEL.builder()
|
final CSVFormat format = CSVFormat.EXCEL.builder()
|
||||||
.setDelimiter("[I]").setHeader("first name", "last name", "address")
|
.setDelimiter("[I]").setHeader("first name", "last name", "address")
|
||||||
.setCommentMarker('#')
|
.setCommentMarker('#')
|
||||||
.setHeaderComments(comment).build();
|
.setHeaderComments(comment).build();
|
||||||
// @formatter:off
|
// @formatter:on
|
||||||
final StringBuilder out = new StringBuilder();
|
final StringBuilder out = new StringBuilder();
|
||||||
try (final CSVPrinter printer = format.print(out)) {
|
try (final CSVPrinter printer = format.print(out)) {
|
||||||
printer.print(record.get(0));
|
printer.print(record.get(0));
|
||||||
|
|
|
@ -30,13 +30,19 @@ public class JiraCsv211Test {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testJiraCsv211Format() throws IOException {
|
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);
|
final String formatted = printFormat.format(values);
|
||||||
assertEquals("ID\tName\tCountry\tAge\r\n1\tJane Doe\tUSA\t", formatted);
|
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))) {
|
try (final CSVParser parser = parseFormat.parse(new StringReader(formatted))) {
|
||||||
for (final CSVRecord record : parser) {
|
for (final CSVRecord record : parser) {
|
||||||
assertEquals("1", record.get(0));
|
assertEquals("1", record.get(0));
|
||||||
|
@ -44,5 +50,6 @@ public class JiraCsv211Test {
|
||||||
assertEquals("USA", record.get(2));
|
assertEquals("USA", record.get(2));
|
||||||
assertEquals("", record.get(3));
|
assertEquals("", record.get(3));
|
||||||
}
|
}
|
||||||
}}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -41,15 +41,16 @@ public class JiraCsv213Test {
|
||||||
|
|
||||||
private void createEndChannel(final File csvFile) {
|
private void createEndChannel(final File csvFile) {
|
||||||
// @formatter:off
|
// @formatter:off
|
||||||
final CSVFormat csvFormat =
|
final CSVFormat csvFormat = CSVFormat.DEFAULT.builder()
|
||||||
CSVFormat.DEFAULT
|
.setDelimiter(';')
|
||||||
.withDelimiter(';')
|
.setHeader()
|
||||||
.withFirstRecordAsHeader()
|
.setSkipHeaderRecord(true)
|
||||||
.withRecordSeparator('\n')
|
.setRecordSeparator('\n')
|
||||||
.withQuoteMode(QuoteMode.ALL);
|
.setQuoteMode(QuoteMode.ALL)
|
||||||
|
.build();
|
||||||
// @formatter:on
|
// @formatter:on
|
||||||
try (CSVParser parser = csvFormat
|
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()) {
|
if (parser.iterator().hasNext()) {
|
||||||
// System.out.println(parser.getCurrentLineNumber());
|
// System.out.println(parser.getCurrentLineNumber());
|
||||||
// System.out.println(parser.getRecordNumber());
|
// System.out.println(parser.getRecordNumber());
|
||||||
|
|
|
@ -36,10 +36,9 @@ public class JiraCsv247Test {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testHeadersMissingOneColumnWhenAllowingMissingColumnNames() throws Exception {
|
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(),
|
assertTrue(format.getAllowMissingColumnNames(), "We should allow missing column names");
|
||||||
"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");
|
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)) {
|
try (final CSVParser parser = format.parse(in)) {
|
||||||
|
@ -62,8 +61,8 @@ public class JiraCsv247Test {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testHeadersMissingThrowsWhenNotAllowingMissingColumnNames() throws Exception {
|
public void testHeadersMissingThrowsWhenNotAllowingMissingColumnNames() {
|
||||||
final CSVFormat format = CSVFormat.DEFAULT.withHeader();
|
final CSVFormat format = CSVFormat.DEFAULT.builder().setHeader().build();
|
||||||
|
|
||||||
assertFalse(format.getAllowMissingColumnNames(), "By default we should not allow missing column names");
|
assertFalse(format.getAllowMissingColumnNames(), "By default we should not allow missing column names");
|
||||||
|
|
||||||
|
|
|
@ -33,8 +33,9 @@ public class JiraCsv248Test {
|
||||||
/**
|
/**
|
||||||
* Test deserialisation of a CSVRecord created using version 1.6.
|
* Test deserialisation of a CSVRecord created using version 1.6.
|
||||||
*
|
*
|
||||||
* <p>This test asserts that serialization from 1.8 onwards is consistent with
|
* <p>
|
||||||
* previous versions. Serialization was broken in version 1.7.
|
* 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 IOException Signals that an I/O exception has occurred.
|
||||||
* @throws ClassNotFoundException If the CSVRecord cannot be deserialized
|
* @throws ClassNotFoundException If the CSVRecord cannot be deserialized
|
||||||
|
@ -42,11 +43,11 @@ public class JiraCsv248Test {
|
||||||
@Test
|
@Test
|
||||||
public void testJiraCsv248() throws IOException, ClassNotFoundException {
|
public void testJiraCsv248() throws IOException, ClassNotFoundException {
|
||||||
// Record was originally created using CSV version 1.6 with the following code:
|
// 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('#'))) {
|
// try (final CSVParser parser = CSVParser.parse("A,B\n#my comment\nOne,Two",
|
||||||
// CSVRecord rec = parser.iterator().next();
|
// CSVFormat.DEFAULT.builder().setHeader().setCommentMarker('#'))) {
|
||||||
//}
|
// CSVRecord rec = parser.iterator().next();
|
||||||
try (InputStream in = getTestInput();
|
// }
|
||||||
final ObjectInputStream ois = new ObjectInputStream(in)) {
|
try (InputStream in = getTestInput(); final ObjectInputStream ois = new ObjectInputStream(in)) {
|
||||||
final Object object = ois.readObject();
|
final Object object = ois.readObject();
|
||||||
assertTrue(object instanceof CSVRecord);
|
assertTrue(object instanceof CSVRecord);
|
||||||
final CSVRecord rec = (CSVRecord) object;
|
final CSVRecord rec = (CSVRecord) object;
|
||||||
|
|
|
@ -34,7 +34,7 @@ public class JiraCsv249Test {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testJiraCsv249() throws IOException {
|
public void testJiraCsv249() throws IOException {
|
||||||
final CSVFormat csvFormat = CSVFormat.DEFAULT.withEscape('\\');
|
final CSVFormat csvFormat = CSVFormat.DEFAULT.builder().setEscape('\\').build();
|
||||||
final StringWriter stringWriter = new StringWriter();
|
final StringWriter stringWriter = new StringWriter();
|
||||||
try (CSVPrinter printer = new CSVPrinter(stringWriter, csvFormat)) {
|
try (CSVPrinter printer = new CSVPrinter(stringWriter, csvFormat)) {
|
||||||
printer.printRecord("foo \\", "bar");
|
printer.printRecord("foo \\", "bar");
|
||||||
|
|
Loading…
Reference in New Issue