@@ -82,6 +82,12 @@ private BOMInputStream createBOMInputStream(final String resource) throws IOExce
8282 return new BOMInputStream (url .openStream ());
8383 }
8484
85+ private void parseFully (final CSVParser parser ) {
86+ for (final Iterator <CSVRecord > records = parser .iterator (); records .hasNext (); ) {
87+ records .next ();
88+ }
89+ }
90+
8591 @ Test
8692 public void testBackslashEscaping () throws IOException {
8793
@@ -183,9 +189,9 @@ public void testBOM() throws IOException {
183189 }
184190
185191 @ Test
186- public void testBOMInputStream_ParserWithReader () throws IOException {
187- try (final Reader reader = new InputStreamReader ( createBOMInputStream ("CSVFileParser/bom.csv" ), UTF_8_NAME );
188- final CSVParser parser = new CSVParser ( reader , CSVFormat .EXCEL .withHeader ())) {
192+ public void testBOMInputStream_ParserWithInputStream () throws IOException {
193+ try (final BOMInputStream inputStream = createBOMInputStream ("CSVFileParser/bom.csv" );
194+ final CSVParser parser = CSVParser . parse ( inputStream , UTF_8 , CSVFormat .EXCEL .withHeader ())) {
189195 for (final CSVRecord record : parser ) {
190196 final String string = record .get ("Date" );
191197 Assert .assertNotNull (string );
@@ -195,9 +201,9 @@ public void testBOMInputStream_ParserWithReader() throws IOException {
195201 }
196202
197203 @ Test
198- public void testBOMInputStream_parseWithReader () throws IOException {
204+ public void testBOMInputStream_ParserWithReader () throws IOException {
199205 try (final Reader reader = new InputStreamReader (createBOMInputStream ("CSVFileParser/bom.csv" ), UTF_8_NAME );
200- final CSVParser parser = CSVParser . parse (reader , CSVFormat .EXCEL .withHeader ())) {
206+ final CSVParser parser = new CSVParser (reader , CSVFormat .EXCEL .withHeader ())) {
201207 for (final CSVRecord record : parser ) {
202208 final String string = record .get ("Date" );
203209 Assert .assertNotNull (string );
@@ -207,9 +213,9 @@ public void testBOMInputStream_parseWithReader() throws IOException {
207213 }
208214
209215 @ Test
210- public void testBOMInputStream_ParserWithInputStream () throws IOException {
211- try (final BOMInputStream inputStream = createBOMInputStream ("CSVFileParser/bom.csv" );
212- final CSVParser parser = CSVParser .parse (inputStream , UTF_8 , CSVFormat .EXCEL .withHeader ())) {
216+ public void testBOMInputStream_parseWithReader () throws IOException {
217+ try (final Reader reader = new InputStreamReader ( createBOMInputStream ("CSVFileParser/bom.csv" ), UTF_8_NAME );
218+ final CSVParser parser = CSVParser .parse (reader , CSVFormat .EXCEL .withHeader ())) {
213219 for (final CSVRecord record : parser ) {
214220 final String string = record .get ("Date" );
215221 Assert .assertNotNull (string );
@@ -236,36 +242,6 @@ public void testCarriageReturnLineFeedEndings() throws IOException {
236242 }
237243 }
238244
239- @ Test
240- public void testFirstEndOfLineCrLf () throws IOException {
241- final String data = "foo\r \n baar,\r \n hello,world\r \n ,kanu" ;
242- try (final CSVParser parser = CSVParser .parse (data , CSVFormat .DEFAULT )) {
243- final List <CSVRecord > records = parser .getRecords ();
244- assertEquals (4 , records .size ());
245- assertEquals ("\r \n " , parser .getFirstEndOfLine ());
246- }
247- }
248-
249- @ Test
250- public void testFirstEndOfLineLf () throws IOException {
251- final String data = "foo\n baar,\n hello,world\n ,kanu" ;
252- try (final CSVParser parser = CSVParser .parse (data , CSVFormat .DEFAULT )) {
253- final List <CSVRecord > records = parser .getRecords ();
254- assertEquals (4 , records .size ());
255- assertEquals ("\n " , parser .getFirstEndOfLine ());
256- }
257- }
258-
259- @ Test
260- public void testFirstEndOfLineCr () throws IOException {
261- final String data = "foo\r baar,\r hello,world\r ,kanu" ;
262- try (final CSVParser parser = CSVParser .parse (data , CSVFormat .DEFAULT )) {
263- final List <CSVRecord > records = parser .getRecords ();
264- assertEquals (4 , records .size ());
265- assertEquals ("\r " , parser .getFirstEndOfLine ());
266- }
267- }
268-
269245 @ Test (expected = NoSuchElementException .class )
270246 public void testClose () throws Exception {
271247 final Reader in = new StringReader ("# comment\n a,b,c\n 1,2,3\n x,y,z" );
@@ -445,6 +421,36 @@ public void testExcelHeaderCountLessThanData() throws Exception {
445421 }
446422 }
447423
424+ @ Test
425+ public void testFirstEndOfLineCr () throws IOException {
426+ final String data = "foo\r baar,\r hello,world\r ,kanu" ;
427+ try (final CSVParser parser = CSVParser .parse (data , CSVFormat .DEFAULT )) {
428+ final List <CSVRecord > records = parser .getRecords ();
429+ assertEquals (4 , records .size ());
430+ assertEquals ("\r " , parser .getFirstEndOfLine ());
431+ }
432+ }
433+
434+ @ Test
435+ public void testFirstEndOfLineCrLf () throws IOException {
436+ final String data = "foo\r \n baar,\r \n hello,world\r \n ,kanu" ;
437+ try (final CSVParser parser = CSVParser .parse (data , CSVFormat .DEFAULT )) {
438+ final List <CSVRecord > records = parser .getRecords ();
439+ assertEquals (4 , records .size ());
440+ assertEquals ("\r \n " , parser .getFirstEndOfLine ());
441+ }
442+ }
443+
444+ @ Test
445+ public void testFirstEndOfLineLf () throws IOException {
446+ final String data = "foo\n baar,\n hello,world\n ,kanu" ;
447+ try (final CSVParser parser = CSVParser .parse (data , CSVFormat .DEFAULT )) {
448+ final List <CSVRecord > records = parser .getRecords ();
449+ assertEquals (4 , records .size ());
450+ assertEquals ("\n " , parser .getFirstEndOfLine ());
451+ }
452+ }
453+
448454 @ Test
449455 public void testForEach () throws Exception {
450456 final List <CSVRecord > records = new ArrayList <>();
@@ -729,6 +735,62 @@ public void testIterator() throws Exception {
729735 }
730736 }
731737
738+ @ Test
739+ public void testIteratorSequenceBreaking () throws IOException {
740+ final String fiveRows = "1\n 2\n 3\n 4\n 5\n " ;
741+
742+ // Iterator hasNext() shouldn't break sequence
743+ CSVParser parser = CSVFormat .DEFAULT .parse (new StringReader (fiveRows ));
744+ int recordNumber = 0 ;
745+ Iterator <CSVRecord > iter = parser .iterator ();
746+ recordNumber = 0 ;
747+ while (iter .hasNext ()) {
748+ CSVRecord record = iter .next ();
749+ recordNumber ++;
750+ assertEquals (String .valueOf (recordNumber ), record .get (0 ));
751+ if (recordNumber >= 2 ) {
752+ break ;
753+ }
754+ }
755+ iter .hasNext ();
756+ while (iter .hasNext ()) {
757+ CSVRecord record = iter .next ();
758+ recordNumber ++;
759+ assertEquals (String .valueOf (recordNumber ), record .get (0 ));
760+ }
761+
762+ // Consecutive enhanced for loops shouldn't break sequence
763+ parser = CSVFormat .DEFAULT .parse (new StringReader (fiveRows ));
764+ recordNumber = 0 ;
765+ for (CSVRecord record : parser ) {
766+ recordNumber ++;
767+ assertEquals (String .valueOf (recordNumber ), record .get (0 ));
768+ if (recordNumber >= 2 ) {
769+ break ;
770+ }
771+ }
772+ for (CSVRecord record : parser ) {
773+ recordNumber ++;
774+ assertEquals (String .valueOf (recordNumber ), record .get (0 ));
775+ }
776+
777+ // Consecutive enhanced for loops with hasNext() peeking shouldn't break sequence
778+ parser = CSVFormat .DEFAULT .parse (new StringReader (fiveRows ));
779+ recordNumber = 0 ;
780+ for (CSVRecord record : parser ) {
781+ recordNumber ++;
782+ assertEquals (String .valueOf (recordNumber ), record .get (0 ));
783+ if (recordNumber >= 2 ) {
784+ break ;
785+ }
786+ }
787+ parser .iterator ().hasNext ();
788+ for (CSVRecord record : parser ) {
789+ recordNumber ++;
790+ assertEquals (String .valueOf (recordNumber ), record .get (0 ));
791+ }
792+ }
793+
732794 @ Test
733795 public void testLineFeedEndings () throws IOException {
734796 final String code = "foo\n baar,\n hello,world\n ,kanu" ;
@@ -737,7 +799,7 @@ public void testLineFeedEndings() throws IOException {
737799 assertEquals (4 , records .size ());
738800 }
739801 }
740-
802+
741803 @ Test
742804 public void testMappedButNotSetAsOutlook2007ContactExport () throws Exception {
743805 final Reader in = new StringReader ("a,b,c\n 1,2\n x,y,z" );
@@ -791,7 +853,7 @@ public void testMongoDbCsv() throws Exception {
791853 assertEquals ("f" , second .get (2 ));
792854 }
793855 }
794-
856+
795857 @ Test
796858 // TODO this may lead to strange behavior, throw an exception if iterator() has already been called?
797859 public void testMultipleIterators () throws Exception {
@@ -864,12 +926,6 @@ public void testParse() throws Exception {
864926 }
865927 }
866928
867- private void parseFully (final CSVParser parser ) {
868- for (final Iterator <CSVRecord > records = parser .iterator (); records .hasNext (); ) {
869- records .next ();
870- }
871- }
872-
873929 @ Test (expected = IllegalArgumentException .class )
874930 public void testParseFileNullFormat () throws Exception {
875931 try (final CSVParser parser = CSVParser .parse (new File ("CSVFileParser/test.csv" ), Charset .defaultCharset (), null )) {
@@ -1066,62 +1122,6 @@ public void testTrim() throws Exception {
10661122 Assert .assertEquals (3 , record .size ());
10671123 }
10681124
1069- @ Test
1070- public void testIteratorSequenceBreaking () throws IOException {
1071- final String fiveRows = "1\n 2\n 3\n 4\n 5\n " ;
1072-
1073- // Iterator hasNext() shouldn't break sequence
1074- CSVParser parser = CSVFormat .DEFAULT .parse (new StringReader (fiveRows ));
1075- int recordNumber = 0 ;
1076- Iterator <CSVRecord > iter = parser .iterator ();
1077- recordNumber = 0 ;
1078- while (iter .hasNext ()) {
1079- CSVRecord record = iter .next ();
1080- recordNumber ++;
1081- assertEquals (String .valueOf (recordNumber ), record .get (0 ));
1082- if (recordNumber >= 2 ) {
1083- break ;
1084- }
1085- }
1086- iter .hasNext ();
1087- while (iter .hasNext ()) {
1088- CSVRecord record = iter .next ();
1089- recordNumber ++;
1090- assertEquals (String .valueOf (recordNumber ), record .get (0 ));
1091- }
1092-
1093- // Consecutive enhanced for loops shouldn't break sequence
1094- parser = CSVFormat .DEFAULT .parse (new StringReader (fiveRows ));
1095- recordNumber = 0 ;
1096- for (CSVRecord record : parser ) {
1097- recordNumber ++;
1098- assertEquals (String .valueOf (recordNumber ), record .get (0 ));
1099- if (recordNumber >= 2 ) {
1100- break ;
1101- }
1102- }
1103- for (CSVRecord record : parser ) {
1104- recordNumber ++;
1105- assertEquals (String .valueOf (recordNumber ), record .get (0 ));
1106- }
1107-
1108- // Consecutive enhanced for loops with hasNext() peeking shouldn't break sequence
1109- parser = CSVFormat .DEFAULT .parse (new StringReader (fiveRows ));
1110- recordNumber = 0 ;
1111- for (CSVRecord record : parser ) {
1112- recordNumber ++;
1113- assertEquals (String .valueOf (recordNumber ), record .get (0 ));
1114- if (recordNumber >= 2 ) {
1115- break ;
1116- }
1117- }
1118- parser .iterator ().hasNext ();
1119- for (CSVRecord record : parser ) {
1120- recordNumber ++;
1121- assertEquals (String .valueOf (recordNumber ), record .get (0 ));
1122- }
1123- }
1124-
11251125 private void validateLineNumbers (final String lineSeparator ) throws IOException {
11261126 try (final CSVParser parser = CSVParser .parse ("a" + lineSeparator + "b" + lineSeparator + "c" ,
11271127 CSVFormat .DEFAULT .withRecordSeparator (lineSeparator ))) {
0 commit comments