-
Notifications
You must be signed in to change notification settings - Fork 93
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add writeRecord() to CsvWriter to allow writing records field by field
- Loading branch information
Showing
5 changed files
with
178 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
example/src/main/java/example/ExampleCsvWriterWithSingleFields.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package example; | ||
|
||
import java.io.StringWriter; | ||
|
||
import de.siegmar.fastcsv.writer.CsvWriter; | ||
|
||
/** | ||
* Example for writing CSV data field by field. | ||
*/ | ||
public class ExampleCsvWriterWithSingleFields { | ||
|
||
public static void main(final String[] args) { | ||
final StringWriter sw = new StringWriter(); | ||
CsvWriter.builder().build(sw) | ||
.writeRecord("header1", "header2") | ||
.writeRecord().writeField("value1").writeField("value2").endRecord(); | ||
|
||
System.out.println(sw); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
lib/src/intTest/java/blackbox/writer/CsvWriterRecordTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package blackbox.writer; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
|
||
import java.io.IOException; | ||
import java.io.StringWriter; | ||
import java.io.UncheckedIOException; | ||
import java.util.List; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import de.siegmar.fastcsv.writer.CsvWriter; | ||
import de.siegmar.fastcsv.writer.LineDelimiter; | ||
|
||
class CsvWriterRecordTest { | ||
|
||
private final CsvWriter.CsvWriterBuilder crw = CsvWriter.builder() | ||
.lineDelimiter(LineDelimiter.LF); | ||
|
||
@Test | ||
void emptyRecord() throws IOException { | ||
final var sw = new StringWriter(); | ||
crw.build(sw) | ||
.writeRecord() | ||
.endRecord() | ||
.close(); | ||
|
||
assertThat(sw).hasToString("\n"); | ||
} | ||
|
||
@Test | ||
void simpleRecord() throws IOException { | ||
final var sw = new StringWriter(); | ||
crw.build(sw) | ||
.writeRecord().writeField("foo").writeField("bar").endRecord() | ||
.writeRecord("baz", "qux") | ||
.close(); | ||
|
||
assertThat(sw).hasToString("foo,bar\nbaz,qux\n"); | ||
} | ||
|
||
@Test | ||
void ioError() { | ||
final var un = new UnwritableWriter(); | ||
final var csv = crw.bufferSize(0).build(un).writeRecord(); | ||
|
||
assertThatThrownBy(() -> csv.writeField("foo")) | ||
.isInstanceOf(UncheckedIOException.class); | ||
|
||
assertThatThrownBy(csv::endRecord) | ||
.isInstanceOf(UncheckedIOException.class); | ||
} | ||
|
||
@Test | ||
void openRecordWriteComment() { | ||
final CsvWriter csv = crw.build(new StringWriter()); | ||
csv.writeRecord(); | ||
|
||
assertThatThrownBy(csv::writeRecord) | ||
.isInstanceOf(IllegalStateException.class) | ||
.hasMessage("Record already started, call end() on CsvWriterRecord first"); | ||
|
||
assertThatThrownBy(() -> csv.writeComment("foo")) | ||
.isInstanceOf(IllegalStateException.class) | ||
.hasMessage("Record already started, call end() on CsvWriterRecord first"); | ||
|
||
assertThatThrownBy(() -> csv.writeRecord("foo")) | ||
.isInstanceOf(IllegalStateException.class) | ||
.hasMessage("Record already started, call end() on CsvWriterRecord first"); | ||
|
||
assertThatThrownBy(() -> csv.writeRecord(List.of("foo"))) | ||
.isInstanceOf(IllegalStateException.class) | ||
.hasMessage("Record already started, call end() on CsvWriterRecord first"); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters