|
69 | 69 | */ |
70 | 70 | public class CSVParserTest { |
71 | 71 |
|
| 72 | + private static final CSVFormat EXCEL_WITH_HEADER = CSVFormat.EXCEL.withHeader(); |
| 73 | + |
72 | 74 | private static final Charset UTF_8 = StandardCharsets.UTF_8; |
73 | 75 |
|
74 | 76 | private static final String UTF_8_NAME = UTF_8.name(); |
75 | 77 |
|
76 | 78 | private static final String CSV_INPUT = "a,b,c,d\n" + " a , b , 1 2 \n" + "\"foo baar\", b,\n" + |
77 | | - // + " \"foo\n,,\n\"\",,\n\\\"\",d,e\n"; |
78 | | - " \"foo\n,,\n\"\",,\n\"\"\",d,e\n"; // changed to use standard CSV escaping |
| 79 | + // + " \"foo\n,,\n\"\",,\n\\\"\",d,e\n"; |
| 80 | + " \"foo\n,,\n\"\",,\n\"\"\",d,e\n"; // changed to use standard CSV escaping |
79 | 81 |
|
80 | 82 | private static final String CSV_INPUT_1 = "a,b,c,d"; |
81 | 83 |
|
@@ -220,48 +222,54 @@ public void testBackslashEscapingOld() throws IOException { |
220 | 222 | @Disabled("CSV-107") |
221 | 223 | public void testBOM() throws IOException { |
222 | 224 | final URL url = ClassLoader.getSystemClassLoader().getResource("org/apache/commons/csv/CSVFileParser/bom.csv"); |
223 | | - try (final CSVParser parser = CSVParser.parse(url, StandardCharsets.UTF_8, CSVFormat.EXCEL.withHeader())) { |
| 225 | + try (final CSVParser parser = CSVParser.parse(url, StandardCharsets.UTF_8, EXCEL_WITH_HEADER)) { |
224 | 226 | parser.forEach(record -> assertNotNull(record.get("Date"))); |
225 | 227 | } |
226 | 228 | } |
227 | 229 |
|
228 | 230 | @Test |
229 | 231 | public void testBOMInputStreamParserWithInputStream() throws IOException { |
230 | 232 | try (final BOMInputStream inputStream = createBOMInputStream("org/apache/commons/csv/CSVFileParser/bom.csv"); |
231 | | - final CSVParser parser = CSVParser.parse(inputStream, UTF_8, CSVFormat.EXCEL.withHeader())) { |
| 233 | + final CSVParser parser = CSVParser.parse(inputStream, UTF_8, EXCEL_WITH_HEADER)) { |
232 | 234 | parser.forEach(record -> assertNotNull(record.get("Date"))); |
233 | 235 | } |
234 | 236 | } |
235 | 237 |
|
236 | 238 | @Test |
237 | 239 | public void testBOMInputStreamParserWithReader() throws IOException { |
238 | 240 | try (final Reader reader = new InputStreamReader(createBOMInputStream("org/apache/commons/csv/CSVFileParser/bom.csv"), UTF_8_NAME); |
239 | | - final CSVParser parser = new CSVParser(reader, CSVFormat.EXCEL.withHeader())) { |
| 241 | + final CSVParser parser = CSVParser.builder() |
| 242 | + .setReader(reader) |
| 243 | + .setFormat(EXCEL_WITH_HEADER) |
| 244 | + .get()) { |
240 | 245 | parser.forEach(record -> assertNotNull(record.get("Date"))); |
241 | 246 | } |
242 | 247 | } |
243 | 248 |
|
244 | 249 | @Test |
245 | 250 | public void testBOMInputStreamParseWithReader() throws IOException { |
246 | 251 | try (final Reader reader = new InputStreamReader(createBOMInputStream("org/apache/commons/csv/CSVFileParser/bom.csv"), UTF_8_NAME); |
247 | | - final CSVParser parser = CSVParser.parse(reader, CSVFormat.EXCEL.withHeader())) { |
| 252 | + final CSVParser parser = CSVParser.builder() |
| 253 | + .setReader(reader) |
| 254 | + .setFormat(EXCEL_WITH_HEADER) |
| 255 | + .get()) { |
248 | 256 | parser.forEach(record -> assertNotNull(record.get("Date"))); |
249 | 257 | } |
250 | 258 | } |
251 | 259 |
|
252 | 260 | @Test |
253 | 261 | public void testCarriageReturnEndings() throws IOException { |
254 | | - final String code = "foo\rbaar,\rhello,world\r,kanu"; |
255 | | - try (final CSVParser parser = CSVParser.parse(code, CSVFormat.DEFAULT)) { |
| 262 | + final String string = "foo\rbaar,\rhello,world\r,kanu"; |
| 263 | + try (final CSVParser parser = CSVParser.builder().setCharSequence(string).get()) { |
256 | 264 | final List<CSVRecord> records = parser.getRecords(); |
257 | 265 | assertEquals(4, records.size()); |
258 | 266 | } |
259 | 267 | } |
260 | 268 |
|
261 | 269 | @Test |
262 | 270 | public void testCarriageReturnLineFeedEndings() throws IOException { |
263 | | - final String code = "foo\r\nbaar,\r\nhello,world\r\n,kanu"; |
264 | | - try (final CSVParser parser = CSVParser.parse(code, CSVFormat.DEFAULT)) { |
| 271 | + final String string = "foo\r\nbaar,\r\nhello,world\r\n,kanu"; |
| 272 | + try (final CSVParser parser = CSVParser.builder().setCharSequence(string).get()) { |
265 | 273 | final List<CSVRecord> records = parser.getRecords(); |
266 | 274 | assertEquals(4, records.size()); |
267 | 275 | } |
@@ -569,7 +577,7 @@ public void testExcelFormat2() throws Exception { |
569 | 577 | @Test |
570 | 578 | public void testExcelHeaderCountLessThanData() throws Exception { |
571 | 579 | final String code = "A,B,C,,\r\na,b,c,d,e\r\n"; |
572 | | - try (final CSVParser parser = CSVParser.parse(code, CSVFormat.EXCEL.withHeader())) { |
| 580 | + try (final CSVParser parser = CSVParser.parse(code, EXCEL_WITH_HEADER)) { |
573 | 581 | parser.getRecords().forEach(record -> { |
574 | 582 | assertEquals("a", record.get("A")); |
575 | 583 | assertEquals("b", record.get("B")); |
@@ -783,7 +791,10 @@ public void testGetOneLine() throws IOException { |
783 | 791 | public void testGetOneLineOneParser() throws IOException { |
784 | 792 | final CSVFormat format = CSVFormat.DEFAULT; |
785 | 793 | try (final PipedWriter writer = new PipedWriter(); |
786 | | - final CSVParser parser = new CSVParser(new PipedReader(writer), format)) { |
| 794 | + final CSVParser parser = CSVParser.builder() |
| 795 | + .setReader(new PipedReader(writer)) |
| 796 | + .setFormat(format) |
| 797 | + .get()) { |
787 | 798 | writer.append(CSV_INPUT_1); |
788 | 799 | writer.append(format.getRecordSeparator()); |
789 | 800 | final CSVRecord record1 = parser.nextRecord(); |
@@ -1232,35 +1243,68 @@ public void testNotValueCSV() throws IOException { |
1232 | 1243 | public void testParse() throws Exception { |
1233 | 1244 | final ClassLoader loader = ClassLoader.getSystemClassLoader(); |
1234 | 1245 | final URL url = loader.getResource("org/apache/commons/csv/CSVFileParser/test.csv"); |
1235 | | - final CSVFormat format = CSVFormat.DEFAULT.withHeader("A", "B", "C", "D"); |
| 1246 | + final CSVFormat format = CSVFormat.DEFAULT.builder().setHeader("A", "B", "C", "D").build(); |
1236 | 1247 | final Charset charset = StandardCharsets.UTF_8; |
1237 | | - |
1238 | | - try (@SuppressWarnings("resource") // CSVParser closes the input resource |
1239 | | - final CSVParser parser = CSVParser.parse(new InputStreamReader(url.openStream(), charset), format)) { |
| 1248 | + // Reader |
| 1249 | + try (final CSVParser parser = CSVParser.parse(new InputStreamReader(url.openStream(), charset), format)) { |
1240 | 1250 | parseFully(parser); |
1241 | 1251 | } |
1242 | | - try (final CSVParser parser = CSVParser.parse(new String(Files.readAllBytes(Paths.get(url.toURI())), charset), format)) { |
| 1252 | + try (final CSVParser parser = CSVParser.builder().setReader(new InputStreamReader(url.openStream(), charset)).setFormat(format).get()) { |
1243 | 1253 | parseFully(parser); |
1244 | 1254 | } |
1245 | | - try (final CSVParser parser = CSVParser.parse(new File(url.toURI()), charset, format)) { |
| 1255 | + // String |
| 1256 | + final Path path = Paths.get(url.toURI()); |
| 1257 | + final String string = new String(Files.readAllBytes(path), charset); |
| 1258 | + try (final CSVParser parser = CSVParser.parse(string, format)) { |
1246 | 1259 | parseFully(parser); |
1247 | 1260 | } |
1248 | | - try (@SuppressWarnings("resource") // CSVParser closes the input resource |
1249 | | - final CSVParser parser = CSVParser.parse(url.openStream(), charset, format)) { |
| 1261 | + try (final CSVParser parser = CSVParser.builder().setCharSequence(string).setFormat(format).get()) { |
1250 | 1262 | parseFully(parser); |
1251 | 1263 | } |
1252 | | - try (final CSVParser parser = CSVParser.parse(Paths.get(url.toURI()), charset, format)) { |
| 1264 | + // File |
| 1265 | + final File file = new File(url.toURI()); |
| 1266 | + try (final CSVParser parser = CSVParser.parse(file, charset, format)) { |
1253 | 1267 | parseFully(parser); |
1254 | 1268 | } |
| 1269 | + try (final CSVParser parser = CSVParser.builder().setFile(file).setCharset(charset).setFormat(format).get()) { |
| 1270 | + parseFully(parser); |
| 1271 | + } |
| 1272 | + // InputStream |
| 1273 | + try (final CSVParser parser = CSVParser.parse(url.openStream(), charset, format)) { |
| 1274 | + parseFully(parser); |
| 1275 | + } |
| 1276 | + try (final CSVParser parser = CSVParser.builder().setInputStream(url.openStream()).setCharset(charset).setFormat(format).get()) { |
| 1277 | + parseFully(parser); |
| 1278 | + } |
| 1279 | + // Path |
| 1280 | + try (final CSVParser parser = CSVParser.parse(path, charset, format)) { |
| 1281 | + parseFully(parser); |
| 1282 | + } |
| 1283 | + try (final CSVParser parser = CSVParser.builder().setPath(path).setCharset(charset).setFormat(format).get()) { |
| 1284 | + parseFully(parser); |
| 1285 | + } |
| 1286 | + // URL |
1255 | 1287 | try (final CSVParser parser = CSVParser.parse(url, charset, format)) { |
1256 | 1288 | parseFully(parser); |
1257 | 1289 | } |
| 1290 | + try (final CSVParser parser = CSVParser.builder().setURI(url.toURI()).setCharset(charset).setFormat(format).get()) { |
| 1291 | + parseFully(parser); |
| 1292 | + } |
| 1293 | + // InputStreamReader |
1258 | 1294 | try (final CSVParser parser = new CSVParser(new InputStreamReader(url.openStream(), charset), format)) { |
1259 | 1295 | parseFully(parser); |
1260 | 1296 | } |
| 1297 | + try (final CSVParser parser = CSVParser.builder().setReader(new InputStreamReader(url.openStream(), charset)).setFormat(format).get()) { |
| 1298 | + parseFully(parser); |
| 1299 | + } |
| 1300 | + // InputStreamReader with longs |
1261 | 1301 | try (final CSVParser parser = new CSVParser(new InputStreamReader(url.openStream(), charset), format, /* characterOffset= */0, /* recordNumber= */1)) { |
1262 | 1302 | parseFully(parser); |
1263 | 1303 | } |
| 1304 | + try (final CSVParser parser = CSVParser.builder().setReader(new InputStreamReader(url.openStream(), charset)).setFormat(format).setCharacterOffset(0) |
| 1305 | + .setRecordNumber(0).get()) { |
| 1306 | + parseFully(parser); |
| 1307 | + } |
1264 | 1308 | } |
1265 | 1309 |
|
1266 | 1310 | @Test |
@@ -1380,7 +1424,10 @@ public void testParsingPrintedEmptyFirstColumn(final CSVFormat.Predefined format |
1380 | 1424 | try (CSVPrinter printer = new CSVPrinter(buf, format.getFormat())) { |
1381 | 1425 | printer.printRecords(Stream.of(lines)); |
1382 | 1426 | } |
1383 | | - try (CSVParser csvRecords = new CSVParser(new StringReader(buf.toString()), format.getFormat())) { |
| 1427 | + try (CSVParser csvRecords = CSVParser.builder() |
| 1428 | + .setReader(new StringReader(buf.toString())) |
| 1429 | + .setFormat(format.getFormat()) |
| 1430 | + .get()) { |
1384 | 1431 | for (final String[] line : lines) { |
1385 | 1432 | assertArrayEquals(line, csvRecords.nextRecord().values()); |
1386 | 1433 | } |
@@ -1654,6 +1701,26 @@ private void validateRecordPosition(final String lineSeparator) throws IOExcepti |
1654 | 1701 | assertEquals(code.indexOf("EOF"), record.getCharacterPosition()); |
1655 | 1702 | } |
1656 | 1703 | // now try to read starting at record 3 |
| 1704 | + try (CSVParser parser = CSVParser.builder() |
| 1705 | + .setReader(new StringReader(code.substring((int) positionRecord3))) |
| 1706 | + .setFormat(format) |
| 1707 | + .setCharacterOffset(positionRecord3) |
| 1708 | + .setRecordNumber(3) |
| 1709 | + .get()) { |
| 1710 | + CSVRecord record; |
| 1711 | + // nextRecord |
| 1712 | + assertNotNull(record = parser.nextRecord()); |
| 1713 | + assertEquals(3, record.getRecordNumber()); |
| 1714 | + assertEquals(code.indexOf("'A"), record.getCharacterPosition()); |
| 1715 | + assertEquals("A" + lineSeparator + "A", record.get(0)); |
| 1716 | + assertEquals("B" + lineSeparator + "B", record.get(1)); |
| 1717 | + assertEquals("CC", record.get(2)); |
| 1718 | + // nextRecord |
| 1719 | + assertNotNull(record = parser.nextRecord()); |
| 1720 | + assertEquals(4, record.getRecordNumber()); |
| 1721 | + assertEquals(code.indexOf('\u00c4'), record.getCharacterPosition()); |
| 1722 | + assertEquals("\u00c4", record.get(0)); |
| 1723 | + } // again with ctor |
1657 | 1724 | try (CSVParser parser = new CSVParser(new StringReader(code.substring((int) positionRecord3)), format, positionRecord3, 3)) { |
1658 | 1725 | CSVRecord record; |
1659 | 1726 | // nextRecord |
|
0 commit comments