This repository has been archived by the owner on Jan 22, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 76
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
issue-128 : Add regression test for headers writing with no data
Signed-off-by: Peter Ansell <[email protected]>
- Loading branch information
Showing
1 changed file
with
61 additions
and
0 deletions.
There are no files selected for viewing
61 changes: 61 additions & 0 deletions
61
src/test/java/com/fasterxml/jackson/dataformat/csv/ser/HeaderWriteTest.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,61 @@ | ||
package com.fasterxml.jackson.dataformat.csv.ser; | ||
|
||
import java.io.IOException; | ||
import java.io.StringWriter; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
import com.fasterxml.jackson.databind.SequenceWriter; | ||
import com.fasterxml.jackson.dataformat.csv.*; | ||
|
||
// Tests for verifying that headers are emitted | ||
public class HeaderWriteTest extends ModuleTestBase | ||
{ | ||
/* | ||
/********************************************************************** | ||
/* Test methods | ||
/********************************************************************** | ||
*/ | ||
|
||
private final CsvMapper MAPPER = mapperForCsv(); | ||
|
||
public void testNoLines() throws Exception | ||
{ | ||
List<String> headers = Arrays.asList("TestHeader1", "TestHeader2"); | ||
List<List<String>> dataSource = Arrays.asList(); | ||
String result = runTest(headers, dataSource); | ||
|
||
assertEquals("Headers should have been written even with no other data", "TestHeader1,TestHeader2\n", result); | ||
} | ||
|
||
public void testOneLine() throws Exception | ||
{ | ||
List<String> headers = Arrays.asList("TestHeader1", "TestHeader2"); | ||
List<List<String>> dataSource = Arrays.asList(Arrays.asList("TestValue1", "TestValue2")); | ||
String result = runTest(headers, dataSource); | ||
|
||
assertEquals("Headers should have been written before line", "TestHeader1,TestHeader2\nTestValue1,TestValue2\n", result); | ||
} | ||
|
||
private String runTest(List<String> headers, List<List<String>> dataSource) throws IOException | ||
{ | ||
StringWriter writer = new StringWriter(); | ||
|
||
CsvSchema.Builder builder = CsvSchema.builder(); | ||
for (String nextHeader : headers) { | ||
builder = builder.addColumn(nextHeader); | ||
} | ||
|
||
CsvSchema schema = builder.setUseHeader(true).build(); | ||
try (SequenceWriter csvWriter = MAPPER.writerWithDefaultPrettyPrinter() | ||
.with(schema) | ||
.forType(List.class) | ||
.writeValues(writer);) { | ||
for(List<String> nextRow : dataSource) { | ||
csvWriter.write(nextRow); | ||
} | ||
} | ||
|
||
return writer.toString(); | ||
} | ||
} |