Skip to content

Commit

Permalink
enhance unit test for CsvOptions to cover new detectDelimiterMethod
Browse files Browse the repository at this point in the history
  • Loading branch information
simei94 committed Feb 26, 2024
1 parent 636c7bf commit 787f938
Showing 1 changed file with 20 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVPrinter;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.junit.jupiter.api.io.TempDir;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;

Expand All @@ -20,18 +22,27 @@ public class CsvOptionsTest {
@Test
void output() throws IOException {

CsvOptions csv = new CsvOptions(CSVFormat.Predefined.TDF);
List<CsvOptions> delimiters = new ArrayList<>();

Path tmp = f.resolve("test.csv");
delimiters.add(new CsvOptions(CSVFormat.Predefined.TDF));
delimiters.add(new CsvOptions(CSVFormat.Predefined.Default));
delimiters.add(new CsvOptions(CSVFormat.Predefined.Default, ';', StandardCharsets.UTF_8));

CSVPrinter printer = csv.createPrinter(tmp);
for (CsvOptions csv : delimiters) {
Path tmp = f.resolve("test.csv");

printer.printRecord("header", "column");
printer.printRecord("1", "2");
printer.close();
CSVPrinter printer = csv.createPrinter(tmp);

assertThat(tmp)
.hasContent("header\tcolumn\n1\t2");
String delimiter = csv.getFormat().getDelimiterString();

printer.printRecord("header", "column");
printer.printRecord("1", "2");
printer.close();

assertThat(tmp)
.hasContent("header" + delimiter + "column\n1" + delimiter + "2");

assertThat(delimiter).isEqualTo(csv.detectDelimiter(tmp.toString()).toString());
}
}
}

0 comments on commit 787f938

Please sign in to comment.