-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
106 additions
and
112 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
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 |
---|---|---|
|
@@ -16,165 +16,164 @@ | |
|
||
package org.metafacture.csv; | ||
|
||
import org.junit.After; | ||
import org.metafacture.framework.ObjectReceiver; | ||
|
||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.metafacture.framework.ObjectReceiver; | ||
|
||
import org.mockito.InOrder; | ||
import org.mockito.Mock; | ||
import org.mockito.Mockito; | ||
import org.mockito.MockitoAnnotations; | ||
import org.mockito.exceptions.base.MockitoAssertionError; | ||
|
||
import static org.mockito.Mockito.inOrder; | ||
import java.util.Arrays; | ||
import java.util.function.Consumer; | ||
|
||
/** | ||
* Tests for {@link CsvEncoder}. | ||
* | ||
* @author eberhardtj ([email protected]) | ||
* @author Pascal Christoph (dr0i) | ||
* @author Jens Wille | ||
*/ | ||
public final class CsvEncoderTest { | ||
|
||
private CsvEncoder encoder; | ||
|
||
@Mock | ||
private ObjectReceiver<String> receiver; | ||
private static final String LITERAL1 = "column 1"; | ||
private static final String LITERAL2 = "column 2"; | ||
private static final String RECORD_ID1 = "1"; | ||
private static final String RECORD_ID2 = "2"; | ||
private static final String RECORD_ID3 = "3"; | ||
private static final String VALUE1 = "a"; | ||
private static final String VALUE2 = "b"; | ||
private static final String VALUE3 = "c"; | ||
private static final String VALUE4 = "d"; | ||
private static final String VALUE5 = "e"; | ||
private static final String VALUE6 = "f"; | ||
|
||
@Before | ||
public void setup() { | ||
MockitoAnnotations.initMocks(this); | ||
encoder = new CsvEncoder(); | ||
encoder.setIncludeHeader(false); | ||
CsvEncoder encoder = new CsvEncoder(); | ||
encoder.setReceiver(receiver); | ||
} | ||
|
||
@After | ||
public void cleanup() { | ||
encoder.closeStream(); | ||
} | ||
|
||
@Test | ||
public void shouldReceiveSingleRecord() { | ||
encoder.startRecord("1"); | ||
encoder.literal("column 1", "a"); | ||
encoder.literal("column 2", "b"); | ||
encoder.endRecord(); | ||
encoder.closeStream(); | ||
|
||
final InOrder ordered = inOrder(receiver); | ||
ordered.verify(receiver).process("\"a\",\"b\""); | ||
assertEncode(i -> { | ||
i.startRecord(RECORD_ID1); | ||
i.literal(LITERAL1, VALUE1); | ||
i.literal(LITERAL2, VALUE2); | ||
i.endRecord(); | ||
}, "\"a\",\"b\""); | ||
} | ||
|
||
@Test | ||
public void shouldHaveNoQuotes() { | ||
encoder.setNoQuotes(true); | ||
encoder.startRecord("1"); | ||
encoder.literal("column 1", "a"); | ||
encoder.literal("column 2", "b"); | ||
encoder.endRecord(); | ||
encoder.closeStream(); | ||
|
||
final InOrder ordered = inOrder(receiver); | ||
ordered.verify(receiver).process("a,b"); | ||
assertEncode(i -> { | ||
i.setNoQuotes(true); | ||
i.startRecord(RECORD_ID1); | ||
i.literal(LITERAL1, VALUE1); | ||
i.literal(LITERAL2, VALUE2); | ||
i.endRecord(); | ||
}, "a,b"); | ||
} | ||
|
||
@Test | ||
public void shouldReceiveSingleRecordWithHeader() { | ||
encoder.setIncludeHeader(true); | ||
|
||
encoder.startRecord("1"); | ||
encoder.literal("column 1", "a"); | ||
encoder.literal("column 2", "b"); | ||
encoder.endRecord(); | ||
encoder.closeStream(); | ||
|
||
final InOrder ordered = inOrder(receiver); | ||
ordered.verify(receiver).process("\"column 1\",\"column 2\""); | ||
ordered.verify(receiver).process("\"a\",\"b\""); | ||
assertEncode(i -> { | ||
i.setIncludeHeader(true); | ||
i.startRecord(RECORD_ID1); | ||
i.literal(LITERAL1, VALUE1); | ||
i.literal(LITERAL2, VALUE2); | ||
i.endRecord(); | ||
}, "\"column 1\",\"column 2\"", "\"a\",\"b\""); | ||
} | ||
|
||
@Test | ||
public void shouldReceiveSingleRecordWithRecordId() { | ||
encoder.setIncludeRecordId(true); | ||
|
||
encoder.startRecord("1"); | ||
encoder.literal("column 1", "a"); | ||
encoder.literal("column 2", "b"); | ||
encoder.endRecord(); | ||
encoder.closeStream(); | ||
|
||
final InOrder ordered = inOrder(receiver); | ||
ordered.verify(receiver).process("\"1\",\"a\",\"b\""); | ||
assertEncode(i -> { | ||
i.setIncludeRecordId(true); | ||
i.startRecord(RECORD_ID1); | ||
i.literal(LITERAL1, VALUE1); | ||
i.literal(LITERAL2, VALUE2); | ||
i.endRecord(); | ||
}, "\"1\",\"a\",\"b\""); | ||
} | ||
|
||
@Test | ||
public void shouldReceiveSingleRecordWithRecordIdAndHeader() { | ||
encoder.setIncludeRecordId(true); | ||
encoder.setIncludeHeader(true); | ||
|
||
encoder.startRecord("1"); | ||
encoder.literal("column 1", "a"); | ||
encoder.literal("column 2", "b"); | ||
encoder.endRecord(); | ||
encoder.closeStream(); | ||
|
||
final InOrder ordered = inOrder(receiver); | ||
ordered.verify(receiver).process("\"record id\",\"column 1\",\"column 2\""); | ||
ordered.verify(receiver).process("\"1\",\"a\",\"b\""); | ||
assertEncode(i -> { | ||
i.setIncludeRecordId(true); | ||
i.setIncludeHeader(true); | ||
i.startRecord(RECORD_ID1); | ||
i.literal(LITERAL1, VALUE1); | ||
i.literal(LITERAL2, VALUE2); | ||
i.endRecord(); | ||
}, "\"record id\",\"column 1\",\"column 2\"", "\"1\",\"a\",\"b\""); | ||
} | ||
|
||
@Test | ||
public void shouldReceiveThreeRows() { | ||
encoder.startRecord("1"); | ||
encoder.literal("column 1", "a"); | ||
encoder.literal("column 2", "b"); | ||
encoder.endRecord(); | ||
encoder.startRecord("2"); | ||
encoder.literal("column 1", "c"); | ||
encoder.literal("column 2", "d"); | ||
encoder.endRecord(); | ||
encoder.startRecord("3"); | ||
encoder.literal("column 1", "e"); | ||
encoder.literal("column 2", "f"); | ||
encoder.endRecord(); | ||
encoder.closeStream(); | ||
|
||
final InOrder ordered = inOrder(receiver); | ||
ordered.verify(receiver).process("\"a\",\"b\""); | ||
ordered.verify(receiver).process("\"c\",\"d\""); | ||
ordered.verify(receiver).process("\"e\",\"f\""); | ||
assertEncode(i -> { | ||
i.startRecord(RECORD_ID1); | ||
i.literal(LITERAL1, VALUE1); | ||
i.literal(LITERAL2, VALUE2); | ||
i.endRecord(); | ||
i.startRecord(RECORD_ID2); | ||
i.literal(LITERAL1, VALUE3); | ||
i.literal(LITERAL2, VALUE4); | ||
i.endRecord(); | ||
i.startRecord(RECORD_ID3); | ||
i.literal(LITERAL1, VALUE5); | ||
i.literal(LITERAL2, VALUE6); | ||
i.endRecord(); | ||
}, "\"a\",\"b\"", "\"c\",\"d\"", "\"e\",\"f\""); | ||
} | ||
|
||
@Test | ||
public void shouldUseTabulatorAsSeparator() { | ||
encoder.setSeparator('\t'); | ||
|
||
encoder.startRecord("1"); | ||
encoder.literal("column 1", "a"); | ||
encoder.literal("column 2", "b"); | ||
encoder.endRecord(); | ||
encoder.startRecord("2"); | ||
encoder.literal("column 1", "c"); | ||
encoder.literal("column 2", "d"); | ||
encoder.endRecord(); | ||
encoder.closeStream(); | ||
|
||
final InOrder ordered = inOrder(receiver); | ||
ordered.verify(receiver).process("\"a\"\t\"b\""); | ||
ordered.verify(receiver).process("\"c\"\t\"d\""); | ||
assertEncode(i -> { | ||
i.setSeparator('\t'); | ||
i.startRecord(RECORD_ID1); | ||
i.literal(LITERAL1, VALUE1); | ||
i.literal(LITERAL2, VALUE2); | ||
i.endRecord(); | ||
}, "\"a\"\t\"b\""); | ||
} | ||
|
||
@Test | ||
public void shouldNotCreateNestedCsvInColumn() { | ||
encoder.startRecord("1"); | ||
encoder.literal("name", "a"); | ||
encoder.literal("alias", "a1"); | ||
encoder.literal("alias", "a2"); | ||
encoder.literal("alias", "a3"); | ||
encoder.endRecord(); | ||
encoder.closeStream(); | ||
|
||
final InOrder ordered = inOrder(receiver); | ||
ordered.verify(receiver).process("\"a\",\"a1\",\"a2\",\"a3\""); | ||
assertEncode(i -> { | ||
i.startRecord(RECORD_ID1); | ||
i.literal(LITERAL1, VALUE1); | ||
i.literal(LITERAL2, VALUE2); | ||
i.literal(LITERAL2, VALUE3); | ||
i.literal(LITERAL2, VALUE4); | ||
i.endRecord(); | ||
}, "\"a\",\"b\",\"c\",\"d\""); | ||
} | ||
|
||
private void assertEncode(final Consumer<CsvEncoder> in, final String... out) { | ||
final InOrder ordered = Mockito.inOrder(receiver); | ||
|
||
final CsvEncoder csvEncoder = new CsvEncoder(); | ||
csvEncoder.setReceiver(receiver); | ||
in.accept(csvEncoder); | ||
|
||
try { | ||
Arrays.stream(out).forEach(s -> ordered.verify(receiver).process(s)); | ||
|
||
ordered.verifyNoMoreInteractions(); | ||
Mockito.verifyNoMoreInteractions(receiver); | ||
} | ||
catch (final MockitoAssertionError e) { | ||
System.out.println(Mockito.mockingDetails(receiver).printInvocations()); | ||
throw e; | ||
} | ||
|
||
} | ||
} |