forked from super-csv/super-csv
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
super-csv#56 - test suite for Tokenizer read/write operations.
1. Added two new tests which explicitly checks features from http://csveed.org/comparison-matrix.html page (Tokenizer section).
- Loading branch information
1 parent
c364152
commit 12dc06a
Showing
2 changed files
with
247 additions
and
0 deletions.
There are no files selected for viewing
131 changes: 131 additions & 0 deletions
131
super-csv/src/test/java/org/supercsv/features/ReadingFeaturesTest.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,131 @@ | ||
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.io.CsvListWriter; | ||
import org.supercsv.prefs.CsvPreference; | ||
|
||
import java.io.IOException; | ||
import java.io.StringReader; | ||
import java.io.StringWriter; | ||
import java.util.Arrays; | ||
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)); | ||
} | ||
} |
116 changes: 116 additions & 0 deletions
116
super-csv/src/test/java/org/supercsv/features/WritingFeaturesTest.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,116 @@ | ||
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.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 = Arrays.asList("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 = Arrays.asList("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(); | ||
} | ||
} |