Skip to content

Commit

Permalink
super-csv#56 - test suite for Tokenizer read/write operations.
Browse files Browse the repository at this point in the history
1. Added two new tests which explicitly checks features from
http://csveed.org/comparison-matrix.html page (Tokenizer section).
  • Loading branch information
ZioberMichal committed Jul 11, 2015
1 parent c364152 commit 961be90
Show file tree
Hide file tree
Showing 2 changed files with 245 additions and 0 deletions.
128 changes: 128 additions & 0 deletions super-csv/src/test/java/org/supercsv/features/ReadingFeaturesTest.java
@@ -0,0 +1,128 @@
package org.supercsv.features;

import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test;
import org.supercsv.cellprocessor.Trim;
import org.supercsv.cellprocessor.constraint.NotNull;
import org.supercsv.cellprocessor.ift.CellProcessor;
import org.supercsv.io.CsvListReader;
import org.supercsv.prefs.CsvPreference;

import java.io.IOException;
import java.io.StringReader;
import java.util.List;

/**
* Test class which checks all features listed on <a href="http://csveed.org/comparison-matrix.html">comparison page</a>
* in "Tokenizer" section for "read" operations.
*
* @author Michał Ziober
*/
public class ReadingFeaturesTest {

@Test
public void testCustomSeparator() throws IOException {
String csv = "John+Connor";
CellProcessor[] processors = { new NotNull(), new NotNull() };

char customSeparator = '+';
CsvPreference customPreference = new CsvPreference.Builder('"', customSeparator, "").build();
CsvListReader listReader = new CsvListReader(new StringReader(csv), customPreference);
List<Object> result = listReader.read(processors);

Assert.assertNotNull(result);
Assert.assertEquals(2, result.size());
Assert.assertEquals("John", result.get(0));
Assert.assertEquals("Connor", result.get(1));
}

@Test
public void testCustomQuote() throws IOException {
String csv = "|John Connor|";
CellProcessor[] processors = { new NotNull() };

char customQuote = '|';
CsvPreference customPreference = new CsvPreference.Builder(customQuote, ',', "").build();
CsvListReader listReader = new CsvListReader(new StringReader(csv), customPreference);
List<Object> result = listReader.read(processors);

Assert.assertNotNull(result);
Assert.assertEquals(1, result.size());
Assert.assertEquals("John Connor", result.get(0));
}

@Ignore
@Test
public void testCustomEscape() throws IOException {
throw new UnsupportedOperationException("Not implemented yet!");
}

@Test
public void testCustomEOL() throws IOException {
String csv = "John,Connor\r>\n";
CellProcessor[] processors = { new NotNull(), new NotNull() };

String customEndOfLine = "\r>\n";
CsvPreference customPreference = new CsvPreference.Builder('"', ',', customEndOfLine).build();
CsvListReader listReader = new CsvListReader(new StringReader(csv), customPreference);
List<Object> result = listReader.read(processors);

Assert.assertNotNull(result);
Assert.assertEquals(2, result.size());
Assert.assertEquals("John", result.get(0));
Assert.assertEquals("Connor", result.get(1));
}

@Test
public void testNewLineInDelimitedField() throws IOException {
String csv = "\"Jo\nhn\",\"Con\nnor\"\n";
CellProcessor[] processors = { new NotNull(), new NotNull() };

CsvPreference customPreference = new CsvPreference.Builder('"', ',', "\n").build();
CsvListReader listReader = new CsvListReader(new StringReader(csv), customPreference);
List<Object> result = listReader.read(processors);

Assert.assertNotNull(result);
Assert.assertEquals(2, result.size());
Assert.assertEquals("Jo\nhn", result.get(0));
Assert.assertEquals("Con\nnor", result.get(1));
}

@Test
public void testEscapedQuoteInQuotedField() throws IOException {
String csv = "\"Joh\"\"n\",\"Con\"\"nor\"";
CellProcessor[] processors = { new NotNull(), new NotNull() };

CsvPreference customPreference = new CsvPreference.Builder('"', ',', "").build();
CsvListReader listReader = new CsvListReader(new StringReader(csv), customPreference);
List<Object> result = listReader.read(processors);

Assert.assertNotNull(result);
Assert.assertEquals(2, result.size());
Assert.assertEquals("Joh\"n", result.get(0));
Assert.assertEquals("Con\"nor", result.get(1));
}

@Ignore
@Test
public void testDifferentEscapeAndQuote() throws IOException {
throw new UnsupportedOperationException("Not implemented yet!");
}

@Test
public void testDealWithLeadingTrailingWhitespace() throws IOException {
String csv = " John , Connor ";
CellProcessor[] processors = { new Trim(), new Trim() };

char customQuote = '"';
CsvPreference customPreference = new CsvPreference.Builder(customQuote, ',', "").build();
CsvListReader listReader = new CsvListReader(new StringReader(csv), customPreference);
List<Object> result = listReader.read(processors);

Assert.assertNotNull(result);
Assert.assertEquals(2, result.size());
Assert.assertEquals("John", result.get(0));
Assert.assertEquals("Connor", result.get(1));
}
}
117 changes: 117 additions & 0 deletions super-csv/src/test/java/org/supercsv/features/WritingFeaturesTest.java
@@ -0,0 +1,117 @@
package org.supercsv.features;

import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test;
import org.supercsv.cellprocessor.Trim;
import org.supercsv.cellprocessor.constraint.NotNull;
import org.supercsv.cellprocessor.ift.CellProcessor;
import org.supercsv.io.CsvListWriter;
import org.supercsv.prefs.CsvPreference;

import java.io.IOException;
import java.io.StringWriter;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
* Test class which checks all features listed on <a href="http://csveed.org/comparison-matrix.html">comparison page</a>
* in "Tokenizer" section for "write" operations.
*
* @author Michał Ziober
*/
public class WritingFeaturesTest {

@Test
public void testCustomSeparator() throws IOException {
List<String> data = Arrays.asList("John", "Connor");
CellProcessor[] processors = { new NotNull(), new NotNull() };

char customSeparator = '+';
CsvPreference customPreference = new CsvPreference.Builder('"', customSeparator, "").build();
String result = writeToCsv(data, processors, customPreference);

Assert.assertEquals("John+Connor", result);
}

@Test
public void testCustomQuote() throws IOException {
List<String> data = Collections.singletonList("John \n Connor");
CellProcessor[] processors = { new NotNull() };

char customQuote = '|';
CsvPreference customPreference = new CsvPreference.Builder(customQuote, ',', "").build();
String result = writeToCsv(data, processors, customPreference);

Assert.assertEquals("|John Connor|", result);
}

@Ignore
@Test
public void testCustomEscape() throws IOException {
throw new UnsupportedOperationException("Not implemented yet!");
}

@Test
public void testCustomEOL() throws IOException {
List<String> data = Collections.singletonList("John Connor");
CellProcessor[] processors = { new NotNull() };

String customEndOfLine = ">\r\n";
CsvPreference customPreference = new CsvPreference.Builder('"', ',', customEndOfLine).build();
String result = writeToCsv(data, processors, customPreference);

Assert.assertEquals("John Connor>\r\n", result);
}

@Test
public void testNewLineInDelimitedField() throws IOException {
List<String> data = Arrays.asList("Jo\nhn", "Con\nnor");
CellProcessor[] processors = { new NotNull(), new NotNull() };

CsvPreference customPreference = new CsvPreference.Builder('"', ',', "\n").build();
String result = writeToCsv(data, processors, customPreference);

Assert.assertEquals("\"Jo\nhn\",\"Con\nnor\"\n", result);
}

@Test
public void testEscapedQuoteInQuotedField() throws IOException {
List<String> data = Arrays.asList("Joh\"n", "Con\"nor");
CellProcessor[] processors = { new NotNull(), new NotNull() };

CsvPreference customPreference = new CsvPreference.Builder('"', ',', "").build();
String result = writeToCsv(data, processors, customPreference);

Assert.assertEquals("\"Joh\"\"n\",\"Con\"\"nor\"", result);
}

@Ignore
@Test
public void testDifferentEscapeAndQuote() throws IOException {
throw new UnsupportedOperationException("Not implemented yet!");
}

@Test
public void testDealWithLeadingTrailingWhitespace() throws IOException {
List<String> data = Arrays.asList(" John ", " Connor ");
CellProcessor[] processors = { new Trim(), new Trim() };

char customQuote = '"';
CsvPreference customPreference = new CsvPreference.Builder(customQuote, ',', "").surroundingSpacesNeedQuotes(false).build();
String result = writeToCsv(data, processors, customPreference);

Assert.assertEquals("John,Connor", result);
}

private String writeToCsv(List<String> data, CellProcessor[] processors, CsvPreference customPreference)
throws IOException {
StringWriter writer = new StringWriter();
CsvListWriter listWriter = new CsvListWriter(writer, customPreference);
listWriter.write(data, processors);
listWriter.close();

return writer.toString();
}
}

0 comments on commit 961be90

Please sign in to comment.