@@ -285,13 +285,15 @@ public void testClose() throws Exception {
285285 @ Test
286286 public void testCSV235 () throws IOException {
287287 final String dqString = "\" aaa\" ,\" b\" \" bb\" ,\" ccc\" " ; // "aaa","b""bb","ccc"
288- final Iterator <CSVRecord > records = CSVFormat .RFC4180 .parse (new StringReader (dqString )).iterator ();
289- final CSVRecord record = records .next ();
290- assertFalse (records .hasNext ());
291- assertEquals (3 , record .size ());
292- assertEquals ("aaa" , record .get (0 ));
293- assertEquals ("b\" bb" , record .get (1 ));
294- assertEquals ("ccc" , record .get (2 ));
288+ try (final CSVParser parser = CSVFormat .RFC4180 .parse (new StringReader (dqString ))) {
289+ final Iterator <CSVRecord > records = parser .iterator ();
290+ final CSVRecord record = records .next ();
291+ assertFalse (records .hasNext ());
292+ assertEquals (3 , record .size ());
293+ assertEquals ("aaa" , record .get (0 ));
294+ assertEquals ("b\" bb" , record .get (1 ));
295+ assertEquals ("ccc" , record .get (2 ));
296+ }
295297 }
296298
297299 @ Test
@@ -333,7 +335,9 @@ public void testDefaultFormat() throws IOException {
333335
334336 @ Test
335337 public void testDuplicateHeadersAllowedByDefault () throws Exception {
336- CSVParser .parse ("a,b,a\n 1,2,3\n x,y,z" , CSVFormat .DEFAULT .withHeader ());
338+ try (CSVParser parser = CSVParser .parse ("a,b,a\n 1,2,3\n x,y,z" , CSVFormat .DEFAULT .withHeader ())) {
339+ // noop
340+ }
337341 }
338342
339343 @ Test
@@ -513,8 +517,8 @@ public void testFirstEndOfLineLf() throws IOException {
513517 @ Test
514518 public void testForEach () throws Exception {
515519 final List <CSVRecord > records = new ArrayList <>();
516- try (final Reader in = new StringReader ("a,b,c\n 1,2,3\n x,y,z" )) {
517- for (final CSVRecord record : CSVFormat . DEFAULT . parse ( in ) ) {
520+ try (final Reader in = new StringReader ("a,b,c\n 1,2,3\n x,y,z" ); final CSVParser parser = CSVFormat . DEFAULT . parse ( in ) ) {
521+ for (final CSVRecord record : parser ) {
518522 records .add (record );
519523 }
520524 assertEquals (3 , records .size ());
@@ -832,62 +836,72 @@ public void testGetTrailerComment_MultilineComment() throws IOException {
832836 public void testHeader () throws Exception {
833837 final Reader in = new StringReader ("a,b,c\n 1,2,3\n x,y,z" );
834838
835- final Iterator <CSVRecord > records = CSVFormat .DEFAULT .withHeader ().parse (in ).iterator ();
839+ try (final CSVParser parser = CSVFormat .DEFAULT .withHeader ().parse (in )) {
840+ final Iterator <CSVRecord > records = parser .iterator ();
836841
837- for (int i = 0 ; i < 2 ; i ++) {
838- assertTrue (records .hasNext ());
839- final CSVRecord record = records .next ();
840- assertEquals (record .get (0 ), record .get ("a" ));
841- assertEquals (record .get (1 ), record .get ("b" ));
842- assertEquals (record .get (2 ), record .get ("c" ));
843- }
842+ for (int i = 0 ; i < 2 ; i ++) {
843+ assertTrue (records .hasNext ());
844+ final CSVRecord record = records .next ();
845+ assertEquals (record .get (0 ), record .get ("a" ));
846+ assertEquals (record .get (1 ), record .get ("b" ));
847+ assertEquals (record .get (2 ), record .get ("c" ));
848+ }
844849
845- assertFalse (records .hasNext ());
850+ assertFalse (records .hasNext ());
851+ }
846852 }
847853
848854 @ Test
849855 public void testHeaderComment () throws Exception {
850856 final Reader in = new StringReader ("# comment\n a,b,c\n 1,2,3\n x,y,z" );
851857
852- final Iterator <CSVRecord > records = CSVFormat .DEFAULT .withCommentMarker ('#' ).withHeader ().parse (in ).iterator ();
858+ try (final CSVParser parser = CSVFormat .DEFAULT .withCommentMarker ('#' ).withHeader ().parse (in )) {
859+ final Iterator <CSVRecord > records = parser .iterator ();
853860
854- for (int i = 0 ; i < 2 ; i ++) {
855- assertTrue (records .hasNext ());
856- final CSVRecord record = records .next ();
857- assertEquals (record .get (0 ), record .get ("a" ));
858- assertEquals (record .get (1 ), record .get ("b" ));
859- assertEquals (record .get (2 ), record .get ("c" ));
860- }
861+ for (int i = 0 ; i < 2 ; i ++) {
862+ assertTrue (records .hasNext ());
863+ final CSVRecord record = records .next ();
864+ assertEquals (record .get (0 ), record .get ("a" ));
865+ assertEquals (record .get (1 ), record .get ("b" ));
866+ assertEquals (record .get (2 ), record .get ("c" ));
867+ }
861868
862- assertFalse (records .hasNext ());
869+ assertFalse (records .hasNext ());
870+ }
863871 }
864872
865873 @ Test
866874 public void testHeaderMissing () throws Exception {
867875 final Reader in = new StringReader ("a,,c\n 1,2,3\n x,y,z" );
868876
869- final Iterator <CSVRecord > records = CSVFormat .DEFAULT .withHeader ().withAllowMissingColumnNames ().parse (in ).iterator ();
877+ try (final CSVParser parser = CSVFormat .DEFAULT .withHeader ().withAllowMissingColumnNames ().parse (in )) {
878+ final Iterator <CSVRecord > records = parser .iterator ();
870879
871- for (int i = 0 ; i < 2 ; i ++) {
872- assertTrue (records .hasNext ());
873- final CSVRecord record = records .next ();
874- assertEquals (record .get (0 ), record .get ("a" ));
875- assertEquals (record .get (2 ), record .get ("c" ));
876- }
880+ for (int i = 0 ; i < 2 ; i ++) {
881+ assertTrue (records .hasNext ());
882+ final CSVRecord record = records .next ();
883+ assertEquals (record .get (0 ), record .get ("a" ));
884+ assertEquals (record .get (2 ), record .get ("c" ));
885+ }
877886
878- assertFalse (records .hasNext ());
887+ assertFalse (records .hasNext ());
888+ }
879889 }
880890
881891 @ Test
882892 public void testHeaderMissingWithNull () throws Exception {
883893 final Reader in = new StringReader ("a,,c,,e\n 1,2,3,4,5\n v,w,x,y,z" );
884- CSVFormat .DEFAULT .withHeader ().withNullString ("" ).withAllowMissingColumnNames ().parse (in ).iterator ();
894+ try (final CSVParser parser = CSVFormat .DEFAULT .withHeader ().withNullString ("" ).withAllowMissingColumnNames ().parse (in )) {
895+ parser .iterator ();
896+ }
885897 }
886898
887899 @ Test
888900 public void testHeadersMissing () throws Exception {
889- final Reader in = new StringReader ("a,,c,,e\n 1,2,3,4,5\n v,w,x,y,z" );
890- CSVFormat .DEFAULT .withHeader ().withAllowMissingColumnNames ().parse (in ).iterator ();
901+ try (final Reader in = new StringReader ("a,,c,,e\n 1,2,3,4,5\n v,w,x,y,z" );
902+ final CSVParser parser = CSVFormat .DEFAULT .withHeader ().withAllowMissingColumnNames ().parse (in )) {
903+ parser .iterator ();
904+ }
891905 }
892906
893907 @ Test
0 commit comments