Use try-with-resources

This commit is contained in:
Gary Gregory 2024-09-14 09:41:02 -04:00
parent 1667e5734d
commit ae79aeaa0b
1 changed files with 8 additions and 12 deletions

View File

@ -94,22 +94,18 @@ public class JiraCsv290Test {
@Test @Test
public void testWriteThenRead() throws Exception { public void testWriteThenRead() throws Exception {
final StringWriter sw = new StringWriter(); final StringWriter sw = new StringWriter();
try (CSVPrinter printer = new CSVPrinter(sw, CSVFormat.POSTGRESQL_CSV.builder().setHeader().setSkipHeaderRecord(true).build())) { try (CSVPrinter printer = new CSVPrinter(sw, CSVFormat.POSTGRESQL_CSV.builder().setHeader().setSkipHeaderRecord(true).build())) {
printer.printRecord("column1", "column2"); printer.printRecord("column1", "column2");
printer.printRecord("v11", "v12"); printer.printRecord("v11", "v12");
printer.printRecord("v21", "v22"); printer.printRecord("v21", "v22");
printer.close(); printer.close();
try (CSVParser parser = new CSVParser(new StringReader(sw.toString()),
final CSVParser parser = new CSVParser(new StringReader(sw.toString()), CSVFormat.POSTGRESQL_CSV.builder().setHeader().setSkipHeaderRecord(true).build())) {
CSVFormat.POSTGRESQL_CSV.builder().setHeader().setSkipHeaderRecord(true).build()); assertArrayEquals(new Object[] { "column1", "column2" }, parser.getHeaderNames().toArray());
final Iterator<CSVRecord> i = parser.iterator();
assertArrayEquals(new Object[] { "column1", "column2" }, parser.getHeaderNames().toArray()); assertArrayEquals(new String[] { "v11", "v12" }, i.next().toList().toArray());
assertArrayEquals(new String[] { "v21", "v22" }, i.next().toList().toArray());
final Iterator<CSVRecord> i = parser.iterator(); }
assertArrayEquals(new String[] { "v11", "v12" }, i.next().toList().toArray());
assertArrayEquals(new String[] { "v21", "v22" }, i.next().toList().toArray());
} }
} }
} }