Use try-with-resources.
This commit is contained in:
parent
5d16917835
commit
dc5d034a47
|
@ -282,9 +282,8 @@ public class CSVPrinterTest {
|
|||
@Test
|
||||
public void testCloseWithFlushOn() throws IOException {
|
||||
try (final Writer writer = mock(Writer.class)) {
|
||||
final CSVFormat csvFormat = CSVFormat.DEFAULT;
|
||||
@SuppressWarnings("resource")
|
||||
final CSVPrinter csvPrinter = new CSVPrinter(writer, csvFormat);
|
||||
final CSVPrinter csvPrinter = new CSVPrinter(writer, CSVFormat.DEFAULT);
|
||||
csvPrinter.close(true);
|
||||
verify(writer, times(1)).flush();
|
||||
}
|
||||
|
@ -328,8 +327,8 @@ public class CSVPrinterTest {
|
|||
@Test
|
||||
public void testCSV259() throws IOException {
|
||||
final StringWriter sw = new StringWriter();
|
||||
final Reader reader = new FileReader("src/test/resources/org/apache/commons/csv/CSV-259/sample.txt");
|
||||
try (final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT.withEscape('!').withQuote(null))) {
|
||||
try (final Reader reader = new FileReader("src/test/resources/org/apache/commons/csv/CSV-259/sample.txt");
|
||||
final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT.withEscape('!').withQuote(null))) {
|
||||
printer.print(reader);
|
||||
assertEquals("x!,y!,z", sw.toString());
|
||||
}
|
||||
|
@ -624,12 +623,12 @@ public class CSVPrinterTest {
|
|||
try (final Connection connection = getH2Connection()) {
|
||||
setUpTable(connection);
|
||||
try (final Statement stmt = connection.createStatement();
|
||||
final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT)) {
|
||||
printer.printRecords(stmt.executeQuery("select ID, NAME, TEXT from TEST"));
|
||||
final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT);
|
||||
final ResultSet resultSet = stmt.executeQuery("select ID, NAME, TEXT from TEST");) {
|
||||
printer.printRecords(resultSet);
|
||||
}
|
||||
}
|
||||
assertEquals("1,r1,\"long text 1\"" + recordSeparator + "2,r2,\"" + longText2 + "\"" + recordSeparator,
|
||||
sw.toString());
|
||||
assertEquals("1,r1,\"long text 1\"" + recordSeparator + "2,r2,\"" + longText2 + "\"" + recordSeparator, sw.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
|
@ -20,7 +20,6 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
|
|||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.Reader;
|
||||
|
||||
|
@ -83,7 +82,6 @@ public class JiraCsv167Test {
|
|||
}
|
||||
|
||||
private Reader getTestInput() {
|
||||
final InputStream is = ClassLoader.getSystemClassLoader().getResourceAsStream("org/apache/commons/csv/csv-167/sample1.csv");
|
||||
return new InputStreamReader(is);
|
||||
return new InputStreamReader(ClassLoader.getSystemClassLoader().getResourceAsStream("org/apache/commons/csv/csv-167/sample1.csv"));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -37,12 +37,12 @@ public class JiraCsv211Test {
|
|||
assertEquals("ID\tName\tCountry\tAge\r\n1\tJane Doe\tUSA\t", formatted);
|
||||
|
||||
final CSVFormat parseFormat = CSVFormat.DEFAULT.withDelimiter('\t').withFirstRecordAsHeader();
|
||||
final CSVParser parser = parseFormat.parse(new StringReader(formatted));
|
||||
for (final CSVRecord record : parser) {
|
||||
assertEquals("1", record.get(0));
|
||||
assertEquals("Jane Doe", record.get(1));
|
||||
assertEquals("USA", record.get(2));
|
||||
assertEquals("", record.get(3));
|
||||
}
|
||||
}
|
||||
try (final CSVParser parser = parseFormat.parse(new StringReader(formatted))) {
|
||||
for (final CSVRecord record : parser) {
|
||||
assertEquals("1", record.get(0));
|
||||
assertEquals("Jane Doe", record.get(1));
|
||||
assertEquals("USA", record.get(2));
|
||||
assertEquals("", record.get(3));
|
||||
}
|
||||
}}
|
||||
}
|
||||
|
|
|
@ -65,17 +65,18 @@ public class JiraCsv247Test {
|
|||
public void testHeadersMissingThrowsWhenNotAllowingMissingColumnNames() throws Exception {
|
||||
final CSVFormat format = CSVFormat.DEFAULT.withHeader();
|
||||
|
||||
assertFalse(format.getAllowMissingColumnNames(),
|
||||
"By default we should not allow missing column names");
|
||||
assertFalse(format.getAllowMissingColumnNames(), "By default we should not allow missing column names");
|
||||
|
||||
assertThrows(IllegalArgumentException.class, () -> {
|
||||
final Reader in = new StringReader("a,,c,d,e\n1,2,3,4,5\nv,w,x,y,z");
|
||||
format.parse(in);
|
||||
try (final Reader in = new StringReader("a,,c,d,e\n1,2,3,4,5\nv,w,x,y,z")) {
|
||||
format.parse(in);
|
||||
}
|
||||
}, "1 missing column header is not allowed");
|
||||
|
||||
assertThrows(IllegalArgumentException.class, () -> {
|
||||
final Reader in = new StringReader("a,,c,d,\n1,2,3,4,5\nv,w,x,y,z");
|
||||
format.parse(in);
|
||||
try (final Reader in = new StringReader("a,,c,d,\n1,2,3,4,5\nv,w,x,y,z")) {
|
||||
format.parse(in);
|
||||
}
|
||||
}, "2+ missing column headers is not allowed!");
|
||||
}
|
||||
}
|
|
@ -94,10 +94,12 @@ public class PerformanceTest {
|
|||
|
||||
public long testParseBigFile(final boolean traverseColumns) throws Exception {
|
||||
final long startMillis = System.currentTimeMillis();
|
||||
final long count = this.parse(this.createBufferedReader(), traverseColumns);
|
||||
final long totalMillis = System.currentTimeMillis() - startMillis;
|
||||
this.println(String.format("File parsed in %,d milliseconds with Commons CSV: %,d lines.", totalMillis, count));
|
||||
return totalMillis;
|
||||
try (final BufferedReader reader = this.createBufferedReader()) {
|
||||
final long count = this.parse(reader, traverseColumns);
|
||||
final long totalMillis = System.currentTimeMillis() - startMillis;
|
||||
this.println(String.format("File parsed in %,d milliseconds with Commons CSV: %,d lines.", totalMillis, count));
|
||||
return totalMillis;
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
Loading…
Reference in New Issue