Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#11 fix unit test #38

Merged
merged 3 commits into from
Jul 6, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,4 @@ Thumbs.db
flatpack/.project

release.properties
*.iml
2 changes: 1 addition & 1 deletion flatpack/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.2</version>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,19 @@
public abstract class AbstractWriter implements Writer {
private final BufferedWriter writer;
private Map<String, Object> rowMap;
protected String lineSeparator = System.lineSeparator();

public AbstractWriter(final java.io.Writer output) {
super();
writer = new BufferedWriter(output);
}

public AbstractWriter(final java.io.Writer writer, final String lineSeparator) {
super();
this.writer = new BufferedWriter(writer);
this.lineSeparator = lineSeparator;
}

@Override
public Writer addRecordEntry(final String columnName, final Object value) {
if (rowMap == null) {
Expand Down Expand Up @@ -53,7 +60,7 @@ public Writer nextRecord() throws IOException {
// the row should have been written out by the subclass so it's safe to
// discard it here
rowMap = null;
writer.newLine();
writer.write(this.lineSeparator);
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@ protected DelimiterWriter(final Map columnMapping, final java.io.Writer output,
super(output);
this.delimiter = delimiter;
this.qualifier = qualifier;
this.lineSeparator = options.getLineSeparator();

columnTitles = new ArrayList<String>();
columnTitles = new ArrayList<>();
final List<ColumnMetaData> columns = (List<ColumnMetaData>) columnMapping.get(FPConstants.DETAIL_ID);
for (final ColumnMetaData cmd : columns) {
columnTitles.add(cmd.getColName());
Expand Down
18 changes: 18 additions & 0 deletions flatpack/src/main/java/net/sf/flatpack/writer/WriterOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
public class WriterOptions {

private boolean autoPrintHeader = true;
private String lineSeparator = System.lineSeparator();

/**
* Returns a DelimiterWriterOptions instance
Expand Down Expand Up @@ -36,4 +37,21 @@ public WriterOptions autoPrintHeader(final boolean autoPrintHeader) {
return this;
}

/**
* Get the current line separator. Default is the system line separator.
* @return
*/
public String getLineSeparator() {
return lineSeparator;
}

/**
* Set the line leperator.
* @param lineSeparator the line separator
* @return
*/
public WriterOptions setLineSeparator(String lineSeparator) {
this.lineSeparator = lineSeparator;
return this;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
package net.sf.flatpack.rfc4180;

import java.io.IOException;
import java.io.StringWriter;

import net.sf.flatpack.writer.DelimiterWriterFactory;
import net.sf.flatpack.writer.Rfc4180TestCase;
import net.sf.flatpack.writer.Writer;
import net.sf.flatpack.writer.WriterOptions;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.junit.Assert.assertThat;

public class CsvWriterFormatDefintionTest extends Rfc4180TestCase {
private final char FIELD_DELIMITER = ',';
private final char FIELD_QUALIFIER = '"';
private final String LINE_SEPARATOR = "\r\n";


/*
* 2.1 Each record is located on a separate line, delimited by a line break (CRLF). For example:
*
* aaa,bbb,ccc CRLF
* zzz,yyy,xxx CRLF
*/
public void testLineSeparation() throws IOException {
final StringWriter out = new StringWriter();

try (Writer writer = getWriterForRfc4180(out, false)) {
// write one line of data ... not in the correct order of fields
writer.addRecordEntry("col1", "aaa")
.addRecordEntry("col2", "bbb")
.addRecordEntry("col3", "ccc")
.nextRecord();
writer.addRecordEntry("col3", "xxx")
.addRecordEntry("col2", "yyy")
.addRecordEntry("col1", "zzz")
.nextRecord();

writer.flush();
}

String result = out.toString();
assertThat(result, is(notNullValue()));
assertThat(result.length(), is(26));

String[] lines = result.split(LINE_SEPARATOR);
assertThat(lines, is(notNullValue()));
assertThat(lines.length, is(2));
assertThat(lines[0], is("aaa,bbb,ccc"));
assertThat(lines[1], is("zzz,yyy,xxx"));
}

/*
* 2.4a. Each line should contain the same number of fields throughout the file
*/
public void testSameNumberOffFields() throws IOException {
final StringWriter out = new StringWriter();

try (Writer writer = getWriterForRfc4180(out, false)) {
// write one line of data ... not in the correct order of fields
writer.addRecordEntry("col1", "aaa")
.addRecordEntry("col2", "bbb")
.addRecordEntry("col3", "ccc")
.nextRecord();
writer.addRecordEntry("col3", "xxx")
//.addRecordEntry("col2", "yyy")
.addRecordEntry("col1", "zzz")
.nextRecord();

writer.flush();
}

String result = out.toString();
assertThat(result, is(notNullValue()));
assertThat(result.length(), is(23));

String[] lines = result.split(LINE_SEPARATOR);
assertThat(lines, is(notNullValue()));
assertThat(lines.length, is(2));
assertThat(lines[0], is("aaa,bbb,ccc"));
assertThat(lines[0].split(String.valueOf(FIELD_DELIMITER)).length, is(3));
assertThat(lines[1], is("zzz,,xxx"));
assertThat(lines[1].split(String.valueOf(FIELD_DELIMITER)).length, is(3));
}

/*
* 2.4b. Spaces are considered part of a field and should not be ignored.
*/
public void testSpacesArePartOfField() throws IOException {
final StringWriter out = new StringWriter();

try (Writer writer = getWriterForRfc4180(out, false)) {
// write one line of data ... not in the correct order of fields
writer.addRecordEntry("col1", "aaa ")
.addRecordEntry("col2", " bbb")
.addRecordEntry("col3", "c cc")
.nextRecord();
writer.addRecordEntry("col3", "x x x")
.addRecordEntry("col2", "yy y")
.addRecordEntry("col1", " zzz ")
.nextRecord();

writer.flush();
}

String result = out.toString();
assertThat(result, is(notNullValue()));
assertThat(result.length(), is(34));

String[] lines = result.split(LINE_SEPARATOR);
assertThat(lines, is(notNullValue()));
assertThat(lines.length, is(2));
assertThat(lines[0], is("aaa , bbb,c cc"));
assertThat(lines[1], is(" zzz ,yy y,x x x"));
}

/*
* 2.4c. The last field in the record must not be followed by a comma
*/
public void testLastFieldShouldNotHaveDelimiter() throws IOException {
final StringWriter out = new StringWriter();

try (Writer writer = getWriterForRfc4180(out, false)) {
// write one line of data ... not in the correct order of fields
writer.addRecordEntry("col1", "aaa")
.addRecordEntry("col2", "bbb")
.addRecordEntry("col3", "ccc")
.nextRecord();
writer.addRecordEntry("col3", "xxx")
.addRecordEntry("col2", "yyy")
.addRecordEntry("col1", "zzz")
.nextRecord();

writer.flush();
}

String result = out.toString();
assertThat(result, is(notNullValue()));
assertThat(result.length(), is(26));

String[] lines = result.split(LINE_SEPARATOR);
assertThat(lines, is(notNullValue()));
assertThat(lines.length, is(2));
assertThat(lines[0].length(), is(11));
assertThat(lines[0].charAt(lines[0].length() -1), is(not(FIELD_DELIMITER)));
assertThat(lines[1].length(), is(11));
assertThat(lines[1].charAt(lines[0].length() -1), is(not(FIELD_DELIMITER)));
}


private Writer getWriterForRfc4180(java.io.Writer out, boolean autoPrintHeader) throws IOException {
WriterOptions options = WriterOptions.getInstance();
options.autoPrintHeader(autoPrintHeader);
options.setLineSeparator(LINE_SEPARATOR);

final DelimiterWriterFactory factory = new DelimiterWriterFactory(FIELD_DELIMITER, FIELD_QUALIFIER)//
.addColumnTitle("col1")
.addColumnTitle("col2")
.addColumnTitle("col3");

return factory.createWriter(out, options);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package net.sf.flatpack.writer;

import junit.framework.TestCase;

public abstract class Rfc4180TestCase extends TestCase {
}
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public void testParseFixedMap() throws IOException, ParserConfigurationException
}

public void testParseComplexFixedMap() throws IOException, ParserConfigurationException, SAXException {
final Map<String, Object> parse = MapParser.parse2(new InputStreamReader(getClass().getResourceAsStream("test-complex-fixed.xml")), null);
final Map<String, Object> parse = MapParser.parse2(new InputStreamReader(getClass().getClassLoader().getResourceAsStream("test-complex-fixed.xml")), null);
assertThat(parse).hasSize(8);
final XMLRecordElement details = (XMLRecordElement) parse.get("exchange");
assertThat(details).isNotNull();
Expand Down