@@ -299,22 +299,23 @@ public void testEmptyLineBehaviourExcel() throws Exception {
299299 }
300300 }
301301
302- // @Test
303- // public void testStartWithEmptyLinesThenHeaders() throws Exception {
304- // final String[] codes = { "\r\n\r\n\r\nhello,\r\n\r\n\r\n", "hello,\n\n\n", "hello,\"\"\r\n\r\n\r\n", "hello,\"\"\n\n\n" };
305- // final String[][] res = { { "hello", "" }, { "" }, // Excel format does not ignore empty lines
306- // { "" } };
307- // for (final String code : codes) {
308- // final CSVParser parser = CSVParser.parse(code, CSVFormat.EXCEL);
309- // final List<CSVRecord> records = parser.getRecords();
310- // assertEquals(res.length, records.size());
311- // assertTrue(records.size() > 0);
312- // for (int i = 0; i < res.length; i++) {
313- // assertArrayEquals(res[i], records.get(i).values());
314- // }
315- // parser.close();
316- // }
317- // }
302+ // @Test
303+ // public void testStartWithEmptyLinesThenHeaders() throws Exception {
304+ // final String[] codes = { "\r\n\r\n\r\nhello,\r\n\r\n\r\n", "hello,\n\n\n", "hello,\"\"\r\n\r\n\r\n",
305+ // "hello,\"\"\n\n\n" };
306+ // final String[][] res = { { "hello", "" }, { "" }, // Excel format does not ignore empty lines
307+ // { "" } };
308+ // for (final String code : codes) {
309+ // final CSVParser parser = CSVParser.parse(code, CSVFormat.EXCEL);
310+ // final List<CSVRecord> records = parser.getRecords();
311+ // assertEquals(res.length, records.size());
312+ // assertTrue(records.size() > 0);
313+ // for (int i = 0; i < res.length; i++) {
314+ // assertArrayEquals(res[i], records.get(i).values());
315+ // }
316+ // parser.close();
317+ // }
318+ // }
318319
319320 @ Test
320321 public void testEndOfFileBehaviorCSV () throws Exception {
@@ -474,6 +475,16 @@ public void testGetLineNumberWithLF() throws Exception {
474475 this .validateLineNumbers (String .valueOf (LF ));
475476 }
476477
478+ @ Test
479+ public void testGetRecordPositionWithCRLF () throws Exception {
480+ this .validateRecordPosition (CRLF );
481+ }
482+
483+ @ Test
484+ public void testGetRecordPositionWithLF () throws Exception {
485+ this .validateRecordPosition (String .valueOf (LF ));
486+ }
487+
477488 @ Test
478489 public void testGetOneLine () throws IOException {
479490 final CSVParser parser = CSVParser .parse (CSV_INPUT_1 , CSVFormat .DEFAULT );
@@ -902,4 +913,65 @@ private void validateRecordNumbers(final String lineSeparator) throws IOExceptio
902913 parser .close ();
903914 }
904915
916+ private void validateRecordPosition (final String lineSeparator ) throws IOException {
917+ final String nl = lineSeparator ; // used as linebreak in values for better distinction
918+
919+ String code = "a,b,c" + lineSeparator + "1,2,3" + lineSeparator +
920+ // to see if recordPosition correctly points to the enclosing quote
921+ "'A" + nl + "A','B" + nl + "B',CC" + lineSeparator +
922+ // unicode test... not very relevant while operating on strings instead of bytes, but for
923+ // completeness...
924+ "\u00c4 ,\u00d6 ,\u00dc " + lineSeparator + "EOF,EOF,EOF" ;
925+
926+ final CSVFormat format = CSVFormat .newFormat (',' ).withQuote ('\'' ).withRecordSeparator (lineSeparator );
927+ CSVParser parser = CSVParser .parse (code , format );
928+
929+ CSVRecord record ;
930+ assertEquals (0 , parser .getRecordNumber ());
931+
932+ assertNotNull (record = parser .nextRecord ());
933+ assertEquals (1 , record .getRecordNumber ());
934+ assertEquals (code .indexOf ('a' ), record .getCharacterPosition ());
935+
936+ assertNotNull (record = parser .nextRecord ());
937+ assertEquals (2 , record .getRecordNumber ());
938+ assertEquals (code .indexOf ('1' ), record .getCharacterPosition ());
939+
940+ assertNotNull (record = parser .nextRecord ());
941+ final long positionRecord3 = record .getCharacterPosition ();
942+ assertEquals (3 , record .getRecordNumber ());
943+ assertEquals (code .indexOf ("'A" ), record .getCharacterPosition ());
944+ assertEquals ("A" + lineSeparator + "A" , record .get (0 ));
945+ assertEquals ("B" + lineSeparator + "B" , record .get (1 ));
946+ assertEquals ("CC" , record .get (2 ));
947+
948+ assertNotNull (record = parser .nextRecord ());
949+ assertEquals (4 , record .getRecordNumber ());
950+ assertEquals (code .indexOf ('\u00c4' ), record .getCharacterPosition ());
951+
952+ assertNotNull (record = parser .nextRecord ());
953+ assertEquals (5 , record .getRecordNumber ());
954+ assertEquals (code .indexOf ("EOF" ), record .getCharacterPosition ());
955+
956+ parser .close ();
957+
958+ // now try to read starting at record 3
959+ parser = CSVParser .parse (code .substring ((int ) positionRecord3 ), format );
960+ parser .setNextRecordNumber (3 );
961+ parser .setNextCharacterPosition (positionRecord3 );
962+
963+ assertNotNull (record = parser .nextRecord ());
964+ assertEquals (3 , record .getRecordNumber ());
965+ assertEquals (code .indexOf ("'A" ), record .getCharacterPosition ());
966+ assertEquals ("A" + lineSeparator + "A" , record .get (0 ));
967+ assertEquals ("B" + lineSeparator + "B" , record .get (1 ));
968+ assertEquals ("CC" , record .get (2 ));
969+
970+ assertNotNull (record = parser .nextRecord ());
971+ assertEquals (4 , record .getRecordNumber ());
972+ assertEquals (code .indexOf ('\u00c4' ), record .getCharacterPosition ());
973+ assertEquals ("\u00c4 " , record .get (0 ));
974+
975+ parser .close ();
976+ }
905977}
0 commit comments