Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id 07966200B11 for ; Mon, 13 Jun 2016 08:57:03 +0200 (CEST) Received: by cust-asf.ponee.io (Postfix) id 0623A160A5B; Mon, 13 Jun 2016 06:57:03 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id 35942160A19 for ; Mon, 13 Jun 2016 08:57:01 +0200 (CEST) Received: (qmail 91185 invoked by uid 500); 13 Jun 2016 06:57:00 -0000 Mailing-List: contact commits-help@commons.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@commons.apache.org Delivered-To: mailing list commits@commons.apache.org Received: (qmail 91173 invoked by uid 99); 13 Jun 2016 06:57:00 -0000 Received: from pnap-us-west-generic-nat.apache.org (HELO spamd3-us-west.apache.org) (209.188.14.142) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 13 Jun 2016 06:57:00 +0000 Received: from localhost (localhost [127.0.0.1]) by spamd3-us-west.apache.org (ASF Mail Server at spamd3-us-west.apache.org) with ESMTP id C0C0318028E for ; Mon, 13 Jun 2016 06:56:59 +0000 (UTC) X-Virus-Scanned: Debian amavisd-new at spamd3-us-west.apache.org X-Spam-Flag: NO X-Spam-Score: 0.374 X-Spam-Level: X-Spam-Status: No, score=0.374 tagged_above=-999 required=6.31 tests=[KAM_ASCII_DIVIDERS=0.8, KAM_LAZY_DOMAIN_SECURITY=1, RP_MATCHES_RCVD=-1.426] autolearn=disabled Received: from mx2-lw-eu.apache.org ([10.40.0.8]) by localhost (spamd3-us-west.apache.org [10.40.0.10]) (amavisd-new, port 10024) with ESMTP id BmB2lUS-H6Bd for ; Mon, 13 Jun 2016 06:56:53 +0000 (UTC) Received: from mailrelay1-us-west.apache.org (mailrelay1-us-west.apache.org [209.188.14.139]) by mx2-lw-eu.apache.org (ASF Mail Server at mx2-lw-eu.apache.org) with ESMTP id 2EC4C5F2F2 for ; Mon, 13 Jun 2016 06:56:52 +0000 (UTC) Received: from svn01-us-west.apache.org (svn.apache.org [10.41.0.6]) by mailrelay1-us-west.apache.org (ASF Mail Server at mailrelay1-us-west.apache.org) with ESMTP id 0CD43E0178 for ; Mon, 13 Jun 2016 06:56:50 +0000 (UTC) Received: from svn01-us-west.apache.org (localhost [127.0.0.1]) by svn01-us-west.apache.org (ASF Mail Server at svn01-us-west.apache.org) with ESMTP id 23D753A0A52 for ; Mon, 13 Jun 2016 06:56:50 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r1748094 [1/3] - in /commons/proper/csv/trunk/src: main/java/org/apache/commons/csv/ test/java/org/apache/commons/csv/ test/java/org/apache/commons/csv/bugs/ test/java/org/apache/commons/csv/perf/ Date: Mon, 13 Jun 2016 06:56:49 -0000 To: commits@commons.apache.org From: ggregory@apache.org X-Mailer: svnmailer-1.0.9 Message-Id: <20160613065650.23D753A0A52@svn01-us-west.apache.org> archived-at: Mon, 13 Jun 2016 06:57:03 -0000 Author: ggregory Date: Mon Jun 13 06:56:49 2016 New Revision: 1748094 URL: http://svn.apache.org/viewvc?rev=1748094&view=rev Log: Use try-with-resources. Modified: commons/proper/csv/trunk/src/main/java/org/apache/commons/csv/CSVFormat.java commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/CSVFileParserTest.java commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/CSVFormatTest.java commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/CSVParserTest.java commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/CSVPrinterTest.java commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/CSVRecordTest.java commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/ExtendedBufferedReaderTest.java commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/LexerTest.java commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/PerformanceTest.java commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/bugs/JiraCsv164Test.java commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/bugs/JiraCsv167Test.java commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/perf/PerformanceTest.java Modified: commons/proper/csv/trunk/src/main/java/org/apache/commons/csv/CSVFormat.java URL: http://svn.apache.org/viewvc/commons/proper/csv/trunk/src/main/java/org/apache/commons/csv/CSVFormat.java?rev=1748094&r1=1748093&r2=1748094&view=diff ============================================================================== --- commons/proper/csv/trunk/src/main/java/org/apache/commons/csv/CSVFormat.java (original) +++ commons/proper/csv/trunk/src/main/java/org/apache/commons/csv/CSVFormat.java Mon Jun 13 06:56:49 2016 @@ -609,8 +609,8 @@ public final class CSVFormat implements */ public String format(final Object... values) { final StringWriter out = new StringWriter(); - try { - new CSVPrinter(out, this).printRecord(values); + try (final CSVPrinter csvPrinter = new CSVPrinter(out, this)) { + csvPrinter.printRecord(values); return out.toString().trim(); } catch (final IOException e) { // should not happen because a StringWriter does not do IO. Modified: commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/CSVFileParserTest.java URL: http://svn.apache.org/viewvc/commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/CSVFileParserTest.java?rev=1748094&r1=1748093&r2=1748094&view=diff ============================================================================== --- commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/CSVFileParserTest.java (original) +++ commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/CSVFileParserTest.java Mon Jun 13 06:56:49 2016 @@ -116,19 +116,19 @@ public class CSVFileParserTest { // Now parse the file and compare against the expected results // We use a buffered reader internally so no need to create one here. - final CSVParser parser = CSVParser.parse(new File(BASE, split[0]), Charset.defaultCharset(), format); - for (final CSVRecord record : parser) { - String parsed = Arrays.toString(record.values()); - if (checkComments) { - final String comment = record.getComment().replace("\n", "\\n"); - if (comment != null) { - parsed += "#" + comment; + try (final CSVParser parser = CSVParser.parse(new File(BASE, split[0]), Charset.defaultCharset(), format)) { + for (final CSVRecord record : parser) { + String parsed = Arrays.toString(record.values()); + if (checkComments) { + final String comment = record.getComment().replace("\n", "\\n"); + if (comment != null) { + parsed += "#" + comment; + } } + final int count = record.size(); + assertEquals(testName, readTestData(), count + ":" + parsed); } - final int count = record.size(); - assertEquals(testName, readTestData(), count + ":" + parsed); } - parser.close(); } @Test @@ -160,18 +160,18 @@ public class CSVFileParserTest { // Now parse the file and compare against the expected results final URL resource = ClassLoader.getSystemResource("CSVFileParser/" + split[0]); - final CSVParser parser = CSVParser.parse(resource, Charset.forName("UTF-8"), format); - for (final CSVRecord record : parser) { - String parsed = Arrays.toString(record.values()); - if (checkComments) { - final String comment = record.getComment().replace("\n", "\\n"); - if (comment != null) { - parsed += "#" + comment; + try (final CSVParser parser = CSVParser.parse(resource, Charset.forName("UTF-8"), format)) { + for (final CSVRecord record : parser) { + String parsed = Arrays.toString(record.values()); + if (checkComments) { + final String comment = record.getComment().replace("\n", "\\n"); + if (comment != null) { + parsed += "#" + comment; + } } + final int count = record.size(); + assertEquals(testName, readTestData(), count + ":" + parsed); } - final int count = record.size(); - assertEquals(testName, readTestData(), count + ":" + parsed); } - parser.close(); } } Modified: commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/CSVFormatTest.java URL: http://svn.apache.org/viewvc/commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/CSVFormatTest.java?rev=1748094&r1=1748093&r2=1748094&view=diff ============================================================================== --- commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/CSVFormatTest.java (original) +++ commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/CSVFormatTest.java Mon Jun 13 06:56:49 2016 @@ -327,10 +327,10 @@ public class CSVFormatTest { public void testSerialization() throws Exception { final ByteArrayOutputStream out = new ByteArrayOutputStream(); - final ObjectOutputStream oos = new ObjectOutputStream(out); - oos.writeObject(CSVFormat.DEFAULT); - oos.flush(); - oos.close(); + try (final ObjectOutputStream oos = new ObjectOutputStream(out)) { + oos.writeObject(CSVFormat.DEFAULT); + oos.flush(); + } final ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(out.toByteArray())); final CSVFormat format = (CSVFormat) in.readObject(); Modified: commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/CSVParserTest.java URL: http://svn.apache.org/viewvc/commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/CSVParserTest.java?rev=1748094&r1=1748093&r2=1748094&view=diff ============================================================================== --- commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/CSVParserTest.java (original) +++ commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/CSVParserTest.java Mon Jun 13 06:56:49 2016 @@ -61,7 +61,7 @@ import org.junit.Test; public class CSVParserTest { private static final String CSV_INPUT = "a,b,c,d\n" + " a , b , 1 2 \n" + "\"foo baar\", b,\n" - // + " \"foo\n,,\n\"\",,\n\\\"\",d,e\n"; + // + " \"foo\n,,\n\"\",,\n\\\"\",d,e\n"; + " \"foo\n,,\n\"\",,\n\"\"\",d,e\n"; // changed to use standard CSV escaping private static final String CSV_INPUT_1 = "a,b,c,d"; @@ -79,7 +79,7 @@ public class CSVParserTest { // quote as the encapsulator. final String code = "one,two,three\n" // 0 - + "'',''\n" // 1) empty encapsulators + + "'',''\n" // 1) empty encapsulators + "/',/'\n" // 2) single encapsulators + "'/'','/''\n" // 3) single encapsulators encapsulated via escape + "'''',''''\n" // 4) single encapsulators encapsulated via doubling @@ -102,12 +102,12 @@ public class CSVParserTest { final CSVFormat format = CSVFormat.newFormat(',').withQuote('\'').withRecordSeparator(CRLF).withEscape('/') .withIgnoreEmptyLines(); - final CSVParser parser = CSVParser.parse(code, format); - final List records = parser.getRecords(); - assertTrue(records.size() > 0); + try (final CSVParser parser = CSVParser.parse(code, format)) { + final List records = parser.getRecords(); + assertTrue(records.size() > 0); - Utils.compare("Records do not match expected result", res, records); - parser.close(); + Utils.compare("Records do not match expected result", res, records); + } } @Test @@ -129,104 +129,98 @@ public class CSVParserTest { final CSVFormat format = CSVFormat.newFormat(',').withRecordSeparator(CRLF).withEscape('/') .withIgnoreEmptyLines(); - final CSVParser parser = CSVParser.parse(code, format); - final List records = parser.getRecords(); - assertTrue(records.size() > 0); + try (final CSVParser parser = CSVParser.parse(code, format)) { + final List records = parser.getRecords(); + assertTrue(records.size() > 0); - Utils.compare("", res, records); - parser.close(); + Utils.compare("", res, records); + } } @Test @Ignore public void testBackslashEscapingOld() throws IOException { - final String code = "one,two,three\n" + "on\\\"e,two\n" + "on\"e,two\n" + "one,\"tw\\\"o\"\n" - + "one,\"t\\,wo\"\n" + "one,two,\"th,ree\"\n" + "\"a\\\\\"\n" + "a\\,b\n" + "\"a\\\\,b\""; + final String code = "one,two,three\n" + "on\\\"e,two\n" + "on\"e,two\n" + "one,\"tw\\\"o\"\n" + + "one,\"t\\,wo\"\n" + "one,two,\"th,ree\"\n" + "\"a\\\\\"\n" + "a\\,b\n" + "\"a\\\\,b\""; final String[][] res = { { "one", "two", "three" }, { "on\\\"e", "two" }, { "on\"e", "two" }, { "one", "tw\"o" }, { "one", "t\\,wo" }, // backslash in quotes only escapes a delimiter (",") { "one", "two", "th,ree" }, { "a\\\\" }, // backslash in quotes only escapes a delimiter (",") { "a\\", "b" }, // a backslash must be returnd { "a\\\\,b" } // backslash in quotes only escapes a delimiter (",") }; - final CSVParser parser = CSVParser.parse(code, CSVFormat.DEFAULT); - final List records = parser.getRecords(); - assertEquals(res.length, records.size()); - assertTrue(records.size() > 0); - for (int i = 0; i < res.length; i++) { - assertArrayEquals(res[i], records.get(i).values()); + try (final CSVParser parser = CSVParser.parse(code, CSVFormat.DEFAULT)) { + final List records = parser.getRecords(); + assertEquals(res.length, records.size()); + assertTrue(records.size() > 0); + for (int i = 0; i < res.length; i++) { + assertArrayEquals(res[i], records.get(i).values()); + } } - parser.close(); } @Test @Ignore("CSV-107") public void testBOM() throws IOException { final URL url = ClassLoader.getSystemClassLoader().getResource("CSVFileParser/bom.csv"); - final CSVParser parser = CSVParser.parse(url, Charset.forName("UTF-8"), CSVFormat.EXCEL.withHeader()); - try { + try (final CSVParser parser = CSVParser.parse(url, Charset.forName("UTF-8"), CSVFormat.EXCEL.withHeader())) { for (final CSVRecord record : parser) { final String string = record.get("Date"); Assert.assertNotNull(string); // System.out.println("date: " + record.get("Date")); } - } finally { - parser.close(); } } @Test public void testBOMInputStream() throws IOException { final URL url = ClassLoader.getSystemClassLoader().getResource("CSVFileParser/bom.csv"); - final Reader reader = new InputStreamReader(new BOMInputStream(url.openStream()), "UTF-8"); - final CSVParser parser = new CSVParser(reader, CSVFormat.EXCEL.withHeader()); - try { + try (final Reader reader = new InputStreamReader(new BOMInputStream(url.openStream()), "UTF-8"); + final CSVParser parser = new CSVParser(reader, CSVFormat.EXCEL.withHeader())) { for (final CSVRecord record : parser) { final String string = record.get("Date"); Assert.assertNotNull(string); // System.out.println("date: " + record.get("Date")); } - } finally { - parser.close(); - reader.close(); } } @Test public void testCarriageReturnEndings() throws IOException { final String code = "foo\rbaar,\rhello,world\r,kanu"; - final CSVParser parser = CSVParser.parse(code, CSVFormat.DEFAULT); - final List records = parser.getRecords(); - assertEquals(4, records.size()); - parser.close(); + try (final CSVParser parser = CSVParser.parse(code, CSVFormat.DEFAULT)) { + final List records = parser.getRecords(); + assertEquals(4, records.size()); + } } @Test public void testCarriageReturnLineFeedEndings() throws IOException { final String code = "foo\r\nbaar,\r\nhello,world\r\n,kanu"; - final CSVParser parser = CSVParser.parse(code, CSVFormat.DEFAULT); - final List records = parser.getRecords(); - assertEquals(4, records.size()); - parser.close(); + try (final CSVParser parser = CSVParser.parse(code, CSVFormat.DEFAULT)) { + final List records = parser.getRecords(); + assertEquals(4, records.size()); + } } @Test(expected = NoSuchElementException.class) public void testClose() throws Exception { final Reader in = new StringReader("# comment\na,b,c\n1,2,3\nx,y,z"); - final CSVParser parser = CSVFormat.DEFAULT.withCommentMarker('#').withHeader().parse(in); - final Iterator records = parser.iterator(); - assertTrue(records.hasNext()); - parser.close(); + final Iterator records; + try (final CSVParser parser = CSVFormat.DEFAULT.withCommentMarker('#').withHeader().parse(in)) { + records = parser.iterator(); + assertTrue(records.hasNext()); + } assertFalse(records.hasNext()); records.next(); } @Test public void testCSV57() throws Exception { - final CSVParser parser = CSVParser.parse("", CSVFormat.DEFAULT); - final List list = parser.getRecords(); - assertNotNull(list); - assertEquals(0, list.size()); - parser.close(); + try (final CSVParser parser = CSVParser.parse("", CSVFormat.DEFAULT)) { + final List list = parser.getRecords(); + assertNotNull(list); + assertEquals(0, list.size()); + } } @Test @@ -235,27 +229,26 @@ public class CSVParserTest { + "\"\n\",\" \",#\n" // 2) + "#,\"\"\n" // 3) + "# Final comment\n"// 4) - ; + ; final String[][] res = { { "a", "b#" }, { "\n", " ", "#" }, { "#", "" }, { "# Final comment" } }; CSVFormat format = CSVFormat.DEFAULT; assertFalse(format.isCommentMarkerSet()); + final String[][] res_comments = { { "a", "b#" }, { "\n", " ", "#" }, }; - CSVParser parser = CSVParser.parse(code, format); - List records = parser.getRecords(); - assertTrue(records.size() > 0); - - Utils.compare("Failed to parse without comments", res, records); + try (final CSVParser parser = CSVParser.parse(code, format)) { + List records = parser.getRecords(); + assertTrue(records.size() > 0); - final String[][] res_comments = { { "a", "b#" }, { "\n", " ", "#" }, }; + Utils.compare("Failed to parse without comments", res, records); - format = CSVFormat.DEFAULT.withCommentMarker('#'); - parser.close(); - parser = CSVParser.parse(code, format); - records = parser.getRecords(); + format = CSVFormat.DEFAULT.withCommentMarker('#'); + } + try (final CSVParser parser = CSVParser.parse(code, format)) { + List records = parser.getRecords(); - Utils.compare("Failed to parse with comments", res_comments, records); - parser.close(); + Utils.compare("Failed to parse with comments", res_comments, records); + } } @Test(expected = IllegalArgumentException.class) @@ -265,9 +258,9 @@ public class CSVParserTest { @Test public void testEmptyFile() throws Exception { - final CSVParser parser = CSVParser.parse("", CSVFormat.DEFAULT); - assertNull(parser.nextRecord()); - parser.close(); + try (final CSVParser parser = CSVParser.parse("", CSVFormat.DEFAULT)) { + assertNull(parser.nextRecord()); + } } @Test @@ -276,14 +269,14 @@ public class CSVParserTest { final String[][] res = { { "hello", "" } // CSV format ignores empty lines }; for (final String code : codes) { - final CSVParser parser = CSVParser.parse(code, CSVFormat.DEFAULT); - final List records = parser.getRecords(); - assertEquals(res.length, records.size()); - assertTrue(records.size() > 0); - for (int i = 0; i < res.length; i++) { - assertArrayEquals(res[i], records.get(i).values()); + try (final CSVParser parser = CSVParser.parse(code, CSVFormat.DEFAULT)) { + final List records = parser.getRecords(); + assertEquals(res.length, records.size()); + assertTrue(records.size() > 0); + for (int i = 0; i < res.length; i++) { + assertArrayEquals(res[i], records.get(i).values()); + } } - parser.close(); } } @@ -293,14 +286,14 @@ public class CSVParserTest { final String[][] res = { { "hello", "" }, { "" }, // Excel format does not ignore empty lines { "" } }; for (final String code : codes) { - final CSVParser parser = CSVParser.parse(code, CSVFormat.EXCEL); - final List records = parser.getRecords(); - assertEquals(res.length, records.size()); - assertTrue(records.size() > 0); - for (int i = 0; i < res.length; i++) { - assertArrayEquals(res[i], records.get(i).values()); + try (final CSVParser parser = CSVParser.parse(code, CSVFormat.EXCEL)) { + final List records = parser.getRecords(); + assertEquals(res.length, records.size()); + assertTrue(records.size() > 0); + for (int i = 0; i < res.length; i++) { + assertArrayEquals(res[i], records.get(i).values()); + } } - parser.close(); } } @@ -312,14 +305,14 @@ public class CSVParserTest { final String[][] res = { { "hello", "" }, // CSV format ignores empty lines { "world", "" } }; for (final String code : codes) { - final CSVParser parser = CSVParser.parse(code, CSVFormat.DEFAULT); - final List records = parser.getRecords(); - assertEquals(res.length, records.size()); - assertTrue(records.size() > 0); - for (int i = 0; i < res.length; i++) { - assertArrayEquals(res[i], records.get(i).values()); + try (final CSVParser parser = CSVParser.parse(code, CSVFormat.DEFAULT)) { + final List records = parser.getRecords(); + assertEquals(res.length, records.size()); + assertTrue(records.size() > 0); + for (int i = 0; i < res.length; i++) { + assertArrayEquals(res[i], records.get(i).values()); + } } - parser.close(); } } @@ -332,45 +325,45 @@ public class CSVParserTest { { "world", "" } }; for (final String code : codes) { - final CSVParser parser = CSVParser.parse(code, CSVFormat.EXCEL); - final List records = parser.getRecords(); - assertEquals(res.length, records.size()); - assertTrue(records.size() > 0); - for (int i = 0; i < res.length; i++) { - assertArrayEquals(res[i], records.get(i).values()); + try (final CSVParser parser = CSVParser.parse(code, CSVFormat.EXCEL)) { + final List records = parser.getRecords(); + assertEquals(res.length, records.size()); + assertTrue(records.size() > 0); + for (int i = 0; i < res.length; i++) { + assertArrayEquals(res[i], records.get(i).values()); + } } - parser.close(); } } @Test public void testExcelFormat1() throws IOException { - final String code = "value1,value2,value3,value4\r\na,b,c,d\r\n x,,," - + "\r\n\r\n\"\"\"hello\"\"\",\" \"\"world\"\"\",\"abc\ndef\",\r\n"; + final String code = "value1,value2,value3,value4\r\na,b,c,d\r\n x,,," + + "\r\n\r\n\"\"\"hello\"\"\",\" \"\"world\"\"\",\"abc\ndef\",\r\n"; final String[][] res = { { "value1", "value2", "value3", "value4" }, { "a", "b", "c", "d" }, { " x", "", "", "" }, { "" }, { "\"hello\"", " \"world\"", "abc\ndef", "" } }; - final CSVParser parser = CSVParser.parse(code, CSVFormat.EXCEL); - final List records = parser.getRecords(); - assertEquals(res.length, records.size()); - assertTrue(records.size() > 0); - for (int i = 0; i < res.length; i++) { - assertArrayEquals(res[i], records.get(i).values()); + try (final CSVParser parser = CSVParser.parse(code, CSVFormat.EXCEL)) { + final List records = parser.getRecords(); + assertEquals(res.length, records.size()); + assertTrue(records.size() > 0); + for (int i = 0; i < res.length; i++) { + assertArrayEquals(res[i], records.get(i).values()); + } } - parser.close(); } @Test public void testExcelFormat2() throws Exception { final String code = "foo,baar\r\n\r\nhello,\r\n\r\nworld,\r\n"; final String[][] res = { { "foo", "baar" }, { "" }, { "hello", "" }, { "" }, { "world", "" } }; - final CSVParser parser = CSVParser.parse(code, CSVFormat.EXCEL); - final List records = parser.getRecords(); - assertEquals(res.length, records.size()); - assertTrue(records.size() > 0); - for (int i = 0; i < res.length; i++) { - assertArrayEquals(res[i], records.get(i).values()); + try (final CSVParser parser = CSVParser.parse(code, CSVFormat.EXCEL)) { + final List records = parser.getRecords(); + assertEquals(res.length, records.size()); + assertTrue(records.size() > 0); + for (int i = 0; i < res.length; i++) { + assertArrayEquals(res[i], records.get(i).values()); + } } - parser.close(); } /** @@ -379,67 +372,63 @@ public class CSVParserTest { @Test public void testExcelHeaderCountLessThanData() throws Exception { final String code = "A,B,C,,\r\na,b,c,d,e\r\n"; - final CSVParser parser = CSVParser.parse(code, CSVFormat.EXCEL.withHeader()); - try { + try (final CSVParser parser = CSVParser.parse(code, CSVFormat.EXCEL.withHeader())) { for (final CSVRecord record : parser.getRecords()) { Assert.assertEquals("a", record.get("A")); Assert.assertEquals("b", record.get("B")); Assert.assertEquals("c", record.get("C")); } - } finally { - parser.close(); } } @Test public void testForEach() throws Exception { final List records = new ArrayList<>(); - - final Reader in = new StringReader("a,b,c\n1,2,3\nx,y,z"); - - for (final CSVRecord record : CSVFormat.DEFAULT.parse(in)) { - records.add(record); + try (final Reader in = new StringReader("a,b,c\n1,2,3\nx,y,z")) { + for (final CSVRecord record : CSVFormat.DEFAULT.parse(in)) { + records.add(record); + } + assertEquals(3, records.size()); + assertArrayEquals(new String[] { "a", "b", "c" }, records.get(0).values()); + assertArrayEquals(new String[] { "1", "2", "3" }, records.get(1).values()); + assertArrayEquals(new String[] { "x", "y", "z" }, records.get(2).values()); } - - assertEquals(3, records.size()); - assertArrayEquals(new String[] { "a", "b", "c" }, records.get(0).values()); - assertArrayEquals(new String[] { "1", "2", "3" }, records.get(1).values()); - assertArrayEquals(new String[] { "x", "y", "z" }, records.get(2).values()); } @Test public void testGetHeaderMap() throws Exception { - final CSVParser parser = CSVParser.parse("a,b,c\n1,2,3\nx,y,z", CSVFormat.DEFAULT.withHeader("A", "B", "C")); - final Map headerMap = parser.getHeaderMap(); - final Iterator columnNames = headerMap.keySet().iterator(); - // Headers are iterated in column order. - Assert.assertEquals("A", columnNames.next()); - Assert.assertEquals("B", columnNames.next()); - Assert.assertEquals("C", columnNames.next()); - final Iterator records = parser.iterator(); + try (final CSVParser parser = CSVParser.parse("a,b,c\n1,2,3\nx,y,z", + CSVFormat.DEFAULT.withHeader("A", "B", "C"))) { + final Map headerMap = parser.getHeaderMap(); + final Iterator columnNames = headerMap.keySet().iterator(); + // Headers are iterated in column order. + Assert.assertEquals("A", columnNames.next()); + Assert.assertEquals("B", columnNames.next()); + Assert.assertEquals("C", columnNames.next()); + final Iterator records = parser.iterator(); + + // Parse to make sure getHeaderMap did not have a side-effect. + for (int i = 0; i < 3; i++) { + assertTrue(records.hasNext()); + final CSVRecord record = records.next(); + assertEquals(record.get(0), record.get("A")); + assertEquals(record.get(1), record.get("B")); + assertEquals(record.get(2), record.get("C")); + } - // Parse to make sure getHeaderMap did not have a side-effect. - for (int i = 0; i < 3; i++) { - assertTrue(records.hasNext()); - final CSVRecord record = records.next(); - assertEquals(record.get(0), record.get("A")); - assertEquals(record.get(1), record.get("B")); - assertEquals(record.get(2), record.get("C")); + assertFalse(records.hasNext()); } - - assertFalse(records.hasNext()); - parser.close(); } @Test public void testGetLine() throws IOException { - final CSVParser parser = CSVParser.parse(CSV_INPUT, CSVFormat.DEFAULT.withIgnoreSurroundingSpaces()); - for (final String[] re : RESULT) { - assertArrayEquals(re, parser.nextRecord().values()); - } + try (final CSVParser parser = CSVParser.parse(CSV_INPUT, CSVFormat.DEFAULT.withIgnoreSurroundingSpaces())) { + for (final String[] re : RESULT) { + assertArrayEquals(re, parser.nextRecord().values()); + } - assertNull(parser.nextRecord()); - parser.close(); + assertNull(parser.nextRecord()); + } } @Test @@ -459,10 +448,10 @@ public class CSVParserTest { @Test public void testGetOneLine() throws IOException { - final CSVParser parser = CSVParser.parse(CSV_INPUT_1, CSVFormat.DEFAULT); - final CSVRecord record = parser.getRecords().get(0); - assertArrayEquals(RESULT[0], record.values()); - parser.close(); + try (final CSVParser parser = CSVParser.parse(CSV_INPUT_1, CSVFormat.DEFAULT)) { + final CSVRecord record = parser.getRecords().get(0); + assertArrayEquals(RESULT[0], record.values()); + } } /** @@ -472,11 +461,9 @@ public class CSVParserTest { */ @Test public void testGetOneLineOneParser() throws IOException { - final PipedWriter writer = new PipedWriter(); - final PipedReader reader = new PipedReader(writer); final CSVFormat format = CSVFormat.DEFAULT; - final CSVParser parser = new CSVParser(reader, format); - try { + try (final PipedWriter writer = new PipedWriter(); + final CSVParser parser = new CSVParser(new PipedReader(writer), format)) { writer.append(CSV_INPUT_1); writer.append(format.getRecordSeparator()); final CSVRecord record1 = parser.nextRecord(); @@ -485,8 +472,6 @@ public class CSVParserTest { writer.append(format.getRecordSeparator()); final CSVRecord record2 = parser.nextRecord(); assertArrayEquals(RESULT[1], record2.values()); - } finally { - parser.close(); } } @@ -517,39 +502,40 @@ public class CSVParserTest { @Test public void testGetRecords() throws IOException { - final CSVParser parser = CSVParser.parse(CSV_INPUT, CSVFormat.DEFAULT.withIgnoreSurroundingSpaces()); - final List records = parser.getRecords(); - assertEquals(RESULT.length, records.size()); - assertTrue(records.size() > 0); - for (int i = 0; i < RESULT.length; i++) { - assertArrayEquals(RESULT[i], records.get(i).values()); + try (final CSVParser parser = CSVParser.parse(CSV_INPUT, CSVFormat.DEFAULT.withIgnoreSurroundingSpaces())) { + final List records = parser.getRecords(); + assertEquals(RESULT.length, records.size()); + assertTrue(records.size() > 0); + for (int i = 0; i < RESULT.length; i++) { + assertArrayEquals(RESULT[i], records.get(i).values()); + } } - parser.close(); } @Test public void testGetRecordWithMultiLineValues() throws Exception { - final CSVParser parser = CSVParser.parse("\"a\r\n1\",\"a\r\n2\"" + CRLF + "\"b\r\n1\",\"b\r\n2\"" + CRLF + - "\"c\r\n1\",\"c\r\n2\"", CSVFormat.DEFAULT.withRecordSeparator(CRLF)); - CSVRecord record; - assertEquals(0, parser.getRecordNumber()); - assertEquals(0, parser.getCurrentLineNumber()); - assertNotNull(record = parser.nextRecord()); - assertEquals(3, parser.getCurrentLineNumber()); - assertEquals(1, record.getRecordNumber()); - assertEquals(1, parser.getRecordNumber()); - assertNotNull(record = parser.nextRecord()); - assertEquals(6, parser.getCurrentLineNumber()); - assertEquals(2, record.getRecordNumber()); - assertEquals(2, parser.getRecordNumber()); - assertNotNull(record = parser.nextRecord()); - assertEquals(8, parser.getCurrentLineNumber()); - assertEquals(3, record.getRecordNumber()); - assertEquals(3, parser.getRecordNumber()); - assertNull(record = parser.nextRecord()); - assertEquals(8, parser.getCurrentLineNumber()); - assertEquals(3, parser.getRecordNumber()); - parser.close(); + try (final CSVParser parser = CSVParser.parse( + "\"a\r\n1\",\"a\r\n2\"" + CRLF + "\"b\r\n1\",\"b\r\n2\"" + CRLF + "\"c\r\n1\",\"c\r\n2\"", + CSVFormat.DEFAULT.withRecordSeparator(CRLF))) { + CSVRecord record; + assertEquals(0, parser.getRecordNumber()); + assertEquals(0, parser.getCurrentLineNumber()); + assertNotNull(record = parser.nextRecord()); + assertEquals(3, parser.getCurrentLineNumber()); + assertEquals(1, record.getRecordNumber()); + assertEquals(1, parser.getRecordNumber()); + assertNotNull(record = parser.nextRecord()); + assertEquals(6, parser.getCurrentLineNumber()); + assertEquals(2, record.getRecordNumber()); + assertEquals(2, parser.getRecordNumber()); + assertNotNull(record = parser.nextRecord()); + assertEquals(8, parser.getCurrentLineNumber()); + assertEquals(3, record.getRecordNumber()); + assertEquals(3, parser.getRecordNumber()); + assertNull(record = parser.nextRecord()); + assertEquals(8, parser.getCurrentLineNumber()); + assertEquals(3, parser.getRecordNumber()); + } } @Test @@ -636,16 +622,18 @@ public class CSVParserTest { final String code = "\nfoo,baar\n\r\n,\n\n,world\r\n\n"; // String code = "world\r\n\n"; // String code = "foo;baar\r\n\r\nhello;\r\n\r\nworld;\r\n"; - final CSVParser parser = CSVParser.parse(code, CSVFormat.DEFAULT); - final List records = parser.getRecords(); - assertEquals(3, records.size()); - parser.close(); + try (final CSVParser parser = CSVParser.parse(code, CSVFormat.DEFAULT)) { + final List records = parser.getRecords(); + assertEquals(3, records.size()); + } } @Test(expected = IllegalArgumentException.class) public void testInvalidFormat() throws Exception { final CSVFormat invalidFormat = CSVFormat.DEFAULT.withDelimiter(CR); - new CSVParser(null, invalidFormat).close(); + try (final CSVParser parser = new CSVParser(null, invalidFormat)) { + Assert.fail("This test should have thrown an exception."); + } } @Test @@ -680,17 +668,17 @@ public class CSVParserTest { @Test public void testLineFeedEndings() throws IOException { final String code = "foo\nbaar,\nhello,world\n,kanu"; - final CSVParser parser = CSVParser.parse(code, CSVFormat.DEFAULT); - final List records = parser.getRecords(); - assertEquals(4, records.size()); - parser.close(); + try (final CSVParser parser = CSVParser.parse(code, CSVFormat.DEFAULT)) { + final List records = parser.getRecords(); + assertEquals(4, records.size()); + } } @Test public void testMappedButNotSetAsOutlook2007ContactExport() throws Exception { final Reader in = new StringReader("a,b,c\n1,2\nx,y,z"); - final Iterator records = CSVFormat.DEFAULT.withHeader("A", "B", "C").withSkipHeaderRecord() - .parse(in).iterator(); + final Iterator records = CSVFormat.DEFAULT.withHeader("A", "B", "C").withSkipHeaderRecord().parse(in) + .iterator(); CSVRecord record; // 1st record @@ -724,38 +712,41 @@ public class CSVParserTest { @Test // TODO this may lead to strange behavior, throw an exception if iterator() has already been called? public void testMultipleIterators() throws Exception { - final CSVParser parser = CSVParser.parse("a,b,c" + CR + "d,e,f", CSVFormat.DEFAULT); - - final Iterator itr1 = parser.iterator(); - final Iterator itr2 = parser.iterator(); - - final CSVRecord first = itr1.next(); - assertEquals("a", first.get(0)); - assertEquals("b", first.get(1)); - assertEquals("c", first.get(2)); - - final CSVRecord second = itr2.next(); - assertEquals("d", second.get(0)); - assertEquals("e", second.get(1)); - assertEquals("f", second.get(2)); - parser.close(); + try (final CSVParser parser = CSVParser.parse("a,b,c" + CR + "d,e,f", CSVFormat.DEFAULT)) { + final Iterator itr1 = parser.iterator(); + final Iterator itr2 = parser.iterator(); + + final CSVRecord first = itr1.next(); + assertEquals("a", first.get(0)); + assertEquals("b", first.get(1)); + assertEquals("c", first.get(2)); + + final CSVRecord second = itr2.next(); + assertEquals("d", second.get(0)); + assertEquals("e", second.get(1)); + assertEquals("f", second.get(2)); + } } @Test(expected = IllegalArgumentException.class) public void testNewCSVParserNullReaderFormat() throws Exception { - new CSVParser(null, CSVFormat.DEFAULT).close(); + try (final CSVParser parser = new CSVParser(null, CSVFormat.DEFAULT)) { + Assert.fail("This test should have thrown an exception."); + } } @Test(expected = IllegalArgumentException.class) public void testNewCSVParserReaderNullFormat() throws Exception { - new CSVParser(new StringReader(""), null).close(); + try (final CSVParser parser = new CSVParser(new StringReader(""), null)) { + Assert.fail("This test should have thrown an exception."); + } } @Test public void testNoHeaderMap() throws Exception { - final CSVParser parser = CSVParser.parse("a,b,c\n1,2,3\nx,y,z", CSVFormat.DEFAULT); - Assert.assertNull(parser.getHeaderMap()); - parser.close(); + try (final CSVParser parser = CSVParser.parse("a,b,c\n1,2,3\nx,y,z", CSVFormat.DEFAULT)) { + Assert.assertNull(parser.getHeaderMap()); + } } @Test(expected = IllegalArgumentException.class) @@ -780,8 +771,9 @@ public class CSVParserTest { @Test(expected = IllegalArgumentException.class) public void testParserUrlNullCharsetFormat() throws Exception { - final CSVParser parser = CSVParser.parse(new URL("http://commons.apache.org"), null, CSVFormat.DEFAULT); - parser.close(); + try (final CSVParser parser = CSVParser.parse(new URL("http://commons.apache.org"), null, CSVFormat.DEFAULT)) { + Assert.fail("This test should have thrown an exception."); + } } @Test(expected = IllegalArgumentException.class) @@ -791,8 +783,9 @@ public class CSVParserTest { @Test(expected = IllegalArgumentException.class) public void testParseUrlCharsetNullFormat() throws Exception { - final CSVParser parser = CSVParser.parse(new URL("http://commons.apache.org"), Charset.defaultCharset(), null); - parser.close(); + try (final CSVParser parser = CSVParser.parse(new URL("http://commons.apache.org"), Charset.defaultCharset(), null)) { + Assert.fail("This test should have thrown an exception."); + } } @Test @@ -840,13 +833,13 @@ public class CSVParserTest { @Test public void testRoundtrip() throws Exception { final StringWriter out = new StringWriter(); - final CSVPrinter printer = new CSVPrinter(out, CSVFormat.DEFAULT); - final String input = "a,b,c\r\n1,2,3\r\nx,y,z\r\n"; - for (final CSVRecord record : CSVParser.parse(input, CSVFormat.DEFAULT)) { - printer.printRecord(record); + try (final CSVPrinter printer = new CSVPrinter(out, CSVFormat.DEFAULT)) { + final String input = "a,b,c\r\n1,2,3\r\nx,y,z\r\n"; + for (final CSVRecord record : CSVParser.parse(input, CSVFormat.DEFAULT)) { + printer.printRecord(record); + } + assertEquals(input, out.toString()); } - assertEquals(input, out.toString()); - printer.close(); } @Test @@ -858,12 +851,12 @@ public class CSVParserTest { assertEquals("2", record.get("b")); assertEquals("3", record.get("c")); } - + @Test public void testSkipHeaderOverrideDuplicateHeaders() throws Exception { final Reader in = new StringReader("a,a,a\n1,2,3\nx,y,z"); - final Iterator records = CSVFormat.DEFAULT.withHeader("X", "Y", "Z").withSkipHeaderRecord() - .parse(in).iterator(); + final Iterator records = CSVFormat.DEFAULT.withHeader("X", "Y", "Z").withSkipHeaderRecord().parse(in) + .iterator(); final CSVRecord record = records.next(); assertEquals("1", record.get("X")); assertEquals("2", record.get("Y")); @@ -873,8 +866,8 @@ public class CSVParserTest { @Test public void testSkipSetAltHeaders() throws Exception { final Reader in = new StringReader("a,b,c\n1,2,3\nx,y,z"); - final Iterator records = CSVFormat.DEFAULT.withHeader("X", "Y", "Z").withSkipHeaderRecord() - .parse(in).iterator(); + final Iterator records = CSVFormat.DEFAULT.withHeader("X", "Y", "Z").withSkipHeaderRecord().parse(in) + .iterator(); final CSVRecord record = records.next(); assertEquals("1", record.get("X")); assertEquals("2", record.get("Y")); @@ -884,8 +877,8 @@ public class CSVParserTest { @Test public void testSkipSetHeader() throws Exception { final Reader in = new StringReader("a,b,c\n1,2,3\nx,y,z"); - final Iterator records = CSVFormat.DEFAULT.withHeader("a", "b", "c").withSkipHeaderRecord() - .parse(in).iterator(); + final Iterator records = CSVFormat.DEFAULT.withHeader("a", "b", "c").withSkipHeaderRecord().parse(in) + .iterator(); final CSVRecord record = records.next(); assertEquals("1", record.get("a")); assertEquals("2", record.get("b")); @@ -895,27 +888,27 @@ public class CSVParserTest { @Test @Ignore public void testStartWithEmptyLinesThenHeaders() throws Exception { - 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"}; - final String[][] res = {{"hello", ""}, {""}, // Excel format does not ignore empty lines - {""}}; + 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" }; + final String[][] res = { { "hello", "" }, { "" }, // Excel format does not ignore empty lines + { "" } }; for (final String code : codes) { - final CSVParser parser = CSVParser.parse(code, CSVFormat.EXCEL); - final List records = parser.getRecords(); - assertEquals(res.length, records.size()); - assertTrue(records.size() > 0); - for (int i = 0; i < res.length; i++) { - assertArrayEquals(res[i], records.get(i).values()); + try (final CSVParser parser = CSVParser.parse(code, CSVFormat.EXCEL)) { + final List records = parser.getRecords(); + assertEquals(res.length, records.size()); + assertTrue(records.size() > 0); + for (int i = 0; i < res.length; i++) { + assertArrayEquals(res[i], records.get(i).values()); + } } - parser.close(); } } @Test public void testTrailingDelimiter() throws Exception { final Reader in = new StringReader("a,a,a,\n\"1\",\"2\",\"3\",\nx,y,z,"); - final Iterator records = CSVFormat.DEFAULT.withHeader("X", "Y", "Z").withSkipHeaderRecord().withTrailingDelimiter() - .parse(in).iterator(); + final Iterator records = CSVFormat.DEFAULT.withHeader("X", "Y", "Z").withSkipHeaderRecord() + .withTrailingDelimiter().parse(in).iterator(); final CSVRecord record = records.next(); assertEquals("1", record.get("X")); assertEquals("2", record.get("Y")); @@ -926,8 +919,8 @@ public class CSVParserTest { @Test public void testTrim() throws Exception { final Reader in = new StringReader("a,a,a\n\" 1 \",\" 2 \",\" 3 \"\nx,y,z"); - final Iterator records = CSVFormat.DEFAULT.withHeader("X", "Y", "Z").withSkipHeaderRecord().withTrim() - .parse(in).iterator(); + final Iterator records = CSVFormat.DEFAULT.withHeader("X", "Y", "Z").withSkipHeaderRecord() + .withTrim().parse(in).iterator(); final CSVRecord record = records.next(); assertEquals("1", record.get("X")); assertEquals("2", record.get("Y")); @@ -936,46 +929,46 @@ public class CSVParserTest { } private void validateLineNumbers(final String lineSeparator) throws IOException { - final CSVParser parser = CSVParser.parse("a" + lineSeparator + "b" + lineSeparator + "c", - CSVFormat.DEFAULT.withRecordSeparator(lineSeparator)); - assertEquals(0, parser.getCurrentLineNumber()); - assertNotNull(parser.nextRecord()); - assertEquals(1, parser.getCurrentLineNumber()); - assertNotNull(parser.nextRecord()); - assertEquals(2, parser.getCurrentLineNumber()); - assertNotNull(parser.nextRecord()); - // Still 2 because the last line is does not have EOL chars - assertEquals(2, parser.getCurrentLineNumber()); - assertNull(parser.nextRecord()); - // Still 2 because the last line is does not have EOL chars - assertEquals(2, parser.getCurrentLineNumber()); - parser.close(); + try (final CSVParser parser = CSVParser.parse("a" + lineSeparator + "b" + lineSeparator + "c", + CSVFormat.DEFAULT.withRecordSeparator(lineSeparator))) { + assertEquals(0, parser.getCurrentLineNumber()); + assertNotNull(parser.nextRecord()); + assertEquals(1, parser.getCurrentLineNumber()); + assertNotNull(parser.nextRecord()); + assertEquals(2, parser.getCurrentLineNumber()); + assertNotNull(parser.nextRecord()); + // Still 2 because the last line is does not have EOL chars + assertEquals(2, parser.getCurrentLineNumber()); + assertNull(parser.nextRecord()); + // Still 2 because the last line is does not have EOL chars + assertEquals(2, parser.getCurrentLineNumber()); + } } private void validateRecordNumbers(final String lineSeparator) throws IOException { - final CSVParser parser = CSVParser.parse("a" + lineSeparator + "b" + lineSeparator + "c", - CSVFormat.DEFAULT.withRecordSeparator(lineSeparator)); - CSVRecord record; - assertEquals(0, parser.getRecordNumber()); - assertNotNull(record = parser.nextRecord()); - assertEquals(1, record.getRecordNumber()); - assertEquals(1, parser.getRecordNumber()); - assertNotNull(record = parser.nextRecord()); - assertEquals(2, record.getRecordNumber()); - assertEquals(2, parser.getRecordNumber()); - assertNotNull(record = parser.nextRecord()); - assertEquals(3, record.getRecordNumber()); - assertEquals(3, parser.getRecordNumber()); - assertNull(record = parser.nextRecord()); - assertEquals(3, parser.getRecordNumber()); - parser.close(); + try (final CSVParser parser = CSVParser.parse("a" + lineSeparator + "b" + lineSeparator + "c", + CSVFormat.DEFAULT.withRecordSeparator(lineSeparator))) { + CSVRecord record; + assertEquals(0, parser.getRecordNumber()); + assertNotNull(record = parser.nextRecord()); + assertEquals(1, record.getRecordNumber()); + assertEquals(1, parser.getRecordNumber()); + assertNotNull(record = parser.nextRecord()); + assertEquals(2, record.getRecordNumber()); + assertEquals(2, parser.getRecordNumber()); + assertNotNull(record = parser.nextRecord()); + assertEquals(3, record.getRecordNumber()); + assertEquals(3, parser.getRecordNumber()); + assertNull(record = parser.nextRecord()); + assertEquals(3, parser.getRecordNumber()); + } } private void validateRecordPosition(final String lineSeparator) throws IOException { final String nl = lineSeparator; // used as linebreak in values for better distinction final String code = "a,b,c" + lineSeparator + "1,2,3" + lineSeparator + - // to see if recordPosition correctly points to the enclosing quote + // to see if recordPosition correctly points to the enclosing quote "'A" + nl + "A','B" + nl + "B',CC" + lineSeparator + // unicode test... not very relevant while operating on strings instead of bytes, but for // completeness...