From 12dc06a6bd471d41e99f9889e9cd1b67a35881e9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Ziober?= Date: Sat, 11 Jul 2015 12:23:05 +0200 Subject: [PATCH] #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). --- .../features/ReadingFeaturesTest.java | 131 ++++++++++++++++++ .../features/WritingFeaturesTest.java | 116 ++++++++++++++++ 2 files changed, 247 insertions(+) create mode 100644 super-csv/src/test/java/org/supercsv/features/ReadingFeaturesTest.java create mode 100644 super-csv/src/test/java/org/supercsv/features/WritingFeaturesTest.java diff --git a/super-csv/src/test/java/org/supercsv/features/ReadingFeaturesTest.java b/super-csv/src/test/java/org/supercsv/features/ReadingFeaturesTest.java new file mode 100644 index 00000000..95c7a4ad --- /dev/null +++ b/super-csv/src/test/java/org/supercsv/features/ReadingFeaturesTest.java @@ -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 comparison page + * 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 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 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 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 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 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 result = listReader.read(processors); + + Assert.assertNotNull(result); + Assert.assertEquals(2, result.size()); + Assert.assertEquals("John", result.get(0)); + Assert.assertEquals("Connor", result.get(1)); + } +} diff --git a/super-csv/src/test/java/org/supercsv/features/WritingFeaturesTest.java b/super-csv/src/test/java/org/supercsv/features/WritingFeaturesTest.java new file mode 100644 index 00000000..a4c1ee5e --- /dev/null +++ b/super-csv/src/test/java/org/supercsv/features/WritingFeaturesTest.java @@ -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 comparison page + * in "Tokenizer" section for "write" operations. + * + * @author Michał Ziober + */ +public class WritingFeaturesTest { + + @Test + public void testCustomSeparator() throws IOException { + List 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 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 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 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 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 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 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(); + } +}