Skip to content

Commit

Permalink
Use stream test; format (#483)
Browse files Browse the repository at this point in the history
  • Loading branch information
dr0i committed May 20, 2023
1 parent 96811da commit 96605ab
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 112 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
* A csv encoder that converts a record into a csv line (Default separator: {@value #DEFAULT_SEP}).
Expand Down Expand Up @@ -139,11 +138,9 @@ public void setNoQuotes(final boolean noQuotes) {
private void initialize() {
writer = new StringWriter();
final String emptyLineEnd = "";
csvWriter = new CSVWriter(writer,
separator,
noQuotes ? CSVWriter.NO_QUOTE_CHARACTER : CSVWriter.DEFAULT_QUOTE_CHARACTER,
CSVWriter.DEFAULT_ESCAPE_CHARACTER,
emptyLineEnd);
csvWriter = new CSVWriter(writer, separator,
noQuotes ? CSVWriter.NO_QUOTE_CHARACTER : CSVWriter.DEFAULT_QUOTE_CHARACTER,
CSVWriter.DEFAULT_ESCAPE_CHARACTER, emptyLineEnd);
}

private String[] arrayOf(final List<String> list) {
Expand Down Expand Up @@ -190,7 +187,6 @@ public void endRecord() {
}

writeRow(rowItems);

resetCaches();
}

Expand All @@ -217,7 +213,6 @@ public void onResetStream() {
this.includeRecordId = false;
this.includeHeader = false;
this.header = new ArrayList<>();

this.isFirstRecord = true;
this.rowItems = new ArrayList<>();
}
Expand Down
207 changes: 103 additions & 104 deletions metafacture-csv/src/test/java/org/metafacture/csv/CsvEncoderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

}
}

0 comments on commit 96605ab

Please sign in to comment.