Skip to content

Commit

Permalink
Port some code from IO to NIO APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
garydgregory committed Jan 27, 2023
1 parent 30329ab commit 347c872
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions src/test/java/org/apache/commons/csv/CSVPrinterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@
import java.io.Writer;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.sql.BatchUpdateException;
import java.sql.Connection;
import java.sql.DriverManager;
Expand Down Expand Up @@ -92,6 +94,14 @@ private static String printable(final String s) {

private final String recordSeparator = CSVFormat.DEFAULT.getRecordSeparator();

private File createTempFile() throws IOException {
return createTempPath().toFile();
}

private Path createTempPath() throws IOException {
return Files.createTempFile(getClass().getName(), ".csv");
}

private void doOneRandom(final CSVFormat format) throws Exception {
final Random r = new Random();

Expand Down Expand Up @@ -1543,7 +1553,7 @@ public void testPrintRecordsWithResultSetOneRow() throws IOException, SQLExcepti

@Test
public void testPrintToFileWithCharsetUtf16Be() throws IOException {
final File file = File.createTempFile(getClass().getName(), ".csv");
final File file = createTempFile();
try (final CSVPrinter printer = CSVFormat.DEFAULT.print(file, StandardCharsets.UTF_16BE)) {
printer.printRecord("a", "b\\c");
}
Expand All @@ -1552,7 +1562,7 @@ public void testPrintToFileWithCharsetUtf16Be() throws IOException {

@Test
public void testPrintToFileWithDefaultCharset() throws IOException {
final File file = File.createTempFile(getClass().getName(), ".csv");
final File file = createTempFile();
try (final CSVPrinter printer = CSVFormat.DEFAULT.print(file, Charset.defaultCharset())) {
printer.printRecord("a", "b\\c");
}
Expand All @@ -1561,11 +1571,11 @@ public void testPrintToFileWithDefaultCharset() throws IOException {

@Test
public void testPrintToPathWithDefaultCharset() throws IOException {
final File file = File.createTempFile(getClass().getName(), ".csv");
try (final CSVPrinter printer = CSVFormat.DEFAULT.print(file.toPath(), Charset.defaultCharset())) {
final Path file = createTempPath();
try (final CSVPrinter printer = CSVFormat.DEFAULT.print(file, Charset.defaultCharset())) {
printer.printRecord("a", "b\\c");
}
assertEquals("a,b\\c" + recordSeparator, FileUtils.readFileToString(file, Charset.defaultCharset()));
assertEquals("a,b\\c" + recordSeparator, new String(Files.readAllBytes(file), Charset.defaultCharset()));
}

@Test
Expand Down

0 comments on commit 347c872

Please sign in to comment.