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